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