| 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 | 
           namespace report_configlog\reportbuilder\local\entities;
  | 
        
        
            | 
            | 
           18 | 
              | 
        
        
            | 
            | 
           19 | 
           use lang_string;
  | 
        
        
            | 
            | 
           20 | 
           use core_reportbuilder\local\entities\base;
  | 
        
        
            | 
            | 
           21 | 
           use core_reportbuilder\local\helpers\format;
  | 
        
        
            | 
            | 
           22 | 
           use core_reportbuilder\local\report\column;
  | 
        
        
            | 
            | 
           23 | 
           use core_reportbuilder\local\report\filter;
  | 
        
        
            | 
            | 
           24 | 
           use core_reportbuilder\local\filters\date;
  | 
        
        
            | 
            | 
           25 | 
           use core_reportbuilder\local\filters\text;
  | 
        
        
            | 
            | 
           26 | 
              | 
        
        
            | 
            | 
           27 | 
           /**
  | 
        
        
            | 
            | 
           28 | 
            * Config change entity class implementation
  | 
        
        
            | 
            | 
           29 | 
            *
  | 
        
        
            | 
            | 
           30 | 
            * Defines all the columns and filters that can be added to reports that use this entity.
  | 
        
        
            | 
            | 
           31 | 
            *
  | 
        
        
            | 
            | 
           32 | 
            * @package    report_configlog
  | 
        
        
            | 
            | 
           33 | 
            * @copyright  2020 Paul Holden <paulh@moodle.com>
  | 
        
        
            | 
            | 
           34 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           35 | 
            */
  | 
        
        
            | 
            | 
           36 | 
           class config_change extends base {
  | 
        
        
            | 
            | 
           37 | 
              | 
        
        
            | 
            | 
           38 | 
               /**
  | 
        
        
            | 
            | 
           39 | 
                * Database tables that this entity uses
  | 
        
        
            | 
            | 
           40 | 
                *
  | 
        
        
            | 
            | 
           41 | 
                * @return string[]
  | 
        
        
            | 
            | 
           42 | 
                */
  | 
        
        
            | 
            | 
           43 | 
               protected function get_default_tables(): array {
  | 
        
        
            | 
            | 
           44 | 
                   return [
  | 
        
        
            | 
            | 
           45 | 
                       'config_log',
  | 
        
        
            | 
            | 
           46 | 
                   ];
  | 
        
        
            | 
            | 
           47 | 
               }
  | 
        
        
            | 
            | 
           48 | 
              | 
        
        
            | 
            | 
           49 | 
               /**
  | 
        
        
            | 
            | 
           50 | 
                * The default title for this entity
  | 
        
        
            | 
            | 
           51 | 
                *
  | 
        
        
            | 
            | 
           52 | 
                * @return lang_string
  | 
        
        
            | 
            | 
           53 | 
                */
  | 
        
        
            | 
            | 
           54 | 
               protected function get_default_entity_title(): lang_string {
  | 
        
        
            | 
            | 
           55 | 
                   return new lang_string('entityconfigchange', 'report_configlog');
  | 
        
        
            | 
            | 
           56 | 
               }
  | 
        
        
            | 
            | 
           57 | 
              | 
        
        
            | 
            | 
           58 | 
               /**
  | 
        
        
            | 
            | 
           59 | 
                * Initialize the entity
  | 
        
        
            | 
            | 
           60 | 
                *
  | 
        
        
            | 
            | 
           61 | 
                * @return base
  | 
        
        
            | 
            | 
           62 | 
                */
  | 
        
        
            | 
            | 
           63 | 
               public function initialise(): base {
  | 
        
        
            | 
            | 
           64 | 
                   $columns = $this->get_all_columns();
  | 
        
        
            | 
            | 
           65 | 
                   foreach ($columns as $column) {
  | 
        
        
            | 
            | 
           66 | 
                       $this->add_column($column);
  | 
        
        
            | 
            | 
           67 | 
                   }
  | 
        
        
            | 
            | 
           68 | 
              | 
        
        
            | 
            | 
           69 | 
                   $filters = $this->get_all_filters();
  | 
        
        
            | 
            | 
           70 | 
                   foreach ($filters as $filter) {
  | 
        
        
            | 
            | 
           71 | 
                       $this->add_filter($filter);
  | 
        
        
            | 
            | 
           72 | 
                   }
  | 
        
        
            | 
            | 
           73 | 
              | 
        
        
            | 
            | 
           74 | 
                   return $this;
  | 
        
        
            | 
            | 
           75 | 
               }
  | 
        
        
            | 
            | 
           76 | 
              | 
        
        
            | 
            | 
           77 | 
               /**
  | 
        
        
            | 
            | 
           78 | 
                * Returns list of all available columns
  | 
        
        
            | 
            | 
           79 | 
                *
  | 
        
        
            | 
            | 
           80 | 
                * @return column[]
  | 
        
        
            | 
            | 
           81 | 
                */
  | 
        
        
            | 
            | 
           82 | 
               protected function get_all_columns(): array {
  | 
        
        
            | 
            | 
           83 | 
                   $tablealias = $this->get_table_alias('config_log');
  | 
        
        
            | 
            | 
           84 | 
              | 
        
        
            | 
            | 
           85 | 
                   // Time modified column.
  | 
        
        
            | 
            | 
           86 | 
                   $columns[] = (new column(
  | 
        
        
            | 
            | 
           87 | 
                       'timemodified',
  | 
        
        
            | 
            | 
           88 | 
                       new lang_string('timemodified', 'report_configlog'),
  | 
        
        
            | 
            | 
           89 | 
                       $this->get_entity_name()
  | 
        
        
            | 
            | 
           90 | 
                   ))
  | 
        
        
            | 
            | 
           91 | 
                       ->add_joins($this->get_joins())
  | 
        
        
            | 
            | 
           92 | 
                       ->set_type(column::TYPE_TIMESTAMP)
  | 
        
        
            | 
            | 
           93 | 
                       ->add_fields("{$tablealias}.timemodified")
  | 
        
        
            | 
            | 
           94 | 
                       ->set_is_sortable(true)
  | 
        
        
            | 
            | 
           95 | 
                       ->add_callback([format::class, 'userdate']);
  | 
        
        
            | 
            | 
           96 | 
              | 
        
        
            | 
            | 
           97 | 
                   // Plugin column.
  | 
        
        
            | 
            | 
           98 | 
                   $columns[] = (new column(
  | 
        
        
            | 
            | 
           99 | 
                       'plugin',
  | 
        
        
            | 
            | 
           100 | 
                       new lang_string('plugin', 'report_configlog'),
  | 
        
        
            | 
            | 
           101 | 
                       $this->get_entity_name()
  | 
        
        
            | 
            | 
           102 | 
                   ))
  | 
        
        
            | 
            | 
           103 | 
                       ->add_joins($this->get_joins())
  | 
        
        
            | 
            | 
           104 | 
                       ->set_type(column::TYPE_TEXT)
  | 
        
        
            | 
            | 
           105 | 
                       ->add_field("{$tablealias}.plugin")
  | 
        
        
            | 
            | 
           106 | 
                       ->set_is_sortable(true)
  | 
        
        
            | 
            | 
           107 | 
                       ->add_callback(static function(?string $plugin): string {
  | 
        
        
            | 
            | 
           108 | 
                           return $plugin ?? 'core';
  | 
        
        
            | 
            | 
           109 | 
                       });
  | 
        
        
            | 
            | 
           110 | 
              | 
        
        
            | 
            | 
           111 | 
                   // Setting column.
  | 
        
        
            | 
            | 
           112 | 
                   $columns[] = (new column(
  | 
        
        
            | 
            | 
           113 | 
                       'setting',
  | 
        
        
            | 
            | 
           114 | 
                       new lang_string('setting', 'report_configlog'),
  | 
        
        
            | 
            | 
           115 | 
                       $this->get_entity_name()
  | 
        
        
            | 
            | 
           116 | 
                   ))
  | 
        
        
            | 
            | 
           117 | 
                       ->add_joins($this->get_joins())
  | 
        
        
            | 
            | 
           118 | 
                       ->set_type(column::TYPE_TEXT)
  | 
        
        
            | 
            | 
           119 | 
                       ->add_field("{$tablealias}.name")
  | 
        
        
            | 
            | 
           120 | 
                       ->set_is_sortable(true);
  | 
        
        
            | 
            | 
           121 | 
              | 
        
        
            | 
            | 
           122 | 
                   // New value column.
  | 
        
        
            | 
            | 
           123 | 
                   $columns[] = (new column(
  | 
        
        
            | 
            | 
           124 | 
                       'newvalue',
  | 
        
        
            | 
            | 
           125 | 
                       new lang_string('valuenew', 'report_configlog'),
  | 
        
        
            | 
            | 
           126 | 
                       $this->get_entity_name()
  | 
        
        
            | 
            | 
           127 | 
                   ))
  | 
        
        
            | 
            | 
           128 | 
                       ->add_joins($this->get_joins())
  | 
        
        
            | 
            | 
           129 | 
                       ->set_type(column::TYPE_TEXT)
  | 
        
        
            | 
            | 
           130 | 
                       ->add_field("{$tablealias}.value")
  | 
        
        
            | 
            | 
           131 | 
                       ->set_is_sortable(true)
  | 
        
        
            | 
            | 
           132 | 
                       ->add_callback(static function(?string $value): string {
  | 
        
        
            | 
            | 
           133 | 
                           return format_text($value, FORMAT_PLAIN);
  | 
        
        
            | 
            | 
           134 | 
                       });
  | 
        
        
            | 
            | 
           135 | 
              | 
        
        
            | 
            | 
           136 | 
                   // Old value column.
  | 
        
        
            | 
            | 
           137 | 
                   $columns[] = (new column(
  | 
        
        
            | 
            | 
           138 | 
                       'oldvalue',
  | 
        
        
            | 
            | 
           139 | 
                       new lang_string('valueold', 'report_configlog'),
  | 
        
        
            | 
            | 
           140 | 
                       $this->get_entity_name()
  | 
        
        
            | 
            | 
           141 | 
                   ))
  | 
        
        
            | 
            | 
           142 | 
                       ->add_joins($this->get_joins())
  | 
        
        
            | 
            | 
           143 | 
                       ->set_type(column::TYPE_TEXT)
  | 
        
        
            | 
            | 
           144 | 
                       ->add_field("{$tablealias}.oldvalue")
  | 
        
        
            | 
            | 
           145 | 
                       ->set_is_sortable(true)
  | 
        
        
            | 
            | 
           146 | 
                       ->add_callback(static function(?string $oldvalue): string {
  | 
        
        
            | 
            | 
           147 | 
                           return format_text($oldvalue, FORMAT_PLAIN);
  | 
        
        
            | 
            | 
           148 | 
                       });
  | 
        
        
            | 
            | 
           149 | 
              | 
        
        
            | 
            | 
           150 | 
                   return $columns;
  | 
        
        
            | 
            | 
           151 | 
               }
  | 
        
        
            | 
            | 
           152 | 
              | 
        
        
            | 
            | 
           153 | 
               /**
  | 
        
        
            | 
            | 
           154 | 
                * Return list of all available filters
  | 
        
        
            | 
            | 
           155 | 
                *
  | 
        
        
            | 
            | 
           156 | 
                * @return filter[]
  | 
        
        
            | 
            | 
           157 | 
                */
  | 
        
        
            | 
            | 
           158 | 
               protected function get_all_filters(): array {
  | 
        
        
            | 
            | 
           159 | 
                   $tablealias = $this->get_table_alias('config_log');
  | 
        
        
            | 
            | 
           160 | 
              | 
        
        
            | 
            | 
           161 | 
                   // Time modified filter.
  | 
        
        
            | 
            | 
           162 | 
                   $filters[] = (new filter(
  | 
        
        
            | 
            | 
           163 | 
                       date::class,
  | 
        
        
            | 
            | 
           164 | 
                       'timemodified',
  | 
        
        
            | 
            | 
           165 | 
                       new lang_string('timemodified', 'report_configlog'),
  | 
        
        
            | 
            | 
           166 | 
                       $this->get_entity_name(),
  | 
        
        
            | 
            | 
           167 | 
                       "{$tablealias}.timemodified"
  | 
        
        
            | 
            | 
           168 | 
                   ))
  | 
        
        
            | 
            | 
           169 | 
                       ->add_joins($this->get_joins())
  | 
        
        
            | 
            | 
           170 | 
                       ->set_limited_operators([
  | 
        
        
            | 
            | 
           171 | 
                           date::DATE_ANY,
  | 
        
        
            | 
            | 
           172 | 
                           date::DATE_RANGE,
  | 
        
        
            | 
            | 
           173 | 
                           date::DATE_PREVIOUS,
  | 
        
        
            | 
            | 
           174 | 
                           date::DATE_CURRENT,
  | 
        
        
            | 
            | 
           175 | 
                       ]);
  | 
        
        
            | 
            | 
           176 | 
              | 
        
        
            | 
            | 
           177 | 
                   // Plugin filter.
  | 
        
        
            | 
            | 
           178 | 
                   $filters[] = (new filter(
  | 
        
        
            | 
            | 
           179 | 
                       text::class,
  | 
        
        
            | 
            | 
           180 | 
                       'plugin',
  | 
        
        
            | 
            | 
           181 | 
                       new lang_string('plugin', 'report_configlog'),
  | 
        
        
            | 
            | 
           182 | 
                       $this->get_entity_name(),
  | 
        
        
            | 
            | 
           183 | 
                       "COALESCE({$tablealias}.plugin, 'core')"
  | 
        
        
            | 
            | 
           184 | 
                   ))
  | 
        
        
            | 
            | 
           185 | 
                       ->add_joins($this->get_joins());
  | 
        
        
            | 
            | 
           186 | 
              | 
        
        
            | 
            | 
           187 | 
                   // Setting filter.
  | 
        
        
            | 
            | 
           188 | 
                   $filters[] = (new filter(
  | 
        
        
            | 
            | 
           189 | 
                       text::class,
  | 
        
        
            | 
            | 
           190 | 
                       'setting',
  | 
        
        
            | 
            | 
           191 | 
                       new lang_string('setting', 'report_configlog'),
  | 
        
        
            | 
            | 
           192 | 
                       $this->get_entity_name(),
  | 
        
        
            | 
            | 
           193 | 
                       "{$tablealias}.name"
  | 
        
        
            | 
            | 
           194 | 
                   ))
  | 
        
        
            | 
            | 
           195 | 
                       ->add_joins($this->get_joins());
  | 
        
        
            | 
            | 
           196 | 
              | 
        
        
            | 
            | 
           197 | 
                   // New value filter.
  | 
        
        
            | 
            | 
           198 | 
                   $filters[] = (new filter(
  | 
        
        
            | 
            | 
           199 | 
                       text::class,
  | 
        
        
            | 
            | 
           200 | 
                       'value',
  | 
        
        
            | 
            | 
           201 | 
                       new lang_string('valuenew', 'report_configlog'),
  | 
        
        
            | 
            | 
           202 | 
                       $this->get_entity_name(),
  | 
        
        
            | 
            | 
           203 | 
                       "{$tablealias}.value"
  | 
        
        
            | 
            | 
           204 | 
                   ))
  | 
        
        
            | 
            | 
           205 | 
                       ->add_joins($this->get_joins());
  | 
        
        
            | 
            | 
           206 | 
              | 
        
        
            | 
            | 
           207 | 
                   // Old value filter.
  | 
        
        
            | 
            | 
           208 | 
                   $filters[] = (new filter(
  | 
        
        
            | 
            | 
           209 | 
                       text::class,
  | 
        
        
            | 
            | 
           210 | 
                       'oldvalue',
  | 
        
        
            | 
            | 
           211 | 
                       new lang_string('valueold', 'report_configlog'),
  | 
        
        
            | 
            | 
           212 | 
                       $this->get_entity_name(),
  | 
        
        
            | 
            | 
           213 | 
                       "{$tablealias}.oldvalue"
  | 
        
        
            | 
            | 
           214 | 
                   ))
  | 
        
        
            | 
            | 
           215 | 
                       ->add_joins($this->get_joins());
  | 
        
        
            | 
            | 
           216 | 
              | 
        
        
            | 
            | 
           217 | 
                   return $filters;
  | 
        
        
            | 
            | 
           218 | 
               }
  | 
        
        
            | 
            | 
           219 | 
           }
  |