Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 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          $userfields = get_all_user_name_fields(true, 'u');
  90          $fields = 'cl.id, cl.timemodified, cl.plugin, cl.name, cl.value, cl.oldvalue, cl.userid, ' . $userfields;
  91  
  92          $from = '{config_log} cl
  93              LEFT JOIN {user} u ON u.id = cl.userid';
  94  
  95          // Report search.
  96          $where = '1=1';
  97          $params = [];
  98  
  99          if (!empty($this->search)) {
 100              // Clean quotes, allow search by 'setting:' prefix.
 101              $searchstring = str_replace(["\\\"", 'setting:'], ["\"", 'subject:'], $this->search);
 102  
 103              $parser = new \search_parser();
 104              $lexer = new \search_lexer($parser);
 105  
 106              if ($lexer->parse($searchstring)) {
 107                  $parsearray = $parser->get_parsed_array();
 108  
 109                  // Data fields should contain both value/oldvalue.
 110                  $datafields = $DB->sql_concat_join("':'", ['cl.value', 'cl.oldvalue']);
 111  
 112                  list($where, $params) = search_generate_SQL($parsearray, $datafields, 'cl.name', 'cl.userid', 'u.id',
 113                      'u.firstname', 'u.lastname', 'cl.timemodified', 'cl.id');
 114              }
 115          }
 116  
 117          $this->set_sql($fields, $from, $where, $params);
 118          $this->set_count_sql('SELECT COUNT(1) FROM ' . $from . ' WHERE ' . $where, $params);
 119      }
 120  
 121      /**
 122       * Cross DB text-compatible sorting for value/oldvalue fields
 123       *
 124       * @return string
 125       */
 126      public function get_sql_sort() {
 127          global $DB;
 128  
 129          $sort = preg_replace_callback('/\b(value|oldvalue)\b/', function(array $matches) use ($DB) {
 130              return $DB->sql_order_by_text($matches[1], 255);
 131          }, parent::get_sql_sort());
 132  
 133          return $sort;
 134      }
 135  
 136      /**
 137       * Format report timemodified field
 138       *
 139       * @param stdClass $row
 140       * @return string
 141       */
 142      public function col_timemodified(\stdClass $row) {
 143          return userdate($row->timemodified);
 144      }
 145  
 146      /**
 147       * Format fullname field
 148       *
 149       * @param stdClass $row
 150       * @return string
 151       */
 152      public function col_fullname($row) {
 153  
 154          $userid = $row->{$this->useridfield};
 155          if (empty($userid)) {
 156              // If the user id is empty it must have been set via the
 157              // admin/cli/cfg.php script or during the initial install.
 158              return get_string('usernone', 'report_configlog');
 159          } else {
 160              return parent::col_fullname($row);
 161          }
 162      }
 163  
 164      /**
 165       * Format report plugin field
 166       *
 167       * @param stdClass $row
 168       * @return string
 169       */
 170      public function col_plugin(\stdClass $row) {
 171          return $row->plugin ?? 'core';
 172      }
 173  
 174      /**
 175       * Format report value field
 176       *
 177       * @param stdClass $row
 178       * @return string
 179       */
 180      public function col_value(\stdClass $row) {
 181          return $this->format_text($row->value, FORMAT_PLAIN);
 182      }
 183  
 184      /**
 185       * Format report old value field
 186       *
 187       * @param stdClass $row
 188       * @return string
 189       */
 190      public function col_oldvalue(\stdClass $row) {
 191          return $this->format_text($row->oldvalue, FORMAT_PLAIN);;
 192      }
 193  }