Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   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 and their default aliases
  40       *
  41       * @return array
  42       */
  43      protected function get_default_table_aliases(): array {
  44          return ['config_log' => 'cl'];
  45      }
  46  
  47      /**
  48       * The default title for this entity
  49       *
  50       * @return lang_string
  51       */
  52      protected function get_default_entity_title(): lang_string {
  53          return new lang_string('entityconfigchange', 'report_configlog');
  54      }
  55  
  56      /**
  57       * Initialize the entity
  58       *
  59       * @return base
  60       */
  61      public function initialise(): base {
  62          $columns = $this->get_all_columns();
  63          foreach ($columns as $column) {
  64              $this->add_column($column);
  65          }
  66  
  67          $filters = $this->get_all_filters();
  68          foreach ($filters as $filter) {
  69              $this->add_filter($filter);
  70          }
  71  
  72          return $this;
  73      }
  74  
  75      /**
  76       * Returns list of all available columns
  77       *
  78       * @return column[]
  79       */
  80      protected function get_all_columns(): array {
  81          $tablealias = $this->get_table_alias('config_log');
  82  
  83          // Time modified column.
  84          $columns[] = (new column(
  85              'timemodified',
  86              new lang_string('timemodified', 'report_configlog'),
  87              $this->get_entity_name()
  88          ))
  89              ->add_joins($this->get_joins())
  90              ->set_type(column::TYPE_TIMESTAMP)
  91              ->add_fields("{$tablealias}.timemodified")
  92              ->set_is_sortable(true)
  93              ->add_callback([format::class, 'userdate']);
  94  
  95          // Plugin column.
  96          $columns[] = (new column(
  97              'plugin',
  98              new lang_string('plugin', 'report_configlog'),
  99              $this->get_entity_name()
 100          ))
 101              ->add_joins($this->get_joins())
 102              ->set_type(column::TYPE_TEXT)
 103              ->add_field("{$tablealias}.plugin")
 104              ->set_is_sortable(true)
 105              ->add_callback(static function(?string $plugin): string {
 106                  return $plugin ?? 'core';
 107              });
 108  
 109          // Setting column.
 110          $columns[] = (new column(
 111              'setting',
 112              new lang_string('setting', 'report_configlog'),
 113              $this->get_entity_name()
 114          ))
 115              ->add_joins($this->get_joins())
 116              ->set_type(column::TYPE_TEXT)
 117              ->add_field("{$tablealias}.name")
 118              ->set_is_sortable(true);
 119  
 120          // New value column.
 121          $columns[] = (new column(
 122              'newvalue',
 123              new lang_string('valuenew', 'report_configlog'),
 124              $this->get_entity_name()
 125          ))
 126              ->add_joins($this->get_joins())
 127              ->set_type(column::TYPE_TEXT)
 128              ->add_field("{$tablealias}.value")
 129              ->set_is_sortable(true)
 130              ->add_callback(static function(?string $value): string {
 131                  return format_text($value, FORMAT_PLAIN);
 132              });
 133  
 134          // Old value column.
 135          $columns[] = (new column(
 136              'oldvalue',
 137              new lang_string('valueold', 'report_configlog'),
 138              $this->get_entity_name()
 139          ))
 140              ->add_joins($this->get_joins())
 141              ->set_type(column::TYPE_TEXT)
 142              ->add_field("{$tablealias}.oldvalue")
 143              ->set_is_sortable(true)
 144              ->add_callback(static function(?string $oldvalue): string {
 145                  return format_text($oldvalue, FORMAT_PLAIN);
 146              });
 147  
 148          return $columns;
 149      }
 150  
 151      /**
 152       * Return list of all available filters
 153       *
 154       * @return filter[]
 155       */
 156      protected function get_all_filters(): array {
 157          $tablealias = $this->get_table_alias('config_log');
 158  
 159          // Time modified filter.
 160          $filters[] = (new filter(
 161              date::class,
 162              'timemodified',
 163              new lang_string('timemodified', 'report_configlog'),
 164              $this->get_entity_name(),
 165              "{$tablealias}.timemodified"
 166          ))
 167              ->add_joins($this->get_joins())
 168              ->set_limited_operators([
 169                  date::DATE_ANY,
 170                  date::DATE_RANGE,
 171                  date::DATE_PREVIOUS,
 172                  date::DATE_CURRENT,
 173              ]);
 174  
 175          // Setting filter.
 176          $filters[] = (new filter(
 177              text::class,
 178              'setting',
 179              new lang_string('setting', 'report_configlog'),
 180              $this->get_entity_name(),
 181              "{$tablealias}.name"
 182          ))
 183              ->add_joins($this->get_joins());
 184  
 185          // New value filter.
 186          $filters[] = (new filter(
 187              text::class,
 188              'value',
 189              new lang_string('valuenew', 'report_configlog'),
 190              $this->get_entity_name(),
 191              "{$tablealias}.value"
 192          ))
 193              ->add_joins($this->get_joins());
 194  
 195          // Old value filter.
 196          $filters[] = (new filter(
 197              text::class,
 198              'oldvalue',
 199              new lang_string('valueold', 'report_configlog'),
 200              $this->get_entity_name(),
 201              "{$tablealias}.oldvalue"
 202          ))
 203              ->add_joins($this->get_joins());
 204  
 205          return $filters;
 206      }
 207  }