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
/**
18
 * The screen with a list of users.
19
 *
20
 * @package   gradereport_singleview
21
 * @copyright 2014 Moodle Pty Ltd (http://moodle.com)
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace gradereport_singleview\local\screen;
26
 
1441 ariadna 27
use context_course;
1 efrain 28
use grade_report;
29
use gradereport_singleview\local\ui\range;
30
use gradereport_singleview\local\ui\bulk_insert;
31
use grade_grade;
32
use grade_item;
33
use moodle_url;
34
use pix_icon;
35
use html_writer;
36
use gradereport_singleview;
37
 
38
defined('MOODLE_INTERNAL') || die;
39
 
40
/**
41
 * The screen with a list of users.
42
 *
43
 * @package   gradereport_singleview
44
 * @copyright 2014 Moodle Pty Ltd (http://moodle.com)
45
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
46
 */
47
class grade extends tablelike implements selectable_items, filterable_items {
48
 
49
    /**
50
     * Used for paging
51
     * @var int $totalitemcount
52
     */
53
    private $totalitemcount = 0;
54
 
55
    /**
56
     * True if this is a manual grade item
57
     * @var bool $requiresextra
58
     */
59
    private $requiresextra = false;
60
 
61
    /**
62
     * True if there are more users than our limit.
63
     * @var bool $requirepaging
64
     */
65
    private $requirespaging = true;
66
 
67
    /**
68
     * To store UI element that generates a grade_item min/max range.
69
     * @var range;
70
     */
71
    protected $range;
72
 
73
    /**
74
     * Returns a grade_item instance or false if none found.
75
     * @var grade_item|bool
76
     */
77
    public $item;
78
 
79
    /**
80
     * True if $CFG->grade_overridecat is true
81
     *
82
     * @return bool
83
     */
84
    public static function allowcategories(): bool {
85
        return get_config('moodle', 'grade_overridecat');
86
    }
87
 
88
    /**
89
     * Filter the list excluding category items (if required)?
90
     * @param grade_item $item The grade item.
91
     * @return bool
92
     */
93
    public static function filter($item): bool {
94
        return get_config('moodle', 'grade_overridecat') ||
95
                !($item->is_course_item() || $item->is_category_item());
96
    }
97
 
98
    /**
99
     * Get the label for the select box that chooses items for this page.
100
     * @return string
101
     */
102
    public function select_label(): string {
103
        return get_string('selectuser', 'gradereport_singleview');
104
    }
105
 
106
    /**
107
     * Get the description of this page
108
     * @return string
109
     */
110
    public function description(): string {
111
        return get_string('users');
112
    }
113
 
114
    /**
115
     * Convert this list of items into an options list
116
     *
117
     * @return array
118
     */
119
    public function options(): array {
120
        $options = [];
121
        foreach ($this->items as $userid => $user) {
122
            $options[$userid] = fullname($user);
123
        }
124
 
125
        return $options;
126
    }
127
 
128
    /**
129
     * Return the type of the things in this list.
130
     * @return string
131
     */
132
    public function item_type(): string {
133
        return 'user';
134
    }
135
 
136
    /**
137
     * Get the original settings for this item
138
     * @return array
139
     */
140
    public function original_definition(): array {
141
        return [
142
            'finalgrade',
143
            'feedback',
144
            'override',
145
            'exclude'
146
        ];
147
    }
148
 
149
    /**
150
     * Init this page
151
     *
152
     * @param bool $selfitemisempty True if we have not selected a user.
153
     */
154
    public function init($selfitemisempty = false) {
155
 
156
        $this->items = grade_report::get_gradable_users($this->courseid, $this->groupid);
157
        $this->totalitemcount = count($this->items);
158
 
159
        if ($selfitemisempty) {
160
            return;
161
        }
162
 
163
        // If we change perpage on pagination we might end up with a page that doesn't exist.
164
        if ($this->perpage) {
165
            $numpages = intval($this->totalitemcount / $this->perpage) + 1;
166
            if ($numpages <= $this->page) {
167
                $this->page = 0;
168
            }
169
        } else {
170
            $this->page = 0;
171
        }
172
 
173
        $params = [
174
            'id' => $this->itemid,
175
            'courseid' => $this->courseid
176
        ];
177
 
178
        $this->item = grade_item::fetch($params);
179
        if (!self::filter($this->item)) {
180
            $this->items = [];
181
            $this->set_init_error(get_string('gradeitemcannotbeoverridden', 'gradereport_singleview'));
182
        }
183
 
184
        $this->requiresextra = !$this->item->is_manual_item();
185
 
186
        $this->setup_structure();
187
 
188
        $this->set_definition($this->original_definition());
189
        $this->set_headers($this->original_headers());
190
    }
191
 
192
    /**
193
     * Get the table headers
194
     *
195
     * @return array
196
     */
197
    public function original_headers() {
198
        return [
199
            get_string('fullnameuser', 'core'),
200
            '', // For filter icon.
201
            get_string('gradenoun'),
202
            get_string('range', 'grades'),
203
            get_string('feedback', 'grades'),
204
            get_string('override', 'gradereport_singleview'),
205
            get_string('exclude', 'gradereport_singleview'),
206
        ];
207
    }
208
 
209
    /**
210
     * Format a row in the table
211
     *
212
     * @param stdClass $item
213
     * @return array
214
     */
215
    public function format_line($item): array {
216
        global $OUTPUT;
217
 
218
        $grade = $this->fetch_grade_or_default($this->item, $item->id);
219
 
220
        $gradestatus = '';
1441 ariadna 221
 
222
        // Show hidden icon if the grade is hidden and the user has permission to view hidden grades.
223
        $showhiddenicon = $grade->is_hidden() &&
224
            has_capability('moodle/grade:viewhidden', context_course::instance($this->courseid));
225
 
1 efrain 226
        $context = [
1441 ariadna 227
            'hidden' => $showhiddenicon,
1 efrain 228
            'locked' => $grade->is_locked(),
229
        ];
230
 
231
        if (in_array(true, $context)) {
232
            $context['classes'] = 'gradestatus';
233
            $gradestatus = $OUTPUT->render_from_template('core_grades/status_icons', $context);
234
        }
235
 
236
        if (has_capability('moodle/site:viewfullnames', \context_course::instance($this->courseid))) {
237
            $fullname = fullname($item, true);
238
        } else {
239
            $fullname = fullname($item);
240
        }
241
 
242
        $item->imagealt = $fullname;
243
        $url = new moodle_url("/user/view.php", ['id' => $item->id, 'course' => $this->courseid]);
244
        $grade->label = $fullname;
245
        $userpic = $OUTPUT->user_picture($item, ['link' => false, 'visibletoscreenreaders' => false]);
246
 
247
        $formatteddefinition = $this->format_definition($grade);
248
 
249
        $line = [
250
            html_writer::link($url, $userpic . $fullname),
251
            $this->get_user_action_menu($item),
252
            $formatteddefinition['finalgrade'] . $gradestatus,
253
            $this->item_range(),
254
            $formatteddefinition['feedback'],
255
            $formatteddefinition['override'],
256
            $formatteddefinition['exclude'],
257
        ];
258
        $lineclasses = [
259
            'user',
260
            'action',
261
            'grade',
262
            'range',
263
        ];
264
        $outputline = [];
265
        $i = 0;
266
        foreach ($line as $key => $value) {
267
            $cell = new \html_table_cell($value);
268
            if ($isheader = $i == 0) {
269
                $cell->header = $isheader;
270
                $cell->scope = "row";
271
            }
272
            if (array_key_exists($key, $lineclasses)) {
273
                $cell->attributes['class'] = $lineclasses[$key];
274
            }
275
            $outputline[] = $cell;
276
            $i++;
277
        }
278
 
279
        return $outputline;
280
    }
281
 
282
    /**
283
     * Get the range ui element for this grade_item
284
     *
285
     * @return element;
286
     */
287
    public function item_range() {
288
        if (empty($this->range)) {
289
            $this->range = new range($this->item);
290
        }
291
 
292
        return $this->range;
293
    }
294
 
295
    /**
296
     * Does this page require paging?
297
     *
298
     * @return bool
299
     */
300
    public function supports_paging(): bool {
301
        return $this->requirespaging;
302
    }
303
 
304
    /**
305
     * Get the pager for this page.
306
     *
307
     * @return string
308
     */
309
    public function pager(): string {
310
        global $OUTPUT;
311
 
312
        return $OUTPUT->paging_bar(
313
            $this->totalitemcount, $this->page, $this->perpage,
314
            new moodle_url('/grade/report/singleview/index.php', [
315
                'perpage' => $this->perpage,
316
                'id' => $this->courseid,
317
                'group' => $this->groupid,
318
                'itemid' => $this->itemid,
319
                'item' => 'grade'
320
            ])
321
        );
322
    }
323
 
324
    /**
325
     * Get the heading for this page.
326
     *
327
     * @return string
328
     */
329
    public function heading(): string {
330
        global $PAGE;
331
        $headinglangstring = $PAGE->user_is_editing() ? 'gradeitemedit' : 'gradeitem';
332
        return get_string($headinglangstring, 'gradereport_singleview', $this->item->get_name());
333
    }
334
 
335
    /**
336
     * Get the summary for this table.
337
     *
338
     * @return string
339
     */
340
    public function summary(): string {
341
        return get_string('summarygrade', 'gradereport_singleview');
342
    }
343
 
344
    /**
345
     * Process the data from the form.
346
     *
347
     * @param array $data
348
     * @return \stdClass of warnings
349
     */
350
    public function process($data): \stdClass {
351
        $bulk = new bulk_insert($this->item);
352
        // Bulk insert messages the data to be passed in
353
        // ie: for all grades of empty grades apply the specified value.
354
        if ($bulk->is_applied($data)) {
355
            $filter = $bulk->get_type($data);
356
            $insertvalue = $bulk->get_insert_value($data);
357
            // Appropriately massage data that may not exist.
358
            if ($this->supports_paging()) {
359
                $gradeitem = grade_item::fetch([
360
                    'courseid' => $this->courseid,
361
                    'id' => $this->item->id
362
                ]);
363
 
364
                $null = $gradeitem->gradetype == GRADE_TYPE_SCALE ? -1 : '';
365
 
366
                foreach ($this->items as $itemid => $item) {
367
                    $field = "finalgrade_{$gradeitem->id}_{$itemid}";
368
                    if (isset($data->$field)) {
369
                        continue;
370
                    }
371
 
372
                    $grade = grade_grade::fetch([
373
                        'itemid' => $gradeitem->id,
374
                        'userid' => $itemid
375
                    ]);
376
 
377
                    $data->$field = empty($grade) ? $null : $grade->finalgrade;
378
                    $data->{"old$field"} = $data->$field;
379
                }
380
            }
381
 
382
            foreach ($data as $varname => $value) {
383
                if (preg_match('/^oldoverride_(\d+)_(\d+)/', $varname, $matches)) {
384
                    // If we've selected overriding all grades.
385
                    if ($filter == 'all') {
386
                        $override = "override_{$matches[1]}_{$matches[2]}";
387
                        $data->$override = '1';
388
                    }
389
                }
390
                if (!preg_match('/^finalgrade_(\d+)_/', $varname, $matches)) {
391
                    continue;
392
                }
393
 
394
                $gradeitem = grade_item::fetch([
395
                    'courseid' => $this->courseid,
396
                    'id' => $matches[1]
397
                ]);
398
 
399
                $isscale = ($gradeitem->gradetype == GRADE_TYPE_SCALE);
400
 
401
                $empties = (trim($value) === '' || ($isscale && $value == -1));
402
 
403
                if ($filter == 'all' || $empties) {
404
                    $data->$varname = ($isscale && empty($insertvalue)) ?
405
                        -1 : $insertvalue;
406
                }
407
            }
408
        }
409
        return parent::process($data);
410
    }
411
 
412
    /**
413
     * Return the action menu HTML for the user item.
414
     *
415
     * @param \stdClass $user
416
     * @return mixed
417
     */
418
    private function get_user_action_menu(\stdClass $user) {
419
        global $OUTPUT;
420
 
421
        $menuitems = [];
422
        $url = new moodle_url($this->format_link('user', $user->id));
423
        $title = get_string('showallgrades', 'core_grades');
424
        $menuitems[] = new \action_menu_link_secondary($url, null, $title);
425
        $menu = new \action_menu($menuitems);
426
        $icon = $OUTPUT->pix_icon('i/moremenu', get_string('actions'));
1441 ariadna 427
        $extraclasses = 'btn btn-icon d-flex';
1 efrain 428
        $menu->set_menu_trigger($icon, $extraclasses);
429
        $menu->set_menu_left();
430
        $menu->set_boundary('window');
431
 
432
        return $OUTPUT->render($menu);
433
    }
434
}