Clean up whitespace.
[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");
0dd3e11c 67 $mform->addElement('hidden', 'initialcategory', 1);
f6232d58 68 $html2 = $this->qtypeobj->print_dataset_definitions_category($this->question);
0dd3e11c 69 $mform->insertElementBefore($mform->createElement('static','listcategory',$label,$html2),'name');
70 $addfieldsname='updatecategory';
1d9bc9a6 71 $addstring=get_string("updatecategory", "qtype_calculated");
0dd3e11c 72 $mform->registerNoSubmitButton($addfieldsname);
73
74 $mform->insertElementBefore( $mform->createElement('submit', $addfieldsname, $addstring),'listcategory');
92186abc 75
76 $mform->addElement('header', 'answerhdr', get_string('answerhdr', 'qtype_calculated'));
77
78 $mform->addElement('text', 'answers[0]', get_string('correctanswerformula', 'quiz'));
79 $mform->setType('answers[0]', PARAM_NOTAGS);
80
81/* $creategrades = get_grade_options();
82 $gradeoptions = $creategrades->gradeoptions;
83 $mform->addElement('select', 'fraction[0]', get_string('grade'), $gradeoptions);
84 $mform->setDefault('fraction[0]', 0);*/
85 $mform->addElement('hidden', 'fraction[0]', 1);
f6232d58 86 // $mform->setConstants(array('fraction[0]'=>PARAM_INT));
92186abc 87
88 $tolgrp = array();
89 $tolgrp[] =& $mform->createElement('text', 'tolerance[0]', get_string('tolerance', 'qtype_calculated'));
90 $mform->setType('tolerance[0]', PARAM_NUMBER);
91 $mform->setDefault('tolerance[0]', 0.01);
92 $tolgrp[] =& $mform->createElement('select', 'tolerancetype[0]', get_string('tolerancetype', 'quiz'), $this->qtypeobj->tolerance_types());
93 $mform->addGroup($tolgrp, 'tolgrp', get_string('tolerance', 'qtype_calculated'), null, false);
94
95 $anslengrp = array();
96 $anslengrp[] =& $mform->createElement('select', 'correctanswerlength[0]', get_string('correctanswershows', 'qtype_calculated'), range(0, 9));
97 $mform->setDefault('correctanswerlength[0]', 2);
98
99 $answerlengthformats = array('1' => get_string('decimalformat', 'quiz'), '2' => get_string('significantfiguresformat', 'quiz'));
100 $anslengrp[] =& $mform->createElement('select', 'correctanswerformat[0]', get_string('correctanswershowsformat', 'qtype_calculated'), $answerlengthformats);
101 $mform->addGroup($anslengrp, 'anslengrp', get_string('correctanswershows', 'qtype_calculated'), null, false);
102
103 $mform->addElement('htmleditor', 'feedback[0]', get_string('feedback', 'quiz'));
3a298174 104 $mform->setType('feedback', PARAM_RAW);
9aa022fe 105
106//------------------------------------------------------------------------------------------
107 $repeated = array();
9aa022fe 108 $repeated[] =& $mform->createElement('header', 'unithdr', get_string('unithdr', 'qtype_numerical', '{no}'));
271ffe3f 109
9aa022fe 110 $repeated[] =& $mform->createElement('text', 'unit', get_string('unit', 'quiz'));
3a298174 111 $mform->setType('unit', PARAM_NOTAGS);
271ffe3f 112
9aa022fe 113 $repeated[] =& $mform->createElement('text', 'multiplier', get_string('multiplier', 'quiz'));
3a298174 114 $mform->setType('multiplier', PARAM_NUMBER);
9aa022fe 115
116 if (isset($this->question->options)){
117 $countunits = count($this->question->options->units);
118 } else {
119 $countunits = 0;
120 }
121 $repeatsatstart = $countunits + 1;
3a298174 122 $this->repeat_elements($repeated, $repeatsatstart, array(), 'nounits', 'addunits', 2, get_string('addmoreunitblanks', 'qtype_calculated', '{no}'));
271ffe3f 123
b4c46a8b 124 $firstunit =& $mform->getElement('multiplier[0]');
9aa022fe 125 $firstunit->freeze();
126 $firstunit->setValue('1.0');
127 $firstunit->setPersistantFreeze(true);
271ffe3f 128
9aa022fe 129 //hidden elements
60b5ecd3 130 $mform->addElement('hidden', 'wizard', 'datasetdefinitions');
131 $mform->setType('wizard', PARAM_ALPHA);
92186abc 132
271ffe3f 133
134 }
135
32db0d42 136 function set_data($question) {
271ffe3f 137 if (isset($question->options)){
138 $answers = $question->options->answers;
139 if (count($answers)) {
140 $key = 0;
141 foreach ($answers as $answer){
92186abc 142 $default_values['answers['.$key.']'] = $answer->answer;
143 $default_values['fraction['.$key.']'] = $answer->fraction;
144 $default_values['tolerance['.$key.']'] = $answer->tolerance;
145 $default_values['correctanswerlength['.$key.']'] = $answer->correctanswerlength;
9aa022fe 146 $default_values['correctanswerformat['.$key.']'] = $answer->correctanswerformat;
271ffe3f 147 $default_values['feedback['.$key.']'] = $answer->feedback;
148 $key++;
149 }
150 }
9aa022fe 151 $units = array_values($question->options->units);
152 // make sure the default unit is at index 0
153 usort($units, create_function('$a, $b', // make sure the default unit is at index 0
154 'if (1.0 === (float)$a->multiplier) { return -1; } else '.
155 'if (1.0 === (float)$b->multiplier) { return 1; } else { return 0; }'));
156 if (count($units)) {
157 $key = 0;
158 foreach ($units as $unit){
159 $default_values['unit['.$key.']'] = $unit->unit;
160 $default_values['multiplier['.$key.']'] = $unit->multiplier;
161 $key++;
162 }
163 }
92186abc 164 }
8fc3e643 165 $default_values['submitbutton'] = get_string('nextpage', 'qtype_calculated');
166 $default_values['makecopy'] = get_string('makecopynextpage', 'qtype_calculated');
0dd3e11c 167 /* set the wild cards category display given that on loading the category element is
168 unselected when processing this function but have a valid value when processing the
169 update category button. The value can be obtain by
170 $qu->category =$this->_form->_elements[$this->_form->_elementIndex['category']]->_values[0];
171 but is coded using existing functions
172 */
173 $qu = new stdClass;
174 $el = new stdClass;
175 /* no need to call elementExists() here */
176 $el=$this->_form->getElement('category');
177 if($value =$el->getSelected()) {
178 $qu->category =$value[0];
179 }else {
180 $qu->category=$question->category;// on load $question->category is set by question.php
181 }
182 $html2 = $this->qtypeobj->print_dataset_definitions_category($qu);
183 $this->_form->_elements[$this->_form->_elementIndex['listcategory']]->_text = $html2 ;
184 $question = (object)((array)$question + $default_values);
92186abc 185
32db0d42 186 parent::set_data($question);
271ffe3f 187 }
188
189 function qtype() {
190 return 'calculated';
191 }
192
193 function validation($data){
194 $errors = array();
9aa022fe 195 $answers = $data['answers'];
271ffe3f 196 $answercount = 0;
9aa022fe 197 //check grades
198 /*$totalfraction = 0;
f6232d58 199 $maxfraction = -1; */
200 $possibledatasets = $this->qtypeobj->find_dataset_names($data['questiontext']);
201 $mandatorydatasets = array();
202 foreach ($answers as $key => $answer){
203 $mandatorydatasets += $this->qtypeobj->find_dataset_names($data['questiontext']);
204 }
205 if (count($possibledatasets) == 0 && count($mandatorydatasets )==0){
206 $errors['questiontext']=get_string('atleastonewildcard', 'qtype_datasetdependent');
207 foreach ($answers as $key => $answer){
208 $errors['answers['.$key.']'] = get_string('atleastonewildcard', 'qtype_datasetdependent');
209 }
210 }
9aa022fe 211 foreach ($answers as $key => $answer){
212 //check no of choices
271ffe3f 213 $trimmedanswer = trim($answer);
845b703b 214 if (($trimmedanswer!='')||$answercount==0){
9aa022fe 215 $eqerror = qtype_calculated_find_formula_errors($trimmedanswer);
216 if (FALSE !== $eqerror){
217 $errors['answers['.$key.']'] = $eqerror;
218 }
8fc3e643 219 }
845b703b 220 if ($trimmedanswer!=''){
9aa022fe 221 if ('2' == $data['correctanswerformat'][$key]
222 && '0' == $data['correctanswerlength'][$key]) {
223 $errors['correctanswerlength['.$key.']'] = get_string('zerosignificantfiguresnotallowed','quiz');
224 }
225 if (!is_numeric($data['tolerance'][$key])){
226 $errors['tolerance['.$key.']'] = get_string('mustbenumeric', 'qtype_calculated');
227 }
228
271ffe3f 229 $answercount++;
230 }
9aa022fe 231 //check grades
232
233 //TODO how should grade checking work here??
234 /*if ($answer != '') {
235 if ($data['fraction'][$key] > 0) {
236 $totalfraction += $data['fraction'][$key];
237 }
238 if ($data['fraction'][$key] > $maxfraction) {
239 $maxfraction = $data['fraction'][$key];
240 }
241 }*/
242 }
243 //grade checking :
244 /// Perform sanity checks on fractional grades
245 /*if ( ) {
246 if ($maxfraction != 1) {
247 $maxfraction = $maxfraction * 100;
248 $errors['fraction[0]'] = get_string('errfractionsnomax', 'qtype_multichoice', $maxfraction);
249 }
250 } else {
251 $totalfraction = round($totalfraction,2);
252 if ($totalfraction != 1) {
253 $totalfraction = $totalfraction * 100;
254 $errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice', $totalfraction);
255 }
256 }*/
257 $units = $data['unit'];
258 if (count($units)) {
259 foreach ($units as $key => $unit){
260 if (is_numeric($unit)){
261 $errors['unit['.$key.']'] = get_string('mustnotbenumeric', 'qtype_calculated');
262 }
263 $trimmedunit = trim($unit);
264 $trimmedmultiplier = trim($data['multiplier'][$key]);
265 if (!empty($trimmedunit)){
266 if (empty($trimmedmultiplier)){
267 $errors['multiplier['.$key.']'] = get_string('youmustenteramultiplierhere', 'qtype_calculated');
268 }
269 if (!is_numeric($trimmedmultiplier)){
270 $errors['multiplier['.$key.']'] = get_string('mustbenumeric', 'qtype_calculated');
271 }
272
273 }
274 }
271ffe3f 275 }
276 if ($answercount==0){
9aa022fe 277 $errors['answers[0]'] = get_string('atleastoneanswer', 'qtype_calculated');
271ffe3f 278 }
9aa022fe 279
271ffe3f 280 return $errors;
281 }
282}
283?>