Differences Between: [Versions 401 and 402] [Versions 402 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_group\reportbuilder\local\entities; 20 21 use context_course; 22 use context_helper; 23 use html_writer; 24 use lang_string; 25 use moodle_url; 26 use stdClass; 27 use core_reportbuilder\local\entities\base; 28 use core_reportbuilder\local\filters\{boolean_select, date, select, text}; 29 use core_reportbuilder\local\helpers\format; 30 use core_reportbuilder\local\report\{column, filter}; 31 32 defined('MOODLE_INTERNAL') || die(); 33 34 global $CFG; 35 require_once("{$CFG->libdir}/grouplib.php"); 36 37 /** 38 * Group entity 39 * 40 * @package core_group 41 * @copyright 2022 Paul Holden <paulh@moodle.com> 42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 43 */ 44 class group extends base { 45 46 /** 47 * Database tables that this entity uses and their default aliases 48 * 49 * @return array 50 */ 51 protected function get_default_table_aliases(): array { 52 return [ 53 'context' => 'gctx', 54 'groups' => 'g', 55 ]; 56 } 57 58 /** 59 * The default title for this entity 60 * 61 * @return lang_string 62 */ 63 protected function get_default_entity_title(): lang_string { 64 return new lang_string('group', 'core_group'); 65 } 66 67 /** 68 * Initialise the entity 69 * 70 * @return base 71 */ 72 public function initialise(): base { 73 $columns = $this->get_all_columns(); 74 foreach ($columns as $column) { 75 $this->add_column($column); 76 } 77 78 // All the filters defined by the entity can also be used as conditions. 79 $filters = $this->get_all_filters(); 80 foreach ($filters as $filter) { 81 $this 82 ->add_filter($filter) 83 ->add_condition($filter); 84 } 85 86 return $this; 87 } 88 89 /** 90 * Returns list of all available columns 91 * 92 * @return column[] 93 */ 94 protected function get_all_columns(): array { 95 global $DB; 96 97 $contextalias = $this->get_table_alias('context'); 98 $groupsalias = $this->get_table_alias('groups'); 99 100 // Name column. 101 $columns[] = (new column( 102 'name', 103 new lang_string('name'), 104 $this->get_entity_name() 105 )) 106 ->add_joins($this->get_joins()) 107 ->set_type(column::TYPE_TEXT) 108 ->add_fields("{$groupsalias}.name, {$groupsalias}.courseid") 109 ->add_fields(context_helper::get_preload_record_columns_sql($contextalias)) 110 ->set_is_sortable(true) 111 ->set_callback(static function($name, stdClass $group): string { 112 if ($name === null) { 113 return ''; 114 } 115 116 context_helper::preload_from_record($group); 117 $context = context_course::instance($group->courseid); 118 119 return format_string($group->name, true, ['context' => $context]); 120 }); 121 122 // ID number column. 123 $columns[] = (new column( 124 'idnumber', 125 new lang_string('idnumber'), 126 $this->get_entity_name() 127 )) 128 ->add_joins($this->get_joins()) 129 ->set_type(column::TYPE_TEXT) 130 ->add_fields("{$groupsalias}.idnumber") 131 ->set_is_sortable(true); 132 133 // Description column. 134 $descriptionfieldsql = "{$groupsalias}.description"; 135 if ($DB->get_dbfamily() === 'oracle') { 136 $descriptionfieldsql = $DB->sql_order_by_text($descriptionfieldsql, 1024); 137 } 138 $columns[] = (new column( 139 'description', 140 new lang_string('description'), 141 $this->get_entity_name() 142 )) 143 ->add_joins($this->get_joins()) 144 ->set_type(column::TYPE_LONGTEXT) 145 ->add_field($descriptionfieldsql, 'description') 146 ->add_fields("{$groupsalias}.descriptionformat, {$groupsalias}.id, {$groupsalias}.courseid") 147 ->add_fields(context_helper::get_preload_record_columns_sql($contextalias)) 148 ->set_is_sortable(false) 149 ->set_callback(static function(?string $description, stdClass $group): string { 150 global $CFG; 151 152 if ($description === null) { 153 return ''; 154 } 155 156 require_once("{$CFG->libdir}/filelib.php"); 157 158 context_helper::preload_from_record($group); 159 $context = context_course::instance($group->courseid); 160 161 $description = file_rewrite_pluginfile_urls($description, 'pluginfile.php', $context->id, 'group', 162 'description', $group->id); 163 164 return format_text($description, $group->descriptionformat, ['context' => $context]); 165 }); 166 167 // Enrolment key column. 168 $columns[] = (new column( 169 'enrolmentkey', 170 new lang_string('enrolmentkey', 'core_group'), 171 $this->get_entity_name() 172 )) 173 ->add_joins($this->get_joins()) 174 ->set_type(column::TYPE_TEXT) 175 ->add_fields("{$groupsalias}.enrolmentkey") 176 ->set_is_sortable(true); 177 178 // Visibility column. 179 $columns[] = (new column( 180 'visibility', 181 new lang_string('visibilityshort', 'core_group'), 182 $this->get_entity_name() 183 )) 184 ->add_joins($this->get_joins()) 185 ->set_type(column::TYPE_INTEGER) 186 ->add_fields("{$groupsalias}.visibility") 187 ->set_is_sortable(true) 188 // It doesn't make sense to offer integer aggregation methods for this column. 189 ->set_disabled_aggregation(['avg', 'max', 'min', 'sum']) 190 ->set_callback(static function(?int $visibility): string { 191 if ($visibility === null) { 192 return ''; 193 } 194 195 $options = [ 196 GROUPS_VISIBILITY_ALL => new lang_string('visibilityall', 'core_group'), 197 GROUPS_VISIBILITY_MEMBERS => new lang_string('visibilitymembers', 'core_group'), 198 GROUPS_VISIBILITY_OWN => new lang_string('visibilityown', 'core_group'), 199 GROUPS_VISIBILITY_NONE => new lang_string('visibilitynone', 'core_group'), 200 ]; 201 202 return (string) ($options[$visibility] ?? $visibility); 203 }); 204 205 // Participation column. 206 $columns[] = (new column( 207 'participation', 208 new lang_string('participationshort', 'core_group'), 209 $this->get_entity_name() 210 )) 211 ->add_joins($this->get_joins()) 212 ->set_type(column::TYPE_BOOLEAN) 213 ->add_fields("{$groupsalias}.participation") 214 ->set_is_sortable(true) 215 ->set_callback([format::class, 'boolean_as_text']); 216 217 // Picture column. 218 $columns[] = (new column( 219 'picture', 220 new lang_string('picture'), 221 $this->get_entity_name() 222 )) 223 ->add_joins($this->get_joins()) 224 ->set_type(column::TYPE_INTEGER) 225 ->add_fields("{$groupsalias}.picture, {$groupsalias}.id, {$contextalias}.id AS contextid") 226 ->set_is_sortable(false) 227 // It doesn't make sense to offer integer aggregation methods for this column. 228 ->set_disabled_aggregation(['avg', 'max', 'min', 'sum']) 229 ->set_callback(static function ($picture, stdClass $group): string { 230 if (empty($group->picture)) { 231 return ''; 232 } 233 234 $pictureurl = moodle_url::make_pluginfile_url($group->contextid, 'group', 'icon', $group->id, '/', 'f2'); 235 $pictureurl->param('rev', $group->picture); 236 237 return html_writer::img($pictureurl, ''); 238 }); 239 240 // Time created column. 241 $columns[] = (new column( 242 'timecreated', 243 new lang_string('timecreated', 'core_reportbuilder'), 244 $this->get_entity_name() 245 )) 246 ->add_joins($this->get_joins()) 247 ->set_type(column::TYPE_TIMESTAMP) 248 ->add_fields("{$groupsalias}.timecreated") 249 ->set_is_sortable(true) 250 ->set_callback([format::class, 'userdate']); 251 252 // Time modified column. 253 $columns[] = (new column( 254 'timemodified', 255 new lang_string('timemodified', 'core_reportbuilder'), 256 $this->get_entity_name() 257 )) 258 ->add_joins($this->get_joins()) 259 ->set_type(column::TYPE_TIMESTAMP) 260 ->add_fields("{$groupsalias}.timemodified") 261 ->set_is_sortable(true) 262 ->set_callback([format::class, 'userdate']); 263 264 return $columns; 265 } 266 267 /** 268 * Return list of all available filters 269 * 270 * @return filter[] 271 */ 272 protected function get_all_filters(): array { 273 $groupsalias = $this->get_table_alias('groups'); 274 275 // Name filter. 276 $filters[] = (new filter( 277 text::class, 278 'name', 279 new lang_string('name'), 280 $this->get_entity_name(), 281 "{$groupsalias}.name" 282 )) 283 ->add_joins($this->get_joins()); 284 285 // ID number filter. 286 $filters[] = (new filter( 287 text::class, 288 'idnumber', 289 new lang_string('idnumber'), 290 $this->get_entity_name(), 291 "{$groupsalias}.idnumber" 292 )) 293 ->add_joins($this->get_joins()); 294 295 // Visibility filter. 296 $filters[] = (new filter( 297 select::class, 298 'visibility', 299 new lang_string('visibilityshort', 'core_group'), 300 $this->get_entity_name(), 301 "{$groupsalias}.visibility" 302 )) 303 ->add_joins($this->get_joins()) 304 ->set_options([ 305 GROUPS_VISIBILITY_ALL => new lang_string('visibilityall', 'core_group'), 306 GROUPS_VISIBILITY_MEMBERS => new lang_string('visibilitymembers', 'core_group'), 307 GROUPS_VISIBILITY_OWN => new lang_string('visibilityown', 'core_group'), 308 GROUPS_VISIBILITY_NONE => new lang_string('visibilitynone', 'core_group'), 309 ]); 310 311 // Participation filter. 312 $filters[] = (new filter( 313 boolean_select::class, 314 'participation', 315 new lang_string('participationshort', 'core_group'), 316 $this->get_entity_name(), 317 "{$groupsalias}.participation" 318 )) 319 ->add_joins($this->get_joins()); 320 321 // Time created filter. 322 $filters[] = (new filter( 323 date::class, 324 'timecreated', 325 new lang_string('timecreated', 'core_reportbuilder'), 326 $this->get_entity_name(), 327 "{$groupsalias}.timecreated" 328 )) 329 ->add_joins($this->get_joins()); 330 331 return $filters; 332 } 333 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body