Typos in comments
[moodle.git] / question / type / calculated / edit_calculated_form.php
CommitLineData
aeb15530 1<?php
271ffe3f 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;
2aef1fe5 22
28a27ef1 23 /**
24 * Get the list of form elements to repeat, one for each answer.
25 * @param object $mform the form being built.
26 * @param $label the label to use for each option.
27 * @param $gradeoptions the possible grades for each answer.
28 * @param $repeatedoptions reference to array of repeated options to fill
29 * @param $answersoption reference to return the name of $question->options field holding an array of answers
30 * @return array of form fields.
31 */
32 /* function get_per_answer_fields(&$mform, $label, $gradeoptions, &$repeatedoptions, &$answersoption) {
33 $repeated = array();
34 $repeated[] =& $mform->createElement('header', 'answerhdr', $label);
35 $repeated[] =& $mform->createElement('text', 'answer', get_string('answer', 'quiz'), array('size' => 50));
36 $repeated[] =& $mform->createElement('select', 'fraction', get_string('grade'), $gradeoptions);
37 $repeated[] =& $mform->createElement('htmleditor', 'feedback', get_string('feedback', 'quiz'),
38 array('course' => $this->coursefilesid));
39 $repeatedoptions['answer']['type'] = PARAM_RAW;
40 $repeatedoptions['fraction']['default'] = 0;
41 $answersoption = 'answers';
42 return $repeated;
43 }*/
2aef1fe5 44 function get_per_answer_fields(&$mform, $label, $gradeoptions, &$repeatedoptions, &$answersoption) {
28a27ef1 45 // $repeated = parent::get_per_answer_fields($mform, $label, $gradeoptions, $repeatedoptions, $answersoption);
46 $repeated = array();
47 $repeated[] =& $mform->createElement('header', 'answerhdr', $label);
28a27ef1 48 $repeated[] =& $mform->createElement('text', 'answer', get_string('answer', 'quiz'), array('size' => 50));
49 $repeated[] =& $mform->createElement('select', 'fraction', get_string('grade'), $gradeoptions);
50 $repeated[] =& $mform->createElement('htmleditor', 'feedback', get_string('feedback', 'quiz'),
51 array('course' => $this->coursefilesid));
52 $repeatedoptions['answer']['type'] = PARAM_RAW;
53 $repeatedoptions['fraction']['default'] = 0;
54 $answersoption = 'answers';
55
2aef1fe5 56 $mform->setType('answer', PARAM_NOTAGS);
57
58 $addrepeated = array();
28a27ef1 59 $addrepeated[] =& $mform->createElement('text', 'tolerance', get_string('tolerance', 'qtype_calculated'));
60 $addrepeated[] =& $mform->createElement('select', 'tolerancetype', get_string('tolerancetype', 'quiz'), $this->qtypeobj->tolerance_types());
2aef1fe5 61 $repeatedoptions['tolerance']['type'] = PARAM_NUMBER;
62 $repeatedoptions['tolerance']['default'] = 0.01;
2aef1fe5 63
64 $addrepeated[] =& $mform->createElement('select', 'correctanswerlength', get_string('correctanswershows', 'qtype_calculated'), range(0, 9));
65 $repeatedoptions['correctanswerlength']['default'] = 2;
66
67 $answerlengthformats = array('1' => get_string('decimalformat', 'quiz'), '2' => get_string('significantfiguresformat', 'quiz'));
68 $addrepeated[] =& $mform->createElement('select', 'correctanswerformat', get_string('correctanswershowsformat', 'qtype_calculated'), $answerlengthformats);
69 array_splice($repeated, 3, 0, $addrepeated);
28a27ef1 70 $repeated[1]->setLabel(get_string('correctanswerformula', 'quiz').'=');
71
2aef1fe5 72 return $repeated;
73 }
74
271ffe3f 75 /**
76 * Add question-type specific form fields.
77 *
9aa022fe 78 * @param MoodleQuickForm $mform the form being built.
271ffe3f 79 */
80 function definition_inner(&$mform) {
9aa022fe 81 global $QTYPES;
92186abc 82 $this->qtypeobj =& $QTYPES[$this->qtype()];
aeb15530 83 // echo code left for testing period
99a77ee5 84 // echo "<p>question ".optional_param('multichoice', '', PARAM_RAW)." optional<pre>";print_r($this->question);echo "</pre></p>";
cf146692 85 $label = get_string('sharedwildcards', 'qtype_calculated');
a6d46515 86 $mform->addElement('hidden', 'initialcategory', 1);
d18e0fe6 87 $mform->setType('initialcategory', PARAM_INT);
a6d46515 88 $html2 = $this->qtypeobj->print_dataset_definitions_category($this->question);
89 $mform->insertElementBefore($mform->createElement('static','listcategory',$label,$html2),'name');
90 $addfieldsname='updatecategory';
91 $addstring=get_string("updatecategory", "qtype_calculated");
92 $mform->registerNoSubmitButton($addfieldsname);
271e6dec 93
a6d46515 94 $mform->insertElementBefore( $mform->createElement('submit', $addfieldsname, $addstring),'listcategory');
28a27ef1 95 $mform->registerNoSubmitButton('createoptionbutton');
aeb15530 96
ab8b5142 97 //editing as regular
d18e0fe6 98 $mform->setType('single', PARAM_INT);
aeb15530 99
28a27ef1 100 $mform->addElement('hidden','shuffleanswers', '1');
d18e0fe6 101 $mform->setType('shuffleanswers', PARAM_INT);
102 $mform->addElement('hidden','answernumbering', 'abc');
103 $mform->setType('answernumbering', PARAM_SAFEDIR);
271ffe3f 104
271ffe3f 105 $creategrades = get_grade_options();
ab8b5142 106
28a27ef1 107 $this->add_per_answer_fields($mform, get_string('answerhdr', 'qtype_calculated', '{no}'),
2aef1fe5 108 $creategrades->gradeoptions, 1, 1);
aeb15530 109
92186abc 110
271e6dec 111 $repeated = array();
271ffe3f 112
cf146692
PP
113 $QTYPES['numerical']->add_units_options($mform,$this);
114 $QTYPES['numerical']->add_units_elements($mform,$this);
ab8b5142
PP
115 foreach (array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback') as $feedbackname) {
116 $mform->addElement('hidden', $feedbackname);
117 $mform->setType($feedbackname, PARAM_RAW);
28a27ef1 118 }
9aa022fe 119 //hidden elements
e593233a 120 $mform->addElement('hidden', 'synchronize', '');
d18e0fe6 121 $mform->setType('synchronize', PARAM_INT);
60b5ecd3 122 $mform->addElement('hidden', 'wizard', 'datasetdefinitions');
123 $mform->setType('wizard', PARAM_ALPHA);
92186abc 124
271ffe3f 125
126 }
127
32db0d42 128 function set_data($question) {
81053284
PP
129 global $QTYPES;
130
cf146692 131 $default_values = array();
271ffe3f 132 if (isset($question->options)){
133 $answers = $question->options->answers;
134 if (count($answers)) {
135 $key = 0;
136 foreach ($answers as $answer){
9af77e9d 137 $default_values['answer['.$key.']'] = $answer->answer;
92186abc 138 $default_values['fraction['.$key.']'] = $answer->fraction;
139 $default_values['tolerance['.$key.']'] = $answer->tolerance;
f7089b63 140 $default_values['tolerancetype['.$key.']'] = $answer->tolerancetype;
92186abc 141 $default_values['correctanswerlength['.$key.']'] = $answer->correctanswerlength;
9aa022fe 142 $default_values['correctanswerformat['.$key.']'] = $answer->correctanswerformat;
271ffe3f 143 $default_values['feedback['.$key.']'] = $answer->feedback;
144 $key++;
145 }
146 }
cf146692 147 $default_values['synchronize'] = $question->options->synchronize ;
bbfda343 148 $QTYPES['numerical']->set_numerical_unit_data($question,$default_values);
81053284 149 }
28a27ef1 150 if (isset($question->options->single)){
cf146692
PP
151 $default_values['single'] = $question->options->single;
152 $default_values['answernumbering'] = $question->options->answernumbering;
153 $default_values['shuffleanswers'] = $question->options->shuffleanswers;
154 $default_values['correctfeedback'] = $question->options->correctfeedback;
155 $default_values['partiallycorrectfeedback'] = $question->options->partiallycorrectfeedback;
156 $default_values['incorrectfeedback'] = $question->options->incorrectfeedback;
157 }
8fc3e643 158 $default_values['submitbutton'] = get_string('nextpage', 'qtype_calculated');
159 $default_values['makecopy'] = get_string('makecopynextpage', 'qtype_calculated');
271e6dec 160 /* set the wild cards category display given that on loading the category element is
161 unselected when processing this function but have a valid value when processing the
0dd3e11c 162 update category button. The value can be obtain by
163 $qu->category =$this->_form->_elements[$this->_form->_elementIndex['category']]->_values[0];
164 but is coded using existing functions
271e6dec 165 */
0dd3e11c 166 $qu = new stdClass;
167 $el = new stdClass;
271e6dec 168 /* no need to call elementExists() here */
79bb7202 169 if ($this->_form->elementExists('category')){
170 $el=$this->_form->getElement('category');
171 } else {
172 $el=$this->_form->getElement('categorymoveto');
173 }
0dd3e11c 174 if($value =$el->getSelected()) {
175 $qu->category =$value[0];
176 }else {
177 $qu->category=$question->category;// on load $question->category is set by question.php
271e6dec 178 }
0dd3e11c 179 $html2 = $this->qtypeobj->print_dataset_definitions_category($qu);
79bb7202 180 $this->_form->_elements[$this->_form->_elementIndex['listcategory']]->_text = $html2 ;
181 $question = (object)((array)$question + $default_values);
92186abc 182
32db0d42 183 parent::set_data($question);
271ffe3f 184 }
185
186 function qtype() {
187 return 'calculated';
188 }
189
a78890d5 190 function validation($data, $files) {
cf146692 191 global $QTYPES;
aeb15530 192 // echo code left for testing period
28a27ef1 193
194 // echo "<p>question <pre>";print_r($this->question);echo "</pre></p>";
195 // echo "<p>data <pre>";print_r($data);echo "</pre></p>";
196
fe93ba83 197 $errors = parent::validation($data, $files);
bfdc0bce 198 //verifying for errors in {=...} in question text;
199 $qtext = "";
200 $qtextremaining = $data['questiontext'] ;
201 $possibledatasets = $this->qtypeobj->find_dataset_names($data['questiontext']);
202 foreach ($possibledatasets as $name => $value) {
203 $qtextremaining = str_replace('{'.$name.'}', '1', $qtextremaining);
204 }
271e6dec 205 // echo "numericalquestion qtextremaining <pre>";print_r($possibledatasets);
6dbcacee 206 while (preg_match('~\{=([^[:space:]}]*)}~', $qtextremaining, $regs1)) {
bfdc0bce 207 $qtextsplits = explode($regs1[0], $qtextremaining, 2);
208 $qtext =$qtext.$qtextsplits[0];
209 $qtextremaining = $qtextsplits[1];
210 if (!empty($regs1[1]) && $formulaerrors = qtype_calculated_find_formula_errors($regs1[1])) {
211 if(!isset($errors['questiontext'])){
212 $errors['questiontext'] = $formulaerrors.':'.$regs1[1] ;
213 }else {
214 $errors['questiontext'] .= '<br/>'.$formulaerrors.':'.$regs1[1];
271e6dec 215 }
bfdc0bce 216 }
271e6dec 217 }
9af77e9d 218 $answers = $data['answer'];
271ffe3f 219 $answercount = 0;
a6d46515 220 $maxgrade = false;
f6232d58 221 $possibledatasets = $this->qtypeobj->find_dataset_names($data['questiontext']);
222 $mandatorydatasets = array();
223 foreach ($answers as $key => $answer){
a6d46515 224 $mandatorydatasets += $this->qtypeobj->find_dataset_names($answer);
271e6dec 225 }
a6d46515 226 if ( count($mandatorydatasets )==0){
227 // $errors['questiontext']=get_string('atleastonewildcard', 'qtype_datasetdependent');
f6232d58 228 foreach ($answers as $key => $answer){
9af77e9d 229 $errors['answer['.$key.']'] = get_string('atleastonewildcard', 'qtype_datasetdependent');
271e6dec 230 }
231 }
ab8b5142 232 // regular calculated
99a77ee5 233 foreach ($answers as $key => $answer){
234 //check no of choices
235 // the * for everykind of answer not actually implemented
236 $trimmedanswer = trim($answer);
237 if (($trimmedanswer!='')||$answercount==0){
238 $eqerror = qtype_calculated_find_formula_errors($trimmedanswer);
239 if (FALSE !== $eqerror){
240 $errors['answer['.$key.']'] = $eqerror;
241 }
9aa022fe 242 }
99a77ee5 243 if ($trimmedanswer!=''){
244 if ('2' == $data['correctanswerformat'][$key]
245 && '0' == $data['correctanswerlength'][$key]) {
246 $errors['correctanswerlength['.$key.']'] = get_string('zerosignificantfiguresnotallowed','quiz');
247 }
248 if (!is_numeric($data['tolerance'][$key])){
249 $errors['tolerance['.$key.']'] = get_string('mustbenumeric', 'qtype_calculated');
250 }
251 if ($data['fraction'][$key] == 1) {
252 $maxgrade = true;
253 }
aeb15530 254
99a77ee5 255 $answercount++;
a6d46515 256 }
99a77ee5 257 //check grades
aeb15530 258
99a77ee5 259 //TODO how should grade checking work here??
260 /*if ($answer != '') {
261 if ($data['fraction'][$key] > 0) {
262 $totalfraction += $data['fraction'][$key];
263 }
264 if ($data['fraction'][$key] > $maxfraction) {
265 $maxfraction = $data['fraction'][$key];
266 }
267 }*/
271ffe3f 268 }
aeb15530 269
99a77ee5 270 //grade checking :
271 /// Perform sanity checks on fractional grades
272 /*if ( ) {
273 if ($maxfraction != 1) {
274 $maxfraction = $maxfraction * 100;
275 $errors['fraction[0]'] = get_string('errfractionsnomax', 'qtype_multichoice', $maxfraction);
9aa022fe 276 }
99a77ee5 277 } else {
278 $totalfraction = round($totalfraction,2);
279 if ($totalfraction != 1) {
280 $totalfraction = $totalfraction * 100;
281 $errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice', $totalfraction);
9aa022fe 282 }
cf146692 283 }
99a77ee5 284 $units = $data['unit'];
285 if (count($units)) {
286 foreach ($units as $key => $unit){
287 if (is_numeric($unit)){
288 $errors['unit['.$key.']'] = get_string('mustnotbenumeric', 'qtype_calculated');
9aa022fe 289 }
99a77ee5 290 $trimmedunit = trim($unit);
291 $trimmedmultiplier = trim($data['multiplier'][$key]);
292 if (!empty($trimmedunit)){
293 if (empty($trimmedmultiplier)){
294 $errors['multiplier['.$key.']'] = get_string('youmustenteramultiplierhere', 'qtype_calculated');
295 }
296 if (!is_numeric($trimmedmultiplier)){
297 $errors['multiplier['.$key.']'] = get_string('mustbenumeric', 'qtype_calculated');
298 }
aeb15530 299
9aa022fe 300 }
cf146692
PP
301 }
302 }*/
303 $QTYPES['numerical']->validate_numerical_options($data, $errors) ;
99a77ee5 304 if ($answercount==0){
305 $errors['answer[0]'] = get_string('atleastoneanswer', 'qtype_calculated');
306 }
307 if ($maxgrade == false) {
308 $errors['fraction[0]'] = get_string('fractionsnomax', 'question');
309 }
ab8b5142 310
9aa022fe 311
271ffe3f 312 return $errors;
313 }
314}
aeb15530 315