Commit | Line | Data |
---|---|---|
0a4abb73 SH |
1 | <?php |
2 | ||
3 | // This file is part of Moodle - http://moodle.org/ | |
4 | // | |
5 | // Moodle is free software: you can redistribute it and/or modify | |
6 | // it under the terms of the GNU General Public License as published by | |
7 | // the Free Software Foundation, either version 3 of the License, or | |
8 | // (at your option) any later version. | |
9 | // | |
10 | // Moodle is distributed in the hope that it will be useful, | |
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | // GNU General Public License for more details. | |
14 | // | |
15 | // You should have received a copy of the GNU General Public License | |
16 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
17 | ||
18 | /** | |
19 | * Matching | |
20 | * | |
cc3dbaaa PS |
21 | * @package mod |
22 | * @subpackage lesson | |
23 | * @copyright 2009 Sam Hemelryk | |
24 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
0a4abb73 SH |
25 | **/ |
26 | ||
1e7f8ea2 PS |
27 | defined('MOODLE_INTERNAL') || die(); |
28 | ||
0a4abb73 SH |
29 | /** Matching question type */ |
30 | define("LESSON_PAGE_MATCHING", "5"); | |
31 | ||
32 | class lesson_page_type_matching extends lesson_page { | |
33 | ||
34 | protected $type = lesson_page::TYPE_QUESTION; | |
35 | protected $typeid = LESSON_PAGE_MATCHING; | |
36 | protected $typeidstring = 'matching'; | |
37 | protected $string = null; | |
38 | ||
39 | public function get_typeid() { | |
40 | return $this->typeid; | |
41 | } | |
42 | public function get_typestring() { | |
43 | if ($this->string===null) { | |
44 | $this->string = get_string($this->typeidstring, 'lesson'); | |
45 | } | |
46 | return $this->string; | |
47 | } | |
48 | public function get_idstring() { | |
49 | return $this->typeidstring; | |
50 | } | |
51 | public function display($renderer, $attempt) { | |
52 | global $USER, $CFG, $PAGE; | |
53 | $mform = $this->make_answer_form($attempt); | |
54 | $data = new stdClass; | |
55 | $data->id = $PAGE->cm->id; | |
56 | $data->pageid = $this->properties->id; | |
57 | $mform->set_data($data); | |
58 | return $mform->display(); | |
59 | } | |
60 | ||
61 | protected function make_answer_form($attempt=null) { | |
62 | global $USER, $CFG; | |
4fb3f253 | 63 | // don't shuffle answers (could be an option??) |
57a4bd03 RW |
64 | $getanswers = array_slice($this->get_answers(), 2); |
65 | ||
57a4bd03 RW |
66 | $answers = array(); |
67 | foreach ($getanswers as $getanswer) { | |
68 | $answers[$getanswer->id] = $getanswer; | |
69 | } | |
70 | ||
0a4abb73 SH |
71 | $responses = array(); |
72 | foreach ($answers as $answer) { | |
73 | // get all the response | |
de421372 RW |
74 | if ($answer->response != null) { |
75 | $responses[] = trim($answer->response); | |
0a4abb73 SH |
76 | } |
77 | } | |
78 | ||
04932409 | 79 | $responseoptions = array(''=>get_string('choosedots')); |
0a4abb73 | 80 | if (!empty($responses)) { |
de421372 RW |
81 | shuffle($responses); |
82 | foreach ($responses as $response) { | |
83 | $responseoptions[htmlspecialchars($response)] = $response; | |
0a4abb73 SH |
84 | } |
85 | } | |
86 | if (isset($USER->modattempts[$this->lesson->id]) && !empty($attempt->useranswer)) { | |
87 | $useranswers = explode(',', $attempt->useranswer); | |
88 | $t = 0; | |
89 | } else { | |
90 | $useranswers = array(); | |
91 | } | |
92 | ||
93 | $action = $CFG->wwwroot.'/mod/lesson/continue.php'; | |
94 | $params = array('answers'=>$answers, 'useranswers'=>$useranswers, 'responseoptions'=>$responseoptions, 'lessonid'=>$this->lesson->id, 'contents'=>$this->get_contents()); | |
95 | $mform = new lesson_display_answer_form_matching($action, $params); | |
96 | return $mform; | |
97 | } | |
98 | ||
99 | public function create_answers($properties) { | |
100 | global $DB; | |
101 | // now add the answers | |
102 | $newanswer = new stdClass; | |
103 | $newanswer->lessonid = $this->lesson->id; | |
104 | $newanswer->pageid = $this->properties->id; | |
105 | $newanswer->timecreated = $this->properties->timecreated; | |
106 | ||
107 | $answers = array(); | |
108 | ||
109 | // need to add two to offset correct response and wrong response | |
110 | $this->lesson->maxanswers = $this->lesson->maxanswers + 2; | |
111 | for ($i = 0; $i < $this->lesson->maxanswers; $i++) { | |
112 | $answer = clone($newanswer); | |
981debb7 | 113 | if (!empty($properties->answer_editor[$i]) && is_array($properties->answer_editor[$i])) { |
01c37ef1 PS |
114 | $answer->answer = $properties->answer_editor[$i]['text']; |
115 | $answer->answerformat = $properties->answer_editor[$i]['format']; | |
981debb7 | 116 | } |
0fabd979 JR |
117 | if (!empty($properties->response_editor[$i])) { |
118 | $answer->response = $properties->response_editor[$i]; | |
119 | $answer->responseformat = 0; | |
981debb7 SH |
120 | } |
121 | ||
b43251b8 RW |
122 | if (isset($properties->jumpto[$i])) { |
123 | $answer->jumpto = $properties->jumpto[$i]; | |
124 | } | |
125 | if ($this->lesson->custom && isset($properties->score[$i])) { | |
126 | $answer->score = $properties->score[$i]; | |
127 | } | |
128 | ||
6d84e82f | 129 | if (isset($answer->answer) && $answer->answer != '') { |
0a4abb73 SH |
130 | $answer->id = $DB->insert_record("lesson_answers", $answer); |
131 | $answers[$answer->id] = new lesson_page_answer($answer); | |
132 | } else if ($i < 2) { | |
133 | $answer->id = $DB->insert_record("lesson_answers", $answer); | |
134 | $answers[$answer->id] = new lesson_page_answer($answer); | |
135 | } else { | |
136 | break; | |
137 | } | |
138 | } | |
139 | $this->answers = $answers; | |
140 | return $answers; | |
141 | } | |
142 | ||
143 | public function check_answer() { | |
df0442c6 | 144 | global $CFG, $PAGE; |
01c37ef1 | 145 | |
39790bd8 | 146 | $formattextdefoptions = new stdClass(); |
01c37ef1 PS |
147 | $formattextdefoptions->noclean = true; |
148 | $formattextdefoptions->para = false; | |
149 | ||
0a4abb73 SH |
150 | $result = parent::check_answer(); |
151 | ||
152 | $mform = $this->make_answer_form(); | |
153 | ||
154 | $data = $mform->get_data(); | |
155 | require_sesskey(); | |
156 | ||
157 | if (!$data) { | |
a6855934 | 158 | redirect(new moodle_url('/mod/lesson/view.php', array('id'=>$PAGE->cm->id, 'pageid'=>$this->properties->id))); |
0a4abb73 | 159 | } |
2f67a9b3 | 160 | |
0a4abb73 | 161 | $response = $data->response; |
c0f177f1 | 162 | $getanswers = $this->get_answers(); |
a675ada5 | 163 | |
c0f177f1 RW |
164 | $correct = array_shift($getanswers); |
165 | $wrong = array_shift($getanswers); | |
a675ada5 | 166 | |
c0f177f1 | 167 | $answers = array(); |
de421372 | 168 | foreach ($getanswers as $key => $answer) { |
7f529009 RW |
169 | if ($answer->answer !== '' or $answer->response !== '') { |
170 | $answers[$answer->id] = $answer; | |
0a4abb73 | 171 | } |
0a4abb73 | 172 | } |
de421372 | 173 | |
152a040a | 174 | // get the user's exact responses for record keeping |
a675ada5 | 175 | $hits = 0; |
0a4abb73 | 176 | $userresponse = array(); |
7f529009 | 177 | foreach ($response as $id => $value) { |
b26066bf RW |
178 | if ($value == '') { |
179 | $result->noanswer = true; | |
180 | return $result; | |
181 | } | |
de421372 | 182 | $value = htmlspecialchars_decode($value); |
919a40f3 | 183 | $userresponse[] = $value; |
152a040a | 184 | // Make sure the user's answer exists in question's answer |
9705bfa8 | 185 | if (array_key_exists($id, $answers)) { |
7f529009 | 186 | $answer = $answers[$id]; |
de421372 RW |
187 | $result->studentanswer .= '<br />'.format_text($answer->answer, $answer->answerformat, $formattextdefoptions).' = '.$value; |
188 | if (trim($answer->response) == trim($value)) { | |
a675ada5 PS |
189 | $hits++; |
190 | } | |
0a4abb73 SH |
191 | } |
192 | } | |
7f529009 | 193 | |
0a4abb73 SH |
194 | $result->userresponse = implode(",", $userresponse); |
195 | ||
a675ada5 | 196 | if ($hits == count($answers)) { |
0a4abb73 | 197 | $result->correctanswer = true; |
a675ada5 PS |
198 | $result->response = format_text($correct->answer, $correct->answerformat, $formattextdefoptions); |
199 | $result->answerid = $correct->id; | |
200 | $result->newpageid = $correct->jumpto; | |
0a4abb73 | 201 | } else { |
a675ada5 PS |
202 | $result->correctanswer = false; |
203 | $result->response = format_text($wrong->answer, $wrong->answerformat, $formattextdefoptions); | |
204 | $result->answerid = $wrong->id; | |
205 | $result->newpageid = $wrong->jumpto; | |
0a4abb73 | 206 | } |
a675ada5 | 207 | |
0a4abb73 SH |
208 | return $result; |
209 | } | |
210 | ||
211 | public function option_description_string() { | |
212 | return get_string("firstanswershould", "lesson"); | |
213 | } | |
214 | ||
215 | public function display_answers(html_table $table) { | |
216 | $answers = $this->get_answers(); | |
217 | $options = new stdClass; | |
218 | $options->noclean = true; | |
219 | $options->para = false; | |
220 | $i = 1; | |
221 | $n = 0; | |
2f67a9b3 | 222 | |
0a4abb73 SH |
223 | foreach ($answers as $answer) { |
224 | if ($n < 2) { | |
225 | if ($answer->answer != NULL) { | |
226 | $cells = array(); | |
227 | if ($n == 0) { | |
228 | $cells[] = "<span class=\"label\">".get_string("correctresponse", "lesson").'</span>'; | |
229 | } else { | |
230 | $cells[] = "<span class=\"label\">".get_string("wrongresponse", "lesson").'</span>'; | |
231 | } | |
01c37ef1 | 232 | $cells[] = format_text($answer->answer, $answer->answerformat, $options); |
8cea545e | 233 | $table->data[] = new html_table_row($cells); |
0a4abb73 | 234 | } |
b43251b8 RW |
235 | |
236 | if ($n == 0) { | |
237 | $cells = array(); | |
238 | $cells[] = '<span class="label">'.get_string("correctanswerscore", "lesson")."</span>: "; | |
239 | $cells[] = $answer->score; | |
240 | $table->data[] = new html_table_row($cells); | |
241 | ||
242 | $cells = array(); | |
243 | $cells[] = '<span class="label">'.get_string("correctanswerjump", "lesson")."</span>: "; | |
244 | $cells[] = $this->get_jump_name($answer->jumpto); | |
245 | $table->data[] = new html_table_row($cells); | |
246 | } elseif ($n == 1) { | |
247 | $cells = array(); | |
248 | $cells[] = '<span class="label">'.get_string("wronganswerscore", "lesson")."</span>: "; | |
249 | $cells[] = $answer->score; | |
250 | $table->data[] = new html_table_row($cells); | |
251 | ||
252 | $cells = array(); | |
253 | $cells[] = '<span class="label">'.get_string("wronganswerjump", "lesson")."</span>: "; | |
254 | $cells[] = $this->get_jump_name($answer->jumpto); | |
255 | $table->data[] = new html_table_row($cells); | |
256 | } | |
257 | ||
258 | if ($n === 0){ | |
259 | $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;'; | |
260 | } | |
0a4abb73 SH |
261 | $n++; |
262 | $i--; | |
263 | } else { | |
264 | $cells = array(); | |
265 | if ($this->lesson->custom && $answer->score > 0) { | |
266 | // if the score is > 0, then it is correct | |
267 | $cells[] = '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n"; | |
268 | } else if ($this->lesson->custom) { | |
269 | $cells[] = '<span class="label">'.get_string("answer", "lesson")." $i</span>: \n"; | |
270 | } else if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) { | |
271 | $cells[] = '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n"; | |
272 | } else { | |
273 | $cells[] = '<span class="label">'.get_string("answer", "lesson")." $i</span>: \n"; | |
274 | } | |
01c37ef1 | 275 | $cells[] = format_text($answer->answer, $answer->answerformat, $options); |
8cea545e | 276 | $table->data[] = new html_table_row($cells); |
0a4abb73 SH |
277 | |
278 | $cells = array(); | |
279 | $cells[] = '<span class="label">'.get_string("matchesanswer", "lesson")." $i</span>: "; | |
01c37ef1 | 280 | $cells[] = format_text($answer->response, $answer->responseformat, $options); |
8cea545e | 281 | $table->data[] = new html_table_row($cells); |
0a4abb73 | 282 | } |
0a4abb73 SH |
283 | $i++; |
284 | } | |
285 | return $table; | |
286 | } | |
981debb7 SH |
287 | /** |
288 | * Updates the page and its answers | |
289 | * | |
290 | * @global moodle_database $DB | |
291 | * @global moodle_page $PAGE | |
292 | * @param stdClass $properties | |
293 | * @return bool | |
294 | */ | |
0ed6f712 | 295 | public function update($properties, $context = null, $maxbytes = null) { |
0a4abb73 SH |
296 | global $DB, $PAGE; |
297 | $answers = $this->get_answers(); | |
298 | $properties->id = $this->properties->id; | |
299 | $properties->lessonid = $this->lesson->id; | |
64f93798 | 300 | $properties = file_postupdate_standard_editor($properties, 'contents', array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes), get_context_instance(CONTEXT_MODULE, $PAGE->cm->id), 'mod_lesson', 'page_contents', $properties->id); |
0a4abb73 SH |
301 | $DB->update_record("lesson_pages", $properties); |
302 | ||
303 | // need to add two to offset correct response and wrong response | |
304 | $this->lesson->maxanswers += 2; | |
305 | for ($i = 0; $i < $this->lesson->maxanswers; $i++) { | |
306 | if (!array_key_exists($i, $this->answers)) { | |
307 | $this->answers[$i] = new stdClass; | |
308 | $this->answers[$i]->lessonid = $this->lesson->id; | |
309 | $this->answers[$i]->pageid = $this->id; | |
310 | $this->answers[$i]->timecreated = $this->timecreated; | |
311 | } | |
981debb7 SH |
312 | |
313 | if (!empty($properties->answer_editor[$i]) && is_array($properties->answer_editor[$i])) { | |
01c37ef1 PS |
314 | $this->answers[$i]->answer = $properties->answer_editor[$i]['text']; |
315 | $this->answers[$i]->answerformat = $properties->answer_editor[$i]['format']; | |
981debb7 | 316 | } |
0fabd979 JR |
317 | if (!empty($properties->response_editor[$i])) { |
318 | $this->answers[$i]->response = $properties->response_editor[$i]; | |
319 | $this->answers[$i]->responseformat = 0; | |
981debb7 SH |
320 | } |
321 | ||
b43251b8 RW |
322 | if (isset($properties->jumpto[$i])) { |
323 | $this->answers[$i]->jumpto = $properties->jumpto[$i]; | |
324 | } | |
325 | if ($this->lesson->custom && isset($properties->score[$i])) { | |
326 | $this->answers[$i]->score = $properties->score[$i]; | |
327 | } | |
328 | ||
6d84e82f RW |
329 | // we don't need to check for isset here because properties called it's own isset method. |
330 | if ($this->answers[$i]->answer != '') { | |
0a4abb73 SH |
331 | if (!isset($this->answers[$i]->id)) { |
332 | $this->answers[$i]->id = $DB->insert_record("lesson_answers", $this->answers[$i]); | |
333 | } else { | |
334 | $DB->update_record("lesson_answers", $this->answers[$i]->properties()); | |
335 | } | |
0a4abb73 SH |
336 | } else if ($i < 2) { |
337 | if (!isset($this->answers[$i]->id)) { | |
338 | $this->answers[$i]->id = $DB->insert_record("lesson_answers", $this->answers[$i]); | |
339 | } else { | |
340 | $DB->update_record("lesson_answers", $this->answers[$i]->properties()); | |
341 | } | |
342 | ||
981debb7 SH |
343 | } else if (isset($this->answers[$i]->id)) { |
344 | $DB->delete_records('lesson_answers', array('id'=>$this->answers[$i]->id)); | |
345 | unset($this->answers[$i]); | |
0a4abb73 SH |
346 | } |
347 | } | |
348 | return true; | |
349 | } | |
350 | public function stats(array &$pagestats, $tries) { | |
351 | if(count($tries) > $this->lesson->maxattempts) { // if there are more tries than the max that is allowed, grab the last "legal" attempt | |
352 | $temp = $tries[$this->lesson->maxattempts - 1]; | |
353 | } else { | |
354 | // else, user attempted the question less than the max, so grab the last one | |
355 | $temp = end($tries); | |
356 | } | |
357 | if ($temp->correct) { | |
358 | if (isset($pagestats[$temp->pageid]["correct"])) { | |
359 | $pagestats[$temp->pageid]["correct"]++; | |
360 | } else { | |
361 | $pagestats[$temp->pageid]["correct"] = 1; | |
362 | } | |
363 | } | |
364 | if (isset($pagestats[$temp->pageid]["total"])) { | |
365 | $pagestats[$temp->pageid]["total"]++; | |
366 | } else { | |
367 | $pagestats[$temp->pageid]["total"] = 1; | |
368 | } | |
369 | return true; | |
370 | } | |
371 | public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) { | |
372 | $answers = array(); | |
373 | foreach ($this->get_answers() as $answer) { | |
374 | $answers[$answer->id] = $answer; | |
375 | } | |
376 | $formattextdefoptions = new stdClass; | |
377 | $formattextdefoptions->para = false; //I'll use it widely in this page | |
378 | foreach ($answers as $answer) { | |
379 | if ($n == 0 && $useranswer != NULL && $useranswer->correct) { | |
380 | if ($answer->response == NULL && $useranswer != NULL) { | |
381 | $answerdata->response = get_string("thatsthecorrectanswer", "lesson"); | |
382 | } else { | |
383 | $answerdata->response = $answer->response; | |
384 | } | |
c90ba16f | 385 | if ($this->lesson->custom) { |
6d3ae910 AB |
386 | $answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score; |
387 | } else { | |
388 | $answerdata->score = get_string("receivedcredit", "lesson"); | |
389 | } | |
0a4abb73 SH |
390 | } elseif ($n == 1 && $useranswer != NULL && !$useranswer->correct) { |
391 | if ($answer->response == NULL && $useranswer != NULL) { | |
392 | $answerdata->response = get_string("thatsthewronganswer", "lesson"); | |
393 | } else { | |
394 | $answerdata->response = $answer->response; | |
395 | } | |
c90ba16f RW |
396 | if ($this->lesson->custom) { |
397 | $answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score; | |
398 | } else { | |
399 | $answerdata->score = get_string("didnotreceivecredit", "lesson"); | |
0a4abb73 | 400 | } |
c90ba16f | 401 | } elseif ($n > 1) { |
077e06f6 | 402 | $data = '<label class="accesshide" for="answer_' . $n . '">' . get_string('answer', 'lesson') . '</label>'; |
de421372 RW |
403 | $data .= strip_tags(format_string($answer->answer)) . ' '; |
404 | if ($useranswer != null) { | |
0a4abb73 | 405 | $userresponse = explode(",", $useranswer->useranswer); |
077e06f6 RW |
406 | $data .= '<label class="accesshide" for="stu_answer_response_' . $n . '">' . get_string('matchesanswer', 'lesson') . '</label>'; |
407 | $data .= "<select id=\"stu_answer_response_" . $n . "\" disabled=\"disabled\"><option selected=\"selected\">"; | |
981debb7 | 408 | if (array_key_exists($i, $userresponse)) { |
de421372 | 409 | $data .= $userresponse[$i]; |
981debb7 SH |
410 | } |
411 | $data .= "</option></select>"; | |
0a4abb73 | 412 | } else { |
077e06f6 RW |
413 | $data .= '<label class="accesshide" for="answer_response_' . $n . '">' . get_string('matchesanswer', 'lesson') . '</label>'; |
414 | $data .= "<select id=\"answer_response_" . $n . "\" disabled=\"disabled\"><option selected=\"selected\">".strip_tags(format_string($answer->response))."</option></select>"; | |
0a4abb73 SH |
415 | } |
416 | ||
417 | if ($n == 2) { | |
418 | if (isset($pagestats[$this->properties->id])) { | |
419 | if (!array_key_exists('correct', $pagestats[$this->properties->id])) { | |
420 | $pagestats[$this->properties->id]["correct"] = 0; | |
421 | } | |
422 | $percent = $pagestats[$this->properties->id]["correct"] / $pagestats[$this->properties->id]["total"] * 100; | |
423 | $percent = round($percent, 2); | |
424 | $percent .= "% ".get_string("answeredcorrectly", "lesson"); | |
425 | } else { | |
426 | $percent = get_string("nooneansweredthisquestion", "lesson"); | |
427 | } | |
428 | } else { | |
c90ba16f | 429 | $percent = ''; |
0a4abb73 SH |
430 | } |
431 | ||
432 | $answerdata->answers[] = array($data, $percent); | |
433 | $i++; | |
434 | } | |
435 | $n++; | |
436 | $answerpage->answerdata = $answerdata; | |
437 | } | |
438 | return $answerpage; | |
439 | } | |
440 | public function get_jumps() { | |
441 | global $DB; | |
86d99db3 | 442 | // The jumps for matching question type are stored in the 1st and 2nd answer record. |
0a4abb73 | 443 | $jumps = array(); |
04932409 | 444 | if ($answers = $DB->get_records("lesson_answers", array("lessonid" => $this->lesson->id, "pageid" => $this->properties->id), 'id', '*', 0, 2)) { |
0a4abb73 SH |
445 | foreach ($answers as $answer) { |
446 | $jumps[] = $this->get_jump_name($answer->jumpto); | |
447 | } | |
9170dea7 SH |
448 | } else { |
449 | $jumps[] = $this->get_jump_name($this->properties->nextpageid); | |
0a4abb73 SH |
450 | } |
451 | return $jumps; | |
452 | } | |
453 | } | |
454 | ||
455 | class lesson_add_page_form_matching extends lesson_add_page_form_base { | |
456 | ||
457 | public $qtype = 'matching'; | |
458 | public $qtypestring = 'matching'; | |
459 | ||
460 | public function custom_definition() { | |
461 | ||
462 | $this->_form->addElement('header', 'correctresponse', get_string('correctresponse', 'lesson')); | |
cb2c1963 | 463 | $this->_form->addElement('editor', 'answer_editor[0]', get_string('correctresponse', 'lesson'), array('rows'=>'4', 'columns'=>'80'), array('noclean'=>true)); |
76f5f66f | 464 | $this->add_jumpto(0, get_string('correctanswerjump','lesson'), LESSON_NEXTPAGE); |
b43251b8 | 465 | $this->add_score(0, get_string("correctanswerscore", "lesson"), 1); |
0a4abb73 SH |
466 | |
467 | $this->_form->addElement('header', 'wrongresponse', get_string('wrongresponse', 'lesson')); | |
cb2c1963 | 468 | $this->_form->addElement('editor', 'answer_editor[1]', get_string('wrongresponse', 'lesson'), array('rows'=>'4', 'columns'=>'80'), array('noclean'=>true)); |
76f5f66f | 469 | $this->add_jumpto(1, get_string('wronganswerjump','lesson'), LESSON_THISPAGE); |
b43251b8 | 470 | $this->add_score(1, get_string("wronganswerscore", "lesson"), 0); |
0a4abb73 SH |
471 | |
472 | for ($i = 2; $i < $this->_customdata['lesson']->maxanswers+2; $i++) { | |
473 | $this->_form->addElement('header', 'matchingpair'.($i-1), get_string('matchingpair', 'lesson', $i-1)); | |
a675ada5 | 474 | $this->add_answer($i, NULL, ($i < 4)); |
0fabd979 JR |
475 | $required = ($i < 4); |
476 | $label = get_string('matchesanswer','lesson'); | |
477 | $count = $i; | |
478 | $this->_form->addElement('text', 'response_editor['.$count.']', $label, array('size'=>'50')); | |
479 | $this->_form->setDefault('response_editor['.$count.']', ''); | |
480 | if ($required) { | |
481 | $this->_form->addRule('response_editor['.$count.']', get_string('required'), 'required', null, 'client'); | |
482 | } | |
0a4abb73 SH |
483 | } |
484 | } | |
485 | } | |
486 | ||
0a4abb73 SH |
487 | class lesson_display_answer_form_matching extends moodleform { |
488 | ||
489 | public function definition() { | |
490 | global $USER, $OUTPUT; | |
491 | $mform = $this->_form; | |
492 | $answers = $this->_customdata['answers']; | |
493 | $useranswers = $this->_customdata['useranswers']; | |
494 | $responseoptions = $this->_customdata['responseoptions']; | |
495 | $lessonid = $this->_customdata['lessonid']; | |
496 | $contents = $this->_customdata['contents']; | |
497 | ||
ffdf7f8a DM |
498 | $mform->addElement('header', 'pageheader'); |
499 | ||
500 | $mform->addElement('html', $OUTPUT->container($contents, 'contents')); | |
0a4abb73 | 501 | |
abd5c24e RW |
502 | $hasattempt = false; |
503 | $disabled = ''; | |
504 | if (isset($useranswers) && !empty($useranswers)) { | |
505 | $hasattempt = true; | |
506 | $disabled = array('disabled' => 'disabled'); | |
507 | } | |
508 | ||
0a4abb73 SH |
509 | $options = new stdClass; |
510 | $options->para = false; | |
511 | $options->noclean = true; | |
512 | ||
513 | $mform->addElement('hidden', 'id'); | |
514 | $mform->setType('id', PARAM_INT); | |
515 | ||
516 | $mform->addElement('hidden', 'pageid'); | |
517 | $mform->setType('pageid', PARAM_INT); | |
518 | ||
519 | $i = 0; | |
de421372 | 520 | |
0a4abb73 SH |
521 | foreach ($answers as $answer) { |
522 | $mform->addElement('html', '<div class="answeroption">'); | |
de421372 | 523 | if ($answer->response != null) { |
abd5c24e RW |
524 | $responseid = 'response['.$answer->id.']'; |
525 | if ($hasattempt) { | |
526 | $responseid = 'response_'.$answer->id; | |
919a40f3 | 527 | $mform->addElement('hidden', 'response['.$answer->id.']', htmlspecialchars($useranswers[$i])); |
abd5c24e RW |
528 | $mform->setType('response['.$answer->id.']', PARAM_TEXT); |
529 | } | |
530 | $mform->addElement('select', $responseid, format_text($answer->answer,$answer->answerformat,$options), $responseoptions, $disabled); | |
531 | $mform->setType($responseid, PARAM_TEXT); | |
532 | if ($hasattempt) { | |
de421372 | 533 | $mform->setDefault($responseid, htmlspecialchars(trim($useranswers[$i]))); |
0a4abb73 | 534 | } else { |
abd5c24e | 535 | $mform->setDefault($responseid, 'answeroption'); |
0a4abb73 SH |
536 | } |
537 | } | |
538 | $mform->addElement('html', '</div>'); | |
539 | $i++; | |
540 | } | |
abd5c24e RW |
541 | if ($hasattempt) { |
542 | $this->add_action_buttons(null, get_string("nextpage", "lesson")); | |
543 | } else { | |
544 | $this->add_action_buttons(null, get_string("submit", "lesson")); | |
545 | } | |
0a4abb73 SH |
546 | } |
547 | ||
ffdf7f8a | 548 | } |