Skip to content

Commit

Permalink
Show feedback after an attempt (hedyorg#1221)
Browse files Browse the repository at this point in the history
* Feedback is shown under the question. 
- Only remove feedback messages from the session when a new answer
  is submitted (instead of when rendering): this makes it so the page
  still contains the feedback if the user refreshes the page (otherwise
  the feedback would be gone).

Co-authored-by: Lotus ter Haar <[email protected]>
Co-authored-by: Felienne <[email protected]>
Co-authored-by: Rico Huijbers <[email protected]>
  • Loading branch information
4 people authored Nov 14, 2021
1 parent b49203a commit ab8d9bc
Show file tree
Hide file tree
Showing 4 changed files with 1,772 additions and 188 deletions.
39 changes: 31 additions & 8 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,12 +521,20 @@ def get_quiz(level_source, question_nr, attempt):
question = quiz.get_question(quiz_data, question_nr)
question_obj = quiz.question_options_for(question)

# Read from session. Don't remove yet: If the user refreshes the
# page here, we want to keep this same information in place (otherwise
# if we removed from the session here it would be gone on page refresh).
chosen_option = session.get('chosenOption', None)
wrong_answer_hint = session.get('wrong_answer_hint', None)

return render_template('quiz_question.html',
quiz=quiz_data,
level_source=level_source,
questionStatus=questionStatus,
questions=quiz_data['questions'],
question_options=question_obj,
chosen_option=chosen_option,
wrong_answer_hint=wrong_answer_hint,
question=question,
question_nr=question_nr,
correct=session.get('correct_answer'),
Expand Down Expand Up @@ -574,7 +582,7 @@ def submit_answer(level_source, question_nr, attempt):
# The number should always be the same as 'question_nr', or otherwise
# be 'question_nr - 1', so is unnecessary. But we'll leave it here for now.
chosen_option = request.form["radio_option"]
chosen_letter = chosen_option.split('-')[1]
chosen_option = chosen_option.split('-')[1]

# Reading yaml file
quiz_data = quiz.quiz_data_file_for(level_source)
Expand All @@ -586,9 +594,15 @@ def submit_answer(level_source, question_nr, attempt):

# Convert the corresponding chosen option to the index of an option
question = quiz.get_question(quiz_data, q_nr)
session['chosenOption'] = chosen_letter

is_correct = quiz.is_correct_answer(question, chosen_letter)
is_correct = quiz.is_correct_answer(question, chosen_option)

session['chosenOption'] = chosen_option
if not is_correct:
session['wrong_answer_hint'] = quiz.get_hint(question, chosen_option)
else:
# Correct answer -- make sure there is no hint on the next display page
session.pop('wrong_answer_hint', None)

# Store the answer in the database. If we don't have a username,
# use the session ID as a username.
Expand All @@ -599,20 +613,21 @@ def submit_answer(level_source, question_nr, attempt):
level=level_source,
is_correct=is_correct,
question_number=question_nr,
answer=chosen_letter)
answer=chosen_option)

if is_correct:
score = quiz.correct_answer_score(question, attempt)
session['total_score'] = session.get('total_score', 0) + score
session['correct_answer'] = session.get('correct_answer', 0) + 1

return redirect(url_for('quiz_feedback', level_source=level_source, question_nr=question_nr))

# Not a correct answer. You can try again if you haven't hit your max attempts yet.
if attempt >= quiz.MAX_ATTEMPTS:
return redirect(url_for('quiz_feedback', level_source=level_source, question_nr=question_nr))

# Redirect to the display page to try again
return redirect(url_for('get_quiz', level_source=level_source, question_nr=question_nr, attempt=attempt + 1))
return redirect(url_for('get_quiz', chosen_option=chosen_option, level_source=level_source, question_nr=question_nr, attempt=attempt + 1))

@app.route('/quiz/feedback/<int:level_source>/<int:question_nr>', methods=["GET"])
def quiz_feedback(level_source, question_nr):
Expand All @@ -628,20 +643,28 @@ def quiz_feedback(level_source, question_nr):
return 'No quiz yaml file found for this level', 404

question = quiz.get_question(quiz_data, question_nr)
chosen_letter = session['chosenOption']
answer_was_correct = quiz.is_correct_answer(question, chosen_letter)

index_option = quiz.index_from_letter(chosen_letter)

# Read from session and remove the variables from it (this is the
# feedback page, the previous answers will never apply anymore).
chosen_option = session.pop('chosenOption', None)
wrong_answer_hint = session.pop('wrong_answer_hint', None)

answer_was_correct = quiz.is_correct_answer(question, chosen_option)

index_option = quiz.index_from_letter(chosen_option)
correct_option = quiz.get_correct_answer(question)

question_options = quiz.question_options_for(question)

return render_template('feedback.html', quiz=quiz_data, question=question,
questions=quiz_data['questions'],
question_options=question_options,
level_source=level_source,
question_nr=question_nr,
correct=session.get('correct_answer'),
answer_was_correct=answer_was_correct,
wrong_answer_hint=wrong_answer_hint,
index_option=index_option,
correct_option=correct_option,
menu=render_main_menu('adventures'), lang=lang,
Expand Down
Loading

0 comments on commit ab8d9bc

Please sign in to comment.