See Release Notes
Long Term Support Release
Differences Between: [Versions 401 and 402] [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_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("LEFT JOIN {context} {$contextalias} ON {$contextalias}.id = {$commentalias}.contextid") 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("LEFT JOIN {context} {$contextalias} ON {$contextalias}.id = {$commentalias}.contextid") 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 ->add_callback(static function($contextid, stdClass $context): string { 134 if ($contextid === null) { 135 return ''; 136 } 137 138 context_helper::preload_from_record($context); 139 return context::instance_by_id($contextid)->get_context_name(); 140 }); 141 142 // Context URL. 143 $columns[] = (new column( 144 'contexturl', 145 new lang_string('contexturl'), 146 $this->get_entity_name() 147 )) 148 ->add_joins($this->get_joins()) 149 ->set_type(column::TYPE_TEXT) 150 ->add_join("LEFT JOIN {context} {$contextalias} ON {$contextalias}.id = {$commentalias}.contextid") 151 ->add_fields("{$commentalias}.contextid, " . context_helper::get_preload_record_columns_sql($contextalias)) 152 // Sorting may not order alphabetically, but will at least group contexts together. 153 ->set_is_sortable(true) 154 ->add_callback(static function($contextid, stdClass $context): string { 155 if ($contextid === null) { 156 return ''; 157 } 158 159 context_helper::preload_from_record($context); 160 $context = context::instance_by_id($contextid); 161 162 return html_writer::link($context->get_url(), $context->get_context_name()); 163 }); 164 165 // Component. 166 $columns[] = (new column( 167 'component', 168 new lang_string('plugin'), 169 $this->get_entity_name() 170 )) 171 ->add_joins($this->get_joins()) 172 ->set_type(column::TYPE_TEXT) 173 ->add_fields("{$commentalias}.component"); 174 175 // Area. 176 $columns[] = (new column( 177 'area', 178 new lang_string('pluginarea'), 179 $this->get_entity_name() 180 )) 181 ->add_joins($this->get_joins()) 182 ->set_type(column::TYPE_TEXT) 183 ->add_fields("{$commentalias}.commentarea"); 184 185 // Item ID. 186 $columns[] = (new column( 187 'itemid', 188 new lang_string('pluginitemid'), 189 $this->get_entity_name() 190 )) 191 ->add_joins($this->get_joins()) 192 ->set_type(column::TYPE_INTEGER) 193 ->add_fields("{$commentalias}.itemid") 194 ->set_disabled_aggregation_all(); 195 196 // Time created. 197 $columns[] = (new column( 198 'timecreated', 199 new lang_string('timecreated', 'core_reportbuilder'), 200 $this->get_entity_name() 201 )) 202 ->add_joins($this->get_joins()) 203 ->set_type(column::TYPE_TIMESTAMP) 204 ->add_fields("{$commentalias}.timecreated") 205 ->add_callback([format::class, 'userdate']); 206 207 return $columns; 208 } 209 210 /** 211 * Return list of all available filters 212 * 213 * @return filter[] 214 */ 215 protected function get_all_filters(): array { 216 global $DB; 217 218 $commentalias = $this->get_table_alias('comments'); 219 220 // Content. 221 $filters[] = (new filter( 222 text::class, 223 'content', 224 new lang_string('content'), 225 $this->get_entity_name(), 226 $DB->sql_cast_to_char("{$commentalias}.content") 227 )) 228 ->add_joins($this->get_joins()); 229 230 // Time created. 231 $filters[] = (new filter( 232 date::class, 233 'timecreated', 234 new lang_string('timecreated', 'core_reportbuilder'), 235 $this->get_entity_name(), 236 "{$commentalias}.timecreated" 237 )) 238 ->add_joins($this->get_joins()) 239 ->set_limited_operators([ 240 date::DATE_ANY, 241 date::DATE_RANGE, 242 date::DATE_LAST, 243 date::DATE_CURRENT, 244 ]); 245 246 return $filters; 247 } 248 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body