MDL-40434 calculatedmulti multiple-response questions not randomised
[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 {
e35ba43c 41
cdece95e
TH
42 /** @var qtype_calculated_dataset_loader helper for loading the dataset. */
43 public $datasetloader;
44
45 /** @var qtype_calculated_variable_substituter stores the dataset we are using. */
46 public $vs;
47
e35ba43c
TH
48 /**
49 * @var bool wheter the dataset item to use should be chose based on attempt
50 * start time, rather than randomly.
51 */
52 public $synchronised;
53
1da821bb 54 public function start_attempt(question_attempt_step $step, $variant) {
c014b989
TH
55 qtype_calculated_question_helper::start_attempt($this, $step, $variant);
56 parent::start_attempt($step, $variant);
cdece95e
TH
57 }
58
59 public function apply_attempt_state(question_attempt_step $step) {
60 qtype_calculated_question_helper::apply_attempt_state($this, $step);
61 parent::apply_attempt_state($step);
62 }
63
64 public function calculate_all_expressions() {
65 qtype_calculatedmulti_calculate_helper::calculate_all_expressions($this);
66 }
c014b989 67
c014b989
TH
68 public function get_num_variants() {
69 return $this->datasetloader->get_number_of_items();
70 }
71
72 public function get_variants_selection_seed() {
73 if (!empty($this->synchronised) &&
409199d6 74 $this->datasetloader->datasets_are_synchronised($this->category)) {
c014b989
TH
75 return 'category' . $this->category;
76 } else {
77 return parent::get_variants_selection_seed();
78 }
79 }
cdece95e
TH
80}
81
82
83/**
84 * Represents a calculated multiple-choice multiple-response question.
85 *
86 * @copyright 2011 The Open University
87 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
88 */
89class qtype_calculatedmulti_multi_question extends qtype_multichoice_multi_question
90 implements qtype_calculated_question_with_expressions {
e35ba43c 91
cdece95e
TH
92 /** @var qtype_calculated_dataset_loader helper for loading the dataset. */
93 public $datasetloader;
94
95 /** @var qtype_calculated_variable_substituter stores the dataset we are using. */
96 public $vs;
97
e35ba43c
TH
98 /**
99 * @var bool wheter the dataset item to use should be chose based on attempt
100 * start time, rather than randomly.
101 */
102 public $synchronised;
103
1da821bb 104 public function start_attempt(question_attempt_step $step, $variant) {
0cba6a8d 105 qtype_calculated_question_helper::start_attempt($this, $step, $variant);
1da821bb 106 parent::start_attempt($step, $variant);
cdece95e
TH
107 }
108
109 public function apply_attempt_state(question_attempt_step $step) {
110 qtype_calculated_question_helper::apply_attempt_state($this, $step);
111 parent::apply_attempt_state($step);
112 }
113
114 public function calculate_all_expressions() {
115 qtype_calculatedmulti_calculate_helper::calculate_all_expressions($this);
116 }
38c34920
TH
117
118 public function get_num_variants() {
119 return $this->datasetloader->get_number_of_items();
120 }
121
122 public function get_variants_selection_seed() {
123 if (!empty($this->synchronised) &&
124 $this->datasetloader->datasets_are_synchronised($this->category)) {
125 return 'category' . $this->category;
126 } else {
127 return parent::get_variants_selection_seed();
128 }
129 }
cdece95e
TH
130}
131
132
133/**
134 * Helper to abstract common code between qtype_calculatedmulti_single_question
135 * and qtype_calculatedmulti_multi_question.
136 *
137 * @copyright 2011 The Open University
138 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
139 */
140abstract class qtype_calculatedmulti_calculate_helper {
141 /**
142 * Calculate all the exressions in a qtype_calculatedmulti_single_question
143 * or qtype_calculatedmulti_multi_question.
144 * @param unknown_type $question
145 */
146 public static function calculate_all_expressions(
147 qtype_calculated_question_with_expressions $question) {
148 $question->questiontext = $question->vs->replace_expressions_in_text(
149 $question->questiontext);
150 $question->generalfeedback = $question->vs->replace_expressions_in_text(
151 $question->generalfeedback);
152
153 foreach ($question->answers as $ans) {
154 if ($ans->answer && $ans->answer !== '*') {
155 $ans->answer = $question->vs->replace_expressions_in_text($ans->answer,
156 $ans->correctanswerlength, $ans->correctanswerformat);
157 }
158 $ans->feedback = $question->vs->replace_expressions_in_text($ans->feedback,
159 $ans->correctanswerlength, $ans->correctanswerformat);
160 }
161 }
162}