| 1 | efrain | 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 lang_string;
 | 
        
           |  |  | 24 | use stdClass;
 | 
        
           |  |  | 25 | use core_reportbuilder\local\entities\base;
 | 
        
           |  |  | 26 | use core_reportbuilder\local\filters\{date, text};
 | 
        
           |  |  | 27 | use core_reportbuilder\local\helpers\{custom_fields, format};
 | 
        
           |  |  | 28 | use core_reportbuilder\local\report\{column, filter};
 | 
        
           |  |  | 29 |   | 
        
           |  |  | 30 | /**
 | 
        
           |  |  | 31 |  * Grouping entity
 | 
        
           |  |  | 32 |  *
 | 
        
           |  |  | 33 |  * @package     core_group
 | 
        
           |  |  | 34 |  * @copyright   2022 Paul Holden <paulh@moodle.com>
 | 
        
           |  |  | 35 |  * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 36 |  */
 | 
        
           |  |  | 37 | class grouping extends base {
 | 
        
           |  |  | 38 |   | 
        
           |  |  | 39 |     /**
 | 
        
           |  |  | 40 |      * Database tables that this entity uses
 | 
        
           |  |  | 41 |      *
 | 
        
           |  |  | 42 |      * @return string[]
 | 
        
           |  |  | 43 |      */
 | 
        
           |  |  | 44 |     protected function get_default_tables(): array {
 | 
        
           |  |  | 45 |         return [
 | 
        
           |  |  | 46 |             'context',
 | 
        
           |  |  | 47 |             'groupings',
 | 
        
           |  |  | 48 |         ];
 | 
        
           |  |  | 49 |     }
 | 
        
           |  |  | 50 |   | 
        
           |  |  | 51 |     /**
 | 
        
           |  |  | 52 |      * The default title for this entity
 | 
        
           |  |  | 53 |      *
 | 
        
           |  |  | 54 |      * @return lang_string
 | 
        
           |  |  | 55 |      */
 | 
        
           |  |  | 56 |     protected function get_default_entity_title(): lang_string {
 | 
        
           |  |  | 57 |         return new lang_string('grouping', 'core_group');
 | 
        
           |  |  | 58 |     }
 | 
        
           |  |  | 59 |   | 
        
           |  |  | 60 |     /**
 | 
        
           |  |  | 61 |      * Initialise the entity
 | 
        
           |  |  | 62 |      *
 | 
        
           |  |  | 63 |      * @return base
 | 
        
           |  |  | 64 |      */
 | 
        
           |  |  | 65 |     public function initialise(): base {
 | 
        
           |  |  | 66 |         $groupingsalias = $this->get_table_alias('groupings');
 | 
        
           |  |  | 67 |   | 
        
           |  |  | 68 |         $customfields = (new custom_fields(
 | 
        
           |  |  | 69 |             "{$groupingsalias}.id",
 | 
        
           |  |  | 70 |             $this->get_entity_name(),
 | 
        
           |  |  | 71 |             'core_group',
 | 
        
           |  |  | 72 |             'grouping',
 | 
        
           |  |  | 73 |         ))
 | 
        
           |  |  | 74 |             ->add_joins($this->get_joins());
 | 
        
           |  |  | 75 |   | 
        
           |  |  | 76 |         $columns = array_merge($this->get_all_columns(), $customfields->get_columns());
 | 
        
           |  |  | 77 |         foreach ($columns as $column) {
 | 
        
           |  |  | 78 |             $this->add_column($column);
 | 
        
           |  |  | 79 |         }
 | 
        
           |  |  | 80 |   | 
        
           |  |  | 81 |         // All the filters defined by the entity can also be used as conditions.
 | 
        
           |  |  | 82 |         $filters = array_merge($this->get_all_filters(), $customfields->get_filters());
 | 
        
           |  |  | 83 |         foreach ($filters as $filter) {
 | 
        
           |  |  | 84 |             $this
 | 
        
           |  |  | 85 |                 ->add_filter($filter)
 | 
        
           |  |  | 86 |                 ->add_condition($filter);
 | 
        
           |  |  | 87 |         }
 | 
        
           |  |  | 88 |   | 
        
           |  |  | 89 |         return $this;
 | 
        
           |  |  | 90 |     }
 | 
        
           |  |  | 91 |   | 
        
           |  |  | 92 |     /**
 | 
        
           |  |  | 93 |      * Returns list of all available columns
 | 
        
           |  |  | 94 |      *
 | 
        
           |  |  | 95 |      * @return column[]
 | 
        
           |  |  | 96 |      */
 | 
        
           |  |  | 97 |     protected function get_all_columns(): array {
 | 
        
           |  |  | 98 |         $contextalias = $this->get_table_alias('context');
 | 
        
           |  |  | 99 |         $groupingsalias = $this->get_table_alias('groupings');
 | 
        
           |  |  | 100 |   | 
        
           |  |  | 101 |         // Name column.
 | 
        
           |  |  | 102 |         $columns[] = (new column(
 | 
        
           |  |  | 103 |             'name',
 | 
        
           |  |  | 104 |             new lang_string('name'),
 | 
        
           |  |  | 105 |             $this->get_entity_name()
 | 
        
           |  |  | 106 |         ))
 | 
        
           |  |  | 107 |             ->add_joins($this->get_joins())
 | 
        
           |  |  | 108 |             ->set_type(column::TYPE_TEXT)
 | 
        
           |  |  | 109 |             ->add_fields("{$groupingsalias}.name, {$groupingsalias}.courseid")
 | 
        
           |  |  | 110 |             ->add_fields(context_helper::get_preload_record_columns_sql($contextalias))
 | 
        
           |  |  | 111 |             ->set_is_sortable(true)
 | 
        
           |  |  | 112 |             ->set_callback(static function($name, stdClass $grouping): string {
 | 
        
           |  |  | 113 |                 if ($name === null) {
 | 
        
           |  |  | 114 |                     return '';
 | 
        
           |  |  | 115 |                 }
 | 
        
           |  |  | 116 |   | 
        
           |  |  | 117 |                 context_helper::preload_from_record($grouping);
 | 
        
           |  |  | 118 |                 $context = context_course::instance($grouping->courseid);
 | 
        
           |  |  | 119 |   | 
        
           |  |  | 120 |                 return format_string($grouping->name, true, ['context' => $context]);
 | 
        
           |  |  | 121 |             });
 | 
        
           |  |  | 122 |   | 
        
           |  |  | 123 |         // ID number column.
 | 
        
           |  |  | 124 |         $columns[] = (new column(
 | 
        
           |  |  | 125 |             'idnumber',
 | 
        
           |  |  | 126 |             new lang_string('idnumber'),
 | 
        
           |  |  | 127 |             $this->get_entity_name()
 | 
        
           |  |  | 128 |         ))
 | 
        
           |  |  | 129 |             ->add_joins($this->get_joins())
 | 
        
           |  |  | 130 |             ->set_type(column::TYPE_TEXT)
 | 
        
           |  |  | 131 |             ->add_fields("{$groupingsalias}.idnumber")
 | 
        
           |  |  | 132 |             ->set_is_sortable(true);
 | 
        
           |  |  | 133 |   | 
        
           |  |  | 134 |         // Description column.
 | 
        
           |  |  | 135 |         $columns[] = (new column(
 | 
        
           |  |  | 136 |             'description',
 | 
        
           |  |  | 137 |             new lang_string('description'),
 | 
        
           |  |  | 138 |             $this->get_entity_name()
 | 
        
           |  |  | 139 |         ))
 | 
        
           |  |  | 140 |             ->add_joins($this->get_joins())
 | 
        
           |  |  | 141 |             ->set_type(column::TYPE_LONGTEXT)
 | 
        
           | 1441 | ariadna | 142 |             ->add_field("{$groupingsalias}.description")
 | 
        
           | 1 | efrain | 143 |             ->add_fields("{$groupingsalias}.descriptionformat, {$groupingsalias}.id, {$groupingsalias}.courseid")
 | 
        
           |  |  | 144 |             ->add_fields(context_helper::get_preload_record_columns_sql($contextalias))
 | 
        
           | 1441 | ariadna | 145 |             ->set_is_sortable(true)
 | 
        
           | 1 | efrain | 146 |             ->set_callback(static function(?string $description, stdClass $grouping): string {
 | 
        
           |  |  | 147 |                 global $CFG;
 | 
        
           |  |  | 148 |   | 
        
           |  |  | 149 |                 if ($description === null) {
 | 
        
           |  |  | 150 |                     return '';
 | 
        
           |  |  | 151 |                 }
 | 
        
           |  |  | 152 |   | 
        
           |  |  | 153 |                 require_once("{$CFG->libdir}/filelib.php");
 | 
        
           |  |  | 154 |   | 
        
           |  |  | 155 |                 context_helper::preload_from_record($grouping);
 | 
        
           |  |  | 156 |                 $context = context_course::instance($grouping->courseid);
 | 
        
           |  |  | 157 |   | 
        
           |  |  | 158 |                 $description = file_rewrite_pluginfile_urls($description, 'pluginfile.php', $context->id, 'grouping',
 | 
        
           |  |  | 159 |                     'description', $grouping->id);
 | 
        
           |  |  | 160 |   | 
        
           |  |  | 161 |                 return format_text($description, $grouping->descriptionformat, ['context' => $context]);
 | 
        
           |  |  | 162 |             });
 | 
        
           |  |  | 163 |   | 
        
           |  |  | 164 |         // Time created column.
 | 
        
           |  |  | 165 |         $columns[] = (new column(
 | 
        
           |  |  | 166 |             'timecreated',
 | 
        
           |  |  | 167 |             new lang_string('timecreated', 'core_reportbuilder'),
 | 
        
           |  |  | 168 |             $this->get_entity_name()
 | 
        
           |  |  | 169 |         ))
 | 
        
           |  |  | 170 |             ->add_joins($this->get_joins())
 | 
        
           |  |  | 171 |             ->set_type(column::TYPE_TIMESTAMP)
 | 
        
           |  |  | 172 |             ->add_fields("{$groupingsalias}.timecreated")
 | 
        
           |  |  | 173 |             ->set_is_sortable(true)
 | 
        
           |  |  | 174 |             ->set_callback([format::class, 'userdate']);
 | 
        
           |  |  | 175 |   | 
        
           |  |  | 176 |         // Time modified column.
 | 
        
           |  |  | 177 |         $columns[] = (new column(
 | 
        
           |  |  | 178 |             'timemodified',
 | 
        
           |  |  | 179 |             new lang_string('timemodified', 'core_reportbuilder'),
 | 
        
           |  |  | 180 |             $this->get_entity_name()
 | 
        
           |  |  | 181 |         ))
 | 
        
           |  |  | 182 |             ->add_joins($this->get_joins())
 | 
        
           |  |  | 183 |             ->set_type(column::TYPE_TIMESTAMP)
 | 
        
           |  |  | 184 |             ->add_fields("{$groupingsalias}.timemodified")
 | 
        
           |  |  | 185 |             ->set_is_sortable(true)
 | 
        
           |  |  | 186 |             ->set_callback([format::class, 'userdate']);
 | 
        
           |  |  | 187 |   | 
        
           |  |  | 188 |         return $columns;
 | 
        
           |  |  | 189 |     }
 | 
        
           |  |  | 190 |   | 
        
           |  |  | 191 |     /**
 | 
        
           |  |  | 192 |      * Return list of all available filters
 | 
        
           |  |  | 193 |      *
 | 
        
           |  |  | 194 |      * @return filter[]
 | 
        
           |  |  | 195 |      */
 | 
        
           |  |  | 196 |     protected function get_all_filters(): array {
 | 
        
           |  |  | 197 |         $groupingsalias = $this->get_table_alias('groupings');
 | 
        
           |  |  | 198 |   | 
        
           |  |  | 199 |         // Name filter.
 | 
        
           |  |  | 200 |         $filters[] = (new filter(
 | 
        
           |  |  | 201 |             text::class,
 | 
        
           |  |  | 202 |             'name',
 | 
        
           |  |  | 203 |             new lang_string('name'),
 | 
        
           |  |  | 204 |             $this->get_entity_name(),
 | 
        
           |  |  | 205 |             "{$groupingsalias}.name"
 | 
        
           |  |  | 206 |         ))
 | 
        
           |  |  | 207 |             ->add_joins($this->get_joins());
 | 
        
           |  |  | 208 |   | 
        
           |  |  | 209 |         // ID number filter.
 | 
        
           |  |  | 210 |         $filters[] = (new filter(
 | 
        
           |  |  | 211 |             text::class,
 | 
        
           |  |  | 212 |             'idnumber',
 | 
        
           |  |  | 213 |             new lang_string('idnumber'),
 | 
        
           |  |  | 214 |             $this->get_entity_name(),
 | 
        
           |  |  | 215 |             "{$groupingsalias}.idnumber"
 | 
        
           |  |  | 216 |         ))
 | 
        
           |  |  | 217 |             ->add_joins($this->get_joins());
 | 
        
           |  |  | 218 |   | 
        
           |  |  | 219 |         // Time created filter.
 | 
        
           |  |  | 220 |         $filters[] = (new filter(
 | 
        
           |  |  | 221 |             date::class,
 | 
        
           |  |  | 222 |             'timecreated',
 | 
        
           |  |  | 223 |             new lang_string('timecreated', 'core_reportbuilder'),
 | 
        
           |  |  | 224 |             $this->get_entity_name(),
 | 
        
           |  |  | 225 |             "{$groupingsalias}.timecreated"
 | 
        
           |  |  | 226 |         ))
 | 
        
           |  |  | 227 |             ->add_joins($this->get_joins());
 | 
        
           |  |  | 228 |   | 
        
           |  |  | 229 |         return $filters;
 | 
        
           |  |  | 230 |     }
 | 
        
           |  |  | 231 | }
 |