Differences Between: [Versions 401 and 403] [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_comment\reportbuilder\local\entities; 20 21 use context; 22 use context_helper; 23 use html_writer; 24 use lang_string; 25 use stdClass; 26 use core_reportbuilder\local\entities\base; 27 use core_reportbuilder\local\filters\{date, text}; 28 use core_reportbuilder\local\helpers\format; 29 use core_reportbuilder\local\report\{column, filter}; 30 31 /** 32 * Comment entity 33 * 34 * @package core_comment 35 * @copyright 2022 Paul Holden <paulh@moodle.com> 36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 37 */ 38 class comment extends base { 39 40 /** 41 * Database tables that this entity uses and their default aliases 42 * 43 * @return array 44 */ 45 protected function get_default_table_aliases(): array { 46 return [ 47 'comments' => 'c', 48 'context' => 'cmctx', 49 ]; 50 } 51 52 /** 53 * The default title for this entity 54 * 55 * @return lang_string 56 */ 57 protected function get_default_entity_title(): lang_string { 58 return new lang_string('comment', 'core_comment'); 59 } 60 61 /** 62 * Initialise the entity 63 * 64 * @return base 65 */ 66 public function initialise(): base { 67 $columns = $this->get_all_columns(); 68 foreach ($columns as $column) { 69 $this->add_column($column); 70 } 71 72 // All the filters defined by the entity can also be used as conditions. 73 $filters = $this->get_all_filters(); 74 foreach ($filters as $filter) { 75 $this 76 ->add_filter($filter) 77 ->add_condition($filter); 78 } 79 80 return $this; 81 } 82 83 /** 84 * Returns list of all available columns 85 * 86 * @return column[] 87 */ 88 protected function get_all_columns(): array { 89 global $DB; 90 91 $commentalias = $this->get_table_alias('comments'); 92 $contextalias = $this->get_table_alias('context'); 93 94 // Content. 95 $contentfieldsql = "{$commentalias}.content"; 96 if ($DB->get_dbfamily() === 'oracle') { 97 $contentfieldsql = $DB->sql_order_by_text($contentfieldsql, 1024); 98 } 99 $columns[] = (new column( 100 'content', 101 new lang_string('content'), 102 $this->get_entity_name() 103 )) 104 ->add_joins($this->get_joins()) 105 ->set_type(column::TYPE_LONGTEXT) 106 ->add_join($this->get_context_join()) 107 ->add_field($contentfieldsql, 'content') 108 ->add_fields("{$commentalias}.format, {$commentalias}.contextid, " . 109 context_helper::get_preload_record_columns_sql($contextalias)) 110 ->add_callback(static function($content, stdClass $comment): string { 111 if ($content === null) { 112 return ''; 113 } 114 115 context_helper::preload_from_record($comment); 116 $context = context::instance_by_id($comment->contextid); 117 118 return format_text($content, $comment->format, ['context' => $context]); 119 }); 120 121 // Context. 122 $columns[] = (new column( 123 'context', 124 new lang_string('context'), 125 $this->get_entity_name() 126 )) 127 ->add_joins($this->get_joins()) 128 ->set_type(column::TYPE_TEXT) 129 ->add_join($this->get_context_join()) 130 ->add_fields("{$commentalias}.contextid, " . context_helper::get_preload_record_columns_sql($contextalias)) 131 // Sorting may not order alphabetically, but will at least group contexts together. 132 ->set_is_sortable(true) 133 ->set_is_deprecated('See \'context:name\' for replacement') 134 ->add_callback(static function($contextid, stdClass $context): string { 135 if ($contextid === null) { 136 return ''; 137 } 138 139 context_helper::preload_from_record($context); 140 return context::instance_by_id($contextid)->get_context_name(); 141 }); 142 143 // Context URL. 144 $columns[] = (new column( 145 'contexturl', 146 new lang_string('contexturl'), 147 $this->get_entity_name() 148 )) 149 ->add_joins($this->get_joins()) 150 ->set_type(column::TYPE_TEXT) 151 ->add_join($this->get_context_join()) 152 ->add_fields("{$commentalias}.contextid, " . context_helper::get_preload_record_columns_sql($contextalias)) 153 // Sorting may not order alphabetically, but will at least group contexts together. 154 ->set_is_sortable(true) 155 ->set_is_deprecated('See \'context:link\' for replacement') 156 ->add_callback(static function($contextid, stdClass $context): string { 157 if ($contextid === null) { 158 return ''; 159 } 160 161 context_helper::preload_from_record($context); 162 $context = context::instance_by_id($contextid); 163 164 return html_writer::link($context->get_url(), $context->get_context_name()); 165 }); 166 167 // Component. 168 $columns[] = (new column( 169 'component', 170 new lang_string('plugin'), 171 $this->get_entity_name() 172 )) 173 ->add_joins($this->get_joins()) 174 ->set_type(column::TYPE_TEXT) 175 ->add_fields("{$commentalias}.component") 176 ->set_is_sortable(true); 177 178 // Area. 179 $columns[] = (new column( 180 'area', 181 new lang_string('pluginarea'), 182 $this->get_entity_name() 183 )) 184 ->add_joins($this->get_joins()) 185 ->set_type(column::TYPE_TEXT) 186 ->add_fields("{$commentalias}.commentarea") 187 ->set_is_sortable(true); 188 189 // Item ID. 190 $columns[] = (new column( 191 'itemid', 192 new lang_string('pluginitemid'), 193 $this->get_entity_name() 194 )) 195 ->add_joins($this->get_joins()) 196 ->set_type(column::TYPE_INTEGER) 197 ->add_fields("{$commentalias}.itemid") 198 ->set_is_sortable(true) 199 ->set_disabled_aggregation_all(); 200 201 // Time created. 202 $columns[] = (new column( 203 'timecreated', 204 new lang_string('timecreated', 'core_reportbuilder'), 205 $this->get_entity_name() 206 )) 207 ->add_joins($this->get_joins()) 208 ->set_type(column::TYPE_TIMESTAMP) 209 ->add_fields("{$commentalias}.timecreated") 210 ->set_is_sortable(true) 211 ->add_callback([format::class, 'userdate']); 212 213 return $columns; 214 } 215 216 /** 217 * Return list of all available filters 218 * 219 * @return filter[] 220 */ 221 protected function get_all_filters(): array { 222 global $DB; 223 224 $commentalias = $this->get_table_alias('comments'); 225 226 // Content. 227 $filters[] = (new filter( 228 text::class, 229 'content', 230 new lang_string('content'), 231 $this->get_entity_name(), 232 $DB->sql_cast_to_char("{$commentalias}.content") 233 )) 234 ->add_joins($this->get_joins()); 235 236 // Time created. 237 $filters[] = (new filter( 238 date::class, 239 'timecreated', 240 new lang_string('timecreated', 'core_reportbuilder'), 241 $this->get_entity_name(), 242 "{$commentalias}.timecreated" 243 )) 244 ->add_joins($this->get_joins()) 245 ->set_limited_operators([ 246 date::DATE_ANY, 247 date::DATE_RANGE, 248 date::DATE_LAST, 249 date::DATE_CURRENT, 250 ]); 251 252 return $filters; 253 } 254 255 /** 256 * Return syntax for joining on the context table 257 * 258 * @return string 259 */ 260 public function get_context_join(): string { 261 $commentalias = $this->get_table_alias('comments'); 262 $contextalias = $this->get_table_alias('context'); 263 264 return "LEFT JOIN {context} {$contextalias} ON {$contextalias}.id = {$commentalias}.contextid"; 265 } 266 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body