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_reportbuilder\reportbuilder\audience;
20
 
21
use context_system;
22
use core_reportbuilder\local\audiences\base;
23
use core_reportbuilder\local\helpers\database;
24
use MoodleQuickForm;
25
 
26
/**
27
 * The backend class for Has system role audience type
28
 *
29
 * @package     core_reportbuilder
30
 * @copyright   2021 David Matamoros <davidmc@moodle.com>
31
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
class systemrole extends base {
34
 
35
    /**
36
     * Adds audience's elements to the given mform
37
     *
38
     * @param MoodleQuickForm $mform The form to add elements to
39
     */
40
    public function get_config_form(MoodleQuickForm $mform): void {
41
        $roles = get_assignable_roles(context_system::instance(), ROLENAME_ALIAS);
42
 
43
        $mform->addElement('autocomplete', 'roles', get_string('selectrole', 'role'), $roles, ['multiple' => true]);
44
        $mform->addRule('roles', null, 'required', null, 'client');
45
    }
46
 
47
    /**
48
     * Helps to build SQL to retrieve users that matches the current audience
49
     *
50
     * @param string $usertablealias
51
     * @return array array of three elements [$join, $where, $params]
52
     */
53
    public function get_sql(string $usertablealias): array {
54
        global $DB;
55
 
56
        $roles = $this->get_configdata()['roles'];
57
        [$insql, $inparams] = $DB->get_in_or_equal($roles, SQL_PARAMS_NAMED, database::generate_param_name('_'));
58
 
59
        // Ensure parameter names and aliases are unique, as the same audience type can be added multiple times to a report.
60
        $paramcontextid = database::generate_param_name();
61
        [$roleassignments, $context] = database::generate_aliases(2);
62
 
63
        $join = "
64
            JOIN {role_assignments} {$roleassignments} ON {$roleassignments}.userid = {$usertablealias}.id
65
            JOIN {context} {$context} ON {$context}.id = {$roleassignments}.contextid";
66
 
67
        $where = "{$roleassignments}.contextid = :{$paramcontextid} AND {$roleassignments}.roleid {$insql}";
68
 
69
        return [$join, $where, $inparams + [$paramcontextid => context_system::instance()->id]];
70
    }
71
 
72
    /**
73
     * Return user friendly name of this audience type
74
     *
75
     * @return string
76
     */
77
    public function get_name(): string {
78
        return get_string('hassystemrole', 'core_reportbuilder');
79
    }
80
 
81
    /**
82
     * Return the description for the audience.
83
     *
84
     * @return string
85
     */
86
    public function get_description(): string {
87
        global $DB;
88
        $rolesids = $this->get_configdata()['roles'];
89
        $roles = $DB->get_records_list('role', 'id', $rolesids, 'sortorder');
90
        $rolesfixed = role_fix_names($roles, context_system::instance(), ROLENAME_ALIAS, true);
91
        return $this->format_description_for_multiselect($rolesfixed);
92
    }
93
 
94
    /**
95
     * If the current user is able to add this audience.
96
     *
97
     * @return bool
98
     */
99
    public function user_can_add(): bool {
100
        // Check if user is able to assign any role from the system context.
101
        $roles = get_assignable_roles(context_system::instance(), ROLENAME_ALIAS);
102
        if (empty($roles)) {
103
            return false;
104
        }
105
 
106
        return true;
107
    }
108
 
109
    /**
110
     * If the current user is able to edit this audience.
111
     *
112
     * @return bool
113
     */
114
    public function user_can_edit(): bool {
115
        global $DB;
116
 
117
        // Check if user can assign all saved role types on this audience instance.
118
        $roleids = $this->get_configdata()['roles'];
119
        $roles = get_assignable_roles(context_system::instance(), ROLENAME_ALIAS);
120
 
121
        // Check that all saved roles still exist.
122
        [$insql, $inparams] = $DB->get_in_or_equal($roleids, SQL_PARAMS_NAMED);
123
        $records = $DB->get_records_select('role', "id $insql", $inparams);
124
 
125
        if (!empty(array_diff(array_keys($records), array_keys($roles)))) {
126
            return false;
127
        }
128
 
129
        return true;
130
    }
131
}