Merge branch '5-formular-fur-quizfragen-erstellen-uberarbeiten' into 'master'

Resolve "Formular für Quizfragen erstellen / überarbeiten"

Closes #5

See merge request enrichment-2024/qivip!5
This commit is contained in:
Juhn-Vitus Saß
2025-03-12 18:08:48 +00:00
3 changed files with 85 additions and 47 deletions

View File

@@ -100,7 +100,7 @@ def new_question(request):
return redirect(f'{base_url}?type=multiple_choice&quiz_id={quiz_id}') return redirect(f'{base_url}?type=multiple_choice&quiz_id={quiz_id}')
if request.method == 'POST': if request.method == 'POST':
question_text = request.POST.get('question') question_text = request.POST.get('question', '')
# Handle different question types # Handle different question types
if question_type == 'true_false': if question_type == 'true_false':
@@ -140,27 +140,28 @@ def new_question(request):
question = QivipQuestion() question = QivipQuestion()
question.data = json.dumps(json_data) question.data = json.dumps(json_data)
question.quiz_id = quiz question.quiz_id = quiz
question.type = question_type
question.question = question_text
question.save() question.save()
return redirect('library:detail_quiz', pk=quiz.pk) return redirect('library:detail_quiz', pk=quiz.pk)
else: else:
# Initialize empty question data for new questions # Initialize empty question data for new questions
if question_type == 'true_false':
question_data = { question_data = {
'type': question_type, 'type': question_type,
'question': '', 'question': '',
'options': [] 'options': [
}
if question_type == 'true_false':
question_data['options'] = [
{'value': 'Wahr', 'is_correct': True}, {'value': 'Wahr', 'is_correct': True},
{'value': 'Falsch', 'is_correct': False} {'value': 'Falsch', 'is_correct': False}
] ]
}
elif question_type == 'multiple_choice': elif question_type == 'multiple_choice':
question_data['options'] = [ question_data = {
'type': question_type,
'question': '',
'options': [
{'value': '', 'is_correct': False}, {'value': '', 'is_correct': False},
{'value': '', 'is_correct': False} {'value': '', 'is_correct': False}
] ]
}
template_name = f'library/question/question_{question_type}.html' template_name = f'library/question/question_{question_type}.html'
context = { context = {
@@ -179,11 +180,53 @@ def edit_question(request, pk):
return redirect('library:question_list') return redirect('library:question_list')
# Parse the existing JSON data # Parse the existing JSON data
try:
question_data = json.loads(question.data) if question.data else {} question_data = json.loads(question.data) if question.data else {}
question_type = question_data.get('type', 'multiple_choice') question_type = question_data.get('type', 'multiple_choice')
# For true/false questions, get the correct answer from the options
if question_type == 'true_false':
# Default to true if no options exist
correct_answer = True
if question_data.get('options'):
correct_answer = question_data['options'][0].get('is_correct', True)
question_data = {
'type': question_type,
'question': question_data.get('question', ''),
'correct_answer': correct_answer, # Add this for template compatibility
'options': [
{'value': 'Wahr', 'is_correct': correct_answer},
{'value': 'Falsch', 'is_correct': not correct_answer}
]
}
else:
# For multiple choice questions
question_data.setdefault('type', question_type)
question_data.setdefault('question', '')
question_data.setdefault('options', [])
except (json.JSONDecodeError, AttributeError):
# Handle invalid JSON or None data
question_type = question_data.get('type', 'multiple_choice')
if question_type == 'true_false':
question_data = {
'type': question_type,
'question': '',
'correct_answer': True, # Default to true for new questions
'options': [
{'value': 'Wahr', 'is_correct': True},
{'value': 'Falsch', 'is_correct': False}
]
}
else:
question_data = {
'type': question_type,
'question': '',
'options': []
}
if request.method == 'POST': if request.method == 'POST':
question_text = request.POST.get('question') question_text = request.POST.get('question', '')
# Handle different question types # Handle different question types
if question_type == 'true_false': if question_type == 'true_false':
@@ -191,7 +234,6 @@ def edit_question(request, pk):
json_data = { json_data = {
'type': question_type, 'type': question_type,
'question': question_text, 'question': question_text,
'correct_answer': correct_answer,
'options': [ 'options': [
{'value': 'Wahr', 'is_correct': correct_answer}, {'value': 'Wahr', 'is_correct': correct_answer},
{'value': 'Falsch', 'is_correct': not correct_answer} {'value': 'Falsch', 'is_correct': not correct_answer}
@@ -205,7 +247,7 @@ def edit_question(request, pk):
options.append({ options.append({
'order': i, 'order': i,
'value': request.POST.get(f'option_{i}'), 'value': request.POST.get(f'option_{i}'),
'is_correct': int(request.POST.get(f'correct_{i}', 0)) 'is_correct': request.POST.get(f'correct_{i}') == '1'
}) })
i += 1 i += 1
@@ -225,17 +267,13 @@ def edit_question(request, pk):
'options': options 'options': options
} }
question.question = question_text
question.type = question_type
question.data = json.dumps(json_data) question.data = json.dumps(json_data)
question.save() question.save()
return redirect('library:detail_quiz', pk=question.quiz_id.pk) return redirect('library:detail_quiz', pk=question.quiz_id.pk)
else: else:
question_data = json.loads(question.data) if question.data else {}
template_name = f'library/question/question_{question_type}.html' template_name = f'library/question/question_{question_type}.html'
context = { context = {
'question': question_data, 'question': {'data': question_data}, # Wrap in data structure expected by template
'quiz_id': question.quiz_id.pk, 'quiz_id': question.quiz_id.pk,
'quiz': question.quiz_id 'quiz': question.quiz_id
} }

View File

@@ -18,9 +18,9 @@
<div> <div>
<label for="question" class="block text-sm font-medium text-gray-700">Frage</label> <label for="question" class="block text-sm font-medium text-gray-700">Frage</label>
<div class="mt-1"> <div class="mt-1">
<input type="text" name="question" id="question" <textarea name="question" id="question" rows="4"
class="shadow-sm focus:ring-blue-500 focus:border-blue-500 block w-full sm:text-sm border-gray-300 rounded-md" class="shadow-sm focus:ring-blue-500 focus:border-blue-500 block w-full sm:text-sm border-gray-300 rounded-md"
value="{{ question.data.question|default:'' }}" required> required>{{ question.data.question|default:'' }}</textarea>
</div> </div>
</div> </div>
@@ -31,9 +31,9 @@
{% if question and question.data.options %} {% if question and question.data.options %}
{% for option in question.data.options %} {% for option in question.data.options %}
<div class="flex items-center space-x-3"> <div class="flex items-center space-x-3">
<input type="text" name="option_{{ forloop.counter }}" <textarea name="option_{{ forloop.counter }}" rows="2"
class="shadow-sm focus:ring-blue-500 focus:border-blue-500 flex-grow sm:text-sm border-gray-300 rounded-md" class="shadow-sm focus:ring-blue-500 focus:border-blue-500 flex-grow sm:text-sm border-gray-300 rounded-md"
value="{{ option.value }}" required> required>{{ option.value }}</textarea>
<label class="inline-flex items-center"> <label class="inline-flex items-center">
<input type="checkbox" name="correct_{{ forloop.counter }}" value="1" <input type="checkbox" name="correct_{{ forloop.counter }}" value="1"
class="focus:ring-blue-500 h-4 w-4 text-blue-600 border-gray-300 rounded" class="focus:ring-blue-500 h-4 w-4 text-blue-600 border-gray-300 rounded"
@@ -50,9 +50,9 @@
{% endfor %} {% endfor %}
{% else %} {% else %}
<div class="flex items-center space-x-3"> <div class="flex items-center space-x-3">
<input type="text" name="option_1" <textarea name="option_1" rows="2"
class="shadow-sm focus:ring-blue-500 focus:border-blue-500 flex-grow sm:text-sm border-gray-300 rounded-md" class="shadow-sm focus:ring-blue-500 focus:border-blue-500 flex-grow sm:text-sm border-gray-300 rounded-md"
required> required></textarea>
<label class="inline-flex items-center"> <label class="inline-flex items-center">
<input type="checkbox" name="correct_1" value="1" <input type="checkbox" name="correct_1" value="1"
class="focus:ring-blue-500 h-4 w-4 text-blue-600 border-gray-300 rounded"> class="focus:ring-blue-500 h-4 w-4 text-blue-600 border-gray-300 rounded">
@@ -66,9 +66,9 @@
</button> </button>
</div> </div>
<div class="flex items-center space-x-3"> <div class="flex items-center space-x-3">
<input type="text" name="option_2" <textarea name="option_2" rows="2"
class="shadow-sm focus:ring-blue-500 focus:border-blue-500 flex-grow sm:text-sm border-gray-300 rounded-md" class="shadow-sm focus:ring-blue-500 focus:border-blue-500 flex-grow sm:text-sm border-gray-300 rounded-md"
required> required></textarea>
<label class="inline-flex items-center"> <label class="inline-flex items-center">
<input type="checkbox" name="correct_2" value="1" <input type="checkbox" name="correct_2" value="1"
class="focus:ring-blue-500 h-4 w-4 text-blue-600 border-gray-300 rounded"> class="focus:ring-blue-500 h-4 w-4 text-blue-600 border-gray-300 rounded">
@@ -119,9 +119,9 @@ function addOption() {
const div = document.createElement('div'); const div = document.createElement('div');
div.className = 'flex items-center space-x-3'; div.className = 'flex items-center space-x-3';
div.innerHTML = ` div.innerHTML = `
<input type="text" name="option_${optionCount}" <textarea name="option_${optionCount}" rows="2"
class="shadow-sm focus:ring-blue-500 focus:border-blue-500 flex-grow sm:text-sm border-gray-300 rounded-md" class="shadow-sm focus:ring-blue-500 focus:border-blue-500 flex-grow sm:text-sm border-gray-300 rounded-md"
required> required></textarea>
<label class="inline-flex items-center"> <label class="inline-flex items-center">
<input type="checkbox" name="correct_${optionCount}" value="1" <input type="checkbox" name="correct_${optionCount}" value="1"
class="focus:ring-blue-500 h-4 w-4 text-blue-600 border-gray-300 rounded"> class="focus:ring-blue-500 h-4 w-4 text-blue-600 border-gray-300 rounded">
@@ -153,7 +153,7 @@ function updateOptionNumbers() {
const options = container.children; const options = container.children;
Array.from(options).forEach((option, index) => { Array.from(options).forEach((option, index) => {
const number = index + 1; const number = index + 1;
const textInput = option.querySelector('input[type="text"]'); const textInput = option.querySelector('textarea');
const checkbox = option.querySelector('input[type="checkbox"]'); const checkbox = option.querySelector('input[type="checkbox"]');
textInput.name = `option_${number}`; textInput.name = `option_${number}`;

View File

@@ -18,9 +18,9 @@
<div> <div>
<label for="question" class="block text-sm font-medium text-gray-700">Frage</label> <label for="question" class="block text-sm font-medium text-gray-700">Frage</label>
<div class="mt-1"> <div class="mt-1">
<input type="text" name="question" id="question" <textarea name="question" id="question" rows="4"
class="shadow-sm focus:ring-blue-500 focus:border-blue-500 block w-full sm:text-sm border-gray-300 rounded-md" class="shadow-sm focus:ring-blue-500 focus:border-blue-500 block w-full sm:text-sm border-gray-300 rounded-md"
value="{{ question.question|default:'' }}" required> required>{{ question.data.question }}</textarea>
</div> </div>
</div> </div>
@@ -31,13 +31,13 @@
<label class="inline-flex items-center"> <label class="inline-flex items-center">
<input type="radio" name="correct_answer" value="true" <input type="radio" name="correct_answer" value="true"
class="focus:ring-blue-500 h-4 w-4 text-blue-600 border-gray-300" class="focus:ring-blue-500 h-4 w-4 text-blue-600 border-gray-300"
{% if question.correct_answer %}checked{% endif %}> {% if question.data.correct_answer %}checked{% endif %}>
<span class="ml-2 text-sm text-gray-600">Wahr</span> <span class="ml-2 text-sm text-gray-600">Wahr</span>
</label> </label>
<label class="inline-flex items-center"> <label class="inline-flex items-center">
<input type="radio" name="correct_answer" value="false" <input type="radio" name="correct_answer" value="false"
class="focus:ring-blue-500 h-4 w-4 text-blue-600 border-gray-300" class="focus:ring-blue-500 h-4 w-4 text-blue-600 border-gray-300"
{% if not question.correct_answer %}checked{% endif %}> {% if not question.data.correct_answer %}checked{% endif %}>
<span class="ml-2 text-sm text-gray-600">Falsch</span> <span class="ml-2 text-sm text-gray-600">Falsch</span>
</label> </label>
</div> </div>