1 |
efrain |
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 |
*
|
|
|
21 |
* @package mod_lesson
|
|
|
22 |
* @copyright 2009 Sam Hemelryk
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
**/
|
|
|
25 |
|
|
|
26 |
defined('MOODLE_INTERNAL') || die();
|
|
|
27 |
|
|
|
28 |
/** Matching question type */
|
|
|
29 |
define("LESSON_PAGE_MATCHING", "5");
|
|
|
30 |
|
|
|
31 |
class lesson_page_type_matching extends lesson_page {
|
|
|
32 |
|
|
|
33 |
protected $type = lesson_page::TYPE_QUESTION;
|
|
|
34 |
protected $typeid = LESSON_PAGE_MATCHING;
|
|
|
35 |
protected $typeidstring = 'matching';
|
|
|
36 |
protected $string = null;
|
|
|
37 |
|
|
|
38 |
public function get_typeid() {
|
|
|
39 |
return $this->typeid;
|
|
|
40 |
}
|
|
|
41 |
public function get_typestring() {
|
|
|
42 |
if ($this->string===null) {
|
|
|
43 |
$this->string = get_string($this->typeidstring, 'lesson');
|
|
|
44 |
}
|
|
|
45 |
return $this->string;
|
|
|
46 |
}
|
|
|
47 |
public function get_idstring() {
|
|
|
48 |
return $this->typeidstring;
|
|
|
49 |
}
|
|
|
50 |
public function display($renderer, $attempt) {
|
|
|
51 |
global $USER, $CFG, $PAGE;
|
|
|
52 |
$mform = $this->make_answer_form($attempt);
|
|
|
53 |
$data = new stdClass;
|
|
|
54 |
$data->id = $PAGE->cm->id;
|
|
|
55 |
$data->pageid = $this->properties->id;
|
|
|
56 |
$mform->set_data($data);
|
|
|
57 |
|
|
|
58 |
// Trigger an event question viewed.
|
|
|
59 |
$eventparams = array(
|
|
|
60 |
'context' => context_module::instance($PAGE->cm->id),
|
|
|
61 |
'objectid' => $this->properties->id,
|
|
|
62 |
'other' => array(
|
|
|
63 |
'pagetype' => $this->get_typestring()
|
|
|
64 |
)
|
|
|
65 |
);
|
|
|
66 |
|
|
|
67 |
$event = \mod_lesson\event\question_viewed::create($eventparams);
|
|
|
68 |
$event->trigger();
|
|
|
69 |
return $mform->display();
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
protected function make_answer_form($attempt=null) {
|
|
|
73 |
global $USER, $CFG;
|
|
|
74 |
// don't shuffle answers (could be an option??)
|
|
|
75 |
$getanswers = array_slice($this->get_answers(), 2);
|
|
|
76 |
|
|
|
77 |
$answers = array();
|
|
|
78 |
foreach ($getanswers as $getanswer) {
|
|
|
79 |
$answers[$getanswer->id] = $getanswer;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
// Calculate the text for the dropdown, keyed by the non formatted version.
|
|
|
83 |
$responses = array();
|
|
|
84 |
foreach ($answers as $answer) {
|
|
|
85 |
// Get all the response.
|
|
|
86 |
if ($answer->response != null) {
|
|
|
87 |
$responses[trim($answer->response)] = format_text(trim($answer->response));
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
// Now shuffle the answers to randomise the order of the items in the dropdown.
|
|
|
92 |
$responseoptions = ['' => get_string('choosedots')];
|
|
|
93 |
if (!empty($responses)) {
|
|
|
94 |
$keys = array_keys($responses);
|
|
|
95 |
shuffle($keys);
|
|
|
96 |
foreach ($keys as $key) {
|
|
|
97 |
$responseoptions[$key] = $responses[$key];
|
|
|
98 |
}
|
|
|
99 |
}
|
|
|
100 |
if (isset($USER->modattempts[$this->lesson->id]) && !empty($attempt->useranswer)) {
|
|
|
101 |
$useranswers = explode(',', $attempt->useranswer);
|
|
|
102 |
$t = 0;
|
|
|
103 |
} else {
|
|
|
104 |
$useranswers = array();
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
$action = $CFG->wwwroot.'/mod/lesson/continue.php';
|
|
|
108 |
$params = array('answers'=>$answers, 'useranswers'=>$useranswers, 'responseoptions'=>$responseoptions, 'lessonid'=>$this->lesson->id, 'contents'=>$this->get_contents());
|
|
|
109 |
$mform = new lesson_display_answer_form_matching($action, $params);
|
|
|
110 |
return $mform;
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
public function create_answers($properties) {
|
|
|
114 |
global $DB, $PAGE;
|
|
|
115 |
// now add the answers
|
|
|
116 |
$newanswer = new stdClass;
|
|
|
117 |
$newanswer->lessonid = $this->lesson->id;
|
|
|
118 |
$newanswer->pageid = $this->properties->id;
|
|
|
119 |
$newanswer->timecreated = $this->properties->timecreated;
|
|
|
120 |
|
|
|
121 |
$cm = get_coursemodule_from_instance('lesson', $this->lesson->id, $this->lesson->course);
|
|
|
122 |
$context = context_module::instance($cm->id);
|
|
|
123 |
|
|
|
124 |
// Check for duplicate response format.
|
|
|
125 |
$duplicateresponse = array();
|
|
|
126 |
if (is_array($properties->response_editor) && // If there are response_editors to iterate.
|
|
|
127 |
is_array(reset($properties->response_editor))) { // And they come split into text & format array.
|
|
|
128 |
foreach ($properties->response_editor as $response) { // Iterate over all them.
|
|
|
129 |
$duplicateresponse[] = $response['text']; // Picking the text only. This pagetype is that way.
|
|
|
130 |
}
|
|
|
131 |
$properties->response_editor = $duplicateresponse;
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
$answers = array();
|
|
|
135 |
|
|
|
136 |
// need to add two to offset correct response and wrong response
|
|
|
137 |
$this->lesson->maxanswers = $this->lesson->maxanswers + 2;
|
|
|
138 |
for ($i = 0; $i < $this->lesson->maxanswers; $i++) {
|
|
|
139 |
$answer = clone($newanswer);
|
|
|
140 |
if (!empty($properties->answer_editor[$i]) && is_array($properties->answer_editor[$i])) {
|
|
|
141 |
$answer->answer = $properties->answer_editor[$i]['text'];
|
|
|
142 |
$answer->answerformat = $properties->answer_editor[$i]['format'];
|
|
|
143 |
}
|
|
|
144 |
if (!empty($properties->response_editor[$i])) {
|
|
|
145 |
$answer->response = $properties->response_editor[$i];
|
|
|
146 |
$answer->responseformat = 0;
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
if (isset($properties->jumpto[$i])) {
|
|
|
150 |
$answer->jumpto = $properties->jumpto[$i];
|
|
|
151 |
}
|
|
|
152 |
if ($this->lesson->custom && isset($properties->score[$i])) {
|
|
|
153 |
$answer->score = $properties->score[$i];
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
if (isset($answer->answer) && $answer->answer != '') {
|
|
|
157 |
$answer->id = $DB->insert_record("lesson_answers", $answer);
|
|
|
158 |
$this->save_answers_files($context, $PAGE->course->maxbytes,
|
|
|
159 |
$answer, $properties->answer_editor[$i]);
|
|
|
160 |
$answers[$answer->id] = new lesson_page_answer($answer);
|
|
|
161 |
} else if ($i < 2) {
|
|
|
162 |
$answer->id = $DB->insert_record("lesson_answers", $answer);
|
|
|
163 |
$answers[$answer->id] = new lesson_page_answer($answer);
|
|
|
164 |
} else {
|
|
|
165 |
break;
|
|
|
166 |
}
|
|
|
167 |
}
|
|
|
168 |
$this->answers = $answers;
|
|
|
169 |
return $answers;
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
public function check_answer() {
|
|
|
173 |
global $CFG, $PAGE;
|
|
|
174 |
|
|
|
175 |
$formattextdefoptions = new stdClass();
|
|
|
176 |
$formattextdefoptions->noclean = true;
|
|
|
177 |
$formattextdefoptions->para = false;
|
|
|
178 |
|
|
|
179 |
$result = parent::check_answer();
|
|
|
180 |
|
|
|
181 |
$mform = $this->make_answer_form();
|
|
|
182 |
|
|
|
183 |
$data = $mform->get_data();
|
|
|
184 |
require_sesskey();
|
|
|
185 |
|
|
|
186 |
if (!$data) {
|
|
|
187 |
$result->inmediatejump = true;
|
|
|
188 |
$result->newpageid = $this->properties->id;
|
|
|
189 |
return $result;
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
$response = $data->response;
|
|
|
193 |
$getanswers = $this->get_answers();
|
|
|
194 |
foreach ($getanswers as $key => $answer) {
|
|
|
195 |
$getanswers[$key] = parent::rewrite_answers_urls($answer);
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
$correct = array_shift($getanswers);
|
|
|
199 |
$wrong = array_shift($getanswers);
|
|
|
200 |
|
|
|
201 |
$answers = array();
|
|
|
202 |
foreach ($getanswers as $key => $answer) {
|
|
|
203 |
if ($answer->answer !== '' or $answer->response !== '') {
|
|
|
204 |
$answers[$answer->id] = $answer;
|
|
|
205 |
}
|
|
|
206 |
}
|
|
|
207 |
|
|
|
208 |
// get the user's exact responses for record keeping
|
|
|
209 |
$hits = 0;
|
|
|
210 |
$userresponse = array();
|
|
|
211 |
$result->studentanswerformat = FORMAT_HTML;
|
|
|
212 |
foreach ($response as $id => $value) {
|
|
|
213 |
if ($value == '') {
|
|
|
214 |
$result->noanswer = true;
|
|
|
215 |
return $result;
|
|
|
216 |
}
|
|
|
217 |
$userresponse[] = $value;
|
|
|
218 |
// Make sure the user's answer exists in question's answer
|
|
|
219 |
if (array_key_exists($id, $answers)) {
|
|
|
220 |
$answer = $answers[$id];
|
|
|
221 |
$result->studentanswer .= '<br />'.format_text($answer->answer, $answer->answerformat, $formattextdefoptions).' = '.$value;
|
|
|
222 |
if (trim($answer->response) == trim($value)) {
|
|
|
223 |
$hits++;
|
|
|
224 |
}
|
|
|
225 |
}
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
$result->userresponse = implode(",", $userresponse);
|
|
|
229 |
|
|
|
230 |
if ($hits == count($answers)) {
|
|
|
231 |
$result->correctanswer = true;
|
|
|
232 |
$result->response = format_text($correct->answer, $correct->answerformat, $formattextdefoptions);
|
|
|
233 |
$result->answerid = $correct->id;
|
|
|
234 |
$result->newpageid = $correct->jumpto;
|
|
|
235 |
} else {
|
|
|
236 |
$result->correctanswer = false;
|
|
|
237 |
$result->response = format_text($wrong->answer, $wrong->answerformat, $formattextdefoptions);
|
|
|
238 |
$result->answerid = $wrong->id;
|
|
|
239 |
$result->newpageid = $wrong->jumpto;
|
|
|
240 |
}
|
|
|
241 |
|
|
|
242 |
return $result;
|
|
|
243 |
}
|
|
|
244 |
|
|
|
245 |
public function option_description_string() {
|
|
|
246 |
return get_string("firstanswershould", "lesson");
|
|
|
247 |
}
|
|
|
248 |
|
|
|
249 |
public function display_answers(html_table $table) {
|
|
|
250 |
$answers = $this->get_answers();
|
|
|
251 |
$options = new stdClass;
|
|
|
252 |
$options->noclean = true;
|
|
|
253 |
$options->para = false;
|
|
|
254 |
$i = 1;
|
|
|
255 |
$n = 0;
|
|
|
256 |
|
|
|
257 |
foreach ($answers as $answer) {
|
|
|
258 |
$answer = parent::rewrite_answers_urls($answer);
|
|
|
259 |
if ($n < 2) {
|
|
|
260 |
if ($answer->answer != null) {
|
|
|
261 |
$cells = array();
|
|
|
262 |
if ($n == 0) {
|
|
|
263 |
$cells[] = '<label>' . get_string('correctresponse', 'lesson') . '</label>';
|
|
|
264 |
} else {
|
|
|
265 |
$cells[] = '<label>' . get_string('wrongresponse', 'lesson') . '</label>';
|
|
|
266 |
}
|
|
|
267 |
$cells[] = format_text($answer->answer, $answer->answerformat, $options);
|
|
|
268 |
$table->data[] = new html_table_row($cells);
|
|
|
269 |
}
|
|
|
270 |
|
|
|
271 |
if ($n == 0) {
|
|
|
272 |
$cells = array();
|
|
|
273 |
$cells[] = '<label>' . get_string('correctanswerscore', 'lesson') . '</label>: ';
|
|
|
274 |
$cells[] = $answer->score;
|
|
|
275 |
$table->data[] = new html_table_row($cells);
|
|
|
276 |
|
|
|
277 |
$cells = array();
|
|
|
278 |
$cells[] = '<label>' . get_string('correctanswerjump', 'lesson') . '</label>: ';
|
|
|
279 |
$cells[] = $this->get_jump_name($answer->jumpto);
|
|
|
280 |
$table->data[] = new html_table_row($cells);
|
|
|
281 |
} elseif ($n == 1) {
|
|
|
282 |
$cells = array();
|
|
|
283 |
$cells[] = '<label>' . get_string('wronganswerscore', 'lesson') . '</label>: ';
|
|
|
284 |
$cells[] = $answer->score;
|
|
|
285 |
$table->data[] = new html_table_row($cells);
|
|
|
286 |
|
|
|
287 |
$cells = array();
|
|
|
288 |
$cells[] = '<label>' . get_string('wronganswerjump', 'lesson') . '</label>: ';
|
|
|
289 |
$cells[] = $this->get_jump_name($answer->jumpto);
|
|
|
290 |
$table->data[] = new html_table_row($cells);
|
|
|
291 |
}
|
|
|
292 |
|
|
|
293 |
if ($n === 0){
|
|
|
294 |
$table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
|
|
|
295 |
}
|
|
|
296 |
$n++;
|
|
|
297 |
$i--;
|
|
|
298 |
} else {
|
|
|
299 |
$cells = array();
|
|
|
300 |
if ($this->lesson->custom && $answer->score > 0) {
|
|
|
301 |
// if the score is > 0, then it is correct
|
|
|
302 |
$cells[] = '<label class="correct">' . get_string('answer', 'lesson') . " {$i}</label>: \n";
|
|
|
303 |
} else if ($this->lesson->custom) {
|
|
|
304 |
$cells[] = '<label>' . get_string('answer', 'lesson') . " {$i}</label>: \n";
|
|
|
305 |
} else if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) {
|
|
|
306 |
$cells[] = '<label class="correct">' . get_string('answer', 'lesson') . " {$i}</label>: \n";
|
|
|
307 |
} else {
|
|
|
308 |
$cells[] = '<label>' . get_string('answer', 'lesson') . " {$i}</label>: \n";
|
|
|
309 |
}
|
|
|
310 |
$cells[] = format_text($answer->answer, $answer->answerformat, $options);
|
|
|
311 |
$table->data[] = new html_table_row($cells);
|
|
|
312 |
|
|
|
313 |
$cells = array();
|
|
|
314 |
$cells[] = '<label>' . get_string('matchesanswer', 'lesson') . " {$i}</label>: \n";
|
|
|
315 |
$cells[] = format_text($answer->response, $answer->responseformat, $options);
|
|
|
316 |
$table->data[] = new html_table_row($cells);
|
|
|
317 |
}
|
|
|
318 |
$i++;
|
|
|
319 |
}
|
|
|
320 |
return $table;
|
|
|
321 |
}
|
|
|
322 |
/**
|
|
|
323 |
* Updates the page and its answers
|
|
|
324 |
*
|
|
|
325 |
* @global moodle_database $DB
|
|
|
326 |
* @global moodle_page $PAGE
|
|
|
327 |
* @param stdClass $properties
|
|
|
328 |
* @return bool
|
|
|
329 |
*/
|
|
|
330 |
public function update($properties, $context = null, $maxbytes = null) {
|
|
|
331 |
global $DB, $PAGE;
|
|
|
332 |
$answers = $this->get_answers();
|
|
|
333 |
$properties->id = $this->properties->id;
|
|
|
334 |
$properties->lessonid = $this->lesson->id;
|
|
|
335 |
$properties->timemodified = time();
|
|
|
336 |
$properties = file_postupdate_standard_editor($properties, 'contents', array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes), context_module::instance($PAGE->cm->id), 'mod_lesson', 'page_contents', $properties->id);
|
|
|
337 |
$DB->update_record("lesson_pages", $properties);
|
|
|
338 |
|
|
|
339 |
// Trigger an event: page updated.
|
|
|
340 |
\mod_lesson\event\page_updated::create_from_lesson_page($this, $context)->trigger();
|
|
|
341 |
|
|
|
342 |
// need to add two to offset correct response and wrong response
|
|
|
343 |
$this->lesson->maxanswers += 2;
|
|
|
344 |
for ($i = 0; $i < $this->lesson->maxanswers; $i++) {
|
|
|
345 |
if (!array_key_exists($i, $this->answers)) {
|
|
|
346 |
$this->answers[$i] = new stdClass;
|
|
|
347 |
$this->answers[$i]->lessonid = $this->lesson->id;
|
|
|
348 |
$this->answers[$i]->pageid = $this->id;
|
|
|
349 |
$this->answers[$i]->timecreated = $this->timecreated;
|
|
|
350 |
}
|
|
|
351 |
|
|
|
352 |
if (!empty($properties->answer_editor[$i]) && is_array($properties->answer_editor[$i])) {
|
|
|
353 |
$this->answers[$i]->answer = $properties->answer_editor[$i]['text'];
|
|
|
354 |
$this->answers[$i]->answerformat = $properties->answer_editor[$i]['format'];
|
|
|
355 |
}
|
|
|
356 |
if (!empty($properties->response_editor[$i])) {
|
|
|
357 |
$this->answers[$i]->response = $properties->response_editor[$i];
|
|
|
358 |
$this->answers[$i]->responseformat = 0;
|
|
|
359 |
}
|
|
|
360 |
|
|
|
361 |
if (isset($properties->jumpto[$i])) {
|
|
|
362 |
$this->answers[$i]->jumpto = $properties->jumpto[$i];
|
|
|
363 |
}
|
|
|
364 |
if ($this->lesson->custom && isset($properties->score[$i])) {
|
|
|
365 |
$this->answers[$i]->score = $properties->score[$i];
|
|
|
366 |
}
|
|
|
367 |
|
|
|
368 |
// we don't need to check for isset here because properties called it's own isset method.
|
|
|
369 |
if ($this->answers[$i]->answer != '') {
|
|
|
370 |
if (!isset($this->answers[$i]->id)) {
|
|
|
371 |
$this->answers[$i]->id = $DB->insert_record("lesson_answers", $this->answers[$i]);
|
|
|
372 |
} else {
|
|
|
373 |
$DB->update_record("lesson_answers", $this->answers[$i]->properties());
|
|
|
374 |
}
|
|
|
375 |
// Save files in answers (no response_editor for matching questions).
|
|
|
376 |
$this->save_answers_files($context, $maxbytes, $this->answers[$i], $properties->answer_editor[$i]);
|
|
|
377 |
} else if ($i < 2) {
|
|
|
378 |
if (!isset($this->answers[$i]->id)) {
|
|
|
379 |
$this->answers[$i]->id = $DB->insert_record("lesson_answers", $this->answers[$i]);
|
|
|
380 |
} else {
|
|
|
381 |
$DB->update_record("lesson_answers", $this->answers[$i]->properties());
|
|
|
382 |
}
|
|
|
383 |
|
|
|
384 |
// Save files in answers (no response_editor for matching questions).
|
|
|
385 |
$this->save_answers_files($context, $maxbytes, $this->answers[$i], $properties->answer_editor[$i]);
|
|
|
386 |
} else if (isset($this->answers[$i]->id)) {
|
|
|
387 |
$DB->delete_records('lesson_answers', array('id'=>$this->answers[$i]->id));
|
|
|
388 |
unset($this->answers[$i]);
|
|
|
389 |
}
|
|
|
390 |
}
|
|
|
391 |
return true;
|
|
|
392 |
}
|
|
|
393 |
public function stats(array &$pagestats, $tries) {
|
|
|
394 |
$temp = $this->lesson->get_last_attempt($tries);
|
|
|
395 |
if ($temp->correct) {
|
|
|
396 |
if (isset($pagestats[$temp->pageid]["correct"])) {
|
|
|
397 |
$pagestats[$temp->pageid]["correct"]++;
|
|
|
398 |
} else {
|
|
|
399 |
$pagestats[$temp->pageid]["correct"] = 1;
|
|
|
400 |
}
|
|
|
401 |
}
|
|
|
402 |
if (isset($pagestats[$temp->pageid]["total"])) {
|
|
|
403 |
$pagestats[$temp->pageid]["total"]++;
|
|
|
404 |
} else {
|
|
|
405 |
$pagestats[$temp->pageid]["total"] = 1;
|
|
|
406 |
}
|
|
|
407 |
return true;
|
|
|
408 |
}
|
|
|
409 |
public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) {
|
|
|
410 |
$answers = array();
|
|
|
411 |
foreach ($this->get_answers() as $answer) {
|
|
|
412 |
$answers[$answer->id] = $answer;
|
|
|
413 |
}
|
|
|
414 |
$formattextdefoptions = new stdClass;
|
|
|
415 |
$formattextdefoptions->para = false; //I'll use it widely in this page
|
|
|
416 |
foreach ($answers as $answer) {
|
|
|
417 |
if ($n == 0 && $useranswer != null && $useranswer->correct) {
|
|
|
418 |
if ($answer->response == null && $useranswer != null) {
|
|
|
419 |
$answerdata->response = get_string("thatsthecorrectanswer", "lesson");
|
|
|
420 |
} else {
|
|
|
421 |
$answerdata->response = $answer->response;
|
|
|
422 |
}
|
|
|
423 |
if ($this->lesson->custom) {
|
|
|
424 |
$answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score;
|
|
|
425 |
} else {
|
|
|
426 |
$answerdata->score = get_string("receivedcredit", "lesson");
|
|
|
427 |
}
|
|
|
428 |
} elseif ($n == 1 && $useranswer != null && !$useranswer->correct) {
|
|
|
429 |
if ($answer->response == null && $useranswer != null) {
|
|
|
430 |
$answerdata->response = get_string("thatsthewronganswer", "lesson");
|
|
|
431 |
} else {
|
|
|
432 |
$answerdata->response = $answer->response;
|
|
|
433 |
}
|
|
|
434 |
if ($this->lesson->custom) {
|
|
|
435 |
$answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score;
|
|
|
436 |
} else {
|
|
|
437 |
$answerdata->score = get_string("didnotreceivecredit", "lesson");
|
|
|
438 |
}
|
|
|
439 |
} elseif ($n > 1) {
|
|
|
440 |
$data = '<label class="accesshide" for="answer_' . $n . '">' . get_string('answer', 'lesson') . '</label>';
|
|
|
441 |
$data .= strip_tags(format_string($answer->answer)) . ' ';
|
|
|
442 |
if ($useranswer != null) {
|
|
|
443 |
$userresponse = explode(",", $useranswer->useranswer);
|
|
|
444 |
$data .= '<label class="accesshide" for="stu_answer_response_' . $n . '">' . get_string('matchesanswer', 'lesson') . '</label>';
|
|
|
445 |
$data .= "<select class=\"custom-select\" id=\"stu_answer_response_" . $n . "\" " .
|
|
|
446 |
"disabled=\"disabled\"><option selected=\"selected\">";
|
|
|
447 |
if (array_key_exists($i, $userresponse)) {
|
|
|
448 |
$data .= $userresponse[$i];
|
|
|
449 |
}
|
|
|
450 |
$data .= "</option></select>";
|
|
|
451 |
} else {
|
|
|
452 |
$data .= '<label class="accesshide" for="answer_response_' . $n . '">' . get_string('matchesanswer', 'lesson') . '</label>';
|
|
|
453 |
$data .= "<select class=\"custom-select\" id=\"answer_response_" . $n . "\" " .
|
|
|
454 |
"disabled=\"disabled\"><option selected=\"selected\">".strip_tags(format_string($answer->response))."</option></select>";
|
|
|
455 |
}
|
|
|
456 |
|
|
|
457 |
if ($n == 2) {
|
|
|
458 |
if (isset($pagestats[$this->properties->id])) {
|
|
|
459 |
if (!array_key_exists('correct', $pagestats[$this->properties->id])) {
|
|
|
460 |
$pagestats[$this->properties->id]["correct"] = 0;
|
|
|
461 |
}
|
|
|
462 |
$percent = $pagestats[$this->properties->id]["correct"] / $pagestats[$this->properties->id]["total"] * 100;
|
|
|
463 |
$percent = round($percent, 2);
|
|
|
464 |
$percent .= "% ".get_string("answeredcorrectly", "lesson");
|
|
|
465 |
} else {
|
|
|
466 |
$percent = get_string("nooneansweredthisquestion", "lesson");
|
|
|
467 |
}
|
|
|
468 |
} else {
|
|
|
469 |
$percent = '';
|
|
|
470 |
}
|
|
|
471 |
|
|
|
472 |
$answerdata->answers[] = array($data, $percent);
|
|
|
473 |
$i++;
|
|
|
474 |
}
|
|
|
475 |
$n++;
|
|
|
476 |
$answerpage->answerdata = $answerdata;
|
|
|
477 |
}
|
|
|
478 |
return $answerpage;
|
|
|
479 |
}
|
|
|
480 |
public function get_jumps() {
|
|
|
481 |
global $DB;
|
|
|
482 |
// The jumps for matching question type are stored in the 1st and 2nd answer record.
|
|
|
483 |
$jumps = array();
|
|
|
484 |
if ($answers = $DB->get_records("lesson_answers", array("lessonid" => $this->lesson->id, "pageid" => $this->properties->id), 'id', '*', 0, 2)) {
|
|
|
485 |
foreach ($answers as $answer) {
|
|
|
486 |
$jumps[] = $this->get_jump_name($answer->jumpto);
|
|
|
487 |
}
|
|
|
488 |
} else {
|
|
|
489 |
$jumps[] = $this->get_jump_name($this->properties->nextpageid);
|
|
|
490 |
}
|
|
|
491 |
return $jumps;
|
|
|
492 |
}
|
|
|
493 |
}
|
|
|
494 |
|
|
|
495 |
class lesson_add_page_form_matching extends lesson_add_page_form_base {
|
|
|
496 |
|
|
|
497 |
public $qtype = 'matching';
|
|
|
498 |
public $qtypestring = 'matching';
|
|
|
499 |
protected $answerformat = LESSON_ANSWER_HTML;
|
|
|
500 |
protected $responseformat = '';
|
|
|
501 |
|
|
|
502 |
public function custom_definition() {
|
|
|
503 |
|
|
|
504 |
$this->_form->addElement('header', 'correctresponse', get_string('correctresponse', 'lesson'));
|
|
|
505 |
$this->_form->addElement('editor', 'answer_editor[0]', get_string('correctresponse', 'lesson'),
|
|
|
506 |
array('rows' => '4', 'columns' => '80'),
|
|
|
507 |
array('noclean' => true, 'maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes' => $this->_customdata['maxbytes']));
|
|
|
508 |
$this->_form->setType('answer_editor[0]', PARAM_RAW);
|
|
|
509 |
$this->_form->setDefault('answer_editor[0]', array('text' => '', 'format' => FORMAT_HTML));
|
|
|
510 |
$this->add_jumpto(0, get_string('correctanswerjump','lesson'), LESSON_NEXTPAGE);
|
|
|
511 |
$this->add_score(0, get_string("correctanswerscore", "lesson"), 1);
|
|
|
512 |
|
|
|
513 |
$this->_form->addElement('header', 'wrongresponse', get_string('wrongresponse', 'lesson'));
|
|
|
514 |
$this->_form->addElement('editor', 'answer_editor[1]', get_string('wrongresponse', 'lesson'),
|
|
|
515 |
array('rows' => '4', 'columns' => '80'),
|
|
|
516 |
array('noclean' => true, 'maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes' => $this->_customdata['maxbytes']));
|
|
|
517 |
$this->_form->setType('answer_editor[1]', PARAM_RAW);
|
|
|
518 |
$this->_form->setDefault('answer_editor[1]', array('text' => '', 'format' => FORMAT_HTML));
|
|
|
519 |
|
|
|
520 |
$this->add_jumpto(1, get_string('wronganswerjump','lesson'), LESSON_THISPAGE);
|
|
|
521 |
$this->add_score(1, get_string("wronganswerscore", "lesson"), 0);
|
|
|
522 |
|
|
|
523 |
for ($i = 2; $i < $this->_customdata['lesson']->maxanswers+2; $i++) {
|
|
|
524 |
$this->_form->addElement('header', 'matchingpair'.($i-1), get_string('matchingpair', 'lesson', $i-1));
|
|
|
525 |
$this->add_answer($i, null, ($i < 4), LESSON_ANSWER_HTML);
|
|
|
526 |
$required = ($i < 4);
|
|
|
527 |
$label = get_string('matchesanswer','lesson');
|
|
|
528 |
$count = $i;
|
|
|
529 |
$this->_form->addElement('text', 'response_editor['.$count.']', $label, array('size'=>'50'));
|
|
|
530 |
$this->_form->setType('response_editor['.$count.']', PARAM_NOTAGS);
|
|
|
531 |
$this->_form->setDefault('response_editor['.$count.']', '');
|
|
|
532 |
if ($required) {
|
|
|
533 |
$this->_form->addRule('response_editor['.$count.']', get_string('required'), 'required', null, 'client');
|
|
|
534 |
}
|
|
|
535 |
}
|
|
|
536 |
}
|
|
|
537 |
}
|
|
|
538 |
|
|
|
539 |
class lesson_display_answer_form_matching extends moodleform {
|
|
|
540 |
|
|
|
541 |
public function definition() {
|
|
|
542 |
global $USER, $OUTPUT, $PAGE;
|
|
|
543 |
$mform = $this->_form;
|
|
|
544 |
$answers = $this->_customdata['answers'];
|
|
|
545 |
$useranswers = $this->_customdata['useranswers'];
|
|
|
546 |
$responseoptions = $this->_customdata['responseoptions'];
|
|
|
547 |
$lessonid = $this->_customdata['lessonid'];
|
|
|
548 |
$contents = $this->_customdata['contents'];
|
|
|
549 |
|
|
|
550 |
// Disable shortforms.
|
|
|
551 |
$mform->setDisableShortforms();
|
|
|
552 |
|
|
|
553 |
$mform->addElement('header', 'pageheader');
|
|
|
554 |
|
|
|
555 |
$mform->addElement('html', $OUTPUT->container($contents, 'contents'));
|
|
|
556 |
|
|
|
557 |
$hasattempt = false;
|
|
|
558 |
$disabled = '';
|
|
|
559 |
if (isset($useranswers) && !empty($useranswers)) {
|
|
|
560 |
$hasattempt = true;
|
|
|
561 |
$disabled = array('disabled' => 'disabled');
|
|
|
562 |
}
|
|
|
563 |
|
|
|
564 |
$options = new stdClass;
|
|
|
565 |
$options->para = false;
|
|
|
566 |
$options->noclean = true;
|
|
|
567 |
|
|
|
568 |
$mform->addElement('hidden', 'id');
|
|
|
569 |
$mform->setType('id', PARAM_INT);
|
|
|
570 |
|
|
|
571 |
$mform->addElement('hidden', 'pageid');
|
|
|
572 |
$mform->setType('pageid', PARAM_INT);
|
|
|
573 |
|
|
|
574 |
$i = 0;
|
|
|
575 |
|
|
|
576 |
foreach ($answers as $answer) {
|
|
|
577 |
$mform->addElement('html', '<div class="answeroption">');
|
|
|
578 |
if ($answer->response != null) {
|
|
|
579 |
$responseid = 'response['.$answer->id.']';
|
|
|
580 |
if ($hasattempt) {
|
|
|
581 |
$responseid = 'response_'.$answer->id;
|
|
|
582 |
$mform->addElement('hidden', 'response['.$answer->id.']', htmlspecialchars($useranswers[$i], ENT_COMPAT));
|
|
|
583 |
// Temporary fixed until MDL-38885 gets integrated
|
|
|
584 |
$mform->setType('response', PARAM_TEXT);
|
|
|
585 |
}
|
|
|
586 |
$answer = lesson_page_type_matching::rewrite_answers_urls($answer);
|
|
|
587 |
$mform->addElement('select', $responseid, format_text($answer->answer,$answer->answerformat,$options), $responseoptions, $disabled);
|
|
|
588 |
$mform->setType($responseid, PARAM_TEXT);
|
|
|
589 |
if ($hasattempt) {
|
|
|
590 |
$mform->setDefault($responseid, htmlspecialchars(trim($useranswers[$i]), ENT_COMPAT));
|
|
|
591 |
} else {
|
|
|
592 |
$mform->setDefault($responseid, 'answeroption');
|
|
|
593 |
}
|
|
|
594 |
}
|
|
|
595 |
$mform->addElement('html', '</div>');
|
|
|
596 |
$i++;
|
|
|
597 |
}
|
|
|
598 |
if ($hasattempt) {
|
|
|
599 |
$this->add_action_buttons(null, get_string("nextpage", "lesson"));
|
|
|
600 |
} else {
|
|
|
601 |
$this->add_action_buttons(null, get_string("submit", "lesson"));
|
|
|
602 |
}
|
|
|
603 |
}
|
|
|
604 |
|
|
|
605 |
}
|