Skip to content

Commit

Permalink
Quiz attempts (#622)
Browse files Browse the repository at this point in the history
* Added yaml files with quiz questions

* If the hint isn't provided the text 'None' is wrongfully displayed under the medal on the feedback page. This is fixed by showing no text.

* When visiting a site from the middle of the quiz ( so if you haven't started it from the beginning) the session variable session['total_score'] gives a NoneType when it's addes with question['question_score']. Is solved by setting the total score and correct_answer on zero if the question is answered incorrect
  • Loading branch information
LterHaar authored Sep 15, 2021
1 parent 3b260d1 commit b972902
Show file tree
Hide file tree
Showing 9 changed files with 1,319 additions and 37 deletions.
78 changes: 57 additions & 21 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,8 @@ def get_quiz_start(level):

# Quiz mode
# Fill in the filename as source
@app.route('/quiz/quiz_questions/<level_source>/<question_nr>', methods=['GET'])
def get_quiz(level_source, question_nr):
@app.route('/quiz/quiz_questions/<level_source>/<question_nr>/<attempt>', methods=['GET'])
def get_quiz(level_source, question_nr, attempt):
if not config['quiz-enabled'] and g.lang != 'nl':
return 'Hedy quiz disabled!', 404
else:
Expand All @@ -475,6 +475,7 @@ def get_quiz(level_source, question_nr):
questions=quiz_data['questions'],
question=quiz_data['questions'][q_nr - 1].get(q_nr), question_nr=q_nr,
correct=session.get('correct_answer'),
attempt = attempt,
char_array=char_array,
menu=render_main_menu('adventures'), lang=lang,
username=current_user(request)['username'],
Expand All @@ -489,9 +490,8 @@ def get_quiz(level_source, question_nr):
is_teacher=is_teacher(request),
auth=TRANSLATIONS.get_translations (requested_lang(), 'Auth'))


@app.route('/submit_answer/<level_source>/<question_nr>', methods=["POST"])
def submit_answer(level_source, question_nr):
@app.route('/submit_answer/<level_source>/<question_nr>/<attempt>', methods=["POST"])
def submit_answer(level_source, question_nr, attempt):
if not config['quiz-enabled'] and g.lang != 'nl':
return 'Hedy quiz disabled!', 404
else:
Expand All @@ -507,28 +507,64 @@ def submit_answer(level_source, question_nr):
# Convert question_nr to an integer
q_nr = int(question_nr)

session['quiz-attempt'] = int(attempt)

# Convert the corresponding chosen option to the index of an option
question = quiz_data['questions'][q_nr - 1].get(q_nr)
index_option = ord(option.split("-")[1]) - 65

# If the correct answer is chosen, update the total score and the number of correct answered questions
if question['correct_answer'] in option:
session['total_score'] = session.get('total_score') + question['question_score']
session['correct_answer'] = session.get('correct_answer') + 1

# Loop through the questions
if q_nr <= len(quiz_data['questions']):
return render_template('feedback.html', quiz=quiz_data, question=question,
questions=quiz_data['questions'],
level_source=level_source,
question_nr=q_nr,
correct=session.get('correct_answer'),
option=option,
index_option=index_option,
menu=render_main_menu('adventures'), lang=lang,
username=current_user(request)['username'],
is_teacher=is_teacher(request),
auth=TRANSLATIONS.get_translations (requested_lang(), 'Auth'))
if session.get('total_score'):
session['total_score'] = session.get('total_score') +(config['quiz-max-attempts'] - session.get('quiz-attempt') )* 0.5 * question['question_score']
else:
session['total_score'] = (config['quiz-max-attempts'] - session.get('quiz-attempt') )* 0.5 * question['question_score']
if session.get('correct_answer'):
session['correct_answer'] = session.get('correct_answer') + 1
else:
session['correct_answer'] = 1
# Loop through the questions and check that the loop doesn't reach out of bounds
q_nr = int(question_nr)
if q_nr <= len(quiz_data['questions']) :
if question['correct_answer'] in option:
return render_template('feedback.html', quiz=quiz_data, question=question,
questions=quiz_data['questions'],
level_source=level_source,
question_nr=q_nr,
correct=session.get('correct_answer'),
option=option,
index_option=index_option,
menu=render_main_menu('adventures'), lang=lang,
username=current_user(request)['username'],
auth=TRANSLATIONS.data[requested_lang()]['Auth'])
elif session.get('quiz-attempt') < config['quiz-max-attempts']:
question = quiz_data['questions'][q_nr - 1].get(q_nr)
# Convert the indices to the corresponding characters
char_array = []
for i in range(len(question['mp_choice_options'])):
char_array.append(chr(ord('@') + (i + 1)))
return render_template('quiz_question.html', quiz=quiz_data, level_source=level_source,
questions=quiz_data['questions'],
question=quiz_data['questions'][q_nr - 1].get(q_nr), question_nr=q_nr,
correct=session.get('correct_answer'),
attempt= session.get('quiz-attempt') ,
questionFalse='false',
char_array=char_array,
menu=render_main_menu('adventures'), lang=lang,
username=current_user(request)['username'],
auth=TRANSLATIONS.data[requested_lang()]['Auth'])
elif session.get('quiz-attempt') > config['quiz-max-attempts']:
return render_template('feedback.html', quiz=quiz_data, question=question,
questions=quiz_data['questions'],
level_source=level_source,
question_nr=q_nr,
correct=session.get('correct_answer'),
questionFalse = 'false',
option=option,
index_option=index_option,
menu=render_main_menu('adventures'), lang=lang,
username=current_user(request)['username'],
auth=TRANSLATIONS.data[requested_lang()]['Auth'])
else: # show a different page for after the last question
return 'No end quiz page!', 404

Expand Down
2 changes: 2 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@
},
#enables the quiz environment by setting the config variable on True
'quiz-enabled': True,
'quiz-max-attempts': 4,

}
121 changes: 121 additions & 0 deletions coursedata/quiz/lvl1_quiz_questions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
level: 1
questions:
- 1:
question_text: "Wat is de naam van de programmeertaal die je hier leert?"
code: "..."
mp_choice_options:
- {option_text:, code: "Hedy", feedback: "Goed zo!"}
- {option_text:, code: "Heddy", feedback: "Deze is het niet!"}
- {option_text:, code: "Haydie", feedback: "Deze is het niet!"}
- {option_text:, code: "Henk", feedback: "Deze is het niet!"}
correct_answer: "A"
hint: "We beginnen met een simpele vraag."
question_score: 10
- 2:
question_text: "Met welk commando kun je tekst laten verschijnen?"
code: "..."
mp_choice_options:
- {option_text:, code: "echo", feedback: "Met echo herhaal je een antwoord..."}
- {option_text:, code: "print", feedback: "Goed! Met print kun je tekst printen!"}
- {option_text:, code: "hello", feedback: "Hello is geen commando, probeer het nog eens..."}
- {option_text:, code: "ask", feedback: "Met ask kun je een vraag stellen..."}
correct_answer: "B"
hint: "(?) Hello world! "
question_score: 10
- 3:
question_text: "Hoe vraag je: Wat is je lievelingskleur?"
code: "..."
mp_choice_options:
- {option_text:, code: "print Wat is je lievelingskleur?", feedback: "Met print print je iets"}
- {option_text:, code: "ask print Wat is je lievelingskleur?", feedback: "Ask en print is dubbelop, je hebt maar één commando nodig."}
- {option_text:, code: "ask Wat is je lievelingskleur?", feedback: "Goed zo!"}
- {option_text:, code: "echo Wat is je lievelingskleur?", feedback: "Echo herhaalt het antwoord."}
correct_answer: "C"
hint: "Het Engelse woord voor vragen is ask"
question_score: 10
- 4:
question_text: "Wat is er mis met deze code?"
code: " Hoi ik ben Hedy!
ask Wie ben jij?
echo Hoi... "
mp_choice_options:
- {option_text:, code: "print in regel 1 mist.", feedback: "Goedzo!"}
- {option_text:, code: "ask in regel 2 is fout gespeld, het moet aks zijn.", feedback: "Ask is goed gespeld."}
- {option_text:, code: "echo is geen commando in level 1,", feedback: "Echo is goed, er is een ander foutje"}
- {option_text:, code: "Niets! De code is goed!", feedback: "Er zit een foutje in, kijk goed!"}
correct_answer: "A"
hint: "Er mist een commando in het begin van de code"
question_score: 10
- 5:
question_text: "Welk commando mist in regel 2?"
code: "ask Wat is jouw lievelingsdier?
? Dus jouw lievelingsdier is..."
mp_choice_options:
- {option_text:, code: "print", feedback: "Je wilt dat in regel 2 het antwoord herhaald wordt..."}
- {option_text:, code: "ego", feedback: "ego is geen commando, probeer het nog eens..."}
- {option_text:, code: "ask", feedback: "Met ask kun je een vraag stellen..."}
- {option_text:, code: "echo", feedback: "Goedzo!"}
correct_answer: "D"
hint: "met 'ask' kun je iets vragen"
question_score: 10
- 6:
question_text: "Wat is er mis met deze code?"
code: " print Hoi ik ben Hedy!
aks Voor welke voetbalclub ben jij?
echo Jij bent voor...
print Cool! Ik ook! "
mp_choice_options:
- {option_text:, code: "In regel 1 moet het print commando worden vervangen door ask.", feedback: "Print in regel 1 is goed, let op de spelling van de commando's"}
- {option_text:, code: "In regel 2 is ask fout gespeld.", feedback: "Goed opgelet!"}
- {option_text:, code: "Regel 3 moet beginnen met print in plaats van echo.", feedback: "Echo is goed! Kijk naar de spelling van de commando's."}
- {option_text:, code: "In regel 4 is print verkeerd gespeld. ", feedback: "Er zit ergens anders een spelfout"}
correct_answer: "B"
hint: "Kijk goed naar de spelling van de commando's"
question_score: 10
- 7:
question_text: "Wat is er mis met deze code?"
code: "print Welkom bij Restaurant Hedy!
ask Wat wilt u eten?
echo Dus u wilt graag ...
print Komt er aan! Eet smakelijk!"
mp_choice_options:
- {option_text:, code: "In regel 1 moest ask staan in plaats van print", feedback: "Weet je zeker dat er iets mis is?"}
- {option_text:, code: "In regel 1 moet echo staan in plaats van print", feedback: "Weet je zeker dat er iets mis is?"}
- {option_text:, code: "In regel 3 moet print staan in plaats van echo", feedback: "Weet je zeker dat er iets mis is?"}
- {option_text:, code: "Niets! Deze code is goed!", feedback: "Correct!"}
correct_answer: "D"
hint: "Controleer de code regel voor regel."
question_score: 10
- 8:
question_text: "Wat doet het commando echo?"
code: "..."
mp_choice_options:
- {option_text:, code: "Met echo stel je een vraag", feedback: "Dat doe je met ask"}
- {option_text:, code: "Met echo laat je tekst verschijnen", feedback: "Dat doe je met print"}
- {option_text:, code: "Met echo herhaal je een gegeven antwoord", feedback: "Goed zo!"}
- {option_text:, code: "Met echo laat je tekst verdwijnen", feedback: "Dat klopt niet"}
correct_answer: "C"
hint: "echo gebruik je nadat iemand een antwoord gegeven heeft bij ask."
question_score: 10
- 9:
question_text: "Wat is er mis met deze code?"
code: "print Hallo!
print Hoe gaat het met jou?
echo Dus het gaat..."
mp_choice_options:
- {option_text:, code: " print in regel 1 moet ask zijn", feedback: "Nee, print is goed. Waar wordt de vraag gesteld?"}
- {option_text:, code: " print is regel 2 moet ask zijn", feedback: "Super!"}
- {option_text:, code: " echo in regel 3 moet ask zijn", feedback: "Nee, echo is goed. Waar wordt de vraag gesteld?"}
- {option_text:, code: "Niets, alles is goed!", feedback: "Er zit wel een foutje in..."}
correct_answer: "B"
hint: "met 'ask' kun je iets vragen"
question_score: 10
- 10:
question_text: "Heb je zin om naar level 2 te gaan?"
code: "..."
mp_choice_options:
- {option_text:, code: "Ja!", feedback: "Mooizo! Op naar level 2!"}
- {option_text:, code: "Nee", feedback: "Jammer..."}
correct_answer: "A"
hint: "Op naar level 2!"
question_score: 10
2 changes: 1 addition & 1 deletion static/css/generated.css

Large diffs are not rendered by default.

Loading

0 comments on commit b972902

Please sign in to comment.