some xhtml validity fixes for the case that select and text elements are 'frozen'
[moodle.git] / question / type / calculated / edit_calculated_form.php
CommitLineData
271ffe3f 1<?php
2/**
3 * Defines the editing form for the calculated 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
8 * @package questions
9 */
10
11/**
12 * calculated editing form definition.
13 */
14class question_edit_calculated_form extends question_edit_form {
9aa022fe 15 /**
16 * Handle to the question type for this question.
17 *
18 * @var question_calculated_qtype
19 */
20 var $qtypeobj;
271ffe3f 21 /**
22 * Add question-type specific form fields.
23 *
9aa022fe 24 * @param MoodleQuickForm $mform the form being built.
271ffe3f 25 */
26 function definition_inner(&$mform) {
9aa022fe 27 global $QTYPES;
28 $this->qtypeobj = $QTYPES['calculated'];
271ffe3f 29
9aa022fe 30//------------------------------------------------------------------------------------------
31 $repeated = array();
32 $repeated[] =& $mform->createElement('header', 'answerhdr', get_string('answerhdr', 'qtype_calculated', '{no}'));
271ffe3f 33
9aa022fe 34 $repeated[] =& $mform->createElement('text', 'answers', get_string('correctanswerformula', 'quiz'));
35 $repeatedoptions['answers']['type'] = PARAM_NOTAGS;
271ffe3f 36
271ffe3f 37 $creategrades = get_grade_options();
9aa022fe 38 $gradeoptions = $creategrades->gradeoptions;
271ffe3f 39 $repeated[] =& $mform->createElement('select', 'fraction', get_string('grade'), $gradeoptions);
9aa022fe 40 $repeatedoptions['fraction']['default'] = 0;
41
42 $repeated[] =& $mform->createElement('text', 'tolerance', get_string('tolerance', 'qtype_calculated'));
43 $repeatedoptions['tolerance']['type'] = PARAM_NUMBER;
44 $repeatedoptions['tolerance']['default'] = 0.01;
45 $repeated[] =& $mform->createElement('select', 'tolerancetype', get_string('tolerancetype', 'quiz'), $this->qtypeobj->tolerance_types());
46
47 $repeated[] =& $mform->createElement('select', 'correctanswerlength', get_string('correctanswershows', 'qtype_calculated'), range(0, 9));
48 $repeatedoptions['correctanswerlength']['default'] = 2;
49
50 $answerlengthformats = array('1' => get_string('decimalformat', 'quiz'), '2' => get_string('significantfiguresformat', 'quiz'));
51 $repeated[] =& $mform->createElement('select', 'correctanswerformat', get_string('correctanswershowsformat', 'qtype_calculated'), $answerlengthformats);
52
53 $repeated[] =& $mform->createElement('htmleditor', 'feedback', get_string('feedback', 'quiz'));
54 $repeatedoptions['feedback']['type'] = PARAM_RAW;
271ffe3f 55
56 if (isset($this->question->options)){
9aa022fe 57 $count = count($this->question->options->answers);
271ffe3f 58 } else {
9aa022fe 59 $count = 0;
271ffe3f 60 }
9aa022fe 61 $repeatsatstart = $count + 1;
62 $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', 1, get_string('addmoreanswerblanks', 'qtype_calculated'));
63
64//------------------------------------------------------------------------------------------
65 $repeated = array();
271ffe3f 66 $repeatedoptions = array();
9aa022fe 67 $repeated[] =& $mform->createElement('header', 'unithdr', get_string('unithdr', 'qtype_numerical', '{no}'));
271ffe3f 68
9aa022fe 69 $repeated[] =& $mform->createElement('text', 'unit', get_string('unit', 'quiz'));
70 $repeatedoptions['unit']['type'] = PARAM_NOTAGS;
271ffe3f 71
9aa022fe 72 $repeated[] =& $mform->createElement('text', 'multiplier', get_string('multiplier', 'quiz'));
73 $repeatedoptions['multiplier']['type'] = PARAM_NUMBER;
74
75 if (isset($this->question->options)){
76 $countunits = count($this->question->options->units);
77 } else {
78 $countunits = 0;
79 }
80 $repeatsatstart = $countunits + 1;
81 $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'nounits', 'addunits', 2, get_string('addmoreunitblanks', 'qtype_calculated', '{no}'));
271ffe3f 82
9aa022fe 83 $firstunit = $mform->getElement('multiplier[0]');
84 $firstunit->freeze();
85 $firstunit->setValue('1.0');
86 $firstunit->setPersistantFreeze(true);
271ffe3f 87
9aa022fe 88 //hidden elements
89 $mform->addElement('hidden', 'wizardpage', 'question');
271ffe3f 90
91 }
92
93 function set_defaults($question) {
94 if (isset($question->options)){
95 $answers = $question->options->answers;
96 if (count($answers)) {
97 $key = 0;
98 foreach ($answers as $answer){
9aa022fe 99 $default_values['answers['.$key.']'] = $answers->answer;
100 $default_values['fraction['.$key.']'] = $answers->fraction;
101 $default_values['tolerance['.$key.']'] = $answers->tolerance;
102 $default_values['correctanswerlength['.$key.']'] = $answers->correctanswerlength;
103 $default_values['correctanswerformat['.$key.']'] = $answer->correctanswerformat;
271ffe3f 104 $default_values['feedback['.$key.']'] = $answer->feedback;
105 $key++;
106 }
107 }
9aa022fe 108 $units = array_values($question->options->units);
109 // make sure the default unit is at index 0
110 usort($units, create_function('$a, $b', // make sure the default unit is at index 0
111 'if (1.0 === (float)$a->multiplier) { return -1; } else '.
112 'if (1.0 === (float)$b->multiplier) { return 1; } else { return 0; }'));
113 if (count($units)) {
114 $key = 0;
115 foreach ($units as $unit){
116 $default_values['unit['.$key.']'] = $unit->unit;
117 $default_values['multiplier['.$key.']'] = $unit->multiplier;
118 $key++;
119 }
120 }
121 $question = (object)((array)$question + $default_values); }
271ffe3f 122 parent::set_defaults($question);
123 }
124
125 function qtype() {
126 return 'calculated';
127 }
128
129 function validation($data){
130 $errors = array();
9aa022fe 131 $answers = $data['answers'];
271ffe3f 132 $answercount = 0;
9aa022fe 133 //check grades
134 /*$totalfraction = 0;
135 $maxfraction = -1;*/
136
137 foreach ($answers as $key => $answer){
138 //check no of choices
271ffe3f 139 $trimmedanswer = trim($answer);
140 if (!empty($trimmedanswer)){
9aa022fe 141 $eqerror = qtype_calculated_find_formula_errors($trimmedanswer);
142 if (FALSE !== $eqerror){
143 $errors['answers['.$key.']'] = $eqerror;
144 }
145 if ('2' == $data['correctanswerformat'][$key]
146 && '0' == $data['correctanswerlength'][$key]) {
147 $errors['correctanswerlength['.$key.']'] = get_string('zerosignificantfiguresnotallowed','quiz');
148 }
149 if (!is_numeric($data['tolerance'][$key])){
150 $errors['tolerance['.$key.']'] = get_string('mustbenumeric', 'qtype_calculated');
151 }
152
271ffe3f 153 $answercount++;
154 }
9aa022fe 155 //check grades
156
157 //TODO how should grade checking work here??
158 /*if ($answer != '') {
159 if ($data['fraction'][$key] > 0) {
160 $totalfraction += $data['fraction'][$key];
161 }
162 if ($data['fraction'][$key] > $maxfraction) {
163 $maxfraction = $data['fraction'][$key];
164 }
165 }*/
166 }
167 //grade checking :
168 /// Perform sanity checks on fractional grades
169 /*if ( ) {
170 if ($maxfraction != 1) {
171 $maxfraction = $maxfraction * 100;
172 $errors['fraction[0]'] = get_string('errfractionsnomax', 'qtype_multichoice', $maxfraction);
173 }
174 } else {
175 $totalfraction = round($totalfraction,2);
176 if ($totalfraction != 1) {
177 $totalfraction = $totalfraction * 100;
178 $errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice', $totalfraction);
179 }
180 }*/
181 $units = $data['unit'];
182 if (count($units)) {
183 foreach ($units as $key => $unit){
184 if (is_numeric($unit)){
185 $errors['unit['.$key.']'] = get_string('mustnotbenumeric', 'qtype_calculated');
186 }
187 $trimmedunit = trim($unit);
188 $trimmedmultiplier = trim($data['multiplier'][$key]);
189 if (!empty($trimmedunit)){
190 if (empty($trimmedmultiplier)){
191 $errors['multiplier['.$key.']'] = get_string('youmustenteramultiplierhere', 'qtype_calculated');
192 }
193 if (!is_numeric($trimmedmultiplier)){
194 $errors['multiplier['.$key.']'] = get_string('mustbenumeric', 'qtype_calculated');
195 }
196
197 }
198 }
271ffe3f 199 }
200 if ($answercount==0){
9aa022fe 201 $errors['answers[0]'] = get_string('atleastoneanswer', 'qtype_calculated');
271ffe3f 202 }
9aa022fe 203
271ffe3f 204 return $errors;
205 }
206}
207?>