Added dmllib.php because we need set_dbfamily in the environment stuff
[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'));
94 $mform->setType('feedback[0]', PARAM_RAW);
9aa022fe 95
96//------------------------------------------------------------------------------------------
97 $repeated = array();
271ffe3f 98 $repeatedoptions = array();
9aa022fe 99 $repeated[] =& $mform->createElement('header', 'unithdr', get_string('unithdr', 'qtype_numerical', '{no}'));
271ffe3f 100
9aa022fe 101 $repeated[] =& $mform->createElement('text', 'unit', get_string('unit', 'quiz'));
102 $repeatedoptions['unit']['type'] = PARAM_NOTAGS;
271ffe3f 103
9aa022fe 104 $repeated[] =& $mform->createElement('text', 'multiplier', get_string('multiplier', 'quiz'));
105 $repeatedoptions['multiplier']['type'] = PARAM_NUMBER;
106
107 if (isset($this->question->options)){
108 $countunits = count($this->question->options->units);
109 } else {
110 $countunits = 0;
111 }
112 $repeatsatstart = $countunits + 1;
113 $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'nounits', 'addunits', 2, get_string('addmoreunitblanks', 'qtype_calculated', '{no}'));
271ffe3f 114
9aa022fe 115 $firstunit = $mform->getElement('multiplier[0]');
116 $firstunit->freeze();
117 $firstunit->setValue('1.0');
118 $firstunit->setPersistantFreeze(true);
271ffe3f 119
9aa022fe 120 //hidden elements
121 $mform->addElement('hidden', 'wizardpage', 'question');
92186abc 122 $mform->setType('wizardpage', PARAM_ALPHA);
123
271ffe3f 124
125 }
126
32db0d42 127 function set_data($question) {
271ffe3f 128 if (isset($question->options)){
129 $answers = $question->options->answers;
130 if (count($answers)) {
131 $key = 0;
132 foreach ($answers as $answer){
92186abc 133 $default_values['answers['.$key.']'] = $answer->answer;
134 $default_values['fraction['.$key.']'] = $answer->fraction;
135 $default_values['tolerance['.$key.']'] = $answer->tolerance;
136 $default_values['correctanswerlength['.$key.']'] = $answer->correctanswerlength;
9aa022fe 137 $default_values['correctanswerformat['.$key.']'] = $answer->correctanswerformat;
271ffe3f 138 $default_values['feedback['.$key.']'] = $answer->feedback;
139 $key++;
140 }
141 }
9aa022fe 142 $units = array_values($question->options->units);
143 // make sure the default unit is at index 0
144 usort($units, create_function('$a, $b', // make sure the default unit is at index 0
145 'if (1.0 === (float)$a->multiplier) { return -1; } else '.
146 'if (1.0 === (float)$b->multiplier) { return 1; } else { return 0; }'));
147 if (count($units)) {
148 $key = 0;
149 foreach ($units as $unit){
150 $default_values['unit['.$key.']'] = $unit->unit;
151 $default_values['multiplier['.$key.']'] = $unit->multiplier;
152 $key++;
153 }
154 }
92186abc 155 $default_values['submitbutton'] = get_string('nextpage', 'qtype_calculated');
156 $question = (object)((array)$question + $default_values);
157 }
158
32db0d42 159 parent::set_data($question);
271ffe3f 160 }
161
162 function qtype() {
163 return 'calculated';
164 }
165
166 function validation($data){
167 $errors = array();
9aa022fe 168 $answers = $data['answers'];
271ffe3f 169 $answercount = 0;
9aa022fe 170 //check grades
171 /*$totalfraction = 0;
172 $maxfraction = -1;*/
173
174 foreach ($answers as $key => $answer){
175 //check no of choices
271ffe3f 176 $trimmedanswer = trim($answer);
177 if (!empty($trimmedanswer)){
9aa022fe 178 $eqerror = qtype_calculated_find_formula_errors($trimmedanswer);
179 if (FALSE !== $eqerror){
180 $errors['answers['.$key.']'] = $eqerror;
181 }
182 if ('2' == $data['correctanswerformat'][$key]
183 && '0' == $data['correctanswerlength'][$key]) {
184 $errors['correctanswerlength['.$key.']'] = get_string('zerosignificantfiguresnotallowed','quiz');
185 }
186 if (!is_numeric($data['tolerance'][$key])){
187 $errors['tolerance['.$key.']'] = get_string('mustbenumeric', 'qtype_calculated');
188 }
189
271ffe3f 190 $answercount++;
191 }
9aa022fe 192 //check grades
193
194 //TODO how should grade checking work here??
195 /*if ($answer != '') {
196 if ($data['fraction'][$key] > 0) {
197 $totalfraction += $data['fraction'][$key];
198 }
199 if ($data['fraction'][$key] > $maxfraction) {
200 $maxfraction = $data['fraction'][$key];
201 }
202 }*/
203 }
204 //grade checking :
205 /// Perform sanity checks on fractional grades
206 /*if ( ) {
207 if ($maxfraction != 1) {
208 $maxfraction = $maxfraction * 100;
209 $errors['fraction[0]'] = get_string('errfractionsnomax', 'qtype_multichoice', $maxfraction);
210 }
211 } else {
212 $totalfraction = round($totalfraction,2);
213 if ($totalfraction != 1) {
214 $totalfraction = $totalfraction * 100;
215 $errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice', $totalfraction);
216 }
217 }*/
218 $units = $data['unit'];
219 if (count($units)) {
220 foreach ($units as $key => $unit){
221 if (is_numeric($unit)){
222 $errors['unit['.$key.']'] = get_string('mustnotbenumeric', 'qtype_calculated');
223 }
224 $trimmedunit = trim($unit);
225 $trimmedmultiplier = trim($data['multiplier'][$key]);
226 if (!empty($trimmedunit)){
227 if (empty($trimmedmultiplier)){
228 $errors['multiplier['.$key.']'] = get_string('youmustenteramultiplierhere', 'qtype_calculated');
229 }
230 if (!is_numeric($trimmedmultiplier)){
231 $errors['multiplier['.$key.']'] = get_string('mustbenumeric', 'qtype_calculated');
232 }
233
234 }
235 }
271ffe3f 236 }
237 if ($answercount==0){
9aa022fe 238 $errors['answers[0]'] = get_string('atleastoneanswer', 'qtype_calculated');
271ffe3f 239 }
9aa022fe 240
271ffe3f 241 return $errors;
242 }
243}
244?>