Skip to content

Commit fecd00f

Browse files
author
Mahmoud Kassaei
committed
Qtype_drawlines: Implement shownumcorrect , showmisplaced and remove clearwrong #882022
1 parent 6fc86aa commit fecd00f

File tree

7 files changed

+85
-23
lines changed

7 files changed

+85
-23
lines changed

classes/line.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public static function is_item_positioned_correctly_on_axis($responsecoord, $lin
177177
* Parse the input and return the parts in a list of 'cx', 'cy' with or whothout 'r'.
178178
*
179179
* @param string $dropzone, the string in a given format with or whithout radius
180-
* @param bool $radius, if set to true, return the list with radius, otherwise with radius
180+
* @param bool $radius, if set to true, return the list with radius, otherwise without radius
181181
* @return int[], a list of 'cx', 'cy' with or whothout 'r'.
182182
*/
183183
public static function parse_into_cx_cy_with_or_without_radius(string $dropzone, bool $radius = false): array {

edit_drawlines_form.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,11 @@ protected function get_per_line_fields(MoodleQuickForm $mform, string $label, ar
172172
/**
173173
* Create the form elements required by one hint.
174174
*
175-
* @param string $withclearwrong whether this quesiton type uses the 'Clear wrong' option on hints.
175+
* @param string $withshowmisplaced whether this quesiton type uses the 'show misplaced' option on hints.
176176
* @param string $withshownumpartscorrect whether this quesiton type uses the 'Show num parts correct' option on hints.
177177
* @return array form field elements for one hint.
178178
*/
179-
protected function get_hint_fields($withclearwrong = false, $withshownumpartscorrect = false) {
179+
protected function get_hint_fields($withshowmisplaced = true, $withshownumpartscorrect = false) {
180180
$mform = $this->_form;
181181

182182
$repeated = [];
@@ -187,7 +187,7 @@ protected function get_hint_fields($withclearwrong = false, $withshownumpartscor
187187
$repeated[] = $mform->createElement('checkbox', 'hintshownumcorrect',
188188
get_string('options', 'question'),
189189
get_string('shownumpartscorrect', 'question'));
190-
$repeated[] = $mform->createElement('checkbox', 'hintoptions', '',
190+
$repeated[] = $mform->createElement('checkbox', 'hintshowmisplaced', '',
191191
get_string('showmisplaced', 'qtype_' . $this->qtype()));
192192

193193
return [$repeated, $repeatedoptions];
@@ -256,16 +256,19 @@ protected function data_preprocessing_lines(stdClass $question): object {
256256
}
257257

258258
#[\Override]
259-
protected function data_preprocessing_hints($question, $withclearwrong = false,
260-
$withshownumpartscorrect = false) {
259+
protected function data_preprocessing_hints($question, $withshowmisplaced = true,
260+
$withshownumpartscorrect = false) {
261261
if (empty($question->hints)) {
262262
return $question;
263263
}
264-
parent::data_preprocessing_hints($question, $withclearwrong, $withshownumpartscorrect);
264+
parent::data_preprocessing_hints($question, false, $withshownumpartscorrect);
265265

266266
$question->hintoptions = [];
267267
foreach ($question->hints as $hint) {
268268
$question->hintoptions[] = $hint->options;
269+
if ($question->hintoptions) {
270+
$question->hintshowmisplaced[] = $hint->options;
271+
}
269272
}
270273

271274
return $question;

lang/en/qtype_drawlines.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
$string['correctanswersare'] = 'The correct answers are: {$a}';
3232
$string['dropbackground'] = 'Background image for Draw lines';
3333

34-
$string['formerror_nobgimage'] = 'You need to select an image to use as the background for the drag and drop area.';
3534
$string['formerror_invalidimagesize'] = 'Image file should not be larger than 600x600 px. The uploaded image file size is {$a->width}x{$a->height} px.';
35+
$string['formerror_nobgimage'] = 'You need to select an image to use as the background for the drag and drop area.';
3636
$string['formerror_nolines'] = 'You need to expand \'Line 1\' and fill the form for it';
3737
$string['formerror_notype'] = 'You have to select a type for Line {$a}';
3838
$string['formerror_zoneend'] = 'End zone coordinates should be in x,y;r format, where x,y are the coordinates of the centre of a circle and r is the radius.';

questiontype.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,17 @@ public function save_hints($fromform, $withparts = false) {
115115
}
116116

117117
if ($withparts) {
118-
if (!empty($fromform->hintclearwrong)) {
119-
$numclears = max(array_keys($fromform->hintclearwrong)) + 1;
118+
if (!empty($fromform->hintoptions)) {
119+
$numshowmisplaced = max(array_keys($fromform->hintshowmisplaced)) + 1;
120120
} else {
121-
$numclears = 0;
121+
$numshowmisplaced = 0;
122122
}
123123
if (!empty($fromform->hintshownumcorrect)) {
124124
$numshows = max(array_keys($fromform->hintshownumcorrect)) + 1;
125125
} else {
126126
$numshows = 0;
127127
}
128-
$numhints = max($numhints, $numclears, $numshows);
128+
$numhints = max($numhints, $numshowmisplaced, $numshows);
129129
}
130130

131131
for ($i = 0; $i < $numhints; $i += 1) {
@@ -134,7 +134,7 @@ public function save_hints($fromform, $withparts = false) {
134134
}
135135

136136
if ($withparts) {
137-
$clearwrong = !empty($fromform->hintclearwrong[$i]);
137+
$statewhichincorrect = !empty($fromform->hintshowmisplaced[$i]);
138138
$shownumcorrect = !empty($fromform->hintshownumcorrect[$i]);
139139
$statewhichincorrect = !empty($fromform->hintoptions[$i]);
140140
}
@@ -157,7 +157,7 @@ public function save_hints($fromform, $withparts = false) {
157157
$context, 'question', 'hint', $hint->id);
158158
$hint->hintformat = $fromform->hint[$i]['format'];
159159
if ($withparts) {
160-
$hint->clearwrong = $clearwrong;
160+
$hint->showmisplaced = $statewhichincorrect;
161161
$hint->shownumcorrect = $shownumcorrect;
162162
$hint->options = $statewhichincorrect;
163163
}

renderer.php

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ public function formulation_and_controls(question_attempt $qa, question_display_
164164

165165
#[\Override]
166166
public function specific_feedback(question_attempt $qa) {
167-
return $this->combined_feedback($qa);
167+
$output = '';
168+
$output .= $this->combined_feedback($qa);
169+
$hint = $qa->get_applicable_hint();
170+
return $output;
168171
}
169172

170173
#[\Override]
@@ -193,6 +196,46 @@ public function correct_response(question_attempt $qa) {
193196
return $this->correct_choices($rightanswers);
194197
}
195198

199+
#[\Override]
200+
protected function hint(question_attempt $qa, question_hint $hint) {
201+
$output = '';
202+
$question = $qa->get_question();
203+
$response = $qa->get_last_qt_data();
204+
// Accumulate the wrong coords for each lines to be displayed and hint options.
205+
$wrongcoords = [];
206+
foreach ($question->lines as $key => $line) {
207+
if (in_array($question->field($key), array_keys($response))) {
208+
$coords = explode(' ', $response[$question->field($key)]);
209+
if ($question->grademethod === 'partial') {
210+
// Label the line.
211+
$linelabelstart = null;
212+
$linelabelend = null;
213+
if (!line::is_dragitem_in_the_right_place($coords[0], $line->zonestart)) {
214+
$linelabelstart = ' L' . $line->number;
215+
$wrongcoords[] = $linelabelstart . html_writer::tag('span',
216+
' start =' . $coords[0], ['class' => 'misplaced']);
217+
}
218+
if (!line::is_dragitem_in_the_right_place($coords[1], $line->zoneend)) {
219+
if (is_null($linelabelstart)) {
220+
// Do not repeat the line number for the end-prt of the line coordinates.
221+
$linelabelend = ' L' . $line->number;
222+
}
223+
$wrongcoords[] = $linelabelend . html_writer::tag('span', ' end =' . $coords[1], ['class' => 'misplaced']);
224+
}
225+
} else {
226+
if (!(line::is_dragitem_in_the_right_place($coords[0], $line->zonestart) &&
227+
line::is_dragitem_in_the_right_place($coords[1], $line->zoneend))) {
228+
$wrongcoords[] = ' L' . $line->number . html_writer::tag('span',
229+
' start=' . $coords[0] . ' end=' . $coords[1], ['class' => 'misplaced']);
230+
}
231+
}
232+
}
233+
}
234+
$output .= html_writer::tag('div', get_string('showmisplaced', 'qtype_drawlines', implode($wrongcoords)));
235+
$output .= parent::hint($qa, $hint);
236+
return $output;
237+
}
238+
196239
/**
197240
* Function returns string based on number of correct answers.
198241
*
@@ -214,16 +257,26 @@ protected function correct_choices(array $right): string {
214257
#[\Override]
215258
protected function num_parts_correct(question_attempt $qa): string {
216259
$a = new stdClass();
217-
list($a->num, $a->outof) = $qa->get_question()->get_num_parts_right(
218-
$qa->get_last_qt_data());
219-
if (is_null($a->outof)) {
260+
$grademethod = $qa->get_question()->grademethod;
261+
if ($grademethod === 'partial') {
262+
[$a->num, $a->outof] = $qa->get_question()->get_num_parts_right_grade_partial($qa->get_last_qt_data());
263+
} else {
264+
[$a->num, $a->outof] = $qa->get_question()->get_num_parts_right_grade_allornone($qa->get_last_qt_data());
265+
}
266+
if ($a->num === 0 || is_null($a->outof)) {
220267
return '';
221268
} else if ($a->num == 1) {
222269
return html_writer::tag('p', get_string('yougot1right', 'qtype_drawlines'));
223270
} else {
224271
$f = new NumberFormatter(current_language(), NumberFormatter::SPELLOUT);
225272
$a->num = $f->format($a->num);
226-
return html_writer::tag('p', get_string('yougotnright', 'qtype_drawlines', $a));
273+
$string['yougotnright'] = 'You have correctly selected {$a->num} points.';
274+
$string['yougotnrightline'] = 'You have correctly selected {$a->num} lines.';
275+
if ($grademethod === 'partial') {
276+
return html_writer::tag('p', get_string('yougotnright', 'qtype_drawlines', $a->num));
277+
} else {
278+
return html_writer::tag('p', get_string('yougotnrightline', 'qtype_drawlines', $a->num));
279+
}
227280
}
228281
}
229282
}

styles.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,9 @@ form.mform fieldset#id_previewareaheader .dropbackground {
6565
form.mform fieldset#id_previewareaheader div.ddarea {
6666
text-align: center;
6767
}
68+
69+
.que.drawlines span.misplaced {
70+
background-color: #ff0;
71+
opacity: .6;
72+
margin: 2px;
73+
}

tests/helper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function get_drawlines_question_data_mkmap_twolines(): stdClass {
6565
$qdata->options->incorrectfeedback = test_question_maker::STANDARD_OVERALL_INCORRECT_FEEDBACK;
6666
$qdata->options->incorrectfeedbackformat = FORMAT_HTML;
6767
$qdata->options->shownumcorrect = 1;
68-
$qdata->options->showmisplaced = 0;
68+
$qdata->options->showmisplaced = 1;
6969

7070
$qdata->lines = [
7171
1 => (object)[
@@ -93,7 +93,7 @@ public function get_drawlines_question_data_mkmap_twolines(): stdClass {
9393
'hint' => 'Hint 1.',
9494
'hintformat' => FORMAT_HTML,
9595
'shownumcorrect' => 1,
96-
'showmisplaced' => 0,
96+
'showmisplaced' => 1,
9797
'options' => 0,
9898
],
9999
2 => (object) [
@@ -168,7 +168,7 @@ public function get_drawlines_question_form_data_mkmap_twolines(): stdClass {
168168
],
169169
];
170170
$fromform->hintshownumcorrect = [1, 1];
171-
$fromform->hintclearwrong = [0, 1];
171+
$fromform->hintshowmisplaced = [0, 1];
172172
$fromform->hintoptions = [0, 1];
173173

174174
$fromform->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
@@ -211,7 +211,7 @@ public function make_drawlines_question_mkmap_twolines(): qtype_drawlines_questi
211211
$question->incorrectfeedback = test_question_maker::STANDARD_OVERALL_INCORRECT_FEEDBACK;
212212
$question->incorrectfeedbackformat = FORMAT_HTML;
213213
$question->shownumcorrect = 1;
214-
$question->showmisplaced = 0;
214+
$question->showmisplaced = 1;
215215

216216
$question->lines = [
217217
0 => new line(

0 commit comments

Comments
 (0)