Skip to content

Commit 6f081da

Browse files
HuongNV13timhunt
authored andcommitted
Instruction for all question types in Attempt sheet screen
1 parent e88513e commit 6f081da

7 files changed

+248
-2
lines changed

classes/output/renderer.php

+43-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
use html_writer;
2929
use moodle_url;
3030
use plugin_renderer_base;
31+
use qbehaviour_renderer;
32+
use qtype_renderer;
33+
use question_attempt;
3134
use question_display_options;
3235
use quiz_answersheets\utils;
3336
use quiz_attempt;
@@ -59,12 +62,17 @@ public function render_question_attempt_content(quiz_attempt $attemptobj, bool $
5962
$displayoptions->manualcommentlink = null;
6063
$displayoptions->context = context_module::instance($attemptobj->get_cmid());
6164
$rightanswer = $this->page->url->get_param('rightanswer');
65+
$qoutput = $this->page->get_renderer('quiz_answersheets', 'qtype_override');
6266

6367
foreach ($slots as $slot) {
6468
$originalslot = $attemptobj->get_original_slot($slot);
6569
$questionnumber = $attemptobj->get_question_number($originalslot);
6670

6771
$qa = $attemptobj->get_question_attempt($slot);
72+
$qtoutput = $qa->get_question()->get_renderer($this->page);
73+
$behaviouroutput = $this->page->get_renderer(get_class($qa->get_behaviour()));
74+
$displayoptions = clone($displayoptions);
75+
$qa->get_behaviour()->adjust_display_options($displayoptions);
6876

6977
if ($rightanswer && $attempt->state == quiz_attempt::IN_PROGRESS) {
7078
$correctresponse = $qa->get_correct_response();
@@ -73,7 +81,7 @@ public function render_question_attempt_content(quiz_attempt $attemptobj, bool $
7381
}
7482
}
7583

76-
$output .= $qa->render($displayoptions, $questionnumber);
84+
$output .= $qoutput->question($qa, $behaviouroutput, $qtoutput, $displayoptions, $questionnumber);
7785
}
7886

7987
return $output;
@@ -145,3 +153,37 @@ public function render_print_button(): string {
145153
}
146154

147155
}
156+
157+
/**
158+
* The override core_question_renderer for the quiz_answersheets module.
159+
*
160+
* @copyright 2019 The Open University
161+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
162+
*/
163+
class qtype_override_renderer extends \core_question_renderer {
164+
165+
protected function formulation(question_attempt $qa, qbehaviour_renderer $behaviouroutput, qtype_renderer $qtoutput,
166+
question_display_options $options) {
167+
global $attemptobj;
168+
// We need to use global trick here because in mod/quiz/report/answersheets/submitresponses.php:23 already loaded the attemptobj
169+
// so we no need to do the extra Database query.
170+
$output = '';
171+
172+
$rightanswer = $this->page->url->get_param('rightanswer');
173+
174+
if ($rightanswer && $attemptobj->get_state() == quiz_attempt::IN_PROGRESS ||
175+
$this->page->pagetype == 'mod-quiz-report-answersheets-submitresponses') {
176+
// Do not show question instruction for right answer sheet and submit responses page.
177+
return parent::formulation($qa, $behaviouroutput, $qtoutput, $options);
178+
}
179+
180+
// Append question instruction if exist.
181+
$qinstruction = utils::get_question_instruction($qa->get_question()->get_type_name());
182+
if (!empty($qtoutput)) {
183+
$output .= html_writer::div($qinstruction, 'question-instruction');
184+
$output .= parent::formulation($qa, $behaviouroutput, $qtoutput, $options);
185+
}
186+
187+
return $output;
188+
}
189+
}

classes/report_table.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,10 @@ public function col_answer_sheet($row) {
131131
*/
132132
public function col_submit_student_responses($row) {
133133
if ($row->state == quiz_attempt::IN_PROGRESS) {
134+
$redirect = $this->options->get_url();
135+
$redirect->param('lastchanged', $row->attempt);
134136
return html_writer::link(new moodle_url('/mod/quiz/report/answersheets/submitresponses.php',
135-
['attempt' => $row->attempt, 'redirect' => $this->options->get_url()->param('lastchanged', $row->attempt)]),
137+
['attempt' => $row->attempt, 'redirect' => $redirect]),
136138
get_string('submit_student_responses_label', 'quiz_answersheets'), ['class' => 'reviewlink']);
137139
} else {
138140
return '-';

classes/utils.php

+15
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,19 @@ public static function can_create_attempt($quizobj, $attempts): bool {
204204
}
205205
return false;
206206
}
207+
/**
208+
* Get instruction text for given question type
209+
*
210+
* @param string $questiontype Question type
211+
* @return string instruction text
212+
*/
213+
public static function get_question_instruction(string $questiontype): string {
214+
$instructionexists = get_string_manager()->string_exists($questiontype . '_instruction', 'quiz_answersheets');
215+
if (!$instructionexists) {
216+
return '';
217+
} else {
218+
return get_string($questiontype . '_instruction', 'quiz_answersheets');
219+
}
220+
}
221+
207222
}

