Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403]

   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   * Reader helper trait.
  19   *
  20   * @package    tool_log
  21   * @copyright  2014 onwards Ankit Agarwal
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace tool_log\helper;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Reader helper trait.
  31   * \tool_log\helper\store must be included before using this trait.
  32   *
  33   * @package    tool_log
  34   * @copyright  2014 onwards Ankit Agarwal
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   *
  37   * @property string $component Frankenstyle plugin name initialised in store trait.
  38   * @property string $store short plugin name initialised in store trait.
  39   */
  40  trait reader {
  41  
  42      /** @var string Frankenstyle plugin name initialised in store trait. */
  43      protected $component;
  44  
  45      /** @var string short plugin name initialised in store trait. */
  46      protected $store;
  47  
  48      /**
  49       * Default get name api.
  50       *
  51       * @return string name of the store.
  52       */
  53      public function get_name() {
  54          if (get_string_manager()->string_exists('pluginname', $this->component)) {
  55              return get_string('pluginname', $this->component);
  56          }
  57          return $this->store;
  58      }
  59  
  60      /**
  61       * Default get description method.
  62       *
  63       * @return string description of the store.
  64       */
  65      public function get_description() {
  66          if (get_string_manager()->string_exists('pluginname_desc', $this->component)) {
  67              return get_string('pluginname_desc', $this->component);
  68          }
  69          return $this->store;
  70      }
  71  
  72      /**
  73       * Function decodes the other field into an array using either PHP serialisation or JSON.
  74       *
  75       * Note that this does not rely on the config setting, it supports both formats, so you can
  76       * use it for data before/after making a change to the config setting.
  77       *
  78       * The return value is usually an array but it can also be null or a boolean or something.
  79       *
  80       * @param string $other Other value
  81       * @return mixed Decoded value
  82       */
  83      public static function decode_other(?string $other) {
  84          if ($other === 'N;' || preg_match('~^.:~', $other ?? '')) {
  85              return unserialize($other, ['allowed_classes' => [stdClass::class]]);
  86          } else {
  87              return json_decode($other ?? '', true);
  88          }
  89      }
  90  
  91      /**
  92       * Adds ID column to $sort to make sure events from one request
  93       * within 1 second are returned in the same order.
  94       *
  95       * @param string $sort
  96       * @return string sort string
  97       */
  98      protected static function tweak_sort_by_id($sort) {
  99          if (empty($sort)) {
 100              // Mysql does this - unlikely to be used in real life because $sort is always expected.
 101              $sort = "id ASC";
 102          } else if (stripos($sort, 'timecreated') === false) {
 103              $sort .= ", id ASC";
 104          } else if (stripos($sort, 'timecreated DESC') !== false) {
 105              $sort .= ", id DESC";
 106          } else {
 107              $sort .= ", id ASC";
 108          }
 109  
 110          return $sort;
 111      }
 112  }