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