lang/en/quiz_answersheets.php

+25
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,28 @@
5353
$string['create_attempt_modal_description'] = 'Are you sure you want to create a quiz attempt for {$a}?';
5454
$string['create_attempt_modal_button'] = 'Create';
5555
$string['webservicecannotcreateattempts'] = 'Cannot create attempt';
56+
57+
// Question instruction.
58+
$string['coderunner_instruction'] = 'Write your answer in the space provided';
59+
$string['ddwtos_instruction'] = 'Write the letter of the corresponding answer (A, B, C, D, ...) in the space provided.
60+
After the item is the maximum number of times it can be used. e.g. (1) means that the item can be used once, (2) means twice etc. An asterisk (*) means that the items’ use is unlimited';
61+
$string['ddmarker_instruction'] = 'Mark the points on the image and write the letter of corresponding answer (A, B, C, D, …) beside them.
62+
After the item is the maximum number of times it can be used. e.g. (1) means that the item can be used once, (2) means twice etc. An asterisk (*) means that the items’ use is unlimited';
63+
$string['ddimageortext_instruction'] = 'Mark the points on the image and write the letter of corresponding answer (A, B, C, D, …) beside them.
64+
After the item is the maximum number of times it can be used. e.g. (1) means that the item can be used once, (2) means twice etc. An asterisk (*) means that the items’ use is unlimited';
65+
$string['essay_instruction'] = 'Write your answer in the space provided';
66+
$string['match_instruction'] = 'Write the letter of the corresponding answer (A, B, C, D, ...) in the space provided';
67+
$string['multichoice_instruction'] = 'Select the correct answer';
68+
$string['numerical_instruction'] = 'Write your answer (in numerical value) in the space provided';
69+
$string['ordering_instruction'] = 'Write the correct order in the space provided';
70+
$string['oumultiresponse_instruction'] = 'Select the correct answer(s)';
71+
$string['pmatch_instruction'] = 'Write your answer in the space provided. Please keep it to a sentence or two';
72+
$string['pmatchjme_instruction'] = 'Write your answer in the space provided';
73+
$string['gapselect_instruction'] = 'Write the letter of the corresponding answer (A, B, C, D, ...) in the space provided';
74+
$string['shortanswer_instruction'] = 'Write your answer in the space provided. Please keep it to a sentence or two';
75+
$string['stack_instruction'] = 'Write your answer in the space provided';
76+
$string['truefalse_instruction'] = 'Select the correct answer';
77+
$string['varnumeric_instruction'] = 'Write your answer in the space provided';
78+
$string['varnumericset_instruction'] = 'Write your answer in the space provided';
79+
$string['varnumunit_instruction'] = 'Write your answer in the space provided';
80+
$string['wordselect_instruction'] = 'Select the answer(s) by circling the key word(s)';

styles.css

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
#page-mod-quiz-report-answersheets-attemptsheet .que .content .question-instruction {
2+
font-weight: bold;
3+
font-style: italic;
4+
padding-bottom: 10px;
5+
}
6+
17
#page-mod-quiz-report-answersheets-submitresponses .submit-responses {
28
float: right;
39
}

tests/behat/attempt_and_answer_sheet.feature

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Feature: Attempt sheet, Review sheet and Answer sheet feature of the Answer shee
7070
And I should see "Student One" in the "table.quizreviewsummary" "css_element"
7171
And I should not see "Started on" in the "table.quizreviewsummary" "css_element"
7272
And I should not see "State" in the "table.quizreviewsummary" "css_element"
73+
And I should see "Select the correct answer" in the ".question-instruction" "css_element"
7374
And I press the "back" button in the browser
7475
When I click on "View right answers" "link" in the "Student One" "table_row"
7576
Then I should see "First question"

tests/report_test.php

