workshop: create empty legacy tables to keep DB consistency with install.xml
[moodle.git] / question / type / calculatedmulti / questiontype.php
CommitLineData
2d279432
PP
1<?php
2
fe6ce234
DC
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
2d279432
PP
18/////////////////
19// CALCULATED ///
20/////////////////
21
22/// QUESTION TYPE CLASS //////////////////
23
2d279432
PP
24class question_calculatedmulti_qtype extends question_calculated_qtype {
25
26 // Used by the function custom_generator_tools:
fe6ce234 27 public $calcgenerateidhasbeenadded = false;
2d279432
PP
28 public $virtualqtype = false;
29
30 function name() {
31 return 'calculatedmulti';
32 }
33
34 function has_wildcards_in_responses($question, $subqid) {
35 return true;
36 }
37
38 function requires_qtypes() {
39 return array('multichoice');
40 }
2d279432 41
2d279432
PP
42
43 function save_question_options($question) {
2d279432 44 global $CFG, $DB, $QTYPES ;
fe6ce234 45 $context = $question->context;
2d279432
PP
46 if (isset($question->answer) && !isset($question->answers)) {
47 $question->answers = $question->answer;
48 }
49 // calculated options
fe6ce234 50 $update = true ;
2d279432
PP
51 $options = $DB->get_record("question_calculated_options", array("question" => $question->id));
52 if (!$options) {
53 $update = false;
54 $options = new stdClass;
55 $options->question = $question->id;
56 }
57 $options->synchronize = $question->synchronize;
2d279432
PP
58 $options->single = $question->single;
59 $options->answernumbering = $question->answernumbering;
60 $options->shuffleanswers = $question->shuffleanswers;
fe6ce234
DC
61
62 // save question feedback files
63 foreach (array('correct', 'partiallycorrect', 'incorrect') as $feedbacktype) {
64 $feedbackname = $feedbacktype . 'feedback';
65 $feedbackformat = $feedbackname . 'format';
66 $feedback = $question->$feedbackname;
67 $options->$feedbackformat = $feedback['format'];
cde2709a
DC
68 if (isset($feedback['files'])) {
69 $options->$feedbackname = trim($feedback['text']);
70 $files = $feedback['files'];
71 foreach ($files as $file) {
72 $this->import_file($question->context, 'qtype_calculatedmulti', $feedbackname, $question->id, $file);
73 }
74 } else {
75 $options->$feedbackname = file_save_draft_area_files($feedback['itemid'], $context->id, 'qtype_calculatedmulti', $feedbackname, $question->id, self::$fileoptions, trim($feedback['text']));
76 }
fe6ce234
DC
77 }
78
2d279432 79 if ($update) {
fe6ce234 80 $DB->update_record("question_calculated_options", $options);
2d279432 81 } else {
fe6ce234 82 $DB->insert_record("question_calculated_options", $options);
2d279432
PP
83 }
84
85 // Get old versions of the objects
86 if (!$oldanswers = $DB->get_records('question_answers', array('question' => $question->id), 'id ASC')) {
87 $oldanswers = array();
88 }
89
90 if (!$oldoptions = $DB->get_records('question_calculated', array('question' => $question->id), 'answer ASC')) {
91 $oldoptions = array();
92 }
93
94 // Save the units.
fe6ce234
DC
95 $virtualqtype = $this->get_virtual_qtype($question);
96 // TODO: What is this?
97 // $result = $virtualqtype->save_numerical_units($question);
2d279432
PP
98 if (isset($result->error)) {
99 return $result;
100 } else {
101 $units = &$result->units;
102 }
103 // Insert all the new answers
104 if (isset($question->answer) && !isset($question->answers)) {
fe6ce234 105 $question->answers = $question->answer;
2d279432
PP
106 }
107 foreach ($question->answers as $key => $dataanswer) {
cde2709a
DC
108 if (is_array($dataanswer)) {
109 $dataanswer = $dataanswer['text'];
110 }
fe6ce234 111 if ( trim($dataanswer) != '' ) {
2d279432
PP
112 $answer = new stdClass;
113 $answer->question = $question->id;
114 $answer->answer = trim($dataanswer);
115 $answer->fraction = $question->fraction[$key];
fe6ce234
DC
116 $answer->feedback = trim($question->feedback[$key]['text']);
117 $answer->feedbackformat = $question->feedback[$key]['format'];
cde2709a
DC
118 if (isset($question->feedback[$key]['files'])) {
119 $files = $question->feedback[$key]['files'];
120 }
2d279432
PP
121
122 if ($oldanswer = array_shift($oldanswers)) { // Existing answer, so reuse it
123 $answer->id = $oldanswer->id;
fe6ce234 124 $answer->feedback = file_save_draft_area_files($question->feedback[$key]['itemid'], $context->id, 'question', 'answerfeedback', $answer->id, self::$fileoptions, $answer->feedback);
2d279432
PP
125 $DB->update_record("question_answers", $answer);
126 } else { // This is a completely new answer
127 $answer->id = $DB->insert_record("question_answers", $answer);
cde2709a
DC
128 if (isset($files)) {
129 $feedbacktext = $answer->feedback;
130 foreach ($files as $file) {
131 $this->import_file($context, 'question', 'answerfeedback', $answer->id, $file);
132 }
133 } else {
134 $feedbacktext = file_save_draft_area_files($question->feedback[$key]['itemid'], $context->id, 'question', 'answerfeedback', $answer->id, self::$fileoptions, $answer->feedback);
135 }
fe6ce234 136 $DB->set_field('question_answers', 'feedback', $feedbacktext, array('id'=>$answer->id));
2d279432
PP
137 }
138
139 // Set up the options object
140 if (!$options = array_shift($oldoptions)) {
141 $options = new stdClass;
142 }
143 $options->question = $question->id;
144 $options->answer = $answer->id;
145 $options->tolerance = trim($question->tolerance[$key]);
146 $options->tolerancetype = trim($question->tolerancetype[$key]);
147 $options->correctanswerlength = trim($question->correctanswerlength[$key]);
148 $options->correctanswerformat = trim($question->correctanswerformat[$key]);
149
150 // Save options
151 if (isset($options->id)) { // reusing existing record
152 $DB->update_record('question_calculated', $options);
153 } else { // new options
154 $DB->insert_record('question_calculated', $options);
155 }
156 }
157 }
158 // delete old answer records
159 if (!empty($oldanswers)) {
160 foreach($oldanswers as $oa) {
161 $DB->delete_records('question_answers', array('id' => $oa->id));
162 }
163 }
164
165 // delete old answer records
166 if (!empty($oldoptions)) {
167 foreach($oldoptions as $oo) {
168 $DB->delete_records('question_calculated', array('id' => $oo->id));
169 }
170 }
2d279432
PP
171
172
173 if( isset($question->import_process)&&$question->import_process){
174 $this->import_datasets($question);
fe6ce234 175 }
2d279432
PP
176 // Report any problems.
177 if (!empty($result->notice)) {
178 return $result;
179 }
180 return true;
181 }
182
183 function create_session_and_responses(&$question, &$state, $cmoptions, $attempt) {
184 // Find out how many datasets are available
d90b016b 185 global $CFG, $DB, $QTYPES, $OUTPUT ;
fe6ce234
DC
186 $maxnumber = (int)$DB->get_field_sql(
187 "SELECT MIN(a.itemcount)
188 FROM {question_dataset_definitions} a, {question_datasets} b
189 WHERE b.question = ? AND a.id = b.datasetdefinition", array($question->id));
190 if (!$maxnumber) {
2d279432
PP
191 print_error('cannotgetdsforquestion', 'question', '', $question->id);
192 }
fe6ce234
DC
193 $sql = "SELECT i.*
194 FROM {question_datasets} d, {question_dataset_definitions} i
195 WHERE d.question = ? AND d.datasetdefinition = i.id AND i.category != 0";
2d279432 196 if (!$question->options->synchronize || !$records = $DB->get_records_sql($sql, array($question->id))) {
fe6ce234
DC
197 $synchronize_calculated = false ;
198 } else {
199 // i.e records is true so test coherence
200 $coherence = true ;
201 $a = new stdClass ;
202 $a->qid = $question->id ;
203 $a->qcat = $question->category ;
204 foreach($records as $def ){
d90b016b
PP
205 if ($def->category != $question->category){
206 $a->name = $def->name;
207 $a->sharedcat = $def->category ;
208 $coherence = false ;
209 break;
210 }
211 }
212 if(!$coherence){
fe6ce234
DC
213 echo $OUTPUT->notification(get_string('nocoherencequestionsdatyasetcategory','qtype_calculated',$a));
214 }
215
216 $synchronize_calculated = true ;
217 }
2d279432
PP
218
219 // Choose a random dataset
d90b016b
PP
220 // maxnumber sould not be breater than 100
221 if ($maxnumber > CALCULATEDQUESTIONMAXITEMNUMBER ){
222 $maxnumber = CALCULATEDQUESTIONMAXITEMNUMBER ;
223 }
2d279432
PP
224 if ( $synchronize_calculated === false ) {
225 $state->options->datasetitem = rand(1, $maxnumber);
226 }else{
fe6ce234 227 $state->options->datasetitem = intval( $maxnumber * substr($attempt->timestart,-2) /100 ) ;
2d279432
PP
228 if ($state->options->datasetitem < 1) {
229 $state->options->datasetitem =1 ;
230 } else if ($state->options->datasetitem > $maxnumber){
231 $state->options->datasetitem = $maxnumber ;
232 }
fe6ce234
DC
233
234 };
2d279432 235 $state->options->dataset =
fe6ce234
DC
236 $this->pick_question_dataset($question,$state->options->datasetitem);
237 // create an array of answerids ??? why so complicated ???
238 $answerids = array_values(array_map(create_function('$val',
239 'return $val->id;'), $question->options->answers));
240 // Shuffle the answers if required
241 if (!empty($cmoptions->shuffleanswers) and !empty($question->options->shuffleanswers)) {
242 $answerids = swapshuffle($answerids);
243 }
244 $state->options->order = $answerids;
245 // Create empty responses
246 if ($question->options->single) {
247 $state->responses = array('' => '');
248 } else {
249 $state->responses = array();
250 }
251 return true;
2d279432 252 }
fe6ce234 253
2d279432
PP
254 function save_session_and_responses(&$question, &$state) {
255 global $DB;
fe6ce234 256 $responses = 'dataset'.$state->options->datasetitem.'-' ;
2d279432
PP
257 $responses .= implode(',', $state->options->order) . ':';
258 $responses .= implode(',', $state->responses);
fe6ce234
DC
259
260 // Set the legacy answer field
f685e830 261 $DB->set_field('question_states', 'answer', $responses, array('id'=> $state->id));
2d279432
PP
262 return true;
263 }
264
265 function create_runtime_question($question, $form) {
78a6d0ba 266 $question = default_questiontype::create_runtime_question($question, $form);
2d279432
PP
267 $question->options->answers = array();
268 foreach ($form->answers as $key => $answer) {
269 $a->answer = trim($form->answer[$key]);
270 $a->fraction = $form->fraction[$key];//new
fe6ce234 271 $a->tolerance = $form->tolerance[$key];
2d279432
PP
272 $a->tolerancetype = $form->tolerancetype[$key];
273 $a->correctanswerlength = $form->correctanswerlength[$key];
274 $a->correctanswerformat = $form->correctanswerformat[$key];
275 $question->options->answers[] = clone($a);
276 }
277
278 return $question;
279 }
280
2d279432 281 function convert_answers (&$question, &$state){
fe6ce234
DC
282 foreach ($question->options->answers as $key => $answer) {
283 $answer->answer = $this->substitute_variables($answer->answer, $state->options->dataset);
284 //evaluate the equations i.e {=5+4)
285 $qtext = "";
286 $qtextremaining = $answer->answer ;
287 // while (preg_match('~\{(=)|%[[:digit]]\.=([^[:space:]}]*)}~', $qtextremaining, $regs1)) {
288 while (preg_match('~\{=([^[:space:]}]*)}~', $qtextremaining, $regs1)) {
289
290 $qtextsplits = explode($regs1[0], $qtextremaining, 2);
291 $qtext = $qtext.$qtextsplits[0];
292 $qtextremaining = $qtextsplits[1];
293 if (empty($regs1[1])) {
294 $str = '';
295 } else {
296 if( $formulaerrors = qtype_calculated_find_formula_errors($regs1[1])){
297 $str=$formulaerrors ;
298 }else {
299 eval('$str = '.$regs1[1].';');
300 $texteval= qtype_calculated_calculate_answer(
301 $str, $state->options->dataset, $answer->tolerance,
302 $answer->tolerancetype, $answer->correctanswerlength,
303 $answer->correctanswerformat, '');
2d279432 304 $str = $texteval->answer;
fe6ce234 305 }
2d279432 306 }
fe6ce234 307 $qtext = $qtext.$str ;
2d279432 308 }
fe6ce234 309 $answer->answer = $qtext.$qtextremaining ; ;
2d279432 310 }
fe6ce234 311 }
2d279432 312
fe6ce234
DC
313 function get_default_numerical_unit($question, $virtualqtype){
314 $unit = '';
315 return $unit ;
316 }
2d279432
PP
317 function grade_responses(&$question, &$state, $cmoptions) {
318 // Forward the grading to the virtual qtype
319 // We modify the question to look like a multichoice question
fe6ce234 320 // for grading nothing to do
2d279432
PP
321/* $numericalquestion = fullclone($question);
322 foreach ($numericalquestion->options->answers as $key => $answer) {
323 $answer = $numericalquestion->options->answers[$key]->answer; // for PHP 4.x
324 $numericalquestion->options->answers[$key]->answer = $this->substitute_variables_and_eval($answer,
325 $state->options->dataset);
fe6ce234
DC
326}*/
327 $virtualqtype = $this->get_virtual_qtype( $question);
2d279432
PP
328 return $virtualqtype->grade_responses($question, $state, $cmoptions) ;
329 }
330
2d279432 331
2d279432
PP
332
333 // ULPGC ecastro
334 function get_actual_response(&$question, &$state) {
335 // Substitute variables in questiontext before giving the data to the
336 // virtual type
337 $virtualqtype = $this->get_virtual_qtype( $question);
338 $unit = '' ;//$virtualqtype->get_default_numerical_unit($question);
339
340 // We modify the question to look like a multichoice question
341 $numericalquestion = clone($question);
342 $this->convert_answers ($numericalquestion, $state);
343 $this->convert_questiontext ($numericalquestion, $state);
344 /* $numericalquestion->questiontext = $this->substitute_variables_and_eval(
fe6ce234 345 $numericalquestion->questiontext, $state->options->dataset);*/
2d279432
PP
346 $responses = $virtualqtype->get_all_responses($numericalquestion, $state);
347 $response = reset($responses->responses);
348 $correct = $response->answer.' : ';
349
350 $responses = $virtualqtype->get_actual_response($numericalquestion, $state);
351
352 foreach ($responses as $key=>$response){
353 $responses[$key] = $correct.$response;
354 }
355
356 return $responses;
357 }
358
359 function create_virtual_qtype() {
360 global $CFG;
fe6ce234
DC
361 require_once("$CFG->dirroot/question/type/multichoice/questiontype.php");
362 return new question_multichoice_qtype();
2d279432
PP
363 }
364
365
366 function comment_header($question) {
367 //$this->get_question_options($question);
368 $strheader = '';
369 $delimiter = '';
370
371 $answers = $question->options->answers;
372
373 foreach ($answers as $key => $answer) {
374 if (is_string($answer)) {
375 $strheader .= $delimiter.$answer;
376 } else {
377 $strheader .= $delimiter.$answer->answer;
378 }
fe6ce234 379 $delimiter = '<br/>';
2d279432
PP
380 }
381 return $strheader;
382 }
383
384 function comment_on_datasetitems($qtypeobj,$questionid,$questiontext, $answers,$data, $number) { //multichoice_
385 global $DB;
386 $comment = new stdClass;
387 $comment->stranswers = array();
388 $comment->outsidelimit = false ;
389 $comment->answers = array();
390 /// Find a default unit:
391 /* if (!empty($questionid) && $unit = $DB->get_record('question_numerical_units', array('question'=> $questionid, 'multiplier' => 1.0))) {
392 $unit = $unit->unit;
393 } else {
394 $unit = '';
fe6ce234 395 }*/
2d279432
PP
396
397 $answers = fullclone($answers);
398 $strmin = get_string('min', 'quiz');
399 $strmax = get_string('max', 'quiz');
400 $errors = '';
401 $delimiter = ': ';
402 foreach ($answers as $key => $answer) {
fe6ce234
DC
403 $answer->answer = $this->substitute_variables($answer->answer, $data);
404 //evaluate the equations i.e {=5+4)
405 $qtext = "";
406 $qtextremaining = $answer->answer ;
407 while (preg_match('~\{=([^[:space:]}]*)}~', $qtextremaining, $regs1)) {
408 $qtextsplits = explode($regs1[0], $qtextremaining, 2);
409 $qtext =$qtext.$qtextsplits[0];
410 $qtextremaining = $qtextsplits[1];
411 if (empty($regs1[1])) {
412 $str = '';
413 } else {
414 if( $formulaerrors = qtype_calculated_find_formula_errors($regs1[1])){
415 $str=$formulaerrors ;
416 }else {
417 eval('$str = '.$regs1[1].';');
418 }
2d279432 419 }
fe6ce234
DC
420 $qtext = $qtext.$str ;
421 }
422 $answer->answer = $qtext.$qtextremaining;
423 $comment->stranswers[$key] = $answer->answer;
424
425
2d279432
PP
426 /* $formula = $this->substitute_variables($answer->answer,$data);
427 $formattedanswer = qtype_calculated_calculate_answer(
428 $answer->answer, $data, $answer->tolerance,
429 $answer->tolerancetype, $answer->correctanswerlength,
430 $answer->correctanswerformat, $unit);
431 if ( $formula === '*'){
432 $answer->min = ' ';
433 $formattedanswer->answer = $answer->answer ;
434 }else {
435 eval('$answer->answer = '.$formula.';') ;
436 $virtualqtype->get_tolerance_interval($answer);
fe6ce234 437 }
2d279432
PP
438 if ($answer->min === '') {
439 // This should mean that something is wrong
440 $comment->stranswers[$key] = " $formattedanswer->answer".'<br/><br/>';
441 } else if ($formula === '*'){
442 $comment->stranswers[$key] = $formula.' = '.get_string('anyvalue','qtype_calculated').'<br/><br/><br/>';
443 }else{
444 $comment->stranswers[$key]= $formula.' = '.$formattedanswer->answer.'<br/>' ;
445 $comment->stranswers[$key] .= $strmin. $delimiter.$answer->min.'---';
446 $comment->stranswers[$key] .= $strmax.$delimiter.$answer->max;
447 $comment->stranswers[$key] .='<br/>';
448 $correcttrue->correct = $formattedanswer->answer ;
449 $correcttrue->true = $answer->answer ;
450 if ($formattedanswer->answer < $answer->min || $formattedanswer->answer > $answer->max){
451 $comment->outsidelimit = true ;
452 $comment->answers[$key] = $key;
453 $comment->stranswers[$key] .=get_string('trueansweroutsidelimits','qtype_calculated',$correcttrue);//<span class="error">ERROR True answer '..' outside limits</span>';
454 }else {
455 $comment->stranswers[$key] .=get_string('trueanswerinsidelimits','qtype_calculated',$correcttrue);//' True answer :'.$calculated->trueanswer.' inside limits';
456 }
457 $comment->stranswers[$key] .='';
fe6ce234 458 }*/
2d279432
PP
459 }
460 return fullclone($comment);
461 }
462
2d279432
PP
463 function get_correct_responses1(&$question, &$state) {
464 $virtualqtype = $this->get_virtual_qtype( $question);
465 /* if ($question->options->multichoice != 1 ) {
466 if($unit = $virtualqtype->get_default_numerical_unit($question)){
467 $unit = $unit->unit;
468 } else {
469 $unit = '';
470 }
471 foreach ($question->options->answers as $answer) {
472 if (((int) $answer->fraction) === 1) {
473 $answernumerical = qtype_calculated_calculate_answer(
474 $answer->answer, $state->options->dataset, $answer->tolerance,
475 $answer->tolerancetype, $answer->correctanswerlength,
476 $answer->correctanswerformat, ''); // remove unit
477 $correct = array('' => $answernumerical->answer);
478 $correct['answer']= $correct[''];
479 if (isset($correct['']) && $correct[''] != '*' && $unit ) {
480 $correct[''] .= ' '.$unit;
481 $correct['unit']= $unit;
482 }
483 return $correct;
484 }
485 }
fe6ce234
DC
486 }else{**/
487 return $virtualqtype->get_correct_responses($question, $state) ;
488 // }
2d279432
PP
489 return null;
490 }
491
2d279432
PP
492 function get_virtual_qtype() {
493 global $QTYPES;
fe6ce234
DC
494 // if ( isset($question->options->multichoice) && $question->options->multichoice == '1'){
495 $this->virtualqtype =& $QTYPES['multichoice'];
496 // }else {
497 // $this->virtualqtype =& $QTYPES['numerical'];
498 // }
2d279432
PP
499 return $this->virtualqtype;
500 }
501
502
fe6ce234
DC
503 /**
504 * Runs all the code required to set up and save an essay question for testing purposes.
505 * Alternate DB table prefix may be used to facilitate data deletion.
506 */
507 function generate_test($name, $courseid = null) {
508 global $DB;
509 list($form, $question) = parent::generate_test($name, $courseid);
510 $form->feedback = 1;
511 $form->multiplier = array(1, 1);
512 $form->shuffleanswers = 1;
513 $form->noanswers = 1;
514 $form->qtype ='calculatedmulti';
515 $question->qtype ='calculatedmulti';
516 $form->answers = array('{a} + {b}');
517 $form->fraction = array(1);
518 $form->tolerance = array(0.01);
519 $form->tolerancetype = array(1);
520 $form->correctanswerlength = array(2);
521 $form->correctanswerformat = array(1);
522 $form->questiontext = "What is {a} + {b}?";
523
524 if ($courseid) {
525 $course = $DB->get_record('course', array('id'=> $courseid));
526 }
527
528 $new_question = $this->save_question($question, $form, $course);
529
530 $dataset_form = new stdClass();
531 $dataset_form->nextpageparam["forceregeneration"]= 1;
532 $dataset_form->calcmin = array(1 => 1.0, 2 => 1.0);
533 $dataset_form->calcmax = array(1 => 10.0, 2 => 10.0);
534 $dataset_form->calclength = array(1 => 1, 2 => 1);
535 $dataset_form->number = array(1 => 5.4 , 2 => 4.9);
536 $dataset_form->itemid = array(1 => '' , 2 => '');
537 $dataset_form->calcdistribution = array(1 => 'uniform', 2 => 'uniform');
538 $dataset_form->definition = array(1 => "1-0-a",
539 2 => "1-0-b");
540 $dataset_form->nextpageparam = array('forceregeneration' => false);
541 $dataset_form->addbutton = 1;
542 $dataset_form->selectadd = 1;
543 $dataset_form->courseid = $courseid;
544 $dataset_form->cmid = 0;
545 $dataset_form->id = $new_question->id;
546 $this->save_dataset_items($new_question, $dataset_form);
547
548 return $new_question;
549 }
550
5d548d3e 551 function move_files($questionid, $oldcontextid, $newcontextid) {
fe6ce234 552 $fs = get_file_storage();
fe6ce234 553
5d548d3e
TH
554 parent::move_files($questionid, $oldcontextid, $newcontextid);
555 $this->move_files_in_answers($questionid, $oldcontextid, $newcontextid, true);
556
557 $fs->move_area_files_to_new_context($oldcontextid,
558 $newcontextid, 'qtype_calculatedmulti', 'correctfeedback', $questionid);
559 $fs->move_area_files_to_new_context($oldcontextid,
560 $newcontextid, 'qtype_calculatedmulti', 'partiallycorrectfeedback', $questionid);
561 $fs->move_area_files_to_new_context($oldcontextid,
562 $newcontextid, 'qtype_calculatedmulti', 'incorrectfeedback', $questionid);
fe6ce234
DC
563 }
564
565 function check_file_access($question, $state, $options, $contextid, $component,
566 $filearea, $args) {
567 $itemid = reset($args);
568
569 if (empty($question->maxgrade)) {
570 $question->maxgrade = $question->defaultgrade;
571 }
572
573 if (in_array($filearea, array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback'))) {
574 $result = $options->feedback && ($itemid == $question->id);
575 if (!$result) {
576 return false;
577 }
578 if ($state->raw_grade >= $question->maxgrade/1.01) {
579 $feedbacktype = 'correctfeedback';
580 } else if ($state->raw_grade > 0) {
581 $feedbacktype = 'partiallycorrectfeedback';
582 } else {
583 $feedbacktype = 'incorrectfeedback';
584 }
585 if ($feedbacktype != $filearea) {
586 return false;
587 }
588 return true;
589 } else if ($component == 'question' && $filearea == 'answerfeedback') {
590 return $options->feedback && (array_key_exists($itemid, $question->options->answers));
591 } else {
592 return parent::check_file_access($question, $state, $options, $contextid, $component,
593 $filearea, $args);
594 }
595 }
2d279432 596}
fe6ce234 597
2d279432
PP
598//// END OF CLASS ////
599
600//////////////////////////////////////////////////////////////////////////
601//// INITIATION - Without this line the question type is not in use... ///
602//////////////////////////////////////////////////////////////////////////
603question_register_questiontype(new question_calculatedmulti_qtype());
34e24cd3
PP
604
605if ( ! defined ("CALCULATEDMULTI")) {
606 define("CALCULATEDMULTI", "calculatedmulti");
607}