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.

Differences Between: [Versions 311 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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 tool_brickfield;
  18  
  19  /**
  20   * Area base class.
  21   *
  22   * @package    tool_brickfield
  23   * @copyright  2020 Brickfield Education Labs https://www.brickfield.ie
  24   */
  25  abstract class area_base {
  26  
  27      /**
  28       * Defines the unknown for the 'checkgroup' field in the tool_brickfield_checks table.
  29       */
  30      const CHECKGROUP_UNKNOWN = 0;
  31  
  32      /**
  33       * Defines the form for the 'checkgroup' field in the tool_brickfield_checks table.
  34       */
  35      const CHECKGROUP_FORM = 1;
  36  
  37      /**
  38       * Defines the image for the 'checkgroup' field in the tool_brickfield_checks table.
  39       */
  40      const CHECKGROUP_IMAGE = 2;
  41  
  42      /**
  43       * Defines the layout for the 'checkgroup' field in the tool_brickfield_checks table.
  44       */
  45      const CHECKGROUP_LAYOUT = 3;
  46  
  47      /**
  48       * Defines the link for the 'checkgroup' field in the tool_brickfield_checks table.
  49       */
  50      const CHECKGROUP_LINK = 4;
  51  
  52      /**
  53       * Defines the media for the 'checkgroup' field in the tool_brickfield_checks table.
  54       */
  55      const CHECKGROUP_MEDIA = 5;
  56  
  57      /**
  58       * Defines the table for the 'checkgroup' field in the tool_brickfield_checks table.
  59       */
  60      const CHECKGROUP_TABLE = 6;
  61  
  62      /**
  63       * Defines the text for the 'checkgroup' field in the tool_brickfield_checks table.
  64       */
  65      const CHECKGROUP_TEXT = 7;
  66  
  67      /** @var string[] Array for quick access of string names for 'checkgroups'. */
  68      const CHECKGROUP_NAMES = [
  69          self::CHECKGROUP_UNKNOWN => 'unknown',
  70          self::CHECKGROUP_FORM => 'form',
  71          self::CHECKGROUP_IMAGE => 'image',
  72          self::CHECKGROUP_LAYOUT => 'layout',
  73          self::CHECKGROUP_LINK => 'link',
  74          self::CHECKGROUP_MEDIA => 'media',
  75          self::CHECKGROUP_TABLE => 'table',
  76          self::CHECKGROUP_TEXT => 'text'
  77      ];
  78  
  79      /**
  80       * Defines field value.
  81       */
  82      const TYPE_FIELD = 0;
  83  
  84      /**
  85       * Defines file value.
  86       */
  87      const TYPE_FILE = 1;
  88  
  89      /**  @var string To store the filter. */
  90      protected string $filter = '';
  91  
  92      /** @var array To store the filter parameters. */
  93      protected array $filterparams = [];
  94  
  95      /** @var string To store the filter field name. */
  96      protected $filterfieldname;
  97  
  98      /**
  99       * Return the name for the specified checkgroup value, or 'unknown' if no valid name for the value. Preferably, use this rather
 100       * than direct access to CHECKGROUP_NAMES, since it checks value boundaries.
 101       * @param int $checkgroupvalue
 102       * @return string
 103       */
 104      final public static function checkgroup_name(int $checkgroupvalue): string {
 105          if (($checkgroupvalue < 0) || ($checkgroupvalue >= count(self::CHECKGROUP_NAMES))) {
 106              return self::CHECKGROUP_NAMES[self::CHECKGROUP_UNKNOWN];
 107          } else {
 108              return self::CHECKGROUP_NAMES[$checkgroupvalue];
 109          }
 110      }
 111  
 112      /**
 113       * Return the value for the specified checkgroup name, or the unknown value if no valid value for the name.
 114       * @param string $checkgroupname
 115       * @return int
 116       */
 117      final public static function checkgroup_value(string $checkgroupname): int {
 118          $value = array_search($checkgroupname, self::CHECKGROUP_NAMES);
 119          return ($value !== false) ? $value : self::CHECKGROUP_UNKNOWN;
 120      }
 121  
 122      /**
 123       * Return the defined content type.
 124       * @return int
 125       */
 126      protected function get_type(): int {
 127          return self::TYPE_FIELD;
 128      }
 129  
 130      /**
 131       * Return the component from the full class name.
 132       * @return mixed|string
 133       */
 134      public function get_component(): string {
 135          $parts = preg_split('|\\\\|', get_class($this));
 136          return $parts[3];
 137      }
 138  
 139      /**
 140       * Check if the system plugin is avaliable.
 141       * @return bool
 142       */
 143      public function is_available(): bool {
 144          list($type, $plugin) = \core_component::normalize_component($this->get_component());
 145          if ($type === 'core') {
 146              // We assume that all core components are defined corretly.
 147              return true;
 148          }
 149          // Some contrib plugins may not be installed.
 150          return ($dir = \core_component::get_component_directory($this->get_component()))
 151              && file_exists($dir . '/version.php');
 152      }
 153  
 154      /**
 155       * Return the name of the database table where information is stored
 156       * @return string
 157       */
 158      abstract public function get_tablename(): string;
 159  
 160      /**
 161       * Return the name of the reference data table name.
 162       * @return string
 163       */
 164      public function get_ref_tablename(): string {
 165          return '';
 166      }
 167  
 168      /**
 169       * Return the name of the field in the table that has the content
 170       * @return string
 171       */
 172      abstract public function get_fieldname(): string;
 173  
 174      /**
 175       * Return a recordset of the relevant areas for the component/module.
 176       * @param \core\event\base $event
 177       * @return \moodle_recordset|null
 178       */
 179      abstract public function find_relevant_areas(\core\event\base $event): ?\moodle_recordset;
 180  
 181      /**
 182       * Return a recordset of the course areas for the course id.
 183       * @param int $courseid
 184       * @return \moodle_recordset|null
 185       */
 186      abstract public function find_course_areas(int $courseid): ?\moodle_recordset;
 187  
 188      /**
 189       * Return an array of area objects that contain content at the site and system levels only. Override this where necessary.
 190       * @return \moodle_recordset|null
 191       */
 192      public function find_system_areas(): ?\moodle_recordset {
 193          return null;
 194      }
 195  
 196      /**
 197       * The standard Moodle parameter DML parameter substitution doesn't work on all versions of MySQL or Postgres, so we need to use
 198       * inline function substitution to ensure that the left side is a string.
 199       * @return string
 200       */
 201      public function get_standard_area_fields_sql(): string {
 202          return '\'' . $this->get_component() . '\' AS component,
 203              \'' . $this->get_tablename() . '\' AS tablename,
 204              \'' . $this->get_fieldname() . '\' AS fieldorarea, ';
 205      }
 206  
 207      /**
 208       * The standard Moodle parameter DML parameter substitution doesn't work on all versions of MySQL or Postgres, so we need to use
 209       * inline function substitution to ensure that the left side is a string.
 210       * @return string
 211       */
 212      public function get_reftable_field_sql(): string {
 213          return '\'' . $this->get_ref_tablename() . '\' AS reftable, ';
 214      }
 215  
 216      /**
 217       * Processes any sql filtering data. Implement in extensions.
 218       *
 219       * @return null
 220       * @throws \coding_exception
 221       * @throws \dml_exception
 222       */
 223      public function get_courseid_filtering() {
 224          $this->filter = '';
 225          $this->filterparams = [];
 226          return null;
 227      }
 228  }