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] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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       * Default get name api.
  43       *
  44       * @return string name of the store.
  45       */
  46      public function get_name() {
  47          if (get_string_manager()->string_exists('pluginname', $this->component)) {
  48              return get_string('pluginname', $this->component);
  49          }
  50          return $this->store;
  51      }
  52  
  53      /**
  54       * Default get description method.
  55       *
  56       * @return string description of the store.
  57       */
  58      public function get_description() {
  59          if (get_string_manager()->string_exists('pluginname_desc', $this->component)) {
  60              return get_string('pluginname_desc', $this->component);
  61          }
  62          return $this->store;
  63      }
  64  
  65      /**
  66       * Function decodes the other field into an array using either PHP serialisation or JSON.
  67       *
  68       * Note that this does not rely on the config setting, it supports both formats, so you can
  69       * use it for data before/after making a change to the config setting.
  70       *
  71       * The return value is usually an array but it can also be null or a boolean or something.
  72       *
  73       * @param string $other Other value
  74       * @return mixed Decoded value
  75       */
  76      public static function decode_other(?string $other) {
  77          if ($other === 'N;' || preg_match('~^.:~', $other)) {
  78              return unserialize($other);
  79          } else {
  80              return json_decode($other, true);
  81          }
  82      }
  83  
  84      /**
  85       * Adds ID column to $sort to make sure events from one request
  86       * within 1 second are returned in the same order.
  87       *
  88       * @param string $sort
  89       * @return string sort string
  90       */
  91      protected static function tweak_sort_by_id($sort) {
  92          if (empty($sort)) {
  93              // Mysql does this - unlikely to be used in real life because $sort is always expected.
  94              $sort = "id ASC";
  95          } else if (stripos($sort, 'timecreated') === false) {
  96              $sort .= ", id ASC";
  97          } else if (stripos($sort, 'timecreated DESC') !== false) {
  98              $sort .= ", id DESC";
  99          } else {
 100              $sort .= ", id ASC";
 101          }
 102  
 103          return $sort;
 104      }
 105  }