Solving MDL-12063 qtype_questions where left orphans when questions whe deleted or
[moodle.git] / question / type / multianswer / edit_multianswer_form.php
CommitLineData
271ffe3f 1<?php
2/**
3 * Defines the editing form for the multianswer question type.
4 *
5 * @copyright &copy; 2007 Jamie Pratt
6 * @author Jamie Pratt me@jamiep.org
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
830e47a8 8 * @package questionbank
9 * @subpackage questiontypes
271ffe3f 10 */
11
12/**
13 * multianswer editing form definition.
14 */
15class question_edit_multianswer_form extends question_edit_form {
16
17 // No question-type specific form fields.
120e5cbf 18 function definition(){
19 parent::definition();
20 $mform =& $this->_form;
21 $mform->addRule('questiontext', null, 'required', null, 'client');
fe648028 22
23 // Remove meaningless defaultgrade field.
24 $mform->removeElement('defaultgrade');
120e5cbf 25 }
32db0d42 26 function set_data($question) {
120e5cbf 27 if (isset($question->id) and $question->id and $question->qtype and $question->questiontext) {
c6fc9988 28
29 foreach ($question->options->questions as $key => $wrapped) {
30 // The old way of restoring the definitions is kept to gradually
31 // update all multianswer questions
32 if (empty($wrapped->questiontext)) {
33 $parsableanswerdef = '{' . $wrapped->defaultgrade . ':';
34 switch ($wrapped->qtype) {
35 case 'multichoice':
36 $parsableanswerdef .= 'MULTICHOICE:';
37 break;
38 case 'shortanswer':
39 $parsableanswerdef .= 'SHORTANSWER:';
40 break;
41 case 'numerical':
42 $parsableanswerdef .= 'NUMERICAL:';
43 break;
44 default:
45 error("questiontype $wrapped->qtype not recognized");
46 }
47 $separator= '';
48 foreach ($wrapped->options->answers as $subanswer) {
49 $parsableanswerdef .= $separator
50 . '%' . round(100*$subanswer->fraction) . '%';
51 $parsableanswerdef .= $subanswer->answer;
52 if (!empty($wrapped->options->tolerance)) {
53 // Special for numerical answers:
54 $parsableanswerdef .= ":{$wrapped->options->tolerance}";
55 // We only want tolerance for the first alternative, it will
56 // be applied to all of the alternatives.
57 unset($wrapped->options->tolerance);
58 }
59 if ($subanswer->feedback) {
60 $parsableanswerdef .= "#$subanswer->feedback";
61 }
62 $separator = '~';
63 }
64 $parsableanswerdef .= '}';
65 // Fix the questiontext fields of old questions
66 set_field('question', 'questiontext', addslashes($parsableanswerdef), 'id', $wrapped->id);
67 } else {
68 $parsableanswerdef = str_replace('&#', '&\#', $wrapped->questiontext);
69 }
70 $question->questiontext = str_replace("{#$key}", $parsableanswerdef, $question->questiontext);
71 }
72 }
120e5cbf 73 parent::set_data($question);
74 }
75
76 function validation($data){
77 //TODO would be nice to parse the question text here and output some error
78 //messages if there is a problem with the text.
271e6dec 79 $errors = parent::validation($data);
120e5cbf 80 //extra check to make sure there is something in the htmlarea besides a <br />
81 $questiontext= trim(strip_tags($data['questiontext']));
82 if ($questiontext==''){
83 $errors['questiontext'] = get_string('err_required', 'form');
84 }
85 return $errors;
c6fc9988 86 }
271ffe3f 87
88 function qtype() {
89 return 'multianswer';
90 }
91}
92?>