MDL-22164 backup - finished cleaning of questions backup code
[moodle.git] / question / type / numerical / edit_numerical_form.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  * Defines the editing form for the numerical question type.
20  *
21  * @copyright &copy; 2007 Jamie Pratt
22  * @author Jamie Pratt me@jamiep.org
23  * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
24  * @package questionbank
25  * @subpackage questiontypes
26  */
28 /**
29  * numerical editing form definition.
30  */
31 class question_edit_numerical_form extends question_edit_form {
33     function get_per_answer_fields(&$mform, $label, $gradeoptions, &$repeatedoptions, &$answersoption) {
34         $repeated = parent::get_per_answer_fields($mform, $label, $gradeoptions, $repeatedoptions, $answersoption);
36         $tolerance =& $mform->createElement('text', 'tolerance', get_string('acceptederror', 'quiz'));
37         $repeatedoptions['tolerance']['type'] = PARAM_NUMBER;
38         array_splice($repeated, 3, 0, array($tolerance));
39         $repeated[1]->setSize(10);
41         return $repeated;
42     }
44     /**
45      * Add question-type specific form fields.
46      *
47      * @param MoodleQuickForm $mform the form being built.
48      */
49     function definition_inner(&$mform) {
50         global $QTYPES ;
52 //------------------------------------------------------------------------------------------
53         $creategrades = get_grade_options();
54         $this->add_per_answer_fields($mform, get_string('answerno', 'qtype_numerical', '{no}'),
55                 $creategrades->gradeoptions);
56 //------------------------------------------------------------------------------------------
57         $QTYPES['numerical']->add_units_options($mform,$this);
58         $QTYPES['numerical']->add_units_elements($mform,$this);
59     }
61     function data_preprocessing($question) {
62         global $QTYPES ;
63         if (isset($question->options)){
64             $answers = $question->options->answers;
65             if (count($answers)) {
66                 $key = 0;
67                 foreach ($answers as $answer){
68                     $draftid = file_get_submitted_draft_itemid('feedback['.$key.']');
69                     $default_values['answer['.$key.']'] = $answer->answer;
70                     $default_values['fraction['.$key.']'] = $answer->fraction;
71                     $default_values['tolerance['.$key.']'] = $answer->tolerance;
72                     $default_values['feedback['.$key.']'] = array();
73                     $default_values['feedback['.$key.']']['format'] = $answer->feedbackformat;
74                     $default_values['feedback['.$key.']']['text'] = file_prepare_draft_area(
75                         $draftid,       // draftid
76                         $this->context->id,    // context
77                         'question',   // component
78                         'answerfeedback',             // filarea
79                         !empty($answer->id)?(int)$answer->id:null, // itemid
80                         $this->fileoptions,    // options
81                         $answer->feedback      // text
82                     );
83                     $default_values['feedback['.$key.']']['itemid'] = $draftid;
84                     $key++;
85                 }
86             }
87             $QTYPES['numerical']->set_numerical_unit_data($this, $question, $default_values);
89           /*  if (isset($question->options->units)){
90                 $units  = array_values($question->options->units);
91                 if (!empty($units)) {
92                     foreach ($units as $key => $unit){
93                         $default_values['unit['.$key.']'] = $unit->unit;
94                         $default_values['multiplier['.$key.']'] = $unit->multiplier;
95                     }
96                 }
97             }*/
98             $question = (object)((array)$question + $default_values);
99         }
100         return $question;
101     }
103     function validation($data, $files) {
104         global $QTYPES;
105         $errors = parent::validation($data, $files);
107         // Check the answers.
108         $answercount = 0;
109         $maxgrade = false;
110         $answers = $data['answer'];
111         foreach ($answers as $key => $answer) {
112             $trimmedanswer = trim($answer);
113             if ($trimmedanswer != '') {
114                 $answercount++;
115                 if (!(is_numeric($trimmedanswer) || $trimmedanswer == '*')) {
116                     $errors["answer[$key]"] = get_string('answermustbenumberorstar', 'qtype_numerical');
117                 }
118                 if ($data['fraction'][$key] == 1) {
119                     $maxgrade = true;
120                 }
121             } else if ($data['fraction'][$key] != 0 || !html_is_blank($data['feedback'][$key]['text'])) {
122                 $errors["answer[$key]"] = get_string('answermustbenumberorstar', 'qtype_numerical');
123                 $answercount++;
124             }
125         }
126         if ($answercount == 0) {
127             $errors['answer[0]'] = get_string('notenoughanswers', 'qtype_numerical');
128         }
129         if ($maxgrade == false) {
130             $errors['fraction[0]'] = get_string('fractionsnomax', 'question');
131         }
132         $QTYPES['numerical']->validate_numerical_options($data, $errors) ;
134         return $errors;
135     }
137     function qtype() {
138         return 'numerical';
139     }