MDL-3030 quiz overdue handling: only finished attempts in stats.
[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
9e67e357 51 /** @var string quiz_attempts_report:: constants. */
ac4d9157 52 public $attempts = quiz_attempts_report::STUDENTS_WITH;
dcd65f1b 53
9e67e357 54 /** @var int the currently selected group. 0 if no group is selected. */
dcd65f1b
TH
55 public $group = 0;
56
57 /**
9e67e357 58 * @var bool whether to show all finished attmepts, or just the one that gave
dcd65f1b
TH
59 * the final grade for the user.
60 */
61 public $onlygraded = false;
62
9e67e357 63 /** @var int Number of attempts to show per page. */
ac4d9157 64 public $pagesize = quiz_attempts_report::DEFAULT_PAGE_SIZE;
dcd65f1b 65
9e67e357 66 /** @var string whether the data should be downloaded in some format, or '' to display it. */
dcd65f1b
TH
67 public $download = '';
68
9e67e357
TH
69 /** @var bool whether the current user has permission to see grades. */
70 public $usercanseegrades;
71
72 /** @var bool whether the report table should have a column of checkboxes. */
73 public $checkboxcolumn = false;
74
dcd65f1b
TH
75 /**
76 * Constructor.
9d58dae3 77 * @param string $mode which report these options are for.
dcd65f1b
TH
78 * @param object $quiz the settings for the quiz being reported on.
79 * @param object $cm the course module objects for the quiz being reported on.
9d58dae3 80 * @param object $coures the course settings for the coures this quiz is in.
dcd65f1b 81 */
e97d60ad
TH
82 public function __construct($mode, $quiz, $cm, $course) {
83 $this->mode = $mode;
dcd65f1b
TH
84 $this->quiz = $quiz;
85 $this->cm = $cm;
86 $this->course = $course;
9e67e357
TH
87
88 $this->usercanseegrades = quiz_report_should_show_grades($quiz);
dcd65f1b
TH
89 }
90
e97d60ad 91 /**
9d58dae3
TH
92 * Get the URL parameters required to show the report with these options.
93 * @return array URL parameter name => value.
e97d60ad
TH
94 */
95 protected function get_url_params() {
96 return array(
97 'id' => $this->cm->id,
98 'mode' => $this->mode,
99 'attemptsmode' => $this->attempts,
100 'qmfilter' => $this->onlygraded,
101 );
102 }
103
104 /**
9d58dae3
TH
105 * Get the URL to show the report with these options.
106 * @return moodle_url the URL.
e97d60ad
TH
107 */
108 public function get_url() {
109 return new moodle_url('/mod/quiz/report.php', $this->get_url_params());
110 }
111
dcd65f1b
TH
112 /**
113 * Process the data we get when the settings form is submitted. This includes
114 * updating the fields of this class, and updating the user preferences
115 * where appropriate.
116 * @param object $fromform The data from $mform->get_data() from the settings form.
117 */
118 public function process_settings_from_form($fromform) {
119 $this->setup_from_form_data($fromform);
120 $this->resolve_dependencies();
121 $this->update_user_preferences();
122 }
123
124 /**
125 * Set up this preferences object using optional_param (using user_preferences
126 * to set anything not specified by the params.
127 */
128 public function process_settings_from_params() {
129 $this->setup_from_user_preferences();
130 $this->setup_from_params();
131 $this->resolve_dependencies();
132 }
133
134 /**
135 * Get the current value of the settings to pass to the settings form.
136 */
137 public function get_initial_form_data() {
138 $toform = new stdClass();
139 $toform->attemptsmode = $this->attempts;
140 $toform->qmfilter = $this->onlygraded;
141 $toform->pagesize = $this->pagesize;
142
143 return $toform;
144 }
145
146 /**
147 * Set the fields of this object from the form data.
148 * @param object $fromform The data from $mform->get_data() from the settings form.
149 */
150 public function setup_from_form_data($fromform) {
151 $this->attempts = $fromform->attemptsmode;
152 $this->group = groups_get_activity_group($this->cm, true);
153 $this->onlygraded = !empty($fromform->qmfilter);
154 $this->pagesize = $fromform->pagesize;
155 }
156
157 /**
158 * Set the fields of this object from the user's preferences.
159 */
160 public function setup_from_params() {
161 $this->attempts = optional_param('attemptsmode', $this->attempts, PARAM_ALPHAEXT);
162 $this->group = groups_get_activity_group($this->cm, true);
163 $this->onlygraded = optional_param('qmfilter', $this->onlygraded, PARAM_BOOL);
164 $this->pagesize = optional_param('pagesize', $this->pagesize, PARAM_INT);
165
166 $this->download = optional_param('download', $this->download, PARAM_ALPHA);
167 }
168
169 /**
170 * Set the fields of this object from the user's preferences.
171 * (For those settings that are backed by user-preferences).
172 */
173 public function setup_from_user_preferences() {
174 $this->pagesize = get_user_preferences('quiz_report_pagesize', $this->pagesize);
175 }
176
177 /**
178 * Update the user preferences so they match the settings in this object.
179 * (For those settings that are backed by user-preferences).
180 */
181 public function update_user_preferences() {
182 set_user_preference('quiz_report_pagesize', $this->pagesize);
183 }
184
185 /**
186 * Check the settings, and remove any 'impossible' combinations.
187 */
188 public function resolve_dependencies() {
189 if ($this->group) {
190 // Default for when a group is selected.
ac4d9157
TH
191 if ($this->attempts === null || $this->attempts == quiz_attempts_report::ALL_ATTEMPTS) {
192 $this->attempts = quiz_attempts_report::STUDENTS_WITH;
dcd65f1b
TH
193 }
194
195 } else if (!$this->group && $this->course->id == SITEID) {
196 // Force report on front page to show all, unless a group is selected.
ac4d9157 197 $this->attempts = quiz_attempts_report::ALL_ATTEMPTS;
dcd65f1b
TH
198
199 } else if ($this->attempts === null) {
ac4d9157 200 $this->attempts = quiz_attempts_report::ALL_ATTEMPTS;
dcd65f1b
TH
201 }
202
203 if ($this->pagesize < 1) {
ac4d9157 204 $this->pagesize = quiz_attempts_report::DEFAULT_PAGE_SIZE;
dcd65f1b
TH
205 }
206
207 if (!quiz_report_qm_filter_select($this->quiz)) {
208 // A grading mode like 'average' has been selected, so we cannot do
209 // the show the attempt that gave the final grade thing.
210 $this->onlygraded = false;
211 }
212
ac4d9157 213 if ($this->attempts == quiz_attempts_report::STUDENTS_WITH_NO) {
dcd65f1b
TH
214 $this->onlygraded = false;
215 }
216 }
217}