forked from wiris/moodle-qtype_wq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
step.php
266 lines (235 loc) · 8.56 KB
/
step.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
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/*
* Class used to store information in the question attempt step data
*/
/**
* Description of step
*
* @author dani
*/
/**
* This class is useful for:
* - No intentar avaluar una pregunta si ha fallat en 3 intents seguits
* en les mateixes condicions. Útil en exàmens.
* - Guardar info en un "step" durant l'avaluació, perquè en aquest moment
* l'step és read-only.
* **/
class qtype_wirisstep {
const MAX_ATTEMPS_SHORTANSWER_WIRIS = 2;
private $step;
private $step_id;
private $extraprefix;
public function __construct() {
$this->step = null;
}
public function load($step) {
$notReadOnly = !($step instanceof question_attempt_step_read_only);
$notAdapterOfReadOnly = !($step instanceof question_attempt_step_subquestion_adapter_wiris && $step->is_adapter_of_read_only());
if ($notReadOnly && $notAdapterOfReadOnly) {
$this->step = $step;
// It is a regrade or the first attempt.
try {
$this->reset_attempts();
return;
} catch (moodle_exception $ex) {
// We assume it is read only so continue.
$this->step = null;
}
}
$s = var_export($step, true);
if (isset($step->get_id)) {
// Moodle 2.3 or superior.
$this->step_id = $step->get_id();
} else {
// Moodle 2.2.
if (preg_match("/'id' *=> *'(.*)'/", $s, $matches)) {
$this->step_id = $matches[1];
} else {
$this->step_id = 0;
}
}
if (preg_match("/'extraprefix' *=> *'(.*)'/", $s, $matches)) {
$this->extraprefix = $matches[1];
} else {
$this->extraprefix = "";
}
}
/**
* Sets a attempt step data value
* @global type $CFG
* @param type $name the name of the property to set
* @param type $value the value
* @param type $quizBool whether it is question level or subquestion level
* @throws dml_exception
*/
public function set_var($name, $value, $subquesbool = true) {
if ($subquesbool && $this->step != null) {
$this->step->set_qt_var($name, $value);
return;
}
if (!isset($this->step_id) || $this->step_id == 0) {
// It doees not exist, do not even try to find in the db.
return null;
}
$DB = $this->get_db();
$name = $this->get_step_var_internal($name, $subquesbool);
if (strlen($name) > 32) {
// Database only allows keys up to length 32.
$name = substr($name, 0, 32);
}
$gc = $DB->get_record('question_attempt_step_data', array('attemptstepid' => $this->step_id, 'name' => $name), 'value');
if ($gc == null) {
$gc = new stdClass();
$gc->attemptstepid = $this->step_id;
$gc->name = $name;
$gc->value = $value;
$DB->insert_record('question_attempt_step_data', $gc);
} else {
$DB->set_field('question_attempt_step_data', 'value',
$value, array('attemptstepid' => $this->step_id, 'name' => $name));
}
}
public function get_qt_data() {
if ($this->step != null) {
return $this->step->get_qt_data();
} else {
$DB = $this->get_db();
}
}
/**
*
* @param type $name
* @param type $subquesbool whether the variable is from the subquestion or the parent (only cloze).
* @return null
*/
public function get_var($name, $subquesbool = true) {
if ($subquesbool && $this->step != null) {
return $this->step->get_qt_var($name);
}
if (!isset($this->step_id) || $this->step_id == 0) {
// It doees not exist, do not even try to find in the db.
return null;
}
$DB = $this->get_db();
$name = $this->get_step_var_internal($name, $subquesbool);
if (strlen($name) > 32) {
$name = substr($name, 0, 32);
}
$gc = $DB->get_record('question_attempt_step_data', array('attemptstepid' => $this->step_id, 'name' => $name), 'value');
if ($gc == null) {
$r = null;
} else {
$r = $gc->value;
}
return $r;
}
private function get_step_var_internal($name, $subquesbool) {
if ($subquesbool && strlen($this->extraprefix) > 0) {
// The prefix is needed when it is a subquestion of a cloze
// (multianswer) question type.
if (substr($name, 0, 2) === '!_') {
return '-_' . $this->extraprefix . substr($name, 2);
} else if (substr($name, 0, 1) === '-') {
return '-' . $this->extraprefix . substr($name, 1);
} else if (substr($name, 0, 1) === '_') {
return '_' . $this->extraprefix . substr($name, 1);
} else {
return $this->extraprefix . $name;
}
} else {
return $name;
}
}
private function get_db() {
global $wirisdb;
if (!isset($wirisdb)) {
global $CFG;
// Create a new connection because the default one is inside a transaction
// and a rollback is done afterwards.
if (!$wirisdb = moodle_database::get_driver_instance($CFG->dbtype, $CFG->dblibrary)) {
throw new dml_exception('dbdriverproblem', "Unknown driver $CFG->dblibrary/$CFG->dbtype");
}
$wirisdb->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix, $CFG->dboptions);
}
return $wirisdb;
}
/**
* Returns whether the max number of attempts limit is reached
*/
public function is_attempt_limit_reached() {
$c = $this->get_var('_gc', false);
if (is_null($c)) {
return false;
}
return $c >= self::MAX_ATTEMPS_SHORTANSWER_WIRIS;
}
/**
* Increment number of failed attempts
*/
public function inc_attempts() {
$c = $this->get_var('_gc', false);
if (is_null($c)) {
$c = 0;
}
$this->set_var('_gc', $c + 1, false);
}
/**
* Set number of failed attempts to zero
*/
public function reset_attempts() {
$this->set_var('_gc', 0, false);
}
/**
* Whether there is any error in the question or in the parent
* @return boolean
*/
public function is_error() {
$c = $this->get_var('_gc', false); // False to look into the parent.
if (!is_null($c) && $c > 0) {
return true;
}
return false;
}
/**
* Returns the number of failed attempts
* @return int
*/
public function get_attempts() {
$c = $this->get_var('_gc', false);
if ($c == null) {
return 0;
}
return $c;
}
public function is_first_step() {
// The original $step object is always a quetsion_attempt_stept_read_only
// object but in the first one of the steps sequence. In $this->load
// method we save $this->step only if it isn't readonly, so in particular
// only if it is the first step.
return (!is_null($this->step));
}
}
class question_attempt_step_subquestion_adapter_wiris extends question_attempt_step_subquestion_adapter {
public function is_adapter_of_read_only() {
// In Moodle 4.1 and earlier the variable $this->realstep was misspelled as $this->realqas.
// Therefore we need to check for both possibilities. In Moodle 4.2 we use this extension class
// because $this->realstep is protected.
return
(isset($this->realqas) && ($this->realqas instanceof question_attempt_step_read_only)) ||
(isset($this->realstep) && ($this->realstep instanceof question_attempt_step_read_only));
}
}