See Release Notes
Long Term Support Release
Differences Between: [Versions 401 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_cohort\reportbuilder\local\entities; 20 21 use context; 22 use context_helper; 23 use lang_string; 24 use stdClass; 25 use core_reportbuilder\local\entities\base; 26 use core_reportbuilder\local\filters\date; 27 use core_reportbuilder\local\filters\select; 28 use core_reportbuilder\local\filters\text; 29 use core_reportbuilder\local\helpers\format; 30 use core_reportbuilder\local\report\column; 31 use core_reportbuilder\local\report\filter; 32 33 /** 34 * Cohort entity 35 * 36 * @package core_cohort 37 * @copyright 2021 Paul Holden <paulh@moodle.com> 38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 39 */ 40 class cohort extends base { 41 42 /** 43 * Database tables that this entity uses and their default aliases 44 * 45 * @return array 46 */ 47 protected function get_default_table_aliases(): array { 48 return [ 49 'cohort' => 'c', 50 'context' => 'chctx', 51 ]; 52 } 53 54 /** 55 * The default title for this entity 56 * 57 * @return lang_string 58 */ 59 protected function get_default_entity_title(): lang_string { 60 return new lang_string('cohort', 'core_cohort'); 61 } 62 63 /** 64 * Initialise the entity 65 * 66 * @return base 67 */ 68 public function initialise(): base { 69 $columns = $this->get_all_columns(); 70 foreach ($columns as $column) { 71 $this->add_column($column); 72 } 73 74 // All the filters defined by the entity can also be used as conditions. 75 $filters = $this->get_all_filters(); 76 foreach ($filters as $filter) { 77 $this 78 ->add_filter($filter) 79 ->add_condition($filter); 80 } 81 82 return $this; 83 } 84 85 /** 86 * Returns list of all available columns 87 * 88 * @return column[] 89 */ 90 protected function get_all_columns(): array { 91 global $DB; 92 93 $tablealias = $this->get_table_alias('cohort'); 94 $contextalias = $this->get_table_alias('context'); 95 96 // Category/context column. 97 $columns[] = (new column( 98 'context', 99 new lang_string('category'), 100 $this->get_entity_name() 101 )) 102 ->add_joins($this->get_joins()) 103 ->add_join("JOIN {context} {$contextalias} ON {$contextalias}.id = {$tablealias}.contextid") 104 ->set_type(column::TYPE_TEXT) 105 ->add_fields("{$tablealias}.contextid, " . context_helper::get_preload_record_columns_sql($contextalias)) 106 ->set_is_sortable(true) 107 ->add_callback(static function($contextid, stdClass $cohort): string { 108 context_helper::preload_from_record($cohort); 109 return context::instance_by_id($cohort->contextid)->get_context_name(false); 110 }); 111 112 // Name column. 113 $columns[] = (new column( 114 'name', 115 new lang_string('name', 'core_cohort'), 116 $this->get_entity_name() 117 )) 118 ->add_joins($this->get_joins()) 119 ->set_type(column::TYPE_TEXT) 120 ->add_fields("{$tablealias}.name") 121 ->set_is_sortable(true); 122 123 // ID number column. 124 $columns[] = (new column( 125 'idnumber', 126 new lang_string('idnumber', 'core_cohort'), 127 $this->get_entity_name() 128 )) 129 ->add_joins($this->get_joins()) 130 ->set_type(column::TYPE_TEXT) 131 ->add_fields("{$tablealias}.idnumber") 132 ->set_is_sortable(true); 133 134 // Description column. 135 $descriptionfieldsql = "{$tablealias}.description"; 136 if ($DB->get_dbfamily() === 'oracle') { 137 $descriptionfieldsql = $DB->sql_order_by_text($descriptionfieldsql, 1024); 138 } 139 $columns[] = (new column( 140 'description', 141 new lang_string('description'), 142 $this->get_entity_name() 143 )) 144 ->add_joins($this->get_joins()) 145 ->add_join("JOIN {context} {$contextalias} ON {$contextalias}.id = {$tablealias}.contextid") 146 ->set_type(column::TYPE_LONGTEXT) 147 ->add_field($descriptionfieldsql, 'description') 148 ->add_fields("{$tablealias}.descriptionformat, {$tablealias}.id, {$tablealias}.contextid") 149 ->add_fields(context_helper::get_preload_record_columns_sql($contextalias)) 150 ->add_callback(static function(?string $description, stdClass $cohort): string { 151 global $CFG; 152 require_once("{$CFG->libdir}/filelib.php"); 153 154 if ($description === null) { 155 return ''; 156 } 157 158 context_helper::preload_from_record($cohort); 159 $context = context::instance_by_id($cohort->contextid); 160 161 $description = file_rewrite_pluginfile_urls($description, 'pluginfile.php', $context->id, 'cohort', 162 'description', $cohort->id); 163 164 return format_text($description, $cohort->descriptionformat, ['context' => $context->id]); 165 }); 166 167 // Visible column. 168 $columns[] = (new column( 169 'visible', 170 new lang_string('visible', 'core_cohort'), 171 $this->get_entity_name() 172 )) 173 ->add_joins($this->get_joins()) 174 ->set_type(column::TYPE_BOOLEAN) 175 ->add_fields("{$tablealias}.visible") 176 ->set_is_sortable(true) 177 ->set_callback([format::class, 'boolean_as_text']); 178 179 // Time created column. 180 $columns[] = (new column( 181 'timecreated', 182 new lang_string('timecreated', 'core_reportbuilder'), 183 $this->get_entity_name() 184 )) 185 ->add_joins($this->get_joins()) 186 ->set_type(column::TYPE_TIMESTAMP) 187 ->add_fields("{$tablealias}.timecreated") 188 ->set_is_sortable(true) 189 ->set_callback([format::class, 'userdate']); 190 191 // Time modified column. 192 $columns[] = (new column( 193 'timemodified', 194 new lang_string('timemodified', 'core_reportbuilder'), 195 $this->get_entity_name() 196 )) 197 ->add_joins($this->get_joins()) 198 ->set_type(column::TYPE_TIMESTAMP) 199 ->add_fields("{$tablealias}.timemodified") 200 ->set_is_sortable(true) 201 ->set_callback([format::class, 'userdate']); 202 203 // Component column. 204 $columns[] = (new column( 205 'component', 206 new lang_string('component', 'core_cohort'), 207 $this->get_entity_name() 208 )) 209 ->add_joins($this->get_joins()) 210 ->set_type(column::TYPE_TEXT) 211 ->add_fields("{$tablealias}.component") 212 ->set_is_sortable(true) 213 ->add_callback(static function(string $component): string { 214 return empty($component) 215 ? get_string('nocomponent', 'cohort') 216 : get_string('pluginname', $component); 217 }); 218 219 // Theme column. 220 $columns[] = (new column( 221 'theme', 222 new lang_string('theme'), 223 $this->get_entity_name() 224 )) 225 ->add_joins($this->get_joins()) 226 ->set_type(column::TYPE_TEXT) 227 ->add_fields("{$tablealias}.theme") 228 ->set_is_sortable(true); 229 230 return $columns; 231 } 232 233 /** 234 * Return list of all available filters 235 * 236 * @return filter[] 237 */ 238 protected function get_all_filters(): array { 239 global $DB; 240 241 $tablealias = $this->get_table_alias('cohort'); 242 243 // Context filter. 244 $filters[] = (new filter( 245 select::class, 246 'context', 247 new lang_string('category'), 248 $this->get_entity_name(), 249 "{$tablealias}.contextid" 250 )) 251 ->add_joins($this->get_joins()) 252 ->set_options_callback(static function(): array { 253 global $DB; 254 255 // Load all contexts in which there are cohorts. 256 $ctxfields = context_helper::get_preload_record_columns_sql('ctx'); 257 $contexts = $DB->get_records_sql(" 258 SELECT DISTINCT {$ctxfields}, c.contextid 259 FROM {context} ctx 260 JOIN {cohort} c ON c.contextid = ctx.id"); 261 262 // Transform context record into it's name (used as the filter options). 263 return array_map(static function(stdClass $contextrecord): string { 264 context_helper::preload_from_record($contextrecord); 265 266 return context::instance_by_id($contextrecord->contextid) 267 ->get_context_name(false); 268 }, $contexts); 269 }); 270 271 // Name filter. 272 $filters[] = (new filter( 273 text::class, 274 'name', 275 new lang_string('name', 'core_cohort'), 276 $this->get_entity_name(), 277 "{$tablealias}.name" 278 )) 279 ->add_joins($this->get_joins()); 280 281 // ID number filter. 282 $filters[] = (new filter( 283 text::class, 284 'idnumber', 285 new lang_string('idnumber', 'core_cohort'), 286 $this->get_entity_name(), 287 "{$tablealias}.idnumber" 288 )) 289 ->add_joins($this->get_joins()); 290 291 // Time created filter. 292 $filters[] = (new filter( 293 date::class, 294 'timecreated', 295 new lang_string('timecreated', 'core_reportbuilder'), 296 $this->get_entity_name(), 297 "{$tablealias}.timecreated" 298 )) 299 ->add_joins($this->get_joins()); 300 301 // Description filter. 302 $filters[] = (new filter( 303 text::class, 304 'description', 305 new lang_string('description'), 306 $this->get_entity_name(), 307 $DB->sql_cast_to_char("{$tablealias}.description") 308 )) 309 ->add_joins($this->get_joins()); 310 311 return $filters; 312 } 313 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body