+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
/**
18+
* Tests for the quiz answer sheet report.
19+
*
20+
* @package quiz_answersheets
21+
* @copyright 2019 The Open University
22+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23+
*/
24+
25+
defined('MOODLE_INTERNAL') || die();
26+
27+
/**
28+
* Tests for the quiz answer sheet report.
29+
*
30+
* @package quiz_answersheets
31+
* @copyright 2019 The Open University
32+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33+
*/
34+
class quiz_answersheets_report_testcase extends advanced_testcase {
35+
36+
/**
37+
* Test get_question_instruction function
38+
*
39+
* @dataProvider get_question_instruction_cases
40+
* @param string $questiontype Question type name
41+
* @param string $expectedinstruction Expected instruction for question
42+
*/
43+
public function test_get_question_instruction(string $questiontype, string $expectedinstruction) {
44+
$this->resetAfterTest();
45+
if (question_bank::is_qtype_installed($questiontype)) {
46+
$instruction = \quiz_answersheets\utils::get_question_instruction($questiontype);
47+
$this->assert_same_instruction($expectedinstruction, $instruction);
48+
} else {
49+
$this->markTestSkipped();
50+
}
51+
}
52+
53+
/**
54+
* Test case for test_get_question_instruction
55+
*
56+
* @return array List of test cases
57+
*/
58+
public function get_question_instruction_cases() {
59+
return [
60+
[
61+
'coderunner',
62+
'Write your answer in the space provided'
63+
],
64+
[
65+
'ddwtos',
66+
'Write the letter of the corresponding answer (A, B, C, D, ...) in the space provided.
67+
After the item is the maximum number of times it can be used. e.g. (1) means that the item can be used once, (2) means twice etc. An asterisk (*) means that the items’ use is unlimited'
68+
],
69+
[
70+
'ddmarker',
71+
'Mark the points on the image and write the letter of corresponding answer (A, B, C, D, …) beside them.
72+
After the item is the maximum number of times it can be used. e.g. (1) means that the item can be used once, (2) means twice etc. An asterisk (*) means that the items’ use is unlimited'
73+
],
74+
[
75+
'ddimageortext',
76+
'Mark the points on the image and write the letter of corresponding answer (A, B, C, D, …) beside them.
77+
After the item is the maximum number of times it can be used. e.g. (1) means that the item can be used once, (2) means twice etc. An asterisk (*) means that the items’ use is unlimited'
78+
],
79+
[
80+
'essay',
81+
'Write your answer in the space provided'
82+
],
83+
[
84+
'match',
85+
'Write the letter of the corresponding answer (A, B, C, D, ...) in the space provided'
86+
],
87+
[
88+
'multichoice',
89+
'Select the correct answer'
90+
],
91+
[
92+
'numerical',
93+
'Write your answer (in numerical value) in the space provided'
94+
],
95+
[
96+
'ordering',
97+
'Write the correct order in the space provided'
98+
],
99+
[
100+
'oumultiresponse',
101+
'Select the correct answer(s)'
102+
],
103+
[
104+
'pmatch',
105+
'Write your answer in the space provided. Please keep it to a sentence or two'
106+
],
107+
[
108+
'pmatchjme',
109+
'Write your answer in the space provided'
110+
],
111+
[
112+
'gapselect',
113+
'Write the letter of the corresponding answer (A, B, C, D, ...) in the space provided'
114+
],
115+
[
116+
'shortanswer',
117+
'Write your answer in the space provided. Please keep it to a sentence or two'
118+
],
119+
[
120+
'stack',
121+
'Write your answer in the space provided'
122+
],
123+
[
124+
'truefalse',
125+
'Select the correct answer'
126+
],
127+
[
128+
'varnumeric',
129+
'Write your answer in the space provided'
130+
],
131+
[
132+
'varnumericset',
133+
'Write your answer in the space provided'
134+
],
135+
[
136+
'varnumunit',
137+
'Write your answer in the space provided'
138+
],
139+
[
140+
'wordselect',
141+
'Select the answer(s) by circling the key word(s)'
142+
]
143+
];
144+
}
145+
146+
/**
147+
* Replace line break and assert the instruction
148+
*
149+
* @param string $expectedinstrction Expected instruction
150+
* @param string $instruction Actual instruction
151+
*/
152+
private function assert_same_instruction(string $expectedinstrction, string $instruction): void {
153+
$this->assertEquals(str_replace("\r\n", "\n", $expectedinstrction), str_replace("\r\n", "\n", $instruction));
154+
}
155+
}

0 commit comments

Comments
 (0)