e59cce842342f091e3fac6e09a5fbbecc7eb5349
[moodle.git] / mod / lesson / pagetypes / shortanswer.php
1 <?php
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/>.
18 /**
19  * Short answer
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  **/
26 defined('MOODLE_INTERNAL') || die();
28  /** Short answer question type */
29 define("LESSON_PAGE_SHORTANSWER",   "1");
31 class lesson_page_type_shortanswer extends lesson_page {
33     protected $type = lesson_page::TYPE_QUESTION;
34     protected $typeidstring = 'shortanswer';
35     protected $typeid = LESSON_PAGE_SHORTANSWER;
36     protected $string = null;
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 = new lesson_display_answer_form_shortanswer($CFG->wwwroot.'/mod/lesson/continue.php', array('contents'=>$this->get_contents(), 'lessonid'=>$this->lesson->id));
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);
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             );
70         $event = \mod_lesson\event\question_viewed::create($eventparams);
71         $event->trigger();
72         return $mform->display();
73     }
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     }
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     }
112     public function check_answer() {
113         global $CFG;
114         $result = parent::check_answer();
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();
120         $studentanswer = trim($data->answer);
121         if ($studentanswer === '') {
122             $result->noanswer = true;
123             return $result;
124         }
126         $i=0;
127         $answers = $this->get_answers();
128         foreach ($answers as $answer) {
129             $answer = parent::rewrite_answers_urls($answer, false);
130             $i++;
131             // Applying PARAM_TEXT as it is applied to the answer submitted by the user.
132             $expectedanswer  = clean_param($answer->answer, PARAM_TEXT);
133             $ismatch         = false;
134             $markit          = false;
135             $useregexp       = ($this->qoption);
137             if ($useregexp) { //we are using 'normal analysis', which ignores case
138                 $ignorecase = '';
139                 if (substr($expectedanswer, -2) == '/i') {
140                     $expectedanswer = substr($expectedanswer, 0, -2);
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
149             if ((!$this->lesson->custom && $this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) or ($this->lesson->custom && $answer->score > 0) ) {
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;
209                 $options = new stdClass();
210                 $options->para = false;
211                 $result->response = format_text($answer->response, $answer->responseformat, $options);
212                 $result->answerid = $answer->id;
213                 break; // quit answer analysis immediately after a match has been found
214             }
215         }
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         }
236         $result->userresponse = $studentanswer;
237         //clean student answer as it goes to output.
238         $result->studentanswer = s($studentanswer);
239         return $result;
240     }
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     }
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) {
256             $answer = parent::rewrite_answers_urls($answer, false);
257             $cells = array();
258             if ($this->lesson->custom && $answer->score > 0) {
259                 // if the score is > 0, then it is correct
260                 $cells[] = '<label class="correct">' . get_string('answer', 'lesson') . ' ' . $i . '</label>:';
261             } else if ($this->lesson->custom) {
262                 $cells[] = '<label>' . get_string('answer', 'lesson') . ' ' . $i . '</label>:';
263             } else if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) {
264                 // underline correct answers
265                 $cells[] = '<span class="correct">' . get_string('answer', 'lesson') . ' ' . $i . '</span>:' . "\n";
266             } else {
267                 $cells[] = '<label class="correct">' . get_string('answer', 'lesson') . ' ' . $i . '</label>:';
268             }
269             $cells[] = format_text($answer->answer, $answer->answerformat, $options);
270             $table->data[] = new html_table_row($cells);
272             $cells = array();
273             $cells[] = '<label>' . get_string('response', 'lesson') . ' ' . $i . '</label>:';
274             $cells[] = format_text($answer->response, $answer->responseformat, $options);
275             $table->data[] = new html_table_row($cells);
277             $cells = array();
278             $cells[] = '<label>' . get_string('score', 'lesson') . '</label>:';
279             $cells[] = $answer->score;
280             $table->data[] = new html_table_row($cells);
282             $cells = array();
283             $cells[] = '<label>' . get_string('jump', 'lesson') . '</label>:';
284             $cells[] = $this->get_jump_name($answer->jumpto);
285             $table->data[] = new html_table_row($cells);
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     }
313     public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) {
314         global $PAGE;
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) {
320             $answer = parent::rewrite_answers_urls($answer, false);
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) {
328                         $data = '<input type="text" size="50" disabled="disabled" class="form-control" ' .
329                                 'readonly="readonly" value="'.s($valentered).'" />';
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++;
339             } else if ($useranswer != null && ($answer->id == $useranswer->answerid || $answer == end($answers))) {
340                  // get in here when what the user entered is not one of the answers
341                 $data = '<input type="text" size="50" disabled="disabled" class="form-control" ' .
342                         'readonly="readonly" value="'.s($useranswer->useranswer).'">';
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);
352                 if ($answer->id == $useranswer->answerid) {
353                     if ($answer->response == null) {
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                     }
369                     // We have found the correct answer, do not process any more answers.
370                     $answerpage->answerdata = $answerdata;
371                     break;
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     }
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     }
415 class lesson_add_page_form_shortanswer extends lesson_add_page_form_base {
416     public $qtype = 'shortanswer';
417     public $qtypestring = 'shortanswer';
418     protected $answerformat = '';
419     protected $responseformat = LESSON_ANSWER_HTML;
421     public function custom_definition() {
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);
425         $this->_form->addHelpButton('qoption', 'casesensitive', 'lesson');
427         $answercount = $this->_customdata['lesson']->maxanswers;
428         for ($i = 0; $i < $answercount; $i++) {
429             $this->_form->addElement('header', 'answertitle'.$i, get_string('answer').' '.($i+1));
430             // Only first answer is required.
431             $this->add_answer($i, null, ($i < 1));
432             $this->add_response($i);
433             $this->add_jumpto($i, null, ($i == 0 ? LESSON_NEXTPAGE : LESSON_THISPAGE));
434             $this->add_score($i, null, ($i===0)?1:0);
435         }
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);
444     }
447 class lesson_display_answer_form_shortanswer extends moodleform {
449     public function definition() {
450         global $OUTPUT, $USER;
451         $mform = $this->_form;
452         $contents = $this->_customdata['contents'];
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         }
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         }
471         // Disable shortforms.
472         $mform->setDisableShortforms();
474         $mform->addElement('header', 'pageheader');
475         $mform->addElement('hidden', 'id');
476         $mform->setType('id', PARAM_INT);
478         $mform->addElement('hidden', 'pageid');
479         $mform->setType('pageid', PARAM_INT);
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);
491         }
492         $mform->setType('answer', PARAM_TEXT);
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         }
499     }