MDL-59081 mod_lesson: Shortanswer form update. Catch all.
[moodle.git] / mod / lesson / pagetypes / shortanswer.php
CommitLineData
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 * Short answer
20 *
9b24f68b 21 * @package mod_lesson
cc3dbaaa
PS
22 * @copyright 2009 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
0a4abb73
SH
24 **/
25
1e7f8ea2
PS
26defined('MOODLE_INTERNAL') || die();
27
0a4abb73
SH
28 /** Short answer question type */
29define("LESSON_PAGE_SHORTANSWER", "1");
30
31class lesson_page_type_shortanswer extends lesson_page {
32
33 protected $type = lesson_page::TYPE_QUESTION;
34 protected $typeidstring = 'shortanswer';
35 protected $typeid = LESSON_PAGE_SHORTANSWER;
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;
abd5c24e 52 $mform = new lesson_display_answer_form_shortanswer($CFG->wwwroot.'/mod/lesson/continue.php', array('contents'=>$this->get_contents(), 'lessonid'=>$this->lesson->id));
0a4abb73
SH
53 $data = new stdClass;
54 $data->id = $PAGE->cm->id;
55 $data->pageid = $this->properties->id;
56 if (isset($USER->modattempts[$this->lesson->id])) {
57 $data->answer = s($attempt->useranswer);
58 }
59 $mform->set_data($data);
8101328a
SB
60
61 // Trigger an event question viewed.
62 $eventparams = array(
63 'context' => context_module::instance($PAGE->cm->id),
64 'objectid' => $this->properties->id,
65 'other' => array(
66 'pagetype' => $this->get_typestring()
67 )
68 );
69
70 $event = \mod_lesson\event\question_viewed::create($eventparams);
71 $event->trigger();
0a4abb73
SH
72 return $mform->display();
73 }
49898ef3
AG
74
75 /**
76 * Creates answers for this page type.
77 *
78 * @param object $properties The answer properties.
79 */
80 public function create_answers($properties) {
81 if (isset($properties->enableotheranswers) && $properties->enableotheranswers) {
82 $properties->response_editor = array_values($properties->response_editor);
83 $properties->jumpto = array_values($properties->jumpto);
84 $properties->score = array_values($properties->score);
85 $wrongresponse = end($properties->response_editor);
86 $wrongkey = key($properties->response_editor);
87 $properties->answer_editor[$wrongkey] = LESSON_OTHER_ANSWERS;
88 }
89 parent::create_answers($properties);
90 }
91
92 /**
93 * Update the answers for this page type.
94 *
95 * @param object $properties The answer properties.
96 * @param context $context The context for this module.
97 * @param int $maxbytes The maximum bytes for any uploades.
98 */
99 public function update($properties, $context = null, $maxbytes = null) {
100 if ($properties->enableotheranswers) {
101 $properties->response_editor = array_values($properties->response_editor);
102 $properties->jumpto = array_values($properties->jumpto);
103 $properties->score = array_values($properties->score);
104 $wrongresponse = end($properties->response_editor);
105 $wrongkey = key($properties->response_editor);
106 $properties->answer_editor[$wrongkey] = LESSON_OTHER_ANSWERS;
107 }
108 parent::update($properties, $context, $maxbytes);
109 }
110
111
0a4abb73
SH
112 public function check_answer() {
113 global $CFG;
114 $result = parent::check_answer();
115
116 $mform = new lesson_display_answer_form_shortanswer($CFG->wwwroot.'/mod/lesson/continue.php', array('contents'=>$this->get_contents()));
117 $data = $mform->get_data();
118 require_sesskey();
119
120 $studentanswer = trim($data->answer);
121 if ($studentanswer === '') {
122 $result->noanswer = true;
123 return $result;
124 }
0a4abb73
SH
125
126 $i=0;
127 $answers = $this->get_answers();
128 foreach ($answers as $answer) {
0abc18cf 129 $answer = parent::rewrite_answers_urls($answer, false);
0a4abb73 130 $i++;
5fbebc3c
FM
131 // Applying PARAM_TEXT as it is applied to the answer submitted by the user.
132 $expectedanswer = clean_param($answer->answer, PARAM_TEXT);
0a4abb73
SH
133 $ismatch = false;
134 $markit = false;
135 $useregexp = ($this->qoption);
136
137 if ($useregexp) { //we are using 'normal analysis', which ignores case
138 $ignorecase = '';
1bb42231
RW
139 if (substr($expectedanswer, -2) == '/i') {
140 $expectedanswer = substr($expectedanswer, 0, -2);
0a4abb73
SH
141 $ignorecase = 'i';
142 }
143 } else {
144 $expectedanswer = str_replace('*', '#####', $expectedanswer);
145 $expectedanswer = preg_quote($expectedanswer, '/');
146 $expectedanswer = str_replace('#####', '.*', $expectedanswer);
147 }
148 // see if user typed in any of the correct answers
9bd3e94a 149 if ((!$this->lesson->custom && $this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) or ($this->lesson->custom && $answer->score > 0) ) {
0a4abb73
SH
150 if (!$useregexp) { // we are using 'normal analysis', which ignores case
151 if (preg_match('/^'.$expectedanswer.'$/i',$studentanswer)) {
152 $ismatch = true;
153 }
154 } else {
155 if (preg_match('/^'.$expectedanswer.'$/'.$ignorecase,$studentanswer)) {
156 $ismatch = true;
157 }
158 }
159 if ($ismatch == true) {
160 $result->correctanswer = true;
161 }
162 } else {
163 if (!$useregexp) { //we are using 'normal analysis'
164 // see if user typed in any of the wrong answers; don't worry about case
165 if (preg_match('/^'.$expectedanswer.'$/i',$studentanswer)) {
166 $ismatch = true;
167 }
168 } else { // we are using regular expressions analysis
169 $startcode = substr($expectedanswer,0,2);
170 switch ($startcode){
171 //1- check for absence of required string in $studentanswer (coded by initial '--')
172 case "--":
173 $expectedanswer = substr($expectedanswer,2);
174 if (!preg_match('/^'.$expectedanswer.'$/'.$ignorecase,$studentanswer)) {
175 $ismatch = true;
176 }
177 break;
178 //2- check for code for marking wrong strings (coded by initial '++')
179 case "++":
180 $expectedanswer=substr($expectedanswer,2);
181 $markit = true;
182 //check for one or several matches
183 if (preg_match_all('/'.$expectedanswer.'/'.$ignorecase,$studentanswer, $matches)) {
184 $ismatch = true;
185 $nb = count($matches[0]);
186 $original = array();
187 $marked = array();
188 $fontStart = '<span class="incorrect matches">';
189 $fontEnd = '</span>';
190 for ($i = 0; $i < $nb; $i++) {
191 array_push($original,$matches[0][$i]);
192 array_push($marked,$fontStart.$matches[0][$i].$fontEnd);
193 }
194 $studentanswer = str_replace($original, $marked, $studentanswer);
195 }
196 break;
197 //3- check for wrong answers belonging neither to -- nor to ++ categories
198 default:
199 if (preg_match('/^'.$expectedanswer.'$/'.$ignorecase,$studentanswer, $matches)) {
200 $ismatch = true;
201 }
202 break;
203 }
204 $result->correctanswer = false;
205 }
206 }
207 if ($ismatch) {
208 $result->newpageid = $answer->jumpto;
0abc18cf
JMV
209 $options = new stdClass();
210 $options->para = false;
211 $result->response = format_text($answer->response, $answer->responseformat, $options);
0a4abb73
SH
212 $result->answerid = $answer->id;
213 break; // quit answer analysis immediately after a match has been found
214 }
215 }
49898ef3
AG
216
217 // We could check here to see if we have a wrong answer jump to use.
218 if ($result->answerid == 0) {
219 // Use the all other answers jump details if it is set up.
220 $lastanswer = end($answers);
221 // Double check that this is the @#wronganswer#@ answer.
222 if (strpos($lastanswer->answer, LESSON_OTHER_ANSWERS) !== false) {
223 $otheranswers = end($answers);
224 $result->newpageid = $otheranswers->jumpto;
225 $options = new stdClass();
226 $options->para = false;
227 $result->response = format_text($otheranswers->response, $otheranswers->responseformat, $options);
228 // Does this also need to do the jumpto_is_correct?
229 if ($this->lesson->custom) {
230 $result->correctanswer = ($otheranswers->score > 0);
231 }
232 $result->answerid = $otheranswers->id;
233 }
234 }
235
92c93432
RT
236 $result->userresponse = $studentanswer;
237 //clean student answer as it goes to output.
238 $result->studentanswer = s($studentanswer);
0a4abb73
SH
239 return $result;
240 }
241
242 public function option_description_string() {
243 if ($this->properties->qoption) {
244 return " - ".get_string("casesensitive", "lesson");
245 }
246 return parent::option_description_string();
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 foreach ($answers as $answer) {
0abc18cf 256 $answer = parent::rewrite_answers_urls($answer, false);
0a4abb73
SH
257 $cells = array();
258 if ($this->lesson->custom && $answer->score > 0) {
259 // if the score is > 0, then it is correct
195480e9 260 $cells[] = '<label class="correct">' . get_string('answer', 'lesson') . ' ' . $i . '</label>:';
0a4abb73 261 } else if ($this->lesson->custom) {
195480e9 262 $cells[] = '<label>' . get_string('answer', 'lesson') . ' ' . $i . '</label>:';
0a4abb73
SH
263 } else if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) {
264 // underline correct answers
d6076942 265 $cells[] = '<span class="correct">' . get_string('answer', 'lesson') . ' ' . $i . '</span>:' . "\n";
0a4abb73 266 } else {
195480e9 267 $cells[] = '<label class="correct">' . get_string('answer', 'lesson') . ' ' . $i . '</label>:';
0a4abb73 268 }
01c37ef1 269 $cells[] = format_text($answer->answer, $answer->answerformat, $options);
8cea545e 270 $table->data[] = new html_table_row($cells);
0a4abb73
SH
271
272 $cells = array();
195480e9 273 $cells[] = '<label>' . get_string('response', 'lesson') . ' ' . $i . '</label>:';
01c37ef1 274 $cells[] = format_text($answer->response, $answer->responseformat, $options);
8cea545e 275 $table->data[] = new html_table_row($cells);
0a4abb73
SH
276
277 $cells = array();
195480e9 278 $cells[] = '<label>' . get_string('score', 'lesson') . '</label>:';
0a4abb73 279 $cells[] = $answer->score;
8cea545e 280 $table->data[] = new html_table_row($cells);
0a4abb73
SH
281
282 $cells = array();
195480e9 283 $cells[] = '<label>' . get_string('jump', 'lesson') . '</label>:';
0a4abb73 284 $cells[] = $this->get_jump_name($answer->jumpto);
8cea545e 285 $table->data[] = new html_table_row($cells);
0a4abb73
SH
286 if ($i === 1){
287 $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
288 }
289 $i++;
290 }
291 return $table;
292 }
293 public function stats(array &$pagestats, $tries) {
294 if(count($tries) > $this->lesson->maxattempts) { // if there are more tries than the max that is allowed, grab the last "legal" attempt
295 $temp = $tries[$this->lesson->maxattempts - 1];
296 } else {
297 // else, user attempted the question less than the max, so grab the last one
298 $temp = end($tries);
299 }
300 if (isset($pagestats[$temp->pageid][$temp->useranswer])) {
301 $pagestats[$temp->pageid][$temp->useranswer]++;
302 } else {
303 $pagestats[$temp->pageid][$temp->useranswer] = 1;
304 }
305 if (isset($pagestats[$temp->pageid]["total"])) {
306 $pagestats[$temp->pageid]["total"]++;
307 } else {
308 $pagestats[$temp->pageid]["total"] = 1;
309 }
310 return true;
311 }
312
313 public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) {
0abc18cf
JMV
314 global $PAGE;
315
0a4abb73
SH
316 $answers = $this->get_answers();
317 $formattextdefoptions = new stdClass;
318 $formattextdefoptions->para = false; //I'll use it widely in this page
319 foreach ($answers as $answer) {
0abc18cf 320 $answer = parent::rewrite_answers_urls($answer, false);
0a4abb73
SH
321 if ($useranswer == null && $i == 0) {
322 // I have the $i == 0 because it is easier to blast through it all at once.
323 if (isset($pagestats[$this->properties->id])) {
324 $stats = $pagestats[$this->properties->id];
325 $total = $stats["total"];
326 unset($stats["total"]);
327 foreach ($stats as $valentered => $ntimes) {
0f760528
DW
328 $data = '<input type="text" size="50" disabled="disabled" class="form-control" ' .
329 'readonly="readonly" value="'.s($valentered).'" />';
0a4abb73
SH
330 $percent = $ntimes / $total * 100;
331 $percent = round($percent, 2);
332 $percent .= "% ".get_string("enteredthis", "lesson");
333 $answerdata->answers[] = array($data, $percent);
334 }
335 } else {
336 $answerdata->answers[] = array(get_string("nooneansweredthisquestion", "lesson"), " ");
337 }
338 $i++;
c90ba16f 339 } else if ($useranswer != null && ($answer->id == $useranswer->answerid || $answer == end($answers))) {
0a4abb73 340 // get in here when what the user entered is not one of the answers
0f760528
DW
341 $data = '<input type="text" size="50" disabled="disabled" class="form-control" ' .
342 'readonly="readonly" value="'.s($useranswer->useranswer).'">';
0a4abb73
SH
343 if (isset($pagestats[$this->properties->id][$useranswer->useranswer])) {
344 $percent = $pagestats[$this->properties->id][$useranswer->useranswer] / $pagestats[$this->properties->id]["total"] * 100;
345 $percent = round($percent, 2);
346 $percent .= "% ".get_string("enteredthis", "lesson");
347 } else {
348 $percent = get_string("nooneenteredthis", "lesson");
349 }
350 $answerdata->answers[] = array($data, $percent);
351
352 if ($answer->id == $useranswer->answerid) {
ecea65ca 353 if ($answer->response == null) {
0a4abb73
SH
354 if ($useranswer->correct) {
355 $answerdata->response = get_string("thatsthecorrectanswer", "lesson");
356 } else {
357 $answerdata->response = get_string("thatsthewronganswer", "lesson");
358 }
359 } else {
360 $answerdata->response = $answer->response;
361 }
362 if ($this->lesson->custom) {
363 $answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score;
364 } elseif ($useranswer->correct) {
365 $answerdata->score = get_string("receivedcredit", "lesson");
366 } else {
367 $answerdata->score = get_string("didnotreceivecredit", "lesson");
368 }
ee8640a9 369 // We have found the correct answer, do not process any more answers.
43deb1a4 370 $answerpage->answerdata = $answerdata;
ee8640a9 371 break;
0a4abb73
SH
372 } else {
373 $answerdata->response = get_string("thatsthewronganswer", "lesson");
374 if ($this->lesson->custom) {
375 $answerdata->score = get_string("pointsearned", "lesson").": 0";
376 } else {
377 $answerdata->score = get_string("didnotreceivecredit", "lesson");
378 }
379 }
380 }
381 $answerpage->answerdata = $answerdata;
382 }
383 return $answerpage;
384 }
49898ef3
AG
385
386 /**
387 * Make updates to the form data if required. In this case to put the all other answer data into the write section of the form.
388 *
389 * @param stdClass $data The form data to update.
390 * @return stdClass The updated fom data.
391 */
392 public function update_form_data(stdClass $data) : stdClass {
393 $answercount = count($this->get_answers());
394 // Check for other answer entry.
395 $lastanswer = $data->{'answer_editor[' . ($answercount - 1) . ']'};
396 if (strpos($lastanswer, LESSON_OTHER_ANSWERS) !== false) {
397 $data->{'answer_editor[' . ($this->lesson->maxanswers + 1) . ']'} =
398 $data->{'answer_editor[' . ($answercount - 1) . ']'};
399 $data->{'response_editor[' . ($this->lesson->maxanswers + 1) . ']'} =
400 $data->{'response_editor[' . ($answercount - 1) . ']'};
401 $data->{'jumpto[' . ($this->lesson->maxanswers + 1) . ']'} = $data->{'jumpto[' . ($answercount - 1) . ']'};
402 $data->{'score[' . ($this->lesson->maxanswers + 1) . ']'} = $data->{'score[' . ($answercount - 1) . ']'};
403 $data->enableotheranswers = true;
404 // Unset the old values.
405 unset($data->{'answer_editor[' . ($answercount - 1) . ']'});
406 unset($data->{'response_editor[' . ($answercount - 1) . ']'});
407 unset($data->{'jumpto[' . ($answercount - 1) . ']'});
408 unset($data->{'score[' . ($answercount - 1) . ']'});
409 }
410 return $data;
411 }
0a4abb73
SH
412}
413
414
415class lesson_add_page_form_shortanswer extends lesson_add_page_form_base {
416 public $qtype = 'shortanswer';
417 public $qtypestring = 'shortanswer';
ceeab150
RT
418 protected $answerformat = '';
419 protected $responseformat = LESSON_ANSWER_HTML;
0a4abb73
SH
420
421 public function custom_definition() {
422
f31daba4
PS
423 $this->_form->addElement('checkbox', 'qoption', get_string('options', 'lesson'), get_string('casesensitive', 'lesson')); //oh my, this is a regex option!
424 $this->_form->setDefault('qoption', 0);
4c80a990 425 $this->_form->addHelpButton('qoption', 'casesensitive', 'lesson');
0a4abb73 426
49898ef3
AG
427 $answercount = $this->_customdata['lesson']->maxanswers;
428 for ($i = 0; $i < $answercount; $i++) {
0a4abb73 429 $this->_form->addElement('header', 'answertitle'.$i, get_string('answer').' '.($i+1));
400fa4a1
JMV
430 // Only first answer is required.
431 $this->add_answer($i, null, ($i < 1));
0a4abb73 432 $this->add_response($i);
ecea65ca 433 $this->add_jumpto($i, null, ($i == 0 ? LESSON_NEXTPAGE : LESSON_THISPAGE));
0a4abb73
SH
434 $this->add_score($i, null, ($i===0)?1:0);
435 }
49898ef3
AG
436
437 // Other answer jump.
438 $this->_form->addElement('header', 'wronganswer', get_string('allotheranswers', 'lesson'));
439 $newcount = $answercount + 1;
440 $this->_form->addElement('advcheckbox', 'enableotheranswers', get_string('enabled', 'lesson'));
441 $this->add_response($newcount);
442 $this->add_jumpto($newcount, get_string('allotheranswersjump', 'lesson'), LESSON_NEXTPAGE);
443 $this->add_score($newcount, get_string('allotheranswersscore', 'lesson'), 0);
0a4abb73
SH
444 }
445}
446
447class lesson_display_answer_form_shortanswer extends moodleform {
448
449 public function definition() {
abd5c24e 450 global $OUTPUT, $USER;
0a4abb73
SH
451 $mform = $this->_form;
452 $contents = $this->_customdata['contents'];
453
abd5c24e
RW
454 $hasattempt = false;
455 $attrs = array('size'=>'50', 'maxlength'=>'200');
456 if (isset($this->_customdata['lessonid'])) {
457 $lessonid = $this->_customdata['lessonid'];
458 if (isset($USER->modattempts[$lessonid]->useranswer)) {
459 $attrs['readonly'] = 'readonly';
460 $hasattempt = true;
461 }
462 }
463
cb9af730
JMV
464 $placeholder = false;
465 if (preg_match('/_____+/', $contents, $matches)) {
466 $placeholder = $matches[0];
467 $contentsparts = explode( $placeholder, $contents, 2);
468 $attrs['size'] = round(strlen($placeholder) * 1.1);
469 }
0a4abb73 470
5b0af6e4
JMV
471 // Disable shortforms.
472 $mform->setDisableShortforms();
473
cb9af730 474 $mform->addElement('header', 'pageheader');
0a4abb73
SH
475 $mform->addElement('hidden', 'id');
476 $mform->setType('id', PARAM_INT);
477
478 $mform->addElement('hidden', 'pageid');
479 $mform->setType('pageid', PARAM_INT);
480
cb9af730
JMV
481 if ($placeholder) {
482 $contentsgroup = array();
483 $contentsgroup[] = $mform->createElement('static', '', '', $contentsparts[0]);
484 $contentsgroup[] = $mform->createElement('text', 'answer', '', $attrs);
485 $contentsgroup[] = $mform->createElement('static', '', '', $contentsparts[1]);
486 $mform->addGroup($contentsgroup, '', '', '', false);
487 } else {
488 $mform->addElement('html', $OUTPUT->container($contents, 'contents'));
489 $mform->addElement('text', 'answer', get_string('youranswer', 'lesson'), $attrs);
490
491 }
0a4abb73
SH
492 $mform->setType('answer', PARAM_TEXT);
493
abd5c24e
RW
494 if ($hasattempt) {
495 $this->add_action_buttons(null, get_string("nextpage", "lesson"));
496 } else {
497 $this->add_action_buttons(null, get_string("submit", "lesson"));
498 }
0a4abb73
SH
499 }
500
4c80a990 501}