2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Base class for the options that control what is visible in an {@link quiz_attempts_report}.
21 * @copyright 2012 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 require_once($CFG->libdir . '/formslib.php');
32 * Base class for the options that control what is visible in an {@link quiz_attempts_report}.
34 * @copyright 2012 The Open University
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class mod_quiz_attempts_report_options {
39 /** @var string the report mode. */
42 /** @var object the settings for the quiz being reported on. */
45 /** @var object the course module objects for the quiz being reported on. */
48 /** @var object the course settings for the course the quiz is in. */
52 * @var array form field name => corresponding quiz_attempt:: state constant.
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,
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
65 public $attempts = quiz_attempts_report::ENROLLED_WITH;
67 /** @var int the currently selected group. 0 if no group is selected. */
71 * @var array|null of quiz_attempt::IN_PROGRESS, etc. constants. null means
74 public $states = array(quiz_attempt::IN_PROGRESS, quiz_attempt::OVERDUE,
75 quiz_attempt::FINISHED, quiz_attempt::ABANDONED);
78 * @var bool whether to show all finished attmepts, or just the one that gave
79 * the final grade for the user.
81 public $onlygraded = false;
83 /** @var int Number of attempts to show per page. */
84 public $pagesize = quiz_attempts_report::DEFAULT_PAGE_SIZE;
86 /** @var string whether the data should be downloaded in some format, or '' to display it. */
87 public $download = '';
89 /** @var bool whether the current user has permission to see grades. */
90 public $usercanseegrades;
92 /** @var bool whether the report table should have a column of checkboxes. */
93 public $checkboxcolumn = false;
97 * @param string $mode which report these options are for.
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.
100 * @param object $coures the course settings for the coures this quiz is in.
102 public function __construct($mode, $quiz, $cm, $course) {
106 $this->course = $course;
108 $this->usercanseegrades = quiz_report_should_show_grades($quiz);
112 * Get the URL parameters required to show the report with these options.
113 * @return array URL parameter name => value.
115 protected function get_url_params() {
117 'id' => $this->cm->id,
118 'mode' => $this->mode,
119 'attempts' => $this->attempts,
120 'onlygraded' => $this->onlygraded,
125 * Get the URL to show the report with these options.
126 * @return moodle_url the URL.
128 public function get_url() {
129 return new moodle_url('/mod/quiz/report.php', $this->get_url_params());
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
136 * @param object $fromform The data from $mform->get_data() from the settings form.
138 public function process_settings_from_form($fromform) {
139 $this->setup_from_form_data($fromform);
140 $this->resolve_dependencies();
141 $this->update_user_preferences();
145 * Set up this preferences object using optional_param (using user_preferences
146 * to set anything not specified by the params.
148 public function process_settings_from_params() {
149 $this->setup_from_user_preferences();
150 $this->setup_from_params();
151 $this->resolve_dependencies();
155 * Get the current value of the settings to pass to the settings form.
157 public function get_initial_form_data() {
158 $toform = new stdClass();
159 $toform->attempts = $this->attempts;
160 $toform->onlygraded = $this->onlygraded;
161 $toform->pagesize = $this->pagesize;
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.
170 public function setup_from_form_data($fromform) {
171 $this->attempts = $fromform->attempts;
172 $this->group = groups_get_activity_group($this->cm, true);
173 $this->onlygraded = !empty($fromform->onlygraded);
174 $this->pagesize = $fromform->pagesize;
176 $this->states = array();
177 foreach (self::$statefields as $field => $state) {
178 if (!empty($fromform->$field)) {
179 $this->states[] = $state;
185 * Set the fields of this object from the user's preferences.
187 public function setup_from_params() {
188 $this->attempts = optional_param('attempts', $this->attempts, PARAM_ALPHAEXT);
189 $this->group = groups_get_activity_group($this->cm, true);
190 $this->onlygraded = optional_param('onlygraded', $this->onlygraded, PARAM_BOOL);
191 $this->pagesize = optional_param('pagesize', $this->pagesize, PARAM_INT);
193 $this->states = explode('-', optional_param('states',
194 implode('-', $this->states), PARAM_ALPHAEXT));
196 $this->download = optional_param('download', $this->download, PARAM_ALPHA);
200 * Set the fields of this object from the user's preferences.
201 * (For those settings that are backed by user-preferences).
203 public function setup_from_user_preferences() {
204 $this->pagesize = get_user_preferences('quiz_report_pagesize', $this->pagesize);
208 * Update the user preferences so they match the settings in this object.
209 * (For those settings that are backed by user-preferences).
211 public function update_user_preferences() {
212 set_user_preference('quiz_report_pagesize', $this->pagesize);
216 * Check the settings, and remove any 'impossible' combinations.
218 public function resolve_dependencies() {
220 // Default for when a group is selected.
221 if ($this->attempts === null || $this->attempts == quiz_attempts_report::ALL_WITH) {
222 $this->attempts = quiz_attempts_report::ENROLLED_WITH;
225 } else if (!$this->group && $this->course->id == SITEID) {
226 // Force report on front page to show all, unless a group is selected.
227 $this->attempts = quiz_attempts_report::ALL_WITH;
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;
234 $cleanstates = array();
235 foreach (self::$statefields as $state) {
236 if (in_array($state, $this->states)) {
237 $cleanstates[] = $state;
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;
247 if (!quiz_report_can_filter_only_graded($this->quiz)) {
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;
253 if ($this->attempts == quiz_attempts_report::ENROLLED_WITHOUT) {
254 $this->states = null;
255 $this->onlygraded = false;
258 if ($this->onlygraded) {
259 $this->states = array(quiz_attempt::FINISHED);
262 if ($this->pagesize < 1) {
263 $this->pagesize = quiz_attempts_report::DEFAULT_PAGE_SIZE;