Proyectos de Subversion Moodle

Rev

| 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
declare(strict_types=1);
18
 
19
namespace core_admin\reportbuilder\local\filters;
20
 
21
use core\context\system;
22
use core_course_category;
23
use MoodleQuickForm;
24
use core_reportbuilder\local\filters\base;
25
use core_reportbuilder\local\helpers\database;
26
 
27
/**
28
 * Course role report filter (by role, category, course)
29
 *
30
 * The provided filter field SQL must refer/return an expression for the user ID (e.g. "{$user}.id")
31
 *
32
 * @package     core_admin
33
 * @copyright   2023 Paul Holden <paulh@moodle.com>
34
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
36
class courserole extends base {
37
 
38
    /**
39
     * Setup form
40
     *
41
     * @param MoodleQuickForm $mform
42
     */
43
    public function setup_form(MoodleQuickForm $mform): void {
44
        $elements = [];
45
 
46
        // Role.
47
        $elements['role'] = $mform->createElement('select', "{$this->name}_role", get_string('rolefullname', 'core_role'),
48
            [0 => get_string('anyrole', 'core_filters')] + get_default_enrol_roles(system::instance()));
49
 
50
        // Category.
51
        $elements['category'] = $mform->createElement('select', "{$this->name}_category", get_string('category'),
52
            [0 => get_string('anycategory', 'core_filters')] + core_course_category::make_categories_list());
53
 
54
        // Course.
55
        $elements['course'] = $mform->createElement('text', "{$this->name}_course", get_string('shortnamecourse'));
56
        $mform->setType("{$this->name}_course", PARAM_RAW_TRIMMED);
57
 
58
        $mform->addGroup($elements, "{$this->name}_group", $this->get_header(), '', false)
59
            ->setHiddenLabel(true);
60
    }
61
 
62
    /**
63
     * Return filter SQL
64
     *
65
     * @param array $values
66
     * @return array
67
     */
68
    public function get_sql_filter(array $values): array {
69
        [$fieldsql, $params] = $this->filter->get_field_sql_and_params();
70
 
71
        [$contextalias, $rolealias, $coursealias] = database::generate_aliases(3);
72
        [$roleparam, $categoryparam, $courseparam] = database::generate_param_names(3);
73
 
74
        // Role.
75
        $role = (int) ($values["{$this->name}_role"] ?? 0);
76
        if ($role > 0) {
77
            $selects[] = "{$rolealias}.roleid = :{$roleparam}";
78
            $params[$roleparam] = $role;
79
        }
80
 
81
        // Category.
82
        $category = (int) ($values["{$this->name}_category"] ?? 0);
83
        if ($category > 0) {
84
            $selects[] = "{$coursealias}.category = :{$categoryparam}";
85
            $params[$categoryparam] = $category;
86
        }
87
 
88
        // Course.
89
        $course = trim($values["{$this->name}_course"] ?? '');
90
        if ($course !== '') {
91
            $selects[] = "{$coursealias}.shortname = :{$courseparam}";
92
            $params[$courseparam] = $course;
93
        }
94
 
95
        // Filter values are not set.
96
        if (empty($selects)) {
97
            return ['', []];
98
        }
99
 
100
        return ["{$fieldsql} IN (
101
            SELECT {$rolealias}.userid
102
              FROM {role_assignments} {$rolealias}
103
              JOIN {context} {$contextalias} ON {$contextalias}.id = {$rolealias}.contextid AND {$contextalias}.contextlevel = 50
104
              JOIN {course} {$coursealias} ON {$coursealias}.id = {$contextalias}.instanceid
105
             WHERE " . implode(' AND ', $selects)  . "
106
        )", $params];
107
    }
108
}