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