weekly release 2.5dev
[moodle.git] / question / type / multianswer / edit_multianswer_form.php
CommitLineData
aeb15530 1<?php
d3603157
TH
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
271ffe3f 17/**
d3603157 18 * Defines the editing form for the multi-answer question type.
271ffe3f 19 *
b04a4319
TH
20 * @package qtype
21 * @subpackage multianswer
22 * @copyright 2007 Jamie Pratt me@jamiep.org
23 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
271ffe3f 24 */
25
d3603157 26
a17b297d
TH
27defined('MOODLE_INTERNAL') || die();
28
29
271ffe3f 30/**
d3603157 31 * Form for editing multi-answer questions.
b04a4319
TH
32 *
33 * @copyright 2007 Jamie Pratt me@jamiep.org
34 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
271ffe3f 35 */
ab50232b 36class qtype_multianswer_edit_form extends question_edit_form {
271ffe3f 37
59a3fcd3
TH
38 // $questiondisplay will contain the qtype_multianswer_extract_question from
39 // the questiontext
40 public $questiondisplay;
41 // $savedquestiondisplay will contain the qtype_multianswer_extract_question
42 // from the questiontext in database
43 public $savedquestion;
44 public $savedquestiondisplay;
45 public $used_in_quiz = false;
46 public $qtype_change = false;
47 public $negative_diff = 0;
fe6ce234
DC
48 public $nb_of_quiz = 0;
49 public $nb_of_attempts = 0;
59a3fcd3
TH
50 public $confirm = 0;
51 public $reload = false;
aeb15530 52
59a3fcd3
TH
53 public function __construct($submiturl, $question, $category, $contexts, $formeditable = true) {
54 global $SESSION, $CFG, $DB;
347fb175 55 $this->regenerate = true;
7d087744 56 $this->reload = optional_param('reload', false, PARAM_BOOL);
59a3fcd3
TH
57
58 $this->used_in_quiz = false;
59
60 if (isset($question->id) && $question->id != 0) {
61 $this->savedquestiondisplay = fullclone($question);
62 if ($list = $DB->get_records('quiz_question_instances',
63 array('question' => $question->id))) {
64 foreach ($list as $key => $li) {
d3e3bd6c 65 $this->nb_of_quiz ++;
59a3fcd3
TH
66 if ($att = $DB->get_records('quiz_attempts',
67 array('quiz' => $li->quiz, 'preview' => '0'))) {
68 $this->nb_of_attempts += count($att);
d3e3bd6c 69 $this->used_in_quiz = true;
70 }
71 }
72 }
73 }
74
59a3fcd3 75 parent::__construct($submiturl, $question, $category, $contexts, $formeditable);
d3e3bd6c 76 }
77
c7df5006 78 protected function definition_inner($mform) {
347fb175
PP
79 $mform->addElement('hidden', 'reload', 1);
80 $mform->setType('reload', PARAM_INT);
705b5874 81
ab50232b
TH
82 // Remove meaningless defaultmark field.
83 $mform->removeElement('defaultmark');
7d087744 84 $this->confirm = optional_param('confirm', false, PARAM_BOOL);
f34488b2 85
ff82d7fd
TH
86 // Make questiontext a required field for this question type.
87 $mform->addRule('questiontext', null, 'required', null, 'client');
88
fe6ce234 89 // display the questions from questiontext;
b8cc71e7
PS
90 if ($questiontext = optional_param_array('questiontext', false, PARAM_RAW)) {
91 $this->questiondisplay = fullclone(qtype_multianswer_extract_question($questiontext));
f34488b2 92
59a3fcd3
TH
93 } else {
94 if (!$this->reload && !empty($this->savedquestiondisplay->id)) {
aeb15530 95 // use database data as this is first pass
d3e3bd6c 96 // question->id == 0 so no stored datasets
97 $this->questiondisplay = fullclone($this->savedquestiondisplay);
59a3fcd3
TH
98 foreach ($this->questiondisplay->options->questions as $subquestion) {
99 if (!empty($subquestion)) {
fe6ce234 100 $subquestion->answer = array('');
59a3fcd3
TH
101 foreach ($subquestion->options->answers as $ans) {
102 $subquestion->answer[] = $ans->answer;
fe6ce234 103 }
d3e3bd6c 104 }
aeb15530 105 }
59a3fcd3 106 } else {
d3e3bd6c 107 $this->questiondisplay = "";
108 }
f34488b2 109 }
705b5874 110
59a3fcd3
TH
111 if (isset($this->savedquestiondisplay->options->questions) &&
112 is_array($this->savedquestiondisplay->options->questions)) {
113 $countsavedsubquestions = 0;
114 foreach ($this->savedquestiondisplay->options->questions as $subquestion) {
115 if (!empty($subquestion)) {
fe6ce234 116 $countsavedsubquestions++;
df79079f 117 }
f34488b2 118 }
705b5874 119 } else {
59a3fcd3 120 $countsavedsubquestions = 0;
705b5874 121 }
59a3fcd3
TH
122 if ($this->reload) {
123 if (isset($this->questiondisplay->options->questions) &&
124 is_array($this->questiondisplay->options->questions)) {
125 $countsubquestions = 0;
126 foreach ($this->questiondisplay->options->questions as $subquestion) {
127 if (!empty($subquestion)) {
fe6ce234 128 $countsubquestions++;
d3e3bd6c 129 }
130 }
fe6ce234 131 } else {
59a3fcd3 132 $countsubquestions = 0;
705b5874 133 }
59a3fcd3
TH
134 } else {
135 $countsubquestions = $countsavedsubquestions;
fe6ce234 136 }
aeb15530 137
59a3fcd3
TH
138 $mform->addElement('submit', 'analyzequestion',
139 get_string('decodeverifyquestiontext', 'qtype_multianswer'));
fe6ce234 140 $mform->registerNoSubmitButton('analyzequestion');
59a3fcd3 141 if ($this->reload) {
59a3fcd3 142 for ($sub = 1; $sub <= $countsubquestions; $sub++) {
aeb15530 143
59a3fcd3 144 if (isset($this->questiondisplay->options->questions[$sub]->qtype)) {
ab50232b 145 $this->editas[$sub] = $this->questiondisplay->options->questions[$sub]->qtype;
7d087744
TH
146 } else {
147 $this->editas[$sub] = optional_param('sub_'.$sub.'_qtype', 'unknown type', PARAM_PLUGIN);
fd97082c 148 }
928e7d2a 149
d3e3bd6c 150 $storemess = '';
59a3fcd3
TH
151 if (isset($this->savedquestiondisplay->options->questions[$sub]->qtype) &&
152 $this->savedquestiondisplay->options->questions[$sub]->qtype !=
153 $this->questiondisplay->options->questions[$sub]->qtype) {
154 $this->qtype_change = true;
635971c7
TH
155 $storemess = ' ' . html_writer::tag('span', get_string(
156 'storedqtype', 'qtype_multianswer', question_bank::get_qtype_name(
157 $this->savedquestiondisplay->options->questions[$sub]->qtype)),
158 array('class' => 'error'));
59a3fcd3 159 }
aeb15530 160
5e8a85aa 161 $mform->addElement('header', 'subhdr'.$sub, get_string('questionno', 'question',
59a3fcd3
TH
162 '{#'.$sub.'}').'&nbsp;'.question_bank::get_qtype_name(
163 $this->questiondisplay->options->questions[$sub]->qtype).$storemess);
aeb15530 164
7d087744 165 $mform->addElement('static', 'sub_'.$sub.'_questiontext',
928e7d2a 166 get_string('questiondefinition', 'qtype_multianswer'));
aeb15530 167
59a3fcd3 168 if (isset ($this->questiondisplay->options->questions[$sub]->questiontext)) {
7d087744 169 $mform->setDefault('sub_'.$sub.'_questiontext',
59a3fcd3 170 $this->questiondisplay->options->questions[$sub]->questiontext['text']);
f34488b2 171 }
aeb15530 172
7d087744 173 $mform->addElement('static', 'sub_'.$sub.'_defaultmark',
ab50232b 174 get_string('defaultmark', 'question'));
7d087744 175 $mform->setDefault('sub_'.$sub.'_defaultmark',
ab50232b 176 $this->questiondisplay->options->questions[$sub]->defaultmark);
aeb15530 177
59a3fcd3 178 if ($this->questiondisplay->options->questions[$sub]->qtype == 'shortanswer') {
7d087744 179 $mform->addElement('static', 'sub_'.$sub.'_usecase',
ab50232b 180 get_string('casesensitive', 'qtype_shortanswer'));
fe6ce234 181 }
aeb15530 182
59a3fcd3 183 if ($this->questiondisplay->options->questions[$sub]->qtype == 'multichoice') {
7d087744 184 $mform->addElement('static', 'sub_'.$sub.'_layout',
928e7d2a 185 get_string('layout', 'qtype_multianswer'));
fe6ce234 186 }
aeb15530 187
928e7d2a 188 foreach ($this->questiondisplay->options->questions[$sub]->answer as $key => $ans) {
7d087744 189 $mform->addElement('static', 'sub_'.$sub.'_answer['.$key.']',
928e7d2a 190 get_string('answer', 'question'));
aeb15530 191
59a3fcd3
TH
192 if ($this->questiondisplay->options->questions[$sub]->qtype == 'numerical' &&
193 $key == 0) {
7d087744 194 $mform->addElement('static', 'sub_'.$sub.'_tolerance['.$key.']',
ab50232b 195 get_string('acceptederror', 'qtype_numerical'));
d3e3bd6c 196 }
aeb15530 197
7d087744 198 $mform->addElement('static', 'sub_'.$sub.'_fraction['.$key.']',
59a3fcd3 199 get_string('grade'));
aeb15530 200
7d087744 201 $mform->addElement('static', 'sub_'.$sub.'_feedback['.$key.']',
59a3fcd3 202 get_string('feedback', 'question'));
d3e3bd6c 203 }
f34488b2 204 }
928e7d2a 205
59a3fcd3
TH
206 $this->negative_diff = $countsavedsubquestions - $countsubquestions;
207 if (($this->negative_diff > 0) ||$this->qtype_change ||
208 ($this->used_in_quiz && $this->negative_diff != 0)) {
209 $mform->addElement('header', 'additemhdr',
210 get_string('warningquestionmodified', 'qtype_multianswer'));
fe6ce234 211 }
59a3fcd3
TH
212 if ($this->negative_diff > 0) {
213 $mform->addElement('static', 'alert1', "<strong>".
214 get_string('questiondeleted', 'qtype_multianswer')."</strong>",
215 get_string('questionsless', 'qtype_multianswer', $this->negative_diff));
d3e3bd6c 216 }
59a3fcd3
TH
217 if ($this->qtype_change) {
218 $mform->addElement('static', 'alert1', "<strong>".
219 get_string('questiontypechanged', 'qtype_multianswer')."</strong>",
220 get_string('questiontypechangedcomment', 'qtype_multianswer'));
d3e3bd6c 221 }
d3e3bd6c 222 }
59a3fcd3
TH
223 if ($this->used_in_quiz) {
224 if ($this->negative_diff < 0) {
c4979f02 225 $diff = $countsubquestions - $countsavedsubquestions;
59a3fcd3
TH
226 $mform->addElement('static', 'alert1', "<strong>".
227 get_string('questionsadded', 'qtype_multianswer')."</strong>",
228 "<strong>".get_string('questionsmore', 'qtype_multianswer', $diff).
229 "</strong>");
c4979f02 230 }
59a3fcd3 231 $a = new stdClass();
c4979f02
PP
232 $a->nb_of_quiz = $this->nb_of_quiz;
233 $a->nb_of_attempts = $this->nb_of_attempts;
59a3fcd3
TH
234 $mform->addElement('header', 'additemhdr2',
235 get_string('questionusedinquiz', 'qtype_multianswer', $a));
236 $mform->addElement('static', 'alertas',
237 get_string('youshouldnot', 'qtype_multianswer'));
d3e3bd6c 238 }
59a3fcd3
TH
239 if (($this->negative_diff > 0 || $this->used_in_quiz &&
240 ($this->negative_diff > 0 || $this->negative_diff < 0 || $this->qtype_change)) &&
241 $this->reload) {
242 $mform->addElement('header', 'additemhdr',
243 get_string('questionsaveasedited', 'qtype_multianswer'));
244 $mform->addElement('checkbox', 'confirm', '',
245 get_string('confirmquestionsaveasedited', 'qtype_multianswer'));
d3e3bd6c 246 $mform->setDefault('confirm', 0);
59a3fcd3
TH
247 } else {
248 $mform->addElement('hidden', 'confirm', 0);
705b5874 249 }
347fb175 250
e9af6091 251 $this->add_interactive_settings(true, true);
120e5cbf 252 }
705b5874 253
f34488b2 254
59a3fcd3 255 public function set_data($question) {
f34488b2 256 global $DB;
59a3fcd3
TH
257 $default_values = array();
258 if (isset($question->id) and $question->id and $question->qtype &&
259 $question->questiontext) {
c6fc9988 260
261 foreach ($question->options->questions as $key => $wrapped) {
59a3fcd3 262 if (!empty($wrapped)) {
fe6ce234
DC
263 // The old way of restoring the definitions is kept to gradually
264 // update all multianswer questions
265 if (empty($wrapped->questiontext)) {
ab50232b 266 $parsableanswerdef = '{' . $wrapped->defaultmark . ':';
fe6ce234 267 switch ($wrapped->qtype) {
59a3fcd3
TH
268 case 'multichoice':
269 $parsableanswerdef .= 'MULTICHOICE:';
270 break;
271 case 'shortanswer':
272 $parsableanswerdef .= 'SHORTANSWER:';
273 break;
274 case 'numerical':
275 $parsableanswerdef .= 'NUMERICAL:';
276 break;
277 default:
278 print_error('unknownquestiontype', 'question', '',
279 $wrapped->qtype);
c6fc9988 280 }
59a3fcd3 281 $separator = '';
fe6ce234
DC
282 foreach ($wrapped->options->answers as $subanswer) {
283 $parsableanswerdef .= $separator
284 . '%' . round(100*$subanswer->fraction) . '%';
c1f15d35
TH
285 if (is_array($subanswer->answer)) {
286 $parsableanswerdef .= $subanswer->answer['text'];
287 } else {
288 $parsableanswerdef .= $subanswer->answer;
289 }
fe6ce234
DC
290 if (!empty($wrapped->options->tolerance)) {
291 // Special for numerical answers:
292 $parsableanswerdef .= ":{$wrapped->options->tolerance}";
293 // We only want tolerance for the first alternative, it will
294 // be applied to all of the alternatives.
295 unset($wrapped->options->tolerance);
296 }
297 if ($subanswer->feedback) {
298 $parsableanswerdef .= "#$subanswer->feedback";
299 }
300 $separator = '~';
c6fc9988 301 }
fe6ce234
DC
302 $parsableanswerdef .= '}';
303 // Fix the questiontext fields of old questions
59a3fcd3
TH
304 $DB->set_field('question', 'questiontext', $parsableanswerdef,
305 array('id' => $wrapped->id));
fe6ce234
DC
306 } else {
307 $parsableanswerdef = str_replace('&#', '&\#', $wrapped->questiontext);
c6fc9988 308 }
59a3fcd3
TH
309 $question->questiontext = str_replace("{#$key}", $parsableanswerdef,
310 $question->questiontext);
c6fc9988 311 }
c6fc9988 312 }
313 }
347fb175 314
705b5874 315 // set default to $questiondisplay questions elements
59a3fcd3 316 if ($this->reload) {
fe6ce234 317 if (isset($this->questiondisplay->options->questions)) {
59a3fcd3 318 $subquestions = fullclone($this->questiondisplay->options->questions);
fe6ce234 319 if (count($subquestions)) {
59a3fcd3 320 $sub = 1;
fe6ce234 321 foreach ($subquestions as $subquestion) {
59a3fcd3 322 $prefix = 'sub_'.$sub.'_';
fe6ce234
DC
323
324 // validate parameters
325 $answercount = 0;
326 $maxgrade = false;
327 $maxfraction = -1;
59a3fcd3 328 if ($subquestion->qtype == 'shortanswer') {
fe6ce234 329 switch ($subquestion->usecase) {
59a3fcd3
TH
330 case '1':
331 $default_values[$prefix.'usecase'] =
332 get_string('caseyes', 'qtype_shortanswer');
333 break;
334 case '0':
335 default :
336 $default_values[$prefix.'usecase'] =
337 get_string('caseno', 'qtype_shortanswer');
fe6ce234 338 }
fd97082c 339 }
fd97082c 340
59a3fcd3
TH
341 if ($subquestion->qtype == 'multichoice') {
342 $default_values[$prefix.'layout'] = $subquestion->layout;
fe6ce234 343 switch ($subquestion->layout) {
59a3fcd3
TH
344 case '0':
345 $default_values[$prefix.'layout'] =
346 get_string('layoutselectinline', 'qtype_multianswer');
347 break;
348 case '1':
349 $default_values[$prefix.'layout'] =
350 get_string('layoutvertical', 'qtype_multianswer');
351 break;
352 case '2':
353 $default_values[$prefix.'layout'] =
354 get_string('layouthorizontal', 'qtype_multianswer');
355 break;
356 default:
357 $default_values[$prefix.'layout'] =
358 get_string('layoutundefined', 'qtype_multianswer');
fe6ce234 359 }
aeb15530 360 }
59a3fcd3
TH
361 foreach ($subquestion->answer as $key => $answer) {
362 if ($subquestion->qtype == 'numerical' && $key == 0) {
363 $default_values[$prefix.'tolerance['.$key.']'] =
364 $subquestion->tolerance[0];
705b5874 365 }
c1f15d35
TH
366 if (is_array($answer)) {
367 $answer = $answer['text'];
368 }
fe6ce234
DC
369 $trimmedanswer = trim($answer);
370 if ($trimmedanswer !== '') {
371 $answercount++;
59a3fcd3
TH
372 if ($subquestion->qtype == 'numerical' &&
373 !(is_numeric($trimmedanswer) || $trimmedanswer == '*')) {
374 $this->_form->setElementError($prefix.'answer['.$key.']',
375 get_string('answermustbenumberorstar',
376 'qtype_numerical'));
fe6ce234
DC
377 }
378 if ($subquestion->fraction[$key] == 1) {
379 $maxgrade = true;
380 }
381 if ($subquestion->fraction[$key] > $maxfraction) {
59a3fcd3 382 $maxfraction = $subquestion->fraction[$key];
fe6ce234 383 }
f34488b2 384 }
fe6ce234 385
59a3fcd3
TH
386 $default_values[$prefix.'answer['.$key.']'] =
387 htmlspecialchars($answer);
fe6ce234
DC
388 }
389 if ($answercount == 0) {
59a3fcd3
TH
390 if ($subquestion->qtype == 'multichoice') {
391 $this->_form->setElementError($prefix.'answer[0]',
392 get_string('notenoughanswers', 'qtype_multichoice', 2));
fe6ce234 393 } else {
59a3fcd3
TH
394 $this->_form->setElementError($prefix.'answer[0]',
395 get_string('notenoughanswers', 'question', 1));
705b5874 396 }
397 }
fe6ce234 398 if ($maxgrade == false) {
59a3fcd3
TH
399 $this->_form->setElementError($prefix.'fraction[0]',
400 get_string('fractionsnomax', 'question'));
705b5874 401 }
59a3fcd3 402 foreach ($subquestion->feedback as $key => $answer) {
f34488b2 403
59a3fcd3
TH
404 $default_values[$prefix.'feedback['.$key.']'] =
405 htmlspecialchars ($answer['text']);
fe6ce234 406 }
59a3fcd3
TH
407 foreach ($subquestion->fraction as $key => $answer) {
408 $default_values[$prefix.'fraction['.$key.']'] = $answer;
fe6ce234 409 }
f34488b2 410
fe6ce234
DC
411 $sub++;
412 }
705b5874 413 }
414 }
415 }
59a3fcd3
TH
416 $default_values['alertas']= "<strong>".get_string('questioninquiz', 'qtype_multianswer').
417 "</strong>";
347fb175 418
59a3fcd3 419 if ($default_values != "") {
705b5874 420 $question = (object)((array)$question + $default_values);
421 }
e9af6091 422 $question = $this->data_preprocessing_hints($question, true, true);
120e5cbf 423 parent::set_data($question);
424 }
425
59a3fcd3 426 public function validation($data, $files) {
a78890d5 427 $errors = parent::validation($data, $files);
8e8f9f3d 428
0bc0a265 429 $questiondisplay = qtype_multianswer_extract_question($data['questiontext']);
f34488b2 430
347fb175 431 if (isset($questiondisplay->options->questions)) {
59a3fcd3 432 $subquestions = fullclone($questiondisplay->options->questions);
705b5874 433 if (count($subquestions)) {
59a3fcd3 434 $sub = 1;
f34488b2 435 foreach ($subquestions as $subquestion) {
59a3fcd3 436 $prefix = 'sub_'.$sub.'_';
705b5874 437 $answercount = 0;
438 $maxgrade = false;
439 $maxfraction = -1;
59a3fcd3
TH
440 if (isset($this->savedquestiondisplay->options->questions[$sub]->qtype) &&
441 $this->savedquestiondisplay->options->questions[$sub]->qtype !=
442 $questiondisplay->options->questions[$sub]->qtype) {
443 $storemess = " STORED QTYPE ".question_bank::get_qtype_name(
444 $this->savedquestiondisplay->options->questions[$sub]->qtype);
445 }
446 foreach ($subquestion->answer as $key => $answer) {
c1f15d35
TH
447 if (is_array($answer)) {
448 $answer = $answer['text'];
449 }
705b5874 450 $trimmedanswer = trim($answer);
451 if ($trimmedanswer !== '') {
452 $answercount++;
59a3fcd3
TH
453 if ($subquestion->qtype == 'numerical' &&
454 !(is_numeric($trimmedanswer) || $trimmedanswer == '*')) {
455 $errors[$prefix.'answer['.$key.']'] =
456 get_string('answermustbenumberorstar', 'qtype_numerical');
fe6ce234 457 }
705b5874 458 if ($subquestion->fraction[$key] == 1) {
459 $maxgrade = true;
f34488b2 460 }
705b5874 461 if ($subquestion->fraction[$key] > $maxfraction) {
59a3fcd3 462 $maxfraction = $subquestion->fraction[$key];
705b5874 463 }
f34488b2 464 }
465 }
59a3fcd3
TH
466 if ($answercount == 0) {
467 if ($subquestion->qtype == 'multichoice') {
468 $errors[$prefix.'answer[0]'] =
469 get_string('notenoughanswers', 'qtype_multichoice', 2);
470 } else {
471 $errors[$prefix.'answer[0]'] =
472 get_string('notenoughanswers', 'question', 1);
705b5874 473 }
474 }
475 if ($maxgrade == false) {
59a3fcd3
TH
476 $errors[$prefix.'fraction[0]'] =
477 get_string('fractionsnomax', 'question');
f34488b2 478 }
479 $sub++;
705b5874 480 }
481 } else {
59a3fcd3 482 $errors['questiontext'] = get_string('questionsmissing', 'qtype_multianswer');
705b5874 483 }
120e5cbf 484 }
705b5874 485
59a3fcd3
TH
486 if (($this->negative_diff > 0 || $this->used_in_quiz &&
487 ($this->negative_diff > 0 || $this->negative_diff < 0 ||
7d087744 488 $this->qtype_change)) && !$this->confirm) {
59a3fcd3
TH
489 $errors['confirm'] =
490 get_string('confirmsave', 'qtype_multianswer', $this->negative_diff);
491 }
fe6ce234
DC
492
493 return $errors;
c6fc9988 494 }
271ffe3f 495
59a3fcd3 496 public function qtype() {
271ffe3f 497 return 'multianswer';
498 }
499}