Skip to content

Commit

Permalink
Quiz 2 by grid (#806)
Browse files Browse the repository at this point in the history
Restructures the quiz to display answers as 2x2 grid.
  • Loading branch information
LterHaar authored Oct 19, 2021
1 parent 23e7639 commit bef1352
Show file tree
Hide file tree
Showing 24 changed files with 5,172 additions and 2,335 deletions.
84 changes: 62 additions & 22 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,11 +497,28 @@ def get_quiz(level_source, question_nr, attempt):
# 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,
char_array.append(chr(ord('@') + (i + 1)))

i = 0
question_obj = []
for options in question['mp_choice_options']:
option_obj = {}
for options_key, options_value in options.items():
for option in options_value:
for key, value in option.items():
option_obj[key] = value
option_obj['char_index'] = char_array[i]
i += 1
question_obj.append(option_obj)

return render_template('quiz_question.html',
quiz=quiz_data,
level_source=level_source,
questionStatus= questionStatus,
questions=quiz_data['questions'],
question=quiz_data['questions'][q_nr - 1].get(q_nr), question_nr=q_nr,
question_options=question_obj,
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,
Expand All @@ -524,7 +541,7 @@ def submit_answer(level_source, question_nr, attempt):
return utils.page_404 (TRANSLATIONS, render_main_menu('adventures'), current_user(request) ['username'], requested_lang (), 'Hedy quiz disabled!')
else:
# Get the chosen option from the request form with radio buttons
option = request.form["radio_option"]
chosen_option = request.form["radio_option"]

# Reading yaml file
quiz_data = quiz_data_file_for(level_source)
Expand All @@ -540,10 +557,10 @@ def submit_answer(level_source, question_nr, attempt):
questionStatus = 'start'
# 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
session['chosen_option'] =option.split("-")[1]
index_option = ord(chosen_option.split("-")[1]) - 65
session['chosen_option'] =chosen_option.split("-")[1]
# If the correct answer is chosen, update the total score and the number of correct answered questions
if question['correct_answer'] in option:
if question['correct_answer'] in chosen_option:
if session.get('total_score'):
session['total_score'] = session.get('total_score') +(config.get('quiz-max-attempts') - session.get('quiz-attempt') )* 0.5 * question['question_score']
else:
Expand All @@ -555,42 +572,65 @@ def submit_answer(level_source, question_nr, attempt):
# 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:
question = quiz_data['questions'][q_nr - 1].get(q_nr)
# Convert the indices to the corresponding characters

# 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)))

i = 0
question_obj = []
for options in question['mp_choice_options']:
option_obj = {}
for options_key, options_value in options.items():
for option in options_value:
for key, value in option.items():
option_obj[key] = value
option_obj['char_index'] = char_array[i]
i += 1
question_obj.append(option_obj)
if question['correct_answer'] in chosen_option:
return render_template('feedback.html', quiz=quiz_data, question=question,
questions=quiz_data['questions'],
question_options=question_obj,
level_source=level_source,
question_nr=q_nr,
correct=session.get('correct_answer'),
option=option,
option=chosen_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.get('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,
return render_template('quiz_question.html',
quiz=quiz_data,
level_source=level_source,
questionStatus=questionStatus,
questions=quiz_data['questions'],
question=quiz_data['questions'][q_nr - 1].get(q_nr), question_nr=q_nr,
question_options=question_obj,
question=quiz_data['questions'][q_nr - 1].get(q_nr),
chosen_option=chosen_option,
question_nr=q_nr,
correct=session.get('correct_answer'),
attempt= session.get('quiz-attempt') ,
questionStatus=questionStatus,
chosen_option = session.get('chosen_option'),
attempt=attempt,
char_array=char_array,
menu=render_main_menu('adventures'), lang=lang,
username=current_user(request)['username'],
auth=TRANSLATIONS.data[requested_lang()]['Auth'])
is_teacher=is_teacher(request),
auth=TRANSLATIONS.get_translations(requested_lang(), 'Auth'))
elif session.get('quiz-attempt') > config.get('quiz-max-attempts'):
return render_template('feedback.html', quiz=quiz_data, question=question,
return render_template('feedback.html',
quiz=quiz_data,
question=question,
question_options=question_obj,
questions=quiz_data['questions'],
level_source=level_source,
question_nr=q_nr,
correct=session.get('correct_answer'),
questionStatus = questionStatus,
option=option,
option=chosen_option,
index_option=index_option,
menu=render_main_menu('adventures'), lang=lang,
username=current_user(request)['username'],
Expand Down
44 changes: 44 additions & 0 deletions coursedata/quiz/convert_to_new_yaml_structure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import sys
from ruamel import yaml
from pathlib import Path
from ruamel.yaml import YAML
import ruamel.yaml
from ruamel.yaml.scalarstring import SingleQuotedScalarString, DoubleQuotedScalarString


def get_original_quiz(level_source):
filename = f'en_quiz_questions_lvl{level_source}.yaml'
yaml = YAML()
yaml.preserve_quotes = True
yaml.explicit_start = True
yaml.default_flow_style = False
quiz_data = yaml.load(Path(filename))
quiz_data = rewrite_quiz_to_new_structure(quiz_data)
quiz_data.yaml_add_eol_comment('# <- double quotes added', 'foo', column=20)
yaml.dump(quiz_data, Path(filename))


def rewrite_quiz_to_new_structure(quiz_data):
for nr in range(len(quiz_data['questions'])):
q_nr = nr+1
if quiz_data['questions'][q_nr-1].get(q_nr):
options_array = []
index = 0
for options in quiz_data['questions'][q_nr - 1].get(q_nr)['mp_choice_options']:
option_obj = []
for options_key, options_value in options.items():
option_obj.append({DoubleQuotedScalarString(options_key):DoubleQuotedScalarString(options_value)})
options_array.append({index: option_obj})
index += 1
quiz_data['questions'][q_nr - 1].get(q_nr)['mp_choice_options'] = options_array
print(quiz_data)
return quiz_data


get_original_quiz(1)
get_original_quiz(2)
get_original_quiz(3)
get_original_quiz(4)
get_original_quiz(5)
get_original_quiz(6)
get_original_quiz(8)
Loading

0 comments on commit bef1352

Please sign in to comment.