MDL-34054 quiz reports: missing context.
[moodle.git] / mod / quiz / report / attemptsreport_options.php
CommitLineData
dcd65f1b
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/**
ac4d9157 18 * Base class for the options that control what is visible in an {@link quiz_attempts_report}.
dcd65f1b
TH
19 *
20 * @package mod_quiz
21 * @copyright 2012 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25
26defined('MOODLE_INTERNAL') || die();
27
28require_once($CFG->libdir . '/formslib.php');
29
30
31/**
ac4d9157 32 * Base class for the options that control what is visible in an {@link quiz_attempts_report}.
dcd65f1b
TH
33 *
34 * @copyright 2012 The Open University
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
ac4d9157 37class mod_quiz_attempts_report_options {
dcd65f1b 38
e97d60ad
TH
39 /** @var string the report mode. */
40 public $mode;
41
dcd65f1b
TH
42 /** @var object the settings for the quiz being reported on. */
43 public $quiz;
44
45 /** @var object the course module objects for the quiz being reported on. */
46 public $cm;
47
48 /** @var object the course settings for the course the quiz is in. */
49 public $course;
50
863872e3
TH
51 /**
52 * @var array form field name => corresponding quiz_attempt:: state constant.
53 */
54 protected static $statefields = array(
55 'stateinprogress' => quiz_attempt::IN_PROGRESS,
56 'stateoverdue' => quiz_attempt::OVERDUE,
57 'statefinished' => quiz_attempt::FINISHED,
58 'stateabandoned' => quiz_attempt::ABANDONED,
59 );
60
61 /**
62 * @var string quiz_attempts_report::ALL_WITH or quiz_attempts_report::ENROLLED_WITH
63 * quiz_attempts_report::ENROLLED_WITHOUT or quiz_attempts_report::ENROLLED_ALL
64 */
65 public $attempts = quiz_attempts_report::ENROLLED_WITH;
dcd65f1b 66
9e67e357 67 /** @var int the currently selected group. 0 if no group is selected. */
dcd65f1b
TH
68 public $group = 0;
69
863872e3
TH
70 /**
71 * @var array|null of quiz_attempt::IN_PROGRESS, etc. constants. null means
72 * no restriction.
73 */
74 public $states = array(quiz_attempt::IN_PROGRESS, quiz_attempt::OVERDUE,
75 quiz_attempt::FINISHED, quiz_attempt::ABANDONED);
76
dcd65f1b 77 /**
9e67e357 78 * @var bool whether to show all finished attmepts, or just the one that gave
dcd65f1b
TH
79 * the final grade for the user.
80 */
81 public $onlygraded = false;
82
9e67e357 83 /** @var int Number of attempts to show per page. */
ac4d9157 84 public $pagesize = quiz_attempts_report::DEFAULT_PAGE_SIZE;
dcd65f1b 85
9e67e357 86 /** @var string whether the data should be downloaded in some format, or '' to display it. */
dcd65f1b
TH
87 public $download = '';
88
9e67e357
TH
89 /** @var bool whether the current user has permission to see grades. */
90 public $usercanseegrades;
91
92 /** @var bool whether the report table should have a column of checkboxes. */
93 public $checkboxcolumn = false;
94
dcd65f1b
TH
95 /**
96 * Constructor.
9d58dae3 97 * @param string $mode which report these options are for.
dcd65f1b
TH
98 * @param object $quiz the settings for the quiz being reported on.
99 * @param object $cm the course module objects for the quiz being reported on.
9d58dae3 100 * @param object $coures the course settings for the coures this quiz is in.
dcd65f1b 101 */
e97d60ad
TH
102 public function __construct($mode, $quiz, $cm, $course) {
103 $this->mode = $mode;
dcd65f1b
TH
104 $this->quiz = $quiz;
105 $this->cm = $cm;
106 $this->course = $course;
9e67e357 107
564b8788 108 $this->usercanseegrades = quiz_report_should_show_grades($quiz, context_module::instance($cm->id));
dcd65f1b
TH
109 }
110
e97d60ad 111 /**
9d58dae3
TH
112 * Get the URL parameters required to show the report with these options.
113 * @return array URL parameter name => value.
e97d60ad
TH
114 */
115 protected function get_url_params() {
116 return array(
863872e3
TH
117 'id' => $this->cm->id,
118 'mode' => $this->mode,
119 'attempts' => $this->attempts,
120 'onlygraded' => $this->onlygraded,
e97d60ad
TH
121 );
122 }
123
124 /**
9d58dae3
TH
125 * Get the URL to show the report with these options.
126 * @return moodle_url the URL.
e97d60ad
TH
127 */
128 public function get_url() {
129 return new moodle_url('/mod/quiz/report.php', $this->get_url_params());
130 }
131
dcd65f1b
TH
132 /**
133 * Process the data we get when the settings form is submitted. This includes
134 * updating the fields of this class, and updating the user preferences
135 * where appropriate.
136 * @param object $fromform The data from $mform->get_data() from the settings form.
137 */
138 public function process_settings_from_form($fromform) {
139 $this->setup_from_form_data($fromform);
140 $this->resolve_dependencies();
141 $this->update_user_preferences();
142 }
143
144 /**
145 * Set up this preferences object using optional_param (using user_preferences
146 * to set anything not specified by the params.
147 */
148 public function process_settings_from_params() {
149 $this->setup_from_user_preferences();
150 $this->setup_from_params();
151 $this->resolve_dependencies();
152 }
153
154 /**
155 * Get the current value of the settings to pass to the settings form.
156 */
157 public function get_initial_form_data() {
158 $toform = new stdClass();
863872e3
TH
159 $toform->attempts = $this->attempts;
160 $toform->onlygraded = $this->onlygraded;
161 $toform->pagesize = $this->pagesize;
dcd65f1b
TH
162
163 return $toform;
164 }
165
166 /**
167 * Set the fields of this object from the form data.
168 * @param object $fromform The data from $mform->get_data() from the settings form.
169 */
170 public function setup_from_form_data($fromform) {
863872e3 171 $this->attempts = $fromform->attempts;
dcd65f1b 172 $this->group = groups_get_activity_group($this->cm, true);
863872e3 173 $this->onlygraded = !empty($fromform->onlygraded);
dcd65f1b 174 $this->pagesize = $fromform->pagesize;
863872e3
TH
175
176 $this->states = array();
177 foreach (self::$statefields as $field => $state) {
178 if (!empty($fromform->$field)) {
179 $this->states[] = $state;
180 }
181 }
dcd65f1b
TH
182 }
183
184 /**
185 * Set the fields of this object from the user's preferences.
186 */
187 public function setup_from_params() {
863872e3 188 $this->attempts = optional_param('attempts', $this->attempts, PARAM_ALPHAEXT);
dcd65f1b 189 $this->group = groups_get_activity_group($this->cm, true);
863872e3 190 $this->onlygraded = optional_param('onlygraded', $this->onlygraded, PARAM_BOOL);
dcd65f1b
TH
191 $this->pagesize = optional_param('pagesize', $this->pagesize, PARAM_INT);
192
863872e3
TH
193 $this->states = explode('-', optional_param('states',
194 implode('-', $this->states), PARAM_ALPHAEXT));
195
dcd65f1b
TH
196 $this->download = optional_param('download', $this->download, PARAM_ALPHA);
197 }
198
199 /**
200 * Set the fields of this object from the user's preferences.
201 * (For those settings that are backed by user-preferences).
202 */
203 public function setup_from_user_preferences() {
204 $this->pagesize = get_user_preferences('quiz_report_pagesize', $this->pagesize);
205 }
206
207 /**
208 * Update the user preferences so they match the settings in this object.
209 * (For those settings that are backed by user-preferences).
210 */
211 public function update_user_preferences() {
212 set_user_preference('quiz_report_pagesize', $this->pagesize);
213 }
214
215 /**
216 * Check the settings, and remove any 'impossible' combinations.
217 */
218 public function resolve_dependencies() {
219 if ($this->group) {
220 // Default for when a group is selected.
863872e3
TH
221 if ($this->attempts === null || $this->attempts == quiz_attempts_report::ALL_WITH) {
222 $this->attempts = quiz_attempts_report::ENROLLED_WITH;
dcd65f1b
TH
223 }
224
225 } else if (!$this->group && $this->course->id == SITEID) {
226 // Force report on front page to show all, unless a group is selected.
863872e3 227 $this->attempts = quiz_attempts_report::ALL_WITH;
dcd65f1b 228
863872e3
TH
229 } else if (!in_array($this->attempts, array(quiz_attempts_report::ALL_WITH, quiz_attempts_report::ENROLLED_WITH,
230 quiz_attempts_report::ENROLLED_WITHOUT, quiz_attempts_report::ENROLLED_ALL))) {
231 $this->attempts = quiz_attempts_report::ENROLLED_WITH;
dcd65f1b
TH
232 }
233
863872e3
TH
234 $cleanstates = array();
235 foreach (self::$statefields as $state) {
236 if (in_array($state, $this->states)) {
237 $cleanstates[] = $state;
238 }
239 }
240 $this->states = $cleanstates;
241 if (count($this->states) == count(self::$statefields)) {
242 // If all states have been selected, then there is no constraint
243 // required in the SQL, so clear the array.
244 $this->states = null;
dcd65f1b
TH
245 }
246
863872e3 247 if (!quiz_report_can_filter_only_graded($this->quiz)) {
dcd65f1b
TH
248 // A grading mode like 'average' has been selected, so we cannot do
249 // the show the attempt that gave the final grade thing.
250 $this->onlygraded = false;
251 }
252
863872e3
TH
253 if ($this->attempts == quiz_attempts_report::ENROLLED_WITHOUT) {
254 $this->states = null;
dcd65f1b
TH
255 $this->onlygraded = false;
256 }
863872e3
TH
257
258 if ($this->onlygraded) {
259 $this->states = array(quiz_attempt::FINISHED);
260 }
261
262 if ($this->pagesize < 1) {
263 $this->pagesize = quiz_attempts_report::DEFAULT_PAGE_SIZE;
264 }
dcd65f1b
TH
265 }
266}