MDL-9151 fixed missing global definition, sorry :-(
[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
9e20fbc2 8 * @package questionbank
9 * @subpackage questiontypes
271ffe3f 10 */
11
12/**
13 * calculated editing form definition.
14 */
15class question_edit_calculated_form extends question_edit_form {
9aa022fe 16 /**
17 * Handle to the question type for this question.
18 *
19 * @var question_calculated_qtype
20 */
21 var $qtypeobj;
271ffe3f 22 /**
23 * Add question-type specific form fields.
24 *
9aa022fe 25 * @param MoodleQuickForm $mform the form being built.
271ffe3f 26 */
27 function definition_inner(&$mform) {
9aa022fe 28 global $QTYPES;
92186abc 29 $this->qtypeobj =& $QTYPES[$this->qtype()];
271ffe3f 30
9aa022fe 31//------------------------------------------------------------------------------------------
92186abc 32/* //not working now datasetdependent code cannot handle multiple answer formulas and not needed ??
9aa022fe 33 $repeated = array();
34 $repeated[] =& $mform->createElement('header', 'answerhdr', get_string('answerhdr', 'qtype_calculated', '{no}'));
271ffe3f 35
9aa022fe 36 $repeated[] =& $mform->createElement('text', 'answers', get_string('correctanswerformula', 'quiz'));
37 $repeatedoptions['answers']['type'] = PARAM_NOTAGS;
271ffe3f 38
271ffe3f 39 $creategrades = get_grade_options();
9aa022fe 40 $gradeoptions = $creategrades->gradeoptions;
271ffe3f 41 $repeated[] =& $mform->createElement('select', 'fraction', get_string('grade'), $gradeoptions);
9aa022fe 42 $repeatedoptions['fraction']['default'] = 0;
43
44 $repeated[] =& $mform->createElement('text', 'tolerance', get_string('tolerance', 'qtype_calculated'));
45 $repeatedoptions['tolerance']['type'] = PARAM_NUMBER;
46 $repeatedoptions['tolerance']['default'] = 0.01;
47 $repeated[] =& $mform->createElement('select', 'tolerancetype', get_string('tolerancetype', 'quiz'), $this->qtypeobj->tolerance_types());
48
49 $repeated[] =& $mform->createElement('select', 'correctanswerlength', get_string('correctanswershows', 'qtype_calculated'), range(0, 9));
50 $repeatedoptions['correctanswerlength']['default'] = 2;
51
52 $answerlengthformats = array('1' => get_string('decimalformat', 'quiz'), '2' => get_string('significantfiguresformat', 'quiz'));
53 $repeated[] =& $mform->createElement('select', 'correctanswerformat', get_string('correctanswershowsformat', 'qtype_calculated'), $answerlengthformats);
54
55 $repeated[] =& $mform->createElement('htmleditor', 'feedback', get_string('feedback', 'quiz'));
56 $repeatedoptions['feedback']['type'] = PARAM_RAW;
271ffe3f 57
58 if (isset($this->question->options)){
9aa022fe 59 $count = count($this->question->options->answers);
271ffe3f 60 } else {
9aa022fe 61 $count = 0;
271ffe3f 62 }
9aa022fe 63 $repeatsatstart = $count + 1;
92186abc 64 $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', 1, get_string('addmoreanswerblanks', 'qtype_calculated'));*/
65//------------------------------------------------------------------------------------------
f6232d58 66 $label = get_string("sharedwildcards", "qtype_datasetdependent");
67 $html2 = $this->qtypeobj->print_dataset_definitions_category($this->question);
68 $mform->insertElementBefore($mform->createElement('static','list',$label,$html2),'questiontext');
92186abc 69
70 $mform->addElement('header', 'answerhdr', get_string('answerhdr', 'qtype_calculated'));
71
72 $mform->addElement('text', 'answers[0]', get_string('correctanswerformula', 'quiz'));
73 $mform->setType('answers[0]', PARAM_NOTAGS);
74
75/* $creategrades = get_grade_options();
76 $gradeoptions = $creategrades->gradeoptions;
77 $mform->addElement('select', 'fraction[0]', get_string('grade'), $gradeoptions);
78 $mform->setDefault('fraction[0]', 0);*/
79 $mform->addElement('hidden', 'fraction[0]', 1);
f6232d58 80 // $mform->setConstants(array('fraction[0]'=>PARAM_INT));
92186abc 81
82 $tolgrp = array();
83 $tolgrp[] =& $mform->createElement('text', 'tolerance[0]', get_string('tolerance', 'qtype_calculated'));
84 $mform->setType('tolerance[0]', PARAM_NUMBER);
85 $mform->setDefault('tolerance[0]', 0.01);
86 $tolgrp[] =& $mform->createElement('select', 'tolerancetype[0]', get_string('tolerancetype', 'quiz'), $this->qtypeobj->tolerance_types());
87 $mform->addGroup($tolgrp, 'tolgrp', get_string('tolerance', 'qtype_calculated'), null, false);
88
89 $anslengrp = array();
90 $anslengrp[] =& $mform->createElement('select', 'correctanswerlength[0]', get_string('correctanswershows', 'qtype_calculated'), range(0, 9));
91 $mform->setDefault('correctanswerlength[0]', 2);
92
93 $answerlengthformats = array('1' => get_string('decimalformat', 'quiz'), '2' => get_string('significantfiguresformat', 'quiz'));
94 $anslengrp[] =& $mform->createElement('select', 'correctanswerformat[0]', get_string('correctanswershowsformat', 'qtype_calculated'), $answerlengthformats);
95 $mform->addGroup($anslengrp, 'anslengrp', get_string('correctanswershows', 'qtype_calculated'), null, false);
96
97 $mform->addElement('htmleditor', 'feedback[0]', get_string('feedback', 'quiz'));
3a298174 98 $mform->setType('feedback', PARAM_RAW);
9aa022fe 99
100//------------------------------------------------------------------------------------------
101 $repeated = array();
9aa022fe 102 $repeated[] =& $mform->createElement('header', 'unithdr', get_string('unithdr', 'qtype_numerical', '{no}'));
271ffe3f 103
9aa022fe 104 $repeated[] =& $mform->createElement('text', 'unit', get_string('unit', 'quiz'));
3a298174 105 $mform->setType('unit', PARAM_NOTAGS);
271ffe3f 106
9aa022fe 107 $repeated[] =& $mform->createElement('text', 'multiplier', get_string('multiplier', 'quiz'));
3a298174 108 $mform->setType('multiplier', PARAM_NUMBER);
9aa022fe 109
110 if (isset($this->question->options)){
111 $countunits = count($this->question->options->units);
112 } else {
113 $countunits = 0;
114 }
115 $repeatsatstart = $countunits + 1;
3a298174 116 $this->repeat_elements($repeated, $repeatsatstart, array(), 'nounits', 'addunits', 2, get_string('addmoreunitblanks', 'qtype_calculated', '{no}'));
271ffe3f 117
b4c46a8b 118 $firstunit =& $mform->getElement('multiplier[0]');
9aa022fe 119 $firstunit->freeze();
120 $firstunit->setValue('1.0');
121 $firstunit->setPersistantFreeze(true);
271ffe3f 122
9aa022fe 123 //hidden elements
60b5ecd3 124 $mform->addElement('hidden', 'wizard', 'datasetdefinitions');
125 $mform->setType('wizard', PARAM_ALPHA);
92186abc 126
271ffe3f 127
128 }
129
32db0d42 130 function set_data($question) {
271ffe3f 131 if (isset($question->options)){
132 $answers = $question->options->answers;
133 if (count($answers)) {
134 $key = 0;
135 foreach ($answers as $answer){
92186abc 136 $default_values['answers['.$key.']'] = $answer->answer;
137 $default_values['fraction['.$key.']'] = $answer->fraction;
138 $default_values['tolerance['.$key.']'] = $answer->tolerance;
139 $default_values['correctanswerlength['.$key.']'] = $answer->correctanswerlength;
9aa022fe 140 $default_values['correctanswerformat['.$key.']'] = $answer->correctanswerformat;
271ffe3f 141 $default_values['feedback['.$key.']'] = $answer->feedback;
142 $key++;
143 }
144 }
9aa022fe 145 $units = array_values($question->options->units);
146 // make sure the default unit is at index 0
147 usort($units, create_function('$a, $b', // make sure the default unit is at index 0
148 'if (1.0 === (float)$a->multiplier) { return -1; } else '.
149 'if (1.0 === (float)$b->multiplier) { return 1; } else { return 0; }'));
150 if (count($units)) {
151 $key = 0;
152 foreach ($units as $unit){
153 $default_values['unit['.$key.']'] = $unit->unit;
154 $default_values['multiplier['.$key.']'] = $unit->multiplier;
155 $key++;
156 }
157 }
92186abc 158 }
8fc3e643 159 $default_values['submitbutton'] = get_string('nextpage', 'qtype_calculated');
160 $default_values['makecopy'] = get_string('makecopynextpage', 'qtype_calculated');
161 $question = (object)((array)$question + $default_values);
162
92186abc 163
32db0d42 164 parent::set_data($question);
271ffe3f 165 }
166
167 function qtype() {
168 return 'calculated';
169 }
170
171 function validation($data){
172 $errors = array();
9aa022fe 173 $answers = $data['answers'];
271ffe3f 174 $answercount = 0;
9aa022fe 175 //check grades
176 /*$totalfraction = 0;
f6232d58 177 $maxfraction = -1; */
178 $possibledatasets = $this->qtypeobj->find_dataset_names($data['questiontext']);
179 $mandatorydatasets = array();
180 foreach ($answers as $key => $answer){
181 $mandatorydatasets += $this->qtypeobj->find_dataset_names($data['questiontext']);
182 }
183 if (count($possibledatasets) == 0 && count($mandatorydatasets )==0){
184 $errors['questiontext']=get_string('atleastonewildcard', 'qtype_datasetdependent');
185 foreach ($answers as $key => $answer){
186 $errors['answers['.$key.']'] = get_string('atleastonewildcard', 'qtype_datasetdependent');
187 }
188 }
9aa022fe 189 foreach ($answers as $key => $answer){
190 //check no of choices
271ffe3f 191 $trimmedanswer = trim($answer);
845b703b 192 if (($trimmedanswer!='')||$answercount==0){
9aa022fe 193 $eqerror = qtype_calculated_find_formula_errors($trimmedanswer);
194 if (FALSE !== $eqerror){
195 $errors['answers['.$key.']'] = $eqerror;
196 }
8fc3e643 197 }
845b703b 198 if ($trimmedanswer!=''){
9aa022fe 199 if ('2' == $data['correctanswerformat'][$key]
200 && '0' == $data['correctanswerlength'][$key]) {
201 $errors['correctanswerlength['.$key.']'] = get_string('zerosignificantfiguresnotallowed','quiz');
202 }
203 if (!is_numeric($data['tolerance'][$key])){
204 $errors['tolerance['.$key.']'] = get_string('mustbenumeric', 'qtype_calculated');
205 }
206
271ffe3f 207 $answercount++;
208 }
9aa022fe 209 //check grades
210
211 //TODO how should grade checking work here??
212 /*if ($answer != '') {
213 if ($data['fraction'][$key] > 0) {
214 $totalfraction += $data['fraction'][$key];
215 }
216 if ($data['fraction'][$key] > $maxfraction) {
217 $maxfraction = $data['fraction'][$key];
218 }
219 }*/
220 }
221 //grade checking :
222 /// Perform sanity checks on fractional grades
223 /*if ( ) {
224 if ($maxfraction != 1) {
225 $maxfraction = $maxfraction * 100;
226 $errors['fraction[0]'] = get_string('errfractionsnomax', 'qtype_multichoice', $maxfraction);
227 }
228 } else {
229 $totalfraction = round($totalfraction,2);
230 if ($totalfraction != 1) {
231 $totalfraction = $totalfraction * 100;
232 $errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice', $totalfraction);
233 }
234 }*/
235 $units = $data['unit'];
236 if (count($units)) {
237 foreach ($units as $key => $unit){
238 if (is_numeric($unit)){
239 $errors['unit['.$key.']'] = get_string('mustnotbenumeric', 'qtype_calculated');
240 }
241 $trimmedunit = trim($unit);
242 $trimmedmultiplier = trim($data['multiplier'][$key]);
243 if (!empty($trimmedunit)){
244 if (empty($trimmedmultiplier)){
245 $errors['multiplier['.$key.']'] = get_string('youmustenteramultiplierhere', 'qtype_calculated');
246 }
247 if (!is_numeric($trimmedmultiplier)){
248 $errors['multiplier['.$key.']'] = get_string('mustbenumeric', 'qtype_calculated');
249 }
250
251 }
252 }
271ffe3f 253 }
254 if ($answercount==0){
9aa022fe 255 $errors['answers[0]'] = get_string('atleastoneanswer', 'qtype_calculated');
271ffe3f 256 }
9aa022fe 257
271ffe3f 258 return $errors;
259 }
260}
261?>