Skip to content

Commit

Permalink
Quiz newlines do not work in answers (hedyorg#1028)
Browse files Browse the repository at this point in the history
Allows for proper newlines in quiz answers
  • Loading branch information
LterHaar authored Oct 19, 2021
1 parent 8d12f80 commit 2a01a05
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 108 deletions.
13 changes: 9 additions & 4 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,12 +506,13 @@ def get_quiz(level_source, question_nr, attempt):
for options_key, options_value in options.items():
for option in options_value:
for key, value in option.items():
option_obj[key] = value
if value:
option_obj[key] = value.replace("\n", '\\n')
option_obj['char_index'] = char_array[i]
i += 1
question_obj.append(option_obj)

return render_template('quiz_question.html',
html_obj = render_template('quiz_question.html',
quiz=quiz_data,
level_source=level_source,
questionStatus= questionStatus,
Expand All @@ -526,6 +527,7 @@ def get_quiz(level_source, question_nr, attempt):
username=current_user(request)['username'],
is_teacher=is_teacher(request),
auth=TRANSLATIONS.get_translations(requested_lang(), 'Auth'))
return html_obj.replace("\\n", '<br />')
else:
return render_template('endquiz.html', correct=session.get('correct_answer'),
total_score=session.get('total_score'),
Expand Down Expand Up @@ -587,7 +589,8 @@ def submit_answer(level_source, question_nr, attempt):
for options_key, options_value in options.items():
for option in options_value:
for key, value in option.items():
option_obj[key] = value
if value:
option_obj[key] = value.replace("\n", '\\n')
option_obj['char_index'] = char_array[i]
i += 1
question_obj.append(option_obj)
Expand All @@ -604,7 +607,8 @@ def submit_answer(level_source, question_nr, attempt):
username=current_user(request)['username'],
auth=TRANSLATIONS.data[requested_lang()]['Auth'])
elif session.get('quiz-attempt') <= config.get('quiz-max-attempts'):
return render_template('quiz_question.html',

html_obj = render_template('quiz_question.html',
quiz=quiz_data,
level_source=level_source,
questionStatus=questionStatus,
Expand All @@ -620,6 +624,7 @@ def submit_answer(level_source, question_nr, attempt):
username=current_user(request)['username'],
is_teacher=is_teacher(request),
auth=TRANSLATIONS.get_translations(requested_lang(), 'Auth'))
return html_obj.replace("\\n", '<br />')
elif session.get('quiz-attempt') > config.get('quiz-max-attempts'):
return render_template('feedback.html',
quiz=quiz_data,
Expand Down
9 changes: 1 addition & 8 deletions coursedata/quiz/convert_to_new_yaml_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,7 @@ def rewrite_quiz_to_new_structure(quiz_data):
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)
get_original_quiz(1)
212 changes: 116 additions & 96 deletions coursedata/quiz/en_quiz_questions_lvl1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@ questions:
code: "..."
mp_choice_options:
- 0:
- "0": "[ordereddict([('option_text', 'Hedy')]), ordereddict([('code', None)]),\
\ ordereddict([('feedback', 'Good job!')])]"
- "option_text": "Hedy"
- "code": "None"
- "feedback": "Good job!"
- 1:
- "1": "[ordereddict([('option_text', 'Heddy')]), ordereddict([('code', None)]),\
\ ordereddict([('feedback', 'Not this one!')])]"
- "option_text": "Heddy"
- "code": "None"
- "feedback": "Not this one!"
- 2:
- "2": "[ordereddict([('option_text', 'Haydie')]), ordereddict([('code', None)]),\
\ ordereddict([('feedback', 'Not this one!')])]"
- "option_text": "Haydie"
- "code": "None"
- "feedback": "Not this one!"
- 3:
- "3": "[ordereddict([('option_text', 'Henk')]), ordereddict([('code', None)]),\
\ ordereddict([('feedback', 'Not this one!')])]"
- "option_text": "Henk"
- "code": "None"
- "feedback": "Not this one!"
correct_answer: "A"
hint: "It's named after Hedy Lamarr."
question_score: 10
Expand All @@ -25,17 +29,21 @@ questions:
code: "..."
mp_choice_options:
- 0:
- "0": "[ordereddict([('option_text', None)]), ordereddict([('code', 'echo')]),\
\ ordereddict([('feedback', 'Echo repeats a given answer.')])]"
- "option_text:": "None"
- "code": "echo"
- "feedback": "Echo repeats a given answer."
- 1:
- "1": "[ordereddict([('option_text', None)]), ordereddict([('code', 'print')]),\
\ ordereddict([('feedback', 'Correct!')])]"
- "option_text:": "None"
- "code": "print"
- "feedback": "Correct!"
- 2:
- "2": "[ordereddict([('option_text', None)]), ordereddict([('code', 'hello')]),\
\ ordereddict([('feedback', \"Hello isn't a command.\")])]"
- "option_text:": "None"
- "code": "hello"
- "feedback": "Hello isn't a command."
- 3:
- "3": "[ordereddict([('option_text', None)]), ordereddict([('code', 'ask')]),\
\ ordereddict([('feedback', 'With ask you can ask a question')])]"
- "option_text:": "None"
- "code": "ask"
- "feedback": "With ask you can ask a question"
correct_answer: "B"
hint: "(?) Hello world! "
question_score: 10
Expand All @@ -44,20 +52,21 @@ questions:
code: "..."
mp_choice_options:
- 0:
- "0": "[ordereddict([('option_text', None)]), ordereddict([('code', 'print\
\ What is your favorite color?')]), ordereddict([('feedback', \"Print prints\
\ text, but it doesn't ask questions\")])]"
- "option_text:": "None"
- "code": "print What is your favorite color?"
- "feedback": "Print prints text, but it doesn't ask questions"
- 1:
- "1": "[ordereddict([('option_text', None)]), ordereddict([('code', 'ask print\
\ What is your favorite color?')]), ordereddict([('feedback', \"You don't\
\ need 2 commands, only one.\")])]"
- "option_text:": "None"
- "code": "ask print What is your favorite color?"
- "feedback": "You don't need 2 commands, only one."
- 2:
- "2": "[ordereddict([('option_text', None)]), ordereddict([('code', 'ask What\
\ is your favorite color?')]), ordereddict([('feedback', 'Great!')])]"
- "option_text:": "None"
- "code": "ask What is your favorite color?"
- "feedback": "Great!"
- 3:
- "3": "[ordereddict([('option_text', None)]), ordereddict([('code', 'echo What\
\ is your favorite color?')]), ordereddict([('feedback', 'Echo repeats your\
\ answer back to you.')])]"
- "option_text:": "None"
- "code": "echo What is your favorite color?"
- "feedback": "Echo repeats your answer back to you."
correct_answer: "C"
hint: "You can ask something with the ask command"
question_score: 10
Expand All @@ -69,22 +78,24 @@ questions:
echo Hi...
mp_choice_options:
- 0:
- "0": "[ordereddict([('option_text', 'print in line 1 is missing.')]), ordereddict([('code',\
\ None)]), ordereddict([('feedback', 'Correct!')])]"
- "option_text": "print in line 1 is missing."
- "code": "None"
- "feedback": "Correct!"
- 1:
- "1": "[ordereddict([('option_text', 'Im in line 1 is spelled wrong.')]), ordereddict([('code',\
\ None)]), ordereddict([('feedback', \"Im is spelled wrong on purpose, Hedy\
\ doesn't understand the apostrophes yet.\")])]"
- "option_text": "Im in line 1 is spelled wrong."
- "code": "None"
- "feedback": "Im is spelled wrong on purpose, Hedy doesn't understand the apostrophes\
\ yet."
- 2:
- "2": "[ordereddict([('option_text', \"Echo isn't a command.\")]), ordereddict([('code',\
\ None)]), ordereddict([('feedback', \"Echo is a command, there's another\
\ mistake.\")])]"
- "option_text": "Echo isn't a command,"
- "code": "None"
- "feedback": "Echo is a command, there's another mistake."
- 3:
- "3": "[ordereddict([('option_text', 'Nothing! This is a perfect code!')]),\
\ ordereddict([('code', None)]), ordereddict([('feedback', 'Wrong, look\
\ carefully!')])]"
- "option_text": "Nothing! This is a perfect code!"
- "code": "None"
- "feedback": "Wrong, look carefully!"
correct_answer: "A"
hint: "Line 1 doesn't seem right"
hint: "Line 1 doens't seem right"
question_score: 10
- 5:
question_text: "Which command is missing in line 2?"
Expand All @@ -93,18 +104,21 @@ questions:
? So your favorite pet is..."
mp_choice_options:
- 0:
- "0": "[ordereddict([('option_text', None)]), ordereddict([('code', 'print')]),\
\ ordereddict([('feedback', 'No, you want the answer to be repeated back\
\ to you')])]"
- "option_text:": "None"
- "code": "print"
- "feedback": "No, you want the answer to be repeated back to you"
- 1:
- "1": "[ordereddict([('option_text', None)]), ordereddict([('code', 'ego')]),\
\ ordereddict([('feedback', \"ego isn't a command.\")])]"
- "option_text:": "None"
- "code": "ego"
- "feedback": "ego isn't a command."
- 2:
- "2": "[ordereddict([('option_text', None)]), ordereddict([('code', 'ask')]),\
\ ordereddict([('feedback', 'With ask you can ask a question.')])]"
- "option_text:": "None"
- "code": "ask"
- "feedback": "With ask you can ask a question."
- 3:
- "3": "[ordereddict([('option_text', None)]), ordereddict([('code', 'echo')]),\
\ ordereddict([('feedback', 'Right on!')])]"
- "option_text:": "None"
- "code": "echo"
- "feedback": "Right on!"
correct_answer: "D"
hint: "You want to see the answer at the end of line 2..."
question_score: 10
Expand All @@ -117,20 +131,21 @@ questions:
print Cool! Me too!
mp_choice_options:
- 0:
- "0": "[ordereddict([('option_text', 'In line 1 print should be replaced with\
\ ask.')]), ordereddict([('code', None)]), ordereddict([('feedback', 'Print\
\ in line 1 is corrcet, mind the spelling of the words.')])]"
- "option_text": "In line 1 print should be replaced with ask."
- "code": "None"
- "feedback": "Print in line 1 is corrcet, mind the spelling of the words."
- 1:
- "1": "[ordereddict([('option_text', 'In line 2 ask is spelled wrong')]), ordereddict([('code',\
\ None)]), ordereddict([('feedback', 'Great! You paid attention!')])]"
- "option_text": "In line 2 ask is spelled wrong"
- "code": "None"
- "feedback": "Great! You paid attention!"
- 2:
- "2": "[ordereddict([('option_text', 'Line 3 has to begin with print instead\
\ of echo.')]), ordereddict([('code', None)]), ordereddict([('feedback',\
\ 'Echo iscorrect, check the spelling of the commands.')])]"
- "option_text": "Line 3 has to begin with print instead of echo."
- "code": "None"
- "feedback": "Echo iscorrect, check the spelling of the commands."
- 3:
- "3": "[ordereddict([('option_text', 'In line 4 print is spelled wrong.')]),\
\ ordereddict([('code', None)]), ordereddict([('feedback', 'There is a spelling\
\ error somewhere else')])]"
- "option_text": "In line 4 print is spelled wrong."
- "code": "None"
- "feedback": "There is a spelling error somewhere else"
correct_answer: "B"
hint: "Check the way the commands are spelled."
question_score: 10
Expand All @@ -143,20 +158,21 @@ questions:
print Coming right up! Enjoy!"
mp_choice_options:
- 0:
- "0": "[ordereddict([('option_text', 'In line 1 print needs to be replaced\
\ with ask')]), ordereddict([('code', None)]), ordereddict([('feedback',\
\ 'Are you sure something is wrong?')])]"
- "option_text": "In line 1 print needs to be replaced with ask"
- "code": "None"
- "feedback": "Are you sure something is wrong?"
- 1:
- "1": "[ordereddict([('option_text', 'In line 1 moet print needs to be replaced\
\ with echo')]), ordereddict([('code', None)]), ordereddict([('feedback',\
\ \"Are you sure something's wrong?\")])]"
- "option_text": "In line 1 moet print needs to be replaced with echo"
- "code": "None"
- "feedback": "Are you sure something's worng?"
- 2:
- "2": "[ordereddict([('option_text', 'In line 3 echo needs to be replaced with\
\ print')]), ordereddict([('code', None)]), ordereddict([('feedback', 'Are\
\ you sure something is wrong?')])]"
- "option_text": "In line 3 echo needs to be replaced with print"
- "code": "None"
- "feedback": "Are you sure something is wrong?"
- 3:
- "3": "[ordereddict([('option_text', 'Nothing! This is a perfect code!')]),\
\ ordereddict([('code', None)]), ordereddict([('feedback', 'Correct!')])]"
- "option_text": "Nothing! This is a perfect code!"
- "code": "None"
- "feedback": "Correct!"
correct_answer: "D"
hint: "Check the code line by line"
question_score: 10
Expand All @@ -165,19 +181,21 @@ questions:
code: "..."
mp_choice_options:
- 0:
- "0": "[ordereddict([('option_text', 'You can use it to ask a question')]),\
\ ordereddict([('code', None)]), ordereddict([('feedback', \"That's what\
\ ask is for\")])]"
- "option_text": "You can use it to ask a question"
- "code": "None"
- "feedback": "That's what ask is for"
- 1:
- "1": "[ordereddict([('option_text', 'You can use it to print text')]), ordereddict([('code',\
\ None)]), ordereddict([('feedback', \"That's what print is for\")])]"
- "option_text": "You can use it to print text"
- "code": "None"
- "feedback": "That's what print is for"
- 2:
- "2": "[ordereddict([('option_text', 'You can use it to repeat an answer')]),\
\ ordereddict([('code', None)]), ordereddict([('feedback', 'Good job!')])]"
- "option_text": "You can use it to repeat an answer"
- "code": "None"
- "feedback": "Good job!"
- 3:
- "3": "[ordereddict([('option_text', 'You can use it to make text disappear')]),\
\ ordereddict([('code', None)]), ordereddict([('feedback', \"That's not\
\ right\")])]"
- "option_text": "You can use it to make text disappear"
- "code": "None"
- "feedback": "That's not right"
correct_answer: "C"
hint: "Echo is used after a ask command."
question_score: 10
Expand All @@ -189,20 +207,21 @@ questions:
echo So you are doing..."
mp_choice_options:
- 0:
- "0": "[ordereddict([('option_text', ' print in line 1 should be ask')]), ordereddict([('code',\
\ None)]), ordereddict([('feedback', 'No, print is right. Where is the question\
\ being asked?')])]"
- "option_text": " print in line 1 should be ask"
- "code": "None"
- "feedback": "No, print is right. Where is the question being asked?"
- 1:
- "1": "[ordereddict([('option_text', ' print in line 2 should be aks')]), ordereddict([('code',\
\ None)]), ordereddict([('feedback', 'Super!')])]"
- "option_text": " print in line 2 should be aks"
- "code": "None"
- "feedback": "Super!"
- 2:
- "2": "[ordereddict([('option_text', ' echo in line 3 should be ask')]), ordereddict([('code',\
\ None)]), ordereddict([('feedback', 'No, echo is right. Where is the question\
\ being asked?')])]"
- "option_text": " echo in line 3 should be ask"
- "code": "None"
- "feedback": "No, echo is right. Where is the question being asked?"
- 3:
- "3": "[ordereddict([('option_text', 'Nothing. this is a perfect code!')]),\
\ ordereddict([('code', None)]), ordereddict([('feedback', 'Look carefully\
\ for the mistake..')])]"
- "option_text": "Nothing. this is a perfect code!"
- "code": "None"
- "feedback": "Look carefully for the mistake.."
correct_answer: "B"
hint: "ask allows you to ask a question"
question_score: 10
Expand All @@ -211,12 +230,13 @@ questions:
code: "..."
mp_choice_options:
- 0:
- "0": "[ordereddict([('option_text', 'Yes!')]), ordereddict([('code', None)]),\
\ ordereddict([('feedback', \"Great! Let's go to the next level!\")])]"
- "option_text": "Yes!"
- "code:": "None"
- "feedback": "Great! Let's go to the next level!"
- 1:
- "1": "[ordereddict([('option_text', 'No')]), ordereddict([('code', None)]),\
\ ordereddict([('feedback', 'Too bad... You can practice some more in level\
\ 1')])]"
- "option_text": "No"
- "code": "None"
- "feedback": "Too bad... You can practice some more in level 1"
correct_answer: "A"
hint: "Let's go!"
question_score: 10

0 comments on commit 2a01a05

Please sign in to comment.