MDL-20636 quiz restart preview button was missing since the renderer refactor.
[moodle.git] / question / type / calculatedmulti / question.php
CommitLineData
cdece95e
TH
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Calculated multiple-choice question definition class.
19 *
20 * @package qtype
21 * @subpackage calculatedmulti
22 * @copyright 2011 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26
27defined('MOODLE_INTERNAL') || die();
28
29require_once($CFG->dirroot . '/question/type/multichoice/question.php');
30require_once($CFG->dirroot . '/question/type/calculated/question.php');
31
32
33/**
34 * Represents a calculated multiple-choice multiple-response question.
35 *
36 * @copyright 2011 The Open University
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class qtype_calculatedmulti_single_question extends qtype_multichoice_single_question
40 implements qtype_calculated_question_with_expressions {
41 /** @var qtype_calculated_dataset_loader helper for loading the dataset. */
42 public $datasetloader;
43
44 /** @var qtype_calculated_variable_substituter stores the dataset we are using. */
45 public $vs;
46
47 public function start_attempt(question_attempt_step $step) {
48 qtype_calculated_question_helper::start_attempt($this, $step);
49 parent::start_attempt($step);
50 }
51
52 public function apply_attempt_state(question_attempt_step $step) {
53 qtype_calculated_question_helper::apply_attempt_state($this, $step);
54 parent::apply_attempt_state($step);
55 }
56
57 public function calculate_all_expressions() {
58 qtype_calculatedmulti_calculate_helper::calculate_all_expressions($this);
59 }
60}
61
62
63/**
64 * Represents a calculated multiple-choice multiple-response question.
65 *
66 * @copyright 2011 The Open University
67 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
68 */
69class qtype_calculatedmulti_multi_question extends qtype_multichoice_multi_question
70 implements qtype_calculated_question_with_expressions {
71 /** @var qtype_calculated_dataset_loader helper for loading the dataset. */
72 public $datasetloader;
73
74 /** @var qtype_calculated_variable_substituter stores the dataset we are using. */
75 public $vs;
76
77 public function start_attempt(question_attempt_step $step) {
78 qtype_calculated_question_helper::start_attempt($this, $step);
79 parent::start_attempt($step);
80 }
81
82 public function apply_attempt_state(question_attempt_step $step) {
83 qtype_calculated_question_helper::apply_attempt_state($this, $step);
84 parent::apply_attempt_state($step);
85 }
86
87 public function calculate_all_expressions() {
88 qtype_calculatedmulti_calculate_helper::calculate_all_expressions($this);
89 }
90}
91
92
93/**
94 * Helper to abstract common code between qtype_calculatedmulti_single_question
95 * and qtype_calculatedmulti_multi_question.
96 *
97 * @copyright 2011 The Open University
98 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
99 */
100abstract class qtype_calculatedmulti_calculate_helper {
101 /**
102 * Calculate all the exressions in a qtype_calculatedmulti_single_question
103 * or qtype_calculatedmulti_multi_question.
104 * @param unknown_type $question
105 */
106 public static function calculate_all_expressions(
107 qtype_calculated_question_with_expressions $question) {
108 $question->questiontext = $question->vs->replace_expressions_in_text(
109 $question->questiontext);
110 $question->generalfeedback = $question->vs->replace_expressions_in_text(
111 $question->generalfeedback);
112
113 foreach ($question->answers as $ans) {
114 if ($ans->answer && $ans->answer !== '*') {
115 $ans->answer = $question->vs->replace_expressions_in_text($ans->answer,
116 $ans->correctanswerlength, $ans->correctanswerformat);
117 }
118 $ans->feedback = $question->vs->replace_expressions_in_text($ans->feedback,
119 $ans->correctanswerlength, $ans->correctanswerformat);
120 }
121 }
122}