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