Merge branch 'MDL-10965-master' of git://github.com/marinaglancy/moodle
[moodle.git] / mod / lesson / pagetypes / numerical.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  * Numerical
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 /** Numerical question type */
29 define("LESSON_PAGE_NUMERICAL",     "8");
31 class lesson_page_type_numerical extends lesson_page {
33     protected $type = lesson_page::TYPE_QUESTION;
34     protected $typeidstring = 'numerical';
35     protected $typeid = LESSON_PAGE_NUMERICAL;
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         $formattextdefoptions = new stdClass();
83         $formattextdefoptions->noclean = true;
84         $formattextdefoptions->para = false;
86         // set defaults
87         $result->response = '';
88         $result->newpageid = 0;
90         if (!isset($data->answer) || !is_numeric($data->answer)) {
91             $result->noanswer = true;
92             return $result;
93         } else {
94             // Just doing default PARAM_RAW, not doing PARAM_INT because it could be a float.
95             $result->useranswer = (float)$data->answer;
96         }
97         $result->studentanswer = $result->userresponse = $result->useranswer;
98         $answers = $this->get_answers();
99         foreach ($answers as $answer) {
100             $answer = parent::rewrite_answers_urls($answer);
101             if (strpos($answer->answer, ':')) {
102                 // there's a pairs of values
103                 list($min, $max) = explode(':', $answer->answer);
104                 $minimum = (float) $min;
105                 $maximum = (float) $max;
106             } else {
107                 // there's only one value
108                 $minimum = (float) $answer->answer;
109                 $maximum = $minimum;
110             }
111             if (($result->useranswer >= $minimum) && ($result->useranswer <= $maximum)) {
112                 $result->newpageid = $answer->jumpto;
113                 $result->response = format_text($answer->response, $answer->responseformat, $formattextdefoptions);
114                 if ($this->lesson->jumpto_is_correct($this->properties->id, $result->newpageid)) {
115                     $result->correctanswer = true;
116                 }
117                 if ($this->lesson->custom) {
118                     if ($answer->score > 0) {
119                         $result->correctanswer = true;
120                     } else {
121                         $result->correctanswer = false;
122                     }
123                 }
124                 $result->answerid = $answer->id;
125                 return $result;
126             }
127         }
128         return $result;
129     }
131     public function display_answers(html_table $table) {
132         $answers = $this->get_answers();
133         $options = new stdClass;
134         $options->noclean = true;
135         $options->para = false;
136         $i = 1;
137         foreach ($answers as $answer) {
138             $answer = parent::rewrite_answers_urls($answer, false);
139             $cells = array();
140             if ($this->lesson->custom && $answer->score > 0) {
141                 // if the score is > 0, then it is correct
142                 $cells[] = '<label class="correct">' . get_string('answer', 'lesson') . ' ' . $i . '</label>:';
143             } else if ($this->lesson->custom) {
144                 $cells[] = '<label>' . get_string('answer', 'lesson') . ' ' . $i . '</label>:';
145             } else if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) {
146                 // underline correct answers
147                 $cells[] = '<span class="correct">' . get_string('answer', 'lesson') . ' ' . $i . '</span>:' . "\n";
148             } else {
149                 $cells[] = '<label class="correct">' . get_string('answer', 'lesson') . ' ' . $i . '</label>:';
150             }
151             $cells[] = format_text($answer->answer, $answer->answerformat, $options);
152             $table->data[] = new html_table_row($cells);
154             $cells = array();
155             $cells[] = '<label>' . get_string('response', 'lesson') . ' ' . $i . '</label>:';
156             $cells[] = format_text($answer->response, $answer->responseformat, $options);
157             $table->data[] = new html_table_row($cells);
159             $cells = array();
160             $cells[] = '<label>' . get_string('score', 'lesson') . '</label>:';
161             $cells[] = $answer->score;
162             $table->data[] = new html_table_row($cells);
164             $cells = array();
165             $cells[] = '<label>' . get_string('jump', 'lesson') . '</label>:';
166             $cells[] = $this->get_jump_name($answer->jumpto);
167             $table->data[] = new html_table_row($cells);
168             if ($i === 1){
169                 $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
170             }
171             $i++;
172         }
173         return $table;
174     }
175     public function stats(array &$pagestats, $tries) {
176         if(count($tries) > $this->lesson->maxattempts) { // if there are more tries than the max that is allowed, grab the last "legal" attempt
177             $temp = $tries[$this->lesson->maxattempts - 1];
178         } else {
179             // else, user attempted the question less than the max, so grab the last one
180             $temp = end($tries);
181         }
182         if (isset($pagestats[$temp->pageid][$temp->useranswer])) {
183             $pagestats[$temp->pageid][$temp->useranswer]++;
184         } else {
185             $pagestats[$temp->pageid][$temp->useranswer] = 1;
186         }
187         if (isset($pagestats[$temp->pageid]["total"])) {
188             $pagestats[$temp->pageid]["total"]++;
189         } else {
190             $pagestats[$temp->pageid]["total"] = 1;
191         }
192         return true;
193     }
195     public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) {
196         $answers = $this->get_answers();
197         $formattextdefoptions = new stdClass;
198         $formattextdefoptions->para = false;  //I'll use it widely in this page
199         foreach ($answers as $answer) {
200             if ($useranswer == null && $i == 0) {
201                 // I have the $i == 0 because it is easier to blast through it all at once.
202                 if (isset($pagestats[$this->properties->id])) {
203                     $stats = $pagestats[$this->properties->id];
204                     $total = $stats["total"];
205                     unset($stats["total"]);
206                     foreach ($stats as $valentered => $ntimes) {
207                         $data = '<input class="form-control" type="text" size="50" ' .
208                                 'disabled="disabled" readonly="readonly" value="'.s($valentered).'" />';
209                         $percent = $ntimes / $total * 100;
210                         $percent = round($percent, 2);
211                         $percent .= "% ".get_string("enteredthis", "lesson");
212                         $answerdata->answers[] = array($data, $percent);
213                     }
214                 } else {
215                     $answerdata->answers[] = array(get_string("nooneansweredthisquestion", "lesson"), " ");
216                 }
217                 $i++;
218             } else if ($useranswer != null && ($answer->id == $useranswer->answerid || ($answer == end($answers) &&
219                     empty($answerdata->answers)))) {
220                 // Get in here when the user answered or for the last answer.
221                 $data = '<input class="form-control" type="text" size="50" ' .
222                         'disabled="disabled" readonly="readonly" value="'.s($useranswer->useranswer).'">';
223                 if (isset($pagestats[$this->properties->id][$useranswer->useranswer])) {
224                     $percent = $pagestats[$this->properties->id][$useranswer->useranswer] / $pagestats[$this->properties->id]["total"] * 100;
225                     $percent = round($percent, 2);
226                     $percent .= "% ".get_string("enteredthis", "lesson");
227                 } else {
228                     $percent = get_string("nooneenteredthis", "lesson");
229                 }
230                 $answerdata->answers[] = array($data, $percent);
232                 if ($answer->id == $useranswer->answerid) {
233                     if ($answer->response == null) {
234                         if ($useranswer->correct) {
235                             $answerdata->response = get_string("thatsthecorrectanswer", "lesson");
236                         } else {
237                             $answerdata->response = get_string("thatsthewronganswer", "lesson");
238                         }
239                     } else {
240                         $answerdata->response = $answer->response;
241                     }
242                     if ($this->lesson->custom) {
243                         $answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score;
244                     } elseif ($useranswer->correct) {
245                         $answerdata->score = get_string("receivedcredit", "lesson");
246                     } else {
247                         $answerdata->score = get_string("didnotreceivecredit", "lesson");
248                     }
249                 } else {
250                     $answerdata->response = get_string("thatsthewronganswer", "lesson");
251                     if ($this->lesson->custom) {
252                         $answerdata->score = get_string("pointsearned", "lesson").": 0";
253                     } else {
254                         $answerdata->score = get_string("didnotreceivecredit", "lesson");
255                     }
256                 }
257             }
258             $answerpage->answerdata = $answerdata;
259         }
260         return $answerpage;
261     }
264 class lesson_add_page_form_numerical extends lesson_add_page_form_base {
266     public $qtype = 'numerical';
267     public $qtypestring = 'numerical';
268     protected $answerformat = '';
269     protected $responseformat = LESSON_ANSWER_HTML;
271     public function custom_definition() {
272         for ($i = 0; $i < $this->_customdata['lesson']->maxanswers; $i++) {
273             $this->_form->addElement('header', 'answertitle'.$i, get_string('answer').' '.($i+1));
274             $this->add_answer($i, null, ($i < 1));
275             $this->add_response($i);
276             $this->add_jumpto($i, null, ($i == 0 ? LESSON_NEXTPAGE : LESSON_THISPAGE));
277             $this->add_score($i, null, ($i===0)?1:0);
278         }
279     }
282 class lesson_display_answer_form_numerical extends moodleform {
284     public function definition() {
285         global $USER, $OUTPUT;
286         $mform = $this->_form;
287         $contents = $this->_customdata['contents'];
289         // Disable shortforms.
290         $mform->setDisableShortforms();
292         $mform->addElement('header', 'pageheader');
294         $mform->addElement('html', $OUTPUT->container($contents, 'contents'));
296         $hasattempt = false;
297         $attrs = array('size'=>'50', 'maxlength'=>'200');
298         if (isset($this->_customdata['lessonid'])) {
299             $lessonid = $this->_customdata['lessonid'];
300             if (isset($USER->modattempts[$lessonid]->useranswer)) {
301                 $attrs['readonly'] = 'readonly';
302                 $hasattempt = true;
303             }
304         }
305         $options = new stdClass;
306         $options->para = false;
307         $options->noclean = true;
309         $mform->addElement('hidden', 'id');
310         $mform->setType('id', PARAM_INT);
312         $mform->addElement('hidden', 'pageid');
313         $mform->setType('pageid', PARAM_INT);
315         $mform->addElement('text', 'answer', get_string('youranswer', 'lesson'), $attrs);
316         $mform->setType('answer', PARAM_FLOAT);
318         if ($hasattempt) {
319             $this->add_action_buttons(null, get_string("nextpage", "lesson"));
320         } else {
321             $this->add_action_buttons(null, get_string("submit", "lesson"));
322         }
323     }