Merge branch 'MDL-10965-master' of git://github.com/marinaglancy/moodle
[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     }
74     public function check_answer() {
75         global $CFG;
76         $result = parent::check_answer();
78         $mform = new lesson_display_answer_form_shortanswer($CFG->wwwroot.'/mod/lesson/continue.php', array('contents'=>$this->get_contents()));
79         $data = $mform->get_data();
80         require_sesskey();
82         $studentanswer = trim($data->answer);
83         if ($studentanswer === '') {
84             $result->noanswer = true;
85             return $result;
86         }
88         $i=0;
89         $answers = $this->get_answers();
90         foreach ($answers as $answer) {
91             $answer = parent::rewrite_answers_urls($answer, false);
92             $i++;
93             // Applying PARAM_TEXT as it is applied to the answer submitted by the user.
94             $expectedanswer  = clean_param($answer->answer, PARAM_TEXT);
95             $ismatch         = false;
96             $markit          = false;
97             $useregexp       = ($this->qoption);
99             if ($useregexp) { //we are using 'normal analysis', which ignores case
100                 $ignorecase = '';
101                 if (substr($expectedanswer, -2) == '/i') {
102                     $expectedanswer = substr($expectedanswer, 0, -2);
103                     $ignorecase = 'i';
104                 }
105             } else {
106                 $expectedanswer = str_replace('*', '#####', $expectedanswer);
107                 $expectedanswer = preg_quote($expectedanswer, '/');
108                 $expectedanswer = str_replace('#####', '.*', $expectedanswer);
109             }
110             // see if user typed in any of the correct answers
111             if ((!$this->lesson->custom && $this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) or ($this->lesson->custom && $answer->score > 0) ) {
112                 if (!$useregexp) { // we are using 'normal analysis', which ignores case
113                     if (preg_match('/^'.$expectedanswer.'$/i',$studentanswer)) {
114                         $ismatch = true;
115                     }
116                 } else {
117                     if (preg_match('/^'.$expectedanswer.'$/'.$ignorecase,$studentanswer)) {
118                         $ismatch = true;
119                     }
120                 }
121                 if ($ismatch == true) {
122                     $result->correctanswer = true;
123                 }
124             } else {
125                if (!$useregexp) { //we are using 'normal analysis'
126                     // see if user typed in any of the wrong answers; don't worry about case
127                     if (preg_match('/^'.$expectedanswer.'$/i',$studentanswer)) {
128                         $ismatch = true;
129                     }
130                 } else { // we are using regular expressions analysis
131                     $startcode = substr($expectedanswer,0,2);
132                     switch ($startcode){
133                         //1- check for absence of required string in $studentanswer (coded by initial '--')
134                         case "--":
135                             $expectedanswer = substr($expectedanswer,2);
136                             if (!preg_match('/^'.$expectedanswer.'$/'.$ignorecase,$studentanswer)) {
137                                 $ismatch = true;
138                             }
139                             break;
140                         //2- check for code for marking wrong strings (coded by initial '++')
141                         case "++":
142                             $expectedanswer=substr($expectedanswer,2);
143                             $markit = true;
144                             //check for one or several matches
145                             if (preg_match_all('/'.$expectedanswer.'/'.$ignorecase,$studentanswer, $matches)) {
146                                 $ismatch   = true;
147                                 $nb        = count($matches[0]);
148                                 $original  = array();
149                                 $marked    = array();
150                                 $fontStart = '<span class="incorrect matches">';
151                                 $fontEnd   = '</span>';
152                                 for ($i = 0; $i < $nb; $i++) {
153                                     array_push($original,$matches[0][$i]);
154                                     array_push($marked,$fontStart.$matches[0][$i].$fontEnd);
155                                 }
156                                 $studentanswer = str_replace($original, $marked, $studentanswer);
157                             }
158                             break;
159                         //3- check for wrong answers belonging neither to -- nor to ++ categories
160                         default:
161                             if (preg_match('/^'.$expectedanswer.'$/'.$ignorecase,$studentanswer, $matches)) {
162                                 $ismatch = true;
163                             }
164                             break;
165                     }
166                     $result->correctanswer = false;
167                 }
168             }
169             if ($ismatch) {
170                 $result->newpageid = $answer->jumpto;
171                 $options = new stdClass();
172                 $options->para = false;
173                 $options->noclean = true;
174                 $result->response = format_text($answer->response, $answer->responseformat, $options);
175                 $result->answerid = $answer->id;
176                 break; // quit answer analysis immediately after a match has been found
177             }
178         }
179         $result->userresponse = $studentanswer;
180         //clean student answer as it goes to output.
181         $result->studentanswer = s($studentanswer);
182         return $result;
183     }
185     public function option_description_string() {
186         if ($this->properties->qoption) {
187             return " - ".get_string("casesensitive", "lesson");
188         }
189         return parent::option_description_string();
190     }
192     public function display_answers(html_table $table) {
193         $answers = $this->get_answers();
194         $options = new stdClass;
195         $options->noclean = true;
196         $options->para = false;
197         $i = 1;
198         foreach ($answers as $answer) {
199             $answer = parent::rewrite_answers_urls($answer, false);
200             $cells = array();
201             if ($this->lesson->custom && $answer->score > 0) {
202                 // if the score is > 0, then it is correct
203                 $cells[] = '<label class="correct">' . get_string('answer', 'lesson') . ' ' . $i . '</label>:';
204             } else if ($this->lesson->custom) {
205                 $cells[] = '<label>' . get_string('answer', 'lesson') . ' ' . $i . '</label>:';
206             } else if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) {
207                 // underline correct answers
208                 $cells[] = '<span class="correct">' . get_string('answer', 'lesson') . ' ' . $i . '</span>:' . "\n";
209             } else {
210                 $cells[] = '<label class="correct">' . get_string('answer', 'lesson') . ' ' . $i . '</label>:';
211             }
212             $cells[] = format_text($answer->answer, $answer->answerformat, $options);
213             $table->data[] = new html_table_row($cells);
215             $cells = array();
216             $cells[] = '<label>' . get_string('response', 'lesson') . ' ' . $i . '</label>:';
217             $cells[] = format_text($answer->response, $answer->responseformat, $options);
218             $table->data[] = new html_table_row($cells);
220             $cells = array();
221             $cells[] = '<label>' . get_string('score', 'lesson') . '</label>:';
222             $cells[] = $answer->score;
223             $table->data[] = new html_table_row($cells);
225             $cells = array();
226             $cells[] = '<label>' . get_string('jump', 'lesson') . '</label>:';
227             $cells[] = $this->get_jump_name($answer->jumpto);
228             $table->data[] = new html_table_row($cells);
229             if ($i === 1){
230                 $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
231             }
232             $i++;
233         }
234         return $table;
235     }
236     public function stats(array &$pagestats, $tries) {
237         if(count($tries) > $this->lesson->maxattempts) { // if there are more tries than the max that is allowed, grab the last "legal" attempt
238             $temp = $tries[$this->lesson->maxattempts - 1];
239         } else {
240             // else, user attempted the question less than the max, so grab the last one
241             $temp = end($tries);
242         }
243         if (isset($pagestats[$temp->pageid][$temp->useranswer])) {
244             $pagestats[$temp->pageid][$temp->useranswer]++;
245         } else {
246             $pagestats[$temp->pageid][$temp->useranswer] = 1;
247         }
248         if (isset($pagestats[$temp->pageid]["total"])) {
249             $pagestats[$temp->pageid]["total"]++;
250         } else {
251             $pagestats[$temp->pageid]["total"] = 1;
252         }
253         return true;
254     }
256     public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) {
257         global $PAGE;
259         $answers = $this->get_answers();
260         $formattextdefoptions = new stdClass;
261         $formattextdefoptions->para = false;  //I'll use it widely in this page
262         foreach ($answers as $answer) {
263             $answer = parent::rewrite_answers_urls($answer, false);
264             if ($useranswer == null && $i == 0) {
265                 // I have the $i == 0 because it is easier to blast through it all at once.
266                 if (isset($pagestats[$this->properties->id])) {
267                     $stats = $pagestats[$this->properties->id];
268                     $total = $stats["total"];
269                     unset($stats["total"]);
270                     foreach ($stats as $valentered => $ntimes) {
271                         $data = '<input type="text" size="50" disabled="disabled" class="form-control" ' .
272                                 'readonly="readonly" value="'.s($valentered).'" />';
273                         $percent = $ntimes / $total * 100;
274                         $percent = round($percent, 2);
275                         $percent .= "% ".get_string("enteredthis", "lesson");
276                         $answerdata->answers[] = array($data, $percent);
277                     }
278                 } else {
279                     $answerdata->answers[] = array(get_string("nooneansweredthisquestion", "lesson"), " ");
280                 }
281                 $i++;
282             } else if ($useranswer != null && ($answer->id == $useranswer->answerid || $answer == end($answers))) {
283                  // get in here when what the user entered is not one of the answers
284                 $data = '<input type="text" size="50" disabled="disabled" class="form-control" ' .
285                         'readonly="readonly" value="'.s($useranswer->useranswer).'">';
286                 if (isset($pagestats[$this->properties->id][$useranswer->useranswer])) {
287                     $percent = $pagestats[$this->properties->id][$useranswer->useranswer] / $pagestats[$this->properties->id]["total"] * 100;
288                     $percent = round($percent, 2);
289                     $percent .= "% ".get_string("enteredthis", "lesson");
290                 } else {
291                     $percent = get_string("nooneenteredthis", "lesson");
292                 }
293                 $answerdata->answers[] = array($data, $percent);
295                 if ($answer->id == $useranswer->answerid) {
296                     if ($answer->response == null) {
297                         if ($useranswer->correct) {
298                             $answerdata->response = get_string("thatsthecorrectanswer", "lesson");
299                         } else {
300                             $answerdata->response = get_string("thatsthewronganswer", "lesson");
301                         }
302                     } else {
303                         $answerdata->response = $answer->response;
304                     }
305                     if ($this->lesson->custom) {
306                         $answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score;
307                     } elseif ($useranswer->correct) {
308                         $answerdata->score = get_string("receivedcredit", "lesson");
309                     } else {
310                         $answerdata->score = get_string("didnotreceivecredit", "lesson");
311                     }
312                     // We have found the correct answer, do not process any more answers.
313                     $answerpage->answerdata = $answerdata;
314                     break;
315                 } else {
316                     $answerdata->response = get_string("thatsthewronganswer", "lesson");
317                     if ($this->lesson->custom) {
318                         $answerdata->score = get_string("pointsearned", "lesson").": 0";
319                     } else {
320                         $answerdata->score = get_string("didnotreceivecredit", "lesson");
321                     }
322                 }
323             }
324             $answerpage->answerdata = $answerdata;
325         }
326         return $answerpage;
327     }
331 class lesson_add_page_form_shortanswer extends lesson_add_page_form_base {
332     public $qtype = 'shortanswer';
333     public $qtypestring = 'shortanswer';
334     protected $answerformat = '';
335     protected $responseformat = LESSON_ANSWER_HTML;
337     public function custom_definition() {
339         $this->_form->addElement('checkbox', 'qoption', get_string('options', 'lesson'), get_string('casesensitive', 'lesson')); //oh my, this is a regex option!
340         $this->_form->setDefault('qoption', 0);
341         $this->_form->addHelpButton('qoption', 'casesensitive', 'lesson');
343         for ($i = 0; $i < $this->_customdata['lesson']->maxanswers; $i++) {
344             $this->_form->addElement('header', 'answertitle'.$i, get_string('answer').' '.($i+1));
345             // Only first answer is required.
346             $this->add_answer($i, null, ($i < 1));
347             $this->add_response($i);
348             $this->add_jumpto($i, null, ($i == 0 ? LESSON_NEXTPAGE : LESSON_THISPAGE));
349             $this->add_score($i, null, ($i===0)?1:0);
350         }
351     }
354 class lesson_display_answer_form_shortanswer extends moodleform {
356     public function definition() {
357         global $OUTPUT, $USER;
358         $mform = $this->_form;
359         $contents = $this->_customdata['contents'];
361         $hasattempt = false;
362         $attrs = array('size'=>'50', 'maxlength'=>'200');
363         if (isset($this->_customdata['lessonid'])) {
364             $lessonid = $this->_customdata['lessonid'];
365             if (isset($USER->modattempts[$lessonid]->useranswer)) {
366                 $attrs['readonly'] = 'readonly';
367                 $hasattempt = true;
368             }
369         }
371         $placeholder = false;
372         if (preg_match('/_____+/', $contents, $matches)) {
373             $placeholder = $matches[0];
374             $contentsparts = explode( $placeholder, $contents, 2);
375             $attrs['size'] = round(strlen($placeholder) * 1.1);
376         }
378         // Disable shortforms.
379         $mform->setDisableShortforms();
381         $mform->addElement('header', 'pageheader');
382         $mform->addElement('hidden', 'id');
383         $mform->setType('id', PARAM_INT);
385         $mform->addElement('hidden', 'pageid');
386         $mform->setType('pageid', PARAM_INT);
388         if ($placeholder) {
389             $contentsgroup = array();
390             $contentsgroup[] = $mform->createElement('static', '', '', $contentsparts[0]);
391             $contentsgroup[] = $mform->createElement('text', 'answer', '', $attrs);
392             $contentsgroup[] = $mform->createElement('static', '', '', $contentsparts[1]);
393             $mform->addGroup($contentsgroup, '', '', '', false);
394         } else {
395             $mform->addElement('html', $OUTPUT->container($contents, 'contents'));
396             $mform->addElement('text', 'answer', get_string('youranswer', 'lesson'), $attrs);
398         }
399         $mform->setType('answer', PARAM_TEXT);
401         if ($hasattempt) {
402             $this->add_action_buttons(null, get_string("nextpage", "lesson"));
403         } else {
404             $this->add_action_buttons(null, get_string("submit", "lesson"));
405         }
406     }