mirrored from git://git.moodle.org/moodle.git
/
edit_calculated_form.php
283 lines (250 loc) · 13.1 KB
/
edit_calculated_form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
/**
* Defines the editing form for the calculated question type.
*
* @copyright © 2007 Jamie Pratt
* @author Jamie Pratt me@jamiep.org
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package questionbank
* @subpackage questiontypes
*/
/**
* calculated editing form definition.
*/
class question_edit_calculated_form extends question_edit_form {
/**
* Handle to the question type for this question.
*
* @var question_calculated_qtype
*/
var $qtypeobj;
/**
* Add question-type specific form fields.
*
* @param MoodleQuickForm $mform the form being built.
*/
function definition_inner(&$mform) {
global $QTYPES;
$this->qtypeobj =& $QTYPES[$this->qtype()];
//------------------------------------------------------------------------------------------
/* //not working now datasetdependent code cannot handle multiple answer formulas and not needed ??
$repeated = array();
$repeated[] =& $mform->createElement('header', 'answerhdr', get_string('answerhdr', 'qtype_calculated', '{no}'));
$repeated[] =& $mform->createElement('text', 'answers', get_string('correctanswerformula', 'quiz'));
$repeatedoptions['answers']['type'] = PARAM_NOTAGS;
$creategrades = get_grade_options();
$gradeoptions = $creategrades->gradeoptions;
$repeated[] =& $mform->createElement('select', 'fraction', get_string('grade'), $gradeoptions);
$repeatedoptions['fraction']['default'] = 0;
$repeated[] =& $mform->createElement('text', 'tolerance', get_string('tolerance', 'qtype_calculated'));
$repeatedoptions['tolerance']['type'] = PARAM_NUMBER;
$repeatedoptions['tolerance']['default'] = 0.01;
$repeated[] =& $mform->createElement('select', 'tolerancetype', get_string('tolerancetype', 'quiz'), $this->qtypeobj->tolerance_types());
$repeated[] =& $mform->createElement('select', 'correctanswerlength', get_string('correctanswershows', 'qtype_calculated'), range(0, 9));
$repeatedoptions['correctanswerlength']['default'] = 2;
$answerlengthformats = array('1' => get_string('decimalformat', 'quiz'), '2' => get_string('significantfiguresformat', 'quiz'));
$repeated[] =& $mform->createElement('select', 'correctanswerformat', get_string('correctanswershowsformat', 'qtype_calculated'), $answerlengthformats);
$repeated[] =& $mform->createElement('htmleditor', 'feedback', get_string('feedback', 'quiz'));
$repeatedoptions['feedback']['type'] = PARAM_RAW;
if (isset($this->question->options)){
$count = count($this->question->options->answers);
} else {
$count = 0;
}
$repeatsatstart = $count + 1;
$this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', 1, get_string('addmoreanswerblanks', 'qtype_calculated'));*/
//------------------------------------------------------------------------------------------
$label = get_string("sharedwildcards", "qtype_datasetdependent");
$mform->addElement('hidden', 'initialcategory', 1);
$html2 = $this->qtypeobj->print_dataset_definitions_category($this->question);
$mform->insertElementBefore($mform->createElement('static','listcategory',$label,$html2),'name');
$addfieldsname='updatecategory';
$addstring=get_string("updatecategory", "qtype_calculated");
$mform->registerNoSubmitButton($addfieldsname);
$mform->insertElementBefore( $mform->createElement('submit', $addfieldsname, $addstring),'listcategory');
$mform->addElement('header', 'answerhdr', get_string('answerhdr', 'qtype_calculated'));
$mform->addElement('text', 'answers[0]', get_string('correctanswerformula', 'quiz'));
$mform->setType('answers[0]', PARAM_NOTAGS);
/* $creategrades = get_grade_options();
$gradeoptions = $creategrades->gradeoptions;
$mform->addElement('select', 'fraction[0]', get_string('grade'), $gradeoptions);
$mform->setDefault('fraction[0]', 0);*/
$mform->addElement('hidden', 'fraction[0]', 1);
// $mform->setConstants(array('fraction[0]'=>PARAM_INT));
$tolgrp = array();
$tolgrp[] =& $mform->createElement('text', 'tolerance[0]', get_string('tolerance', 'qtype_calculated'));
$mform->setType('tolerance[0]', PARAM_NUMBER);
$mform->setDefault('tolerance[0]', 0.01);
$tolgrp[] =& $mform->createElement('select', 'tolerancetype[0]', get_string('tolerancetype', 'quiz'), $this->qtypeobj->tolerance_types());
$mform->addGroup($tolgrp, 'tolgrp', get_string('tolerance', 'qtype_calculated'), null, false);
$anslengrp = array();
$anslengrp[] =& $mform->createElement('select', 'correctanswerlength[0]', get_string('correctanswershows', 'qtype_calculated'), range(0, 9));
$mform->setDefault('correctanswerlength[0]', 2);
$answerlengthformats = array('1' => get_string('decimalformat', 'quiz'), '2' => get_string('significantfiguresformat', 'quiz'));
$anslengrp[] =& $mform->createElement('select', 'correctanswerformat[0]', get_string('correctanswershowsformat', 'qtype_calculated'), $answerlengthformats);
$mform->addGroup($anslengrp, 'anslengrp', get_string('correctanswershows', 'qtype_calculated'), null, false);
$mform->addElement('htmleditor', 'feedback[0]', get_string('feedback', 'quiz'));
$mform->setType('feedback', PARAM_RAW);
//------------------------------------------------------------------------------------------
$repeated = array();
$repeated[] =& $mform->createElement('header', 'unithdr', get_string('unithdr', 'qtype_numerical', '{no}'));
$repeated[] =& $mform->createElement('text', 'unit', get_string('unit', 'quiz'));
$mform->setType('unit', PARAM_NOTAGS);
$repeated[] =& $mform->createElement('text', 'multiplier', get_string('multiplier', 'quiz'));
$mform->setType('multiplier', PARAM_NUMBER);
if (isset($this->question->options)){
$countunits = count($this->question->options->units);
} else {
$countunits = 0;
}
$repeatsatstart = $countunits + 1;
$this->repeat_elements($repeated, $repeatsatstart, array(), 'nounits', 'addunits', 2, get_string('addmoreunitblanks', 'qtype_calculated', '{no}'));
$firstunit =& $mform->getElement('multiplier[0]');
$firstunit->freeze();
$firstunit->setValue('1.0');
$firstunit->setPersistantFreeze(true);
//hidden elements
$mform->addElement('hidden', 'wizard', 'datasetdefinitions');
$mform->setType('wizard', PARAM_ALPHA);
}
function set_data($question) {
if (isset($question->options)){
$answers = $question->options->answers;
if (count($answers)) {
$key = 0;
foreach ($answers as $answer){
$default_values['answers['.$key.']'] = $answer->answer;
$default_values['fraction['.$key.']'] = $answer->fraction;
$default_values['tolerance['.$key.']'] = $answer->tolerance;
$default_values['correctanswerlength['.$key.']'] = $answer->correctanswerlength;
$default_values['correctanswerformat['.$key.']'] = $answer->correctanswerformat;
$default_values['feedback['.$key.']'] = $answer->feedback;
$key++;
}
}
$units = array_values($question->options->units);
// make sure the default unit is at index 0
usort($units, create_function('$a, $b', // make sure the default unit is at index 0
'if (1.0 === (float)$a->multiplier) { return -1; } else '.
'if (1.0 === (float)$b->multiplier) { return 1; } else { return 0; }'));
if (count($units)) {
$key = 0;
foreach ($units as $unit){
$default_values['unit['.$key.']'] = $unit->unit;
$default_values['multiplier['.$key.']'] = $unit->multiplier;
$key++;
}
}
}
$default_values['submitbutton'] = get_string('nextpage', 'qtype_calculated');
$default_values['makecopy'] = get_string('makecopynextpage', 'qtype_calculated');
/* set the wild cards category display given that on loading the category element is
unselected when processing this function but have a valid value when processing the
update category button. The value can be obtain by
$qu->category =$this->_form->_elements[$this->_form->_elementIndex['category']]->_values[0];
but is coded using existing functions
*/
$qu = new stdClass;
$el = new stdClass;
/* no need to call elementExists() here */
$el=$this->_form->getElement('category');
if($value =$el->getSelected()) {
$qu->category =$value[0];
}else {
$qu->category=$question->category;// on load $question->category is set by question.php
}
$html2 = $this->qtypeobj->print_dataset_definitions_category($qu);
$this->_form->_elements[$this->_form->_elementIndex['listcategory']]->_text = $html2 ;
$question = (object)((array)$question + $default_values);
parent::set_data($question);
}
function qtype() {
return 'calculated';
}
function validation($data){
$errors = array();
$answers = $data['answers'];
$answercount = 0;
//check grades
/*$totalfraction = 0;
$maxfraction = -1; */
$possibledatasets = $this->qtypeobj->find_dataset_names($data['questiontext']);
$mandatorydatasets = array();
foreach ($answers as $key => $answer){
$mandatorydatasets += $this->qtypeobj->find_dataset_names($data['questiontext']);
}
if (count($possibledatasets) == 0 && count($mandatorydatasets )==0){
$errors['questiontext']=get_string('atleastonewildcard', 'qtype_datasetdependent');
foreach ($answers as $key => $answer){
$errors['answers['.$key.']'] = get_string('atleastonewildcard', 'qtype_datasetdependent');
}
}
foreach ($answers as $key => $answer){
//check no of choices
$trimmedanswer = trim($answer);
if (($trimmedanswer!='')||$answercount==0){
$eqerror = qtype_calculated_find_formula_errors($trimmedanswer);
if (FALSE !== $eqerror){
$errors['answers['.$key.']'] = $eqerror;
}
}
if ($trimmedanswer!=''){
if ('2' == $data['correctanswerformat'][$key]
&& '0' == $data['correctanswerlength'][$key]) {
$errors['correctanswerlength['.$key.']'] = get_string('zerosignificantfiguresnotallowed','quiz');
}
if (!is_numeric($data['tolerance'][$key])){
$errors['tolerance['.$key.']'] = get_string('mustbenumeric', 'qtype_calculated');
}
$answercount++;
}
//check grades
//TODO how should grade checking work here??
/*if ($answer != '') {
if ($data['fraction'][$key] > 0) {
$totalfraction += $data['fraction'][$key];
}
if ($data['fraction'][$key] > $maxfraction) {
$maxfraction = $data['fraction'][$key];
}
}*/
}
//grade checking :
/// Perform sanity checks on fractional grades
/*if ( ) {
if ($maxfraction != 1) {
$maxfraction = $maxfraction * 100;
$errors['fraction[0]'] = get_string('errfractionsnomax', 'qtype_multichoice', $maxfraction);
}
} else {
$totalfraction = round($totalfraction,2);
if ($totalfraction != 1) {
$totalfraction = $totalfraction * 100;
$errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice', $totalfraction);
}
}*/
$units = $data['unit'];
if (count($units)) {
foreach ($units as $key => $unit){
if (is_numeric($unit)){
$errors['unit['.$key.']'] = get_string('mustnotbenumeric', 'qtype_calculated');
}
$trimmedunit = trim($unit);
$trimmedmultiplier = trim($data['multiplier'][$key]);
if (!empty($trimmedunit)){
if (empty($trimmedmultiplier)){
$errors['multiplier['.$key.']'] = get_string('youmustenteramultiplierhere', 'qtype_calculated');
}
if (!is_numeric($trimmedmultiplier)){
$errors['multiplier['.$key.']'] = get_string('mustbenumeric', 'qtype_calculated');
}
}
}
}
if ($answercount==0){
$errors['answers[0]'] = get_string('atleastoneanswer', 'qtype_calculated');
}
return $errors;
}
}
?>