mirrored from git://git.moodle.org/moodle.git
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
attemptsreport_options.php
270 lines (228 loc) · 9.54 KB
/
attemptsreport_options.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
267
268
269
270
<?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/>.
/**
* Base class for the options that control what is visible in an {@link quiz_attempts_report}.
*
* @package mod_quiz
* @copyright 2012 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/formslib.php');
/**
* Base class for the options that control what is visible in an {@link quiz_attempts_report}.
*
* @copyright 2012 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_quiz_attempts_report_options {
/** @var string the report mode. */
public $mode;
/** @var object the settings for the quiz being reported on. */
public $quiz;
/** @var object the course module objects for the quiz being reported on. */
public $cm;
/** @var object the course settings for the course the quiz is in. */
public $course;
/**
* @var array form field name => corresponding quiz_attempt:: state constant.
*/
protected static $statefields = array(
'stateinprogress' => quiz_attempt::IN_PROGRESS,
'stateoverdue' => quiz_attempt::OVERDUE,
'statefinished' => quiz_attempt::FINISHED,
'stateabandoned' => quiz_attempt::ABANDONED,
);
/**
* @var string quiz_attempts_report::ALL_WITH or quiz_attempts_report::ENROLLED_WITH
* quiz_attempts_report::ENROLLED_WITHOUT or quiz_attempts_report::ENROLLED_ALL
*/
public $attempts = quiz_attempts_report::ENROLLED_WITH;
/** @var int the currently selected group. 0 if no group is selected. */
public $group = 0;
/**
* @var array|null of quiz_attempt::IN_PROGRESS, etc. constants. null means
* no restriction.
*/
public $states = array(quiz_attempt::IN_PROGRESS, quiz_attempt::OVERDUE,
quiz_attempt::FINISHED, quiz_attempt::ABANDONED);
/**
* @var bool whether to show all finished attmepts, or just the one that gave
* the final grade for the user.
*/
public $onlygraded = false;
/** @var int Number of attempts to show per page. */
public $pagesize = quiz_attempts_report::DEFAULT_PAGE_SIZE;
/** @var string whether the data should be downloaded in some format, or '' to display it. */
public $download = '';
/** @var bool whether the current user has permission to see grades. */
public $usercanseegrades;
/** @var bool whether the report table should have a column of checkboxes. */
public $checkboxcolumn = false;
/**
* Constructor.
* @param string $mode which report these options are for.
* @param object $quiz the settings for the quiz being reported on.
* @param object $cm the course module objects for the quiz being reported on.
* @param object $coures the course settings for the coures this quiz is in.
*/
public function __construct($mode, $quiz, $cm, $course) {
$this->mode = $mode;
$this->quiz = $quiz;
$this->cm = $cm;
$this->course = $course;
$this->usercanseegrades = quiz_report_should_show_grades($quiz, context_module::instance($cm->id));
}
/**
* Get the URL parameters required to show the report with these options.
* @return array URL parameter name => value.
*/
protected function get_url_params() {
$params = array(
'id' => $this->cm->id,
'mode' => $this->mode,
'attempts' => $this->attempts,
'onlygraded' => $this->onlygraded,
);
if (groups_get_activity_groupmode($this->cm, $this->course)) {
$params['group'] = $this->group;
}
return $params;
}
/**
* Get the URL to show the report with these options.
* @return moodle_url the URL.
*/
public function get_url() {
return new moodle_url('/mod/quiz/report.php', $this->get_url_params());
}
/**
* Process the data we get when the settings form is submitted. This includes
* updating the fields of this class, and updating the user preferences
* where appropriate.
* @param object $fromform The data from $mform->get_data() from the settings form.
*/
public function process_settings_from_form($fromform) {
$this->setup_from_form_data($fromform);
$this->resolve_dependencies();
$this->update_user_preferences();
}
/**
* Set up this preferences object using optional_param (using user_preferences
* to set anything not specified by the params.
*/
public function process_settings_from_params() {
$this->setup_from_user_preferences();
$this->setup_from_params();
$this->resolve_dependencies();
}
/**
* Get the current value of the settings to pass to the settings form.
*/
public function get_initial_form_data() {
$toform = new stdClass();
$toform->attempts = $this->attempts;
$toform->onlygraded = $this->onlygraded;
$toform->pagesize = $this->pagesize;
return $toform;
}
/**
* Set the fields of this object from the form data.
* @param object $fromform The data from $mform->get_data() from the settings form.
*/
public function setup_from_form_data($fromform) {
$this->attempts = $fromform->attempts;
$this->group = groups_get_activity_group($this->cm, true);
$this->onlygraded = !empty($fromform->onlygraded);
$this->pagesize = $fromform->pagesize;
$this->states = array();
foreach (self::$statefields as $field => $state) {
if (!empty($fromform->$field)) {
$this->states[] = $state;
}
}
}
/**
* Set the fields of this object from the user's preferences.
*/
public function setup_from_params() {
$this->attempts = optional_param('attempts', $this->attempts, PARAM_ALPHAEXT);
$this->group = groups_get_activity_group($this->cm, true);
$this->onlygraded = optional_param('onlygraded', $this->onlygraded, PARAM_BOOL);
$this->pagesize = optional_param('pagesize', $this->pagesize, PARAM_INT);
$this->states = explode('-', optional_param('states',
implode('-', $this->states), PARAM_ALPHAEXT));
$this->download = optional_param('download', $this->download, PARAM_ALPHA);
}
/**
* Set the fields of this object from the user's preferences.
* (For those settings that are backed by user-preferences).
*/
public function setup_from_user_preferences() {
$this->pagesize = get_user_preferences('quiz_report_pagesize', $this->pagesize);
}
/**
* Update the user preferences so they match the settings in this object.
* (For those settings that are backed by user-preferences).
*/
public function update_user_preferences() {
set_user_preference('quiz_report_pagesize', $this->pagesize);
}
/**
* Check the settings, and remove any 'impossible' combinations.
*/
public function resolve_dependencies() {
if ($this->group) {
// Default for when a group is selected.
if ($this->attempts === null || $this->attempts == quiz_attempts_report::ALL_WITH) {
$this->attempts = quiz_attempts_report::ENROLLED_WITH;
}
} else if (!$this->group && $this->course->id == SITEID) {
// Force report on front page to show all, unless a group is selected.
$this->attempts = quiz_attempts_report::ALL_WITH;
} else if (!in_array($this->attempts, array(quiz_attempts_report::ALL_WITH, quiz_attempts_report::ENROLLED_WITH,
quiz_attempts_report::ENROLLED_WITHOUT, quiz_attempts_report::ENROLLED_ALL))) {
$this->attempts = quiz_attempts_report::ENROLLED_WITH;
}
$cleanstates = array();
foreach (self::$statefields as $state) {
if (in_array($state, $this->states)) {
$cleanstates[] = $state;
}
}
$this->states = $cleanstates;
if (count($this->states) == count(self::$statefields)) {
// If all states have been selected, then there is no constraint
// required in the SQL, so clear the array.
$this->states = null;
}
if (!quiz_report_can_filter_only_graded($this->quiz)) {
// A grading mode like 'average' has been selected, so we cannot do
// the show the attempt that gave the final grade thing.
$this->onlygraded = false;
}
if ($this->attempts == quiz_attempts_report::ENROLLED_WITHOUT) {
$this->states = null;
$this->onlygraded = false;
}
if ($this->onlygraded) {
$this->states = array(quiz_attempt::FINISHED);
}
if ($this->pagesize < 1) {
$this->pagesize = quiz_attempts_report::DEFAULT_PAGE_SIZE;
}
}
}