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\systemreports;
  18  
  19  use context_system;
  20  use report_configlog\reportbuilder\local\entities\config_change;
  21  use core_reportbuilder\system_report;
  22  use core_reportbuilder\local\entities\user;
  23  use stdClass;
  24  
  25  /**
  26   * Config changes system report class implementation
  27   *
  28   * @package    report_configlog
  29   * @copyright  2020 Paul Holden <paulh@moodle.com>
  30   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  class config_changes extends system_report {
  33  
  34      /**
  35       * Initialise report, we need to set the main table, load our entities and set columns/filters
  36       */
  37      protected function initialise(): void {
  38          // Our main entity, it contains all of the column definitions that we need.
  39          $entitymain = new config_change();
  40          $entitymainalias = $entitymain->get_table_alias('config_log');
  41  
  42          $this->set_main_table('config_log', $entitymainalias);
  43          $this->add_entity($entitymain);
  44  
  45          // We can join the "user" entity to our "main" entity using standard SQL JOIN.
  46          $entityuser = new user();
  47          $entityuseralias = $entityuser->get_table_alias('user');
  48          $this->add_entity($entityuser
  49              ->add_join("LEFT JOIN {user} {$entityuseralias} ON {$entityuseralias}.id = {$entitymainalias}.userid")
  50          );
  51  
  52          // Now we can call our helper methods to add the content we want to include in the report.
  53          $this->add_columns();
  54          $this->add_filters();
  55  
  56          // Set if report can be downloaded.
  57          $this->set_downloadable(true, get_string('pluginname', 'report_configlog'));
  58      }
  59  
  60      /**
  61       * Validates access to view this report
  62       *
  63       * @return bool
  64       */
  65      protected function can_view(): bool {
  66          return has_capability('moodle/site:config', context_system::instance());
  67      }
  68  
  69      /**
  70       * Adds the columns we want to display in the report
  71       *
  72       * They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their
  73       * unique identifier
  74       */
  75      protected function add_columns(): void {
  76          $columns = [
  77              'config_change:timemodified',
  78              'user:fullnamewithlink',
  79              'config_change:plugin',
  80              'config_change:setting',
  81              'config_change:newvalue',
  82              'config_change:oldvalue',
  83          ];
  84  
  85          $this->add_columns_from_entities($columns);
  86  
  87          // Default sorting.
  88          $this->set_initial_sort_column('config_change:timemodified', SORT_DESC);
  89  
  90          // Custom callback to show 'CLI or install' in fullname column when there is no user.
  91          if ($column = $this->get_column('user:fullnamewithlink')) {
  92              $column->add_callback(static function(string $fullname, stdClass $row): string {
  93                  return $fullname ?: get_string('usernone', 'report_configlog');
  94              });
  95          }
  96      }
  97  
  98      /**
  99       * Adds the filters we want to display in the report
 100       *
 101       * They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their
 102       * unique identifier
 103       */
 104      protected function add_filters(): void {
 105          $filters = [
 106              'config_change:setting',
 107              'config_change:value',
 108              'config_change:oldvalue',
 109              'user:fullname',
 110              'config_change:timemodified',
 111          ];
 112  
 113          $this->add_filters_from_entities($filters);
 114      }
 115  }