adding equation display in calculated question text(MDL-10070)
[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
9aa022fe 43 $repeated[] =& $mform->createElement('text', 'answers', get_string('correctanswerformula', 'quiz'));
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;
111 $default_values['correctanswerlength['.$key.']'] = $answer->correctanswerlength;
9aa022fe 112 $default_values['correctanswerformat['.$key.']'] = $answer->correctanswerformat;
271ffe3f 113 $default_values['feedback['.$key.']'] = $answer->feedback;
114 $key++;
115 }
116 }
9aa022fe 117 $units = array_values($question->options->units);
118 // make sure the default unit is at index 0
a6d46515 119 usort($units, create_function('$a, $b',
9aa022fe 120 'if (1.0 === (float)$a->multiplier) { return -1; } else '.
121 'if (1.0 === (float)$b->multiplier) { return 1; } else { return 0; }'));
122 if (count($units)) {
123 $key = 0;
124 foreach ($units as $unit){
125 $default_values['unit['.$key.']'] = $unit->unit;
126 $default_values['multiplier['.$key.']'] = $unit->multiplier;
127 $key++;
128 }
129 }
92186abc 130 }
8fc3e643 131 $default_values['submitbutton'] = get_string('nextpage', 'qtype_calculated');
132 $default_values['makecopy'] = get_string('makecopynextpage', 'qtype_calculated');
0dd3e11c 133 /* set the wild cards category display given that on loading the category element is
134 unselected when processing this function but have a valid value when processing the
135 update category button. The value can be obtain by
136 $qu->category =$this->_form->_elements[$this->_form->_elementIndex['category']]->_values[0];
137 but is coded using existing functions
138 */
139 $qu = new stdClass;
140 $el = new stdClass;
141 /* no need to call elementExists() here */
142 $el=$this->_form->getElement('category');
143 if($value =$el->getSelected()) {
144 $qu->category =$value[0];
145 }else {
146 $qu->category=$question->category;// on load $question->category is set by question.php
147 }
148 $html2 = $this->qtypeobj->print_dataset_definitions_category($qu);
149 $this->_form->_elements[$this->_form->_elementIndex['listcategory']]->_text = $html2 ;
150 $question = (object)((array)$question + $default_values);
92186abc 151
32db0d42 152 parent::set_data($question);
271ffe3f 153 }
154
155 function qtype() {
156 return 'calculated';
157 }
158
159 function validation($data){
160 $errors = array();
9aa022fe 161 $answers = $data['answers'];
271ffe3f 162 $answercount = 0;
a6d46515 163 $maxgrade = false;
f6232d58 164 $possibledatasets = $this->qtypeobj->find_dataset_names($data['questiontext']);
165 $mandatorydatasets = array();
166 foreach ($answers as $key => $answer){
a6d46515 167 $mandatorydatasets += $this->qtypeobj->find_dataset_names($answer);
f6232d58 168 }
a6d46515 169 if ( count($mandatorydatasets )==0){
170 // $errors['questiontext']=get_string('atleastonewildcard', 'qtype_datasetdependent');
f6232d58 171 foreach ($answers as $key => $answer){
172 $errors['answers['.$key.']'] = get_string('atleastonewildcard', 'qtype_datasetdependent');
173 }
174 }
9aa022fe 175 foreach ($answers as $key => $answer){
176 //check no of choices
a6d46515 177 // the * for everykind of answer not actually implemented
271ffe3f 178 $trimmedanswer = trim($answer);
845b703b 179 if (($trimmedanswer!='')||$answercount==0){
9aa022fe 180 $eqerror = qtype_calculated_find_formula_errors($trimmedanswer);
181 if (FALSE !== $eqerror){
182 $errors['answers['.$key.']'] = $eqerror;
183 }
8fc3e643 184 }
845b703b 185 if ($trimmedanswer!=''){
9aa022fe 186 if ('2' == $data['correctanswerformat'][$key]
187 && '0' == $data['correctanswerlength'][$key]) {
188 $errors['correctanswerlength['.$key.']'] = get_string('zerosignificantfiguresnotallowed','quiz');
189 }
190 if (!is_numeric($data['tolerance'][$key])){
191 $errors['tolerance['.$key.']'] = get_string('mustbenumeric', 'qtype_calculated');
192 }
a6d46515 193 if ($data['fraction'][$key] == 1) {
194 $maxgrade = true;
195 }
9aa022fe 196
271ffe3f 197 $answercount++;
198 }
9aa022fe 199 //check grades
200
201 //TODO how should grade checking work here??
202 /*if ($answer != '') {
203 if ($data['fraction'][$key] > 0) {
204 $totalfraction += $data['fraction'][$key];
205 }
206 if ($data['fraction'][$key] > $maxfraction) {
207 $maxfraction = $data['fraction'][$key];
208 }
209 }*/
210 }
211 //grade checking :
212 /// Perform sanity checks on fractional grades
213 /*if ( ) {
214 if ($maxfraction != 1) {
215 $maxfraction = $maxfraction * 100;
216 $errors['fraction[0]'] = get_string('errfractionsnomax', 'qtype_multichoice', $maxfraction);
217 }
218 } else {
219 $totalfraction = round($totalfraction,2);
220 if ($totalfraction != 1) {
221 $totalfraction = $totalfraction * 100;
222 $errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice', $totalfraction);
223 }
224 }*/
225 $units = $data['unit'];
226 if (count($units)) {
227 foreach ($units as $key => $unit){
228 if (is_numeric($unit)){
229 $errors['unit['.$key.']'] = get_string('mustnotbenumeric', 'qtype_calculated');
230 }
231 $trimmedunit = trim($unit);
232 $trimmedmultiplier = trim($data['multiplier'][$key]);
233 if (!empty($trimmedunit)){
234 if (empty($trimmedmultiplier)){
235 $errors['multiplier['.$key.']'] = get_string('youmustenteramultiplierhere', 'qtype_calculated');
236 }
237 if (!is_numeric($trimmedmultiplier)){
238 $errors['multiplier['.$key.']'] = get_string('mustbenumeric', 'qtype_calculated');
239 }
240
241 }
242 }
271ffe3f 243 }
244 if ($answercount==0){
9aa022fe 245 $errors['answers[0]'] = get_string('atleastoneanswer', 'qtype_calculated');
271ffe3f 246 }
a6d46515 247 if ($maxgrade == false) {
248 $errors['fraction[0]'] = get_string('fractionsnomax', 'question');
249 }
9aa022fe 250
271ffe3f 251 return $errors;
252 }
253}
254?>