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