forked from moodleou/moodle-qbehaviour_opaque
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbehaviour.php
executable file
·207 lines (178 loc) · 7.72 KB
/
behaviour.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
<?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/>.
/**
* This behaviour that is used when the actual qim was not
* available.
*
* @package qbehaviour
* @subpackage opaque
* @copyright 2010 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* This behaviour is specifically for use with the Opaque question type.
*
* @copyright 2010 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qbehaviour_opaque extends question_behaviour {
/** @var string */
protected $preferredbehaviour;
/** @var string */
protected $questionsummary;
public function __construct(question_attempt $qa, $preferredbehaviour) {
parent::__construct($qa, $preferredbehaviour);
$this->preferredbehaviour = $preferredbehaviour;
}
public function required_question_definition_type() {
return 'qtype_opaque_question';
}
public function get_state_string($showcorrectness) {
$state = $this->qa->get_state();
$omstate = $this->qa->get_last_behaviour_var('_statestring');
if ($state->is_finished()) {
return $state->default_string($showcorrectness);
} else if ($omstate) {
return $omstate;
} else {
return get_string('notcomplete', 'qbehaviour_opaque');
}
}
public function init_first_step(question_attempt_step $step, $variant) {
global $USER;
// Set up the random seed to be the current time in milliseconds.
list($micros, $sec) = explode(' ', microtime());
$step->set_behaviour_var('_randomseed', $sec . floor($micros * 1000));
$step->set_behaviour_var('_userid', $USER->id);
$step->set_behaviour_var('_language', current_language());
$step->set_behaviour_var('_preferredbehaviour', $this->preferredbehaviour);
$opaquestate = qtype_opaque_update_state($this->qa, $step);
if(!empty($opaquestate->progressinfo))
$step->set_behaviour_var('_statestring', $opaquestate->progressinfo);
// Remember the question summary.
$this->questionsummary = html_to_text($opaquestate->xhtml, 0, false);
}
public function adjust_display_options(question_display_options $options) {
if (!$this->qa->has_marks()) {
$options->correctness = false;
}
if ($this->qa->get_state()->is_finished()) {
$options->readonly = true;
}
}
public function get_question_summary() {
return $this->questionsummary;
}
protected function is_same_response(question_attempt_step $pendingstep) {
$newdata = $pendingstep->get_submitted_data();
foreach ($newdata as $key => $ignored) {
// If an omact_ button has been clicked, never treat this as a duplicate submission.
if (strpos($key, 'omact_') === 0) {
return false;
}
}
$olddata = $this->qa->get_last_step()->get_submitted_data();
return question_utils::arrays_have_same_keys_and_values($newdata, $olddata);
}
public function summarise_action(question_attempt_step $step) {
if ($step->has_behaviour_var('finish')) {
return $this->summarise_finish($step);
} else if ($step->has_behaviour_var('comment')) {
return $this->summarise_manual_comment($step);
} else {
$data = qtype_opaque_get_submitted_data($step);
$formatteddata = array();
foreach ($data as $name => $value) {
$formatteddata[] = $name . ' => ' . s($value);
}
if ($formatteddata) {
return get_string('submitted', 'question', implode(', ', $formatteddata));
} else {
return $this->summarise_start($step);
}
}
}
public function process_action(question_attempt_pending_step $pendingstep) {
if ($pendingstep->has_behaviour_var('finish')) {
return $this->process_finish($pendingstep);
}
if ($pendingstep->has_behaviour_var('comment')) {
return $this->process_comment($pendingstep);
} else if ($this->is_same_response($pendingstep) ||
$this->qa->get_state()->is_finished()) {
return question_attempt::DISCARD;
} else {
return $this->process_remote_action($pendingstep);
}
}
public function process_finish(question_attempt_pending_step $pendingstep) {
if ($this->qa->get_state()->is_finished()) {
return question_attempt::DISCARD;
}
// Try to get the question to stop.
$result = $this->process_remote_action($pendingstep);
if ($result == question_attempt::KEEP && !$pendingstep->get_state()->is_finished()) {
// They tried to finish but the question is not finished, so all we
// can do is to set the state to gave up. This lets the renderer
// handle the review page appropriately.
$pendingstep->set_state(question_state::$gaveup);
}
return $result;
}
public function process_remote_action(question_attempt_pending_step $pendingstep) {
try {
$opaquestate = qtype_opaque_update_state($this->qa, $pendingstep);
} catch (SoapFault $sf) {
print_object($sf);
return question_attempt::DISCARD; // TODO better Opaque error handling.
}
if ($opaquestate->resultssequencenumber != $this->qa->get_num_steps()) {
$pendingstep->set_state(question_state::$todo);
$pendingstep->set_behaviour_var('_statestring', $opaquestate->progressinfo);
} else {
// Look for a score on the default axis.
$pendingstep->set_fraction(0);
foreach ($opaquestate->results->scores as $score) {
if ($score->axis == '') {
$pendingstep->set_fraction($score->marks / $this->question->defaultmark);
}
}
if ($opaquestate->results->attempts > 0) {
$pendingstep->set_state(question_state::$gradedright);
} else {
$pendingstep->set_state(
question_state::graded_state_for_fraction($pendingstep->get_fraction()));
}
if (!empty($opaquestate->results->questionLine)) {
$this->qa->set_question_summary(
$this->cleanup_results($opaquestate->results->questionLine));
}
if (!empty($opaquestate->results->answerLine)) {
$pendingstep->set_new_response_summary(
$this->cleanup_results($opaquestate->results->answerLine));
}
if (!empty($opaquestate->results->actionSummary)) {
$pendingstep->set_behaviour_var('_actionsummary',
$this->cleanup_results($opaquestate->results->actionSummary));
}
}
return question_attempt::KEEP;
}
protected function cleanup_results($line) {
return preg_replace('/\\s+/', ' ', $line);
}
}