Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

   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  /**
  18   * Report table class.
  19   *
  20   * @package    report_configlog
  21   * @copyright  2019 Paul Holden (pholden@greenhead.ac.uk)
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace report_configlog\output;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  require_once($CFG->libdir . '/searchlib.php');
  30  require_once($CFG->libdir . '/tablelib.php');
  31  
  32  /**
  33   * Report table class.
  34   *
  35   * @package    report_configlog
  36   * @copyright  2019 Paul Holden (pholden@greenhead.ac.uk)
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class report_table extends \table_sql implements \renderable {
  40  
  41      /** @var string $search */
  42      protected $search;
  43  
  44      /**
  45       * Constructor
  46       *
  47       * @param string $search
  48       */
  49      public function __construct(string $search) {
  50          parent::__construct('report-configlog-report-table');
  51  
  52          $this->search = trim($search);
  53  
  54          // Define columns.
  55          $columns = [
  56              'timemodified' => get_string('timemodified', 'report_configlog'),
  57              'fullname'     => get_string('name'),
  58              'plugin'       => get_string('plugin', 'report_configlog'),
  59              'name'         => get_string('setting', 'report_configlog'),
  60              'value'        => get_string('valuenew', 'report_configlog'),
  61              'oldvalue'     => get_string('valueold', 'report_configlog'),
  62          ];
  63          $this->define_columns(array_keys($columns));
  64          $this->define_headers(array_values($columns));
  65  
  66          // Table configuration.
  67          $this->set_attribute('id', $this->uniqueid);
  68          $this->set_attribute('cellspacing', '0');
  69  
  70          $this->sortable(true, 'timemodified', SORT_DESC);
  71  
  72          $this->initialbars(false);
  73          $this->collapsible(false);
  74  
  75          $this->useridfield = 'userid';
  76  
  77          // Initialize table SQL properties.
  78          $this->init_sql();
  79      }
  80  
  81      /**
  82       * Initializes table SQL properties
  83       *
  84       * @return void
  85       */
  86      protected function init_sql() {
  87          global $DB;
  88  
  89          $userfieldsapi = \core_user\fields::for_name();
  90          $userfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
  91          $fields = 'cl.id, cl.timemodified, cl.plugin, cl.name, cl.value, cl.oldvalue, cl.userid, ' . $userfields;
  92  
  93          $from = '{config_log} cl
  94              LEFT JOIN {user} u ON u.id = cl.userid';
  95  
  96          // Report search.
  97          $where = '1=1';
  98          $params = [];
  99  
 100          if (!empty($this->search)) {
 101              // Clean quotes, allow search by 'setting:' prefix.
 102              $searchstring = str_replace(["\\\"", 'setting:'], ["\"", 'subject:'], $this->search);
 103  
 104              $parser = new \search_parser();
 105              $lexer = new \search_lexer($parser);
 106  
 107              if ($lexer->parse($searchstring)) {
 108                  $parsearray = $parser->get_parsed_array();
 109  
 110                  // Data fields should contain both value/oldvalue.
 111                  $datafields = $DB->sql_concat_join("':'", ['cl.value', 'cl.oldvalue']);
 112  
 113                  list($where, $params) = search_generate_SQL($parsearray, $datafields, 'cl.name', 'cl.userid', 'u.id',
 114                      'u.firstname', 'u.lastname', 'cl.timemodified', 'cl.id');
 115              }
 116          }
 117  
 118          $this->set_sql($fields, $from, $where, $params);
 119          $this->set_count_sql('SELECT COUNT(1) FROM ' . $from . ' WHERE ' . $where, $params);
 120      }
 121  
 122      /**
 123       * Cross DB text-compatible sorting for value/oldvalue fields
 124       *
 125       * @return string
 126       */
 127      public function get_sql_sort() {
 128          global $DB;
 129  
 130          $sort = preg_replace_callback('/\b(value|oldvalue)\b/', function(array $matches) use ($DB) {
 131              return $DB->sql_order_by_text($matches[1], 255);
 132          }, parent::get_sql_sort());
 133  
 134          return $sort;
 135      }
 136  
 137      /**
 138       * Format report timemodified field
 139       *
 140       * @param stdClass $row
 141       * @return string
 142       */
 143      public function col_timemodified(\stdClass $row) {
 144          return userdate($row->timemodified);
 145      }
 146  
 147      /**
 148       * Format fullname field
 149       *
 150       * @param stdClass $row
 151       * @return string
 152       */
 153      public function col_fullname($row) {
 154  
 155          $userid = $row->{$this->useridfield};
 156          if (empty($userid)) {
 157              // If the user id is empty it must have been set via the
 158              // admin/cli/cfg.php script or during the initial install.
 159              return get_string('usernone', 'report_configlog');
 160          } else {
 161              return parent::col_fullname($row);
 162          }
 163      }
 164  
 165      /**
 166       * Format report plugin field
 167       *
 168       * @param stdClass $row
 169       * @return string
 170       */
 171      public function col_plugin(\stdClass $row) {
 172          return $row->plugin ?? 'core';
 173      }
 174  
 175      /**
 176       * Format report value field
 177       *
 178       * @param stdClass $row
 179       * @return string
 180       */
 181      public function col_value(\stdClass $row) {
 182          return $this->format_text($row->value, FORMAT_PLAIN);
 183      }
 184  
 185      /**
 186       * Format report old value field
 187       *
 188       * @param stdClass $row
 189       * @return string
 190       */
 191      public function col_oldvalue(\stdClass $row) {
 192          return $this->format_text($row->oldvalue, FORMAT_PLAIN);;
 193      }
 194  }