Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

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