Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]
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 $prefix = database::generate_param_name() . '_'; 58 [$insql, $inparams] = $DB->get_in_or_equal($roles, SQL_PARAMS_NAMED, $prefix); 59 60 $contextid = database::generate_param_name(); 61 $ra = database::generate_alias(); 62 $ctx = database::generate_alias(); 63 64 $join = " 65 JOIN {role_assignments} {$ra} ON {$ra}.userid = {$usertablealias}.id 66 JOIN {context} {$ctx} ON {$ctx}.id = {$ra}.contextid"; 67 68 $where = "{$ra}.contextid = :{$contextid} AND {$ra}.roleid {$insql}"; 69 70 return [$join, $where, $inparams + [$contextid => context_system::instance()->id]]; 71 } 72 73 /** 74 * Return user friendly name of this audience type 75 * 76 * @return string 77 */ 78 public function get_name(): string { 79 return get_string('hassystemrole', 'core_reportbuilder'); 80 } 81 82 /** 83 * Return the description for the audience. 84 * 85 * @return string 86 */ 87 public function get_description(): string { 88 global $DB; 89 $rolesids = $this->get_configdata()['roles']; 90 $roles = $DB->get_records_list('role', 'id', $rolesids, 'sortorder'); 91 $rolesfixed = role_fix_names($roles, context_system::instance(), ROLENAME_ALIAS, true); 92 return $this->format_description_for_multiselect($rolesfixed); 93 } 94 95 /** 96 * If the current user is able to add this audience. 97 * 98 * @return bool 99 */ 100 public function user_can_add(): bool { 101 // Check if user is able to assign any role from the system context. 102 $roles = get_assignable_roles(context_system::instance(), ROLENAME_ALIAS); 103 if (empty($roles)) { 104 return false; 105 } 106 107 return true; 108 } 109 110 /** 111 * If the current user is able to edit this audience. 112 * 113 * @return bool 114 */ 115 public function user_can_edit(): bool { 116 global $DB; 117 118 // Check if user can assign all saved role types on this audience instance. 119 $roleids = $this->get_configdata()['roles']; 120 $roles = get_assignable_roles(context_system::instance(), ROLENAME_ALIAS); 121 122 // Check that all saved roles still exist. 123 [$insql, $inparams] = $DB->get_in_or_equal($roleids, SQL_PARAMS_NAMED); 124 $records = $DB->get_records_select('role', "id $insql", $inparams); 125 126 if (!empty(array_diff(array_keys($records), array_keys($roles)))) { 127 return false; 128 } 129 130 return true; 131 } 132 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body