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 core_role\reportbuilder\local\entities; 18 19 use context; 20 use context_helper; 21 use lang_string; 22 use stdClass; 23 use core_reportbuilder\local\entities\base; 24 use core_reportbuilder\local\filters\select; 25 use core_reportbuilder\local\report\{column, filter}; 26 27 /** 28 * Role entity 29 * 30 * @package core_role 31 * @copyright 2023 Paul Holden <paulh@moodle.com> 32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 33 */ 34 class role extends base { 35 36 /** 37 * Database tables that this entity uses and their default aliases 38 * 39 * @return array 40 */ 41 protected function get_default_table_aliases(): array { 42 return [ 43 'context' => 'rctx', 44 'role' => 'r', 45 ]; 46 } 47 48 /** 49 * The default title for this entity 50 * 51 * @return lang_string 52 */ 53 protected function get_default_entity_title(): lang_string { 54 return new lang_string('role'); 55 } 56 57 /** 58 * Initialise the entity 59 * 60 * @return base 61 */ 62 public function initialise(): base { 63 $columns = $this->get_all_columns(); 64 foreach ($columns as $column) { 65 $this->add_column($column); 66 } 67 68 // All the filters defined by the entity can also be used as conditions. 69 $filters = $this->get_all_filters(); 70 foreach ($filters as $filter) { 71 $this 72 ->add_filter($filter) 73 ->add_condition($filter); 74 } 75 76 return $this; 77 } 78 79 /** 80 * Returns list of all available columns 81 * 82 * @return column[] 83 */ 84 protected function get_all_columns(): array { 85 global $DB; 86 87 $contextalias = $this->get_table_alias('context'); 88 $rolealias = $this->get_table_alias('role'); 89 90 // Name column. 91 $columns[] = (new column( 92 'name', 93 new lang_string('rolefullname', 'core_role'), 94 $this->get_entity_name() 95 )) 96 ->add_joins($this->get_joins()) 97 ->set_type(column::TYPE_TEXT) 98 ->add_fields("{$rolealias}.name, {$rolealias}.shortname, {$rolealias}.id, {$contextalias}.id AS contextid") 99 ->add_fields(context_helper::get_preload_record_columns_sql($contextalias)) 100 ->set_is_sortable(true, ["CASE WHEN {$rolealias}.name = '' THEN {$rolealias}.shortname ELSE {$rolealias}.name END"]) 101 ->set_callback(static function($name, stdClass $role): string { 102 if ($name === null) { 103 return ''; 104 } 105 106 context_helper::preload_from_record($role); 107 $context = context::instance_by_id($role->contextid); 108 109 return role_get_name($role, $context, ROLENAME_BOTH); 110 }); 111 112 // Short name column. 113 $columns[] = (new column( 114 'shortname', 115 new lang_string('roleshortname', 'core_role'), 116 $this->get_entity_name() 117 )) 118 ->add_joins($this->get_joins()) 119 ->set_type(column::TYPE_TEXT) 120 ->add_fields("{$rolealias}.shortname") 121 ->set_is_sortable(true); 122 123 // Description column. 124 $descriptionfieldsql = "{$rolealias}.description"; 125 if ($DB->get_dbfamily() === 'oracle') { 126 $descriptionfieldsql = $DB->sql_order_by_text($descriptionfieldsql, 1024); 127 } 128 $columns[] = (new column( 129 'description', 130 new lang_string('description'), 131 $this->get_entity_name() 132 )) 133 ->add_joins($this->get_joins()) 134 ->set_type(column::TYPE_LONGTEXT) 135 ->add_field($descriptionfieldsql, 'description') 136 ->add_field("{$rolealias}.shortname") 137 ->set_callback(static function($description, stdClass $role): string { 138 if ($description === null) { 139 return ''; 140 } 141 142 return role_get_description($role); 143 }); 144 145 return $columns; 146 } 147 148 /** 149 * Return list of all available filters 150 * 151 * @return filter[] 152 */ 153 protected function get_all_filters(): array { 154 $rolealias = $this->get_table_alias('role'); 155 156 // Name filter. 157 $filters[] = (new filter( 158 select::class, 159 'name', 160 new lang_string('rolefullname', 'core_role'), 161 $this->get_entity_name(), 162 "{$rolealias}.id" 163 )) 164 ->add_joins($this->get_joins()) 165 ->set_options_callback(static function(): array { 166 return role_get_names(null, ROLENAME_ORIGINAL, true); 167 }); 168 169 return $filters; 170 } 171 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body