Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [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  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      /**
  90       * Return the name for the specified checkgroup value, or 'unknown' if no valid name for the value. Preferably, use this rather
  91       * than direct access to CHECKGROUP_NAMES, since it checks value boundaries.
  92       * @param int $checkgroupvalue
  93       * @return string
  94       */
  95      final public static function checkgroup_name(int $checkgroupvalue): string {
  96          if (($checkgroupvalue < 0) || ($checkgroupvalue >= count(self::CHECKGROUP_NAMES))) {
  97              return self::CHECKGROUP_NAMES[self::CHECKGROUP_UNKNOWN];
  98          } else {
  99              return self::CHECKGROUP_NAMES[$checkgroupvalue];
 100          }
 101      }
 102  
 103      /**
 104       * Return the value for the specified checkgroup name, or the unknown value if no valid value for the name.
 105       * @param string $checkgroupname
 106       * @return int
 107       */
 108      final public static function checkgroup_value(string $checkgroupname): int {
 109          $value = array_search($checkgroupname, self::CHECKGROUP_NAMES);
 110          return ($value !== false) ? $value : self::CHECKGROUP_UNKNOWN;
 111      }
 112  
 113      /**
 114       * Return the defined content type.
 115       * @return int
 116       */
 117      protected function get_type(): int {
 118          return self::TYPE_FIELD;
 119      }
 120  
 121      /**
 122       * Return the component from the full class name.
 123       * @return mixed|string
 124       */
 125      public function get_component(): string {
 126          $parts = preg_split('|\\\\|', get_class($this));
 127          return $parts[3];
 128      }
 129  
 130      /**
 131       * Check if the system plugin is avaliable.
 132       * @return bool
 133       */
 134      public function is_available(): bool {
 135          list($type, $plugin) = \core_component::normalize_component($this->get_component());
 136          if ($type === 'core') {
 137              // We assume that all core components are defined corretly.
 138              return true;
 139          }
 140          // Some contrib plugins may not be installed.
 141          return ($dir = \core_component::get_component_directory($this->get_component()))
 142              && file_exists($dir . '/version.php');
 143      }
 144  
 145      /**
 146       * Return the name of the database table where information is stored
 147       * @return string
 148       */
 149      abstract public function get_tablename(): string;
 150  
 151      /**
 152       * Return the name of the reference data table name.
 153       * @return string
 154       */
 155      public function get_ref_tablename(): string {
 156          return '';
 157      }
 158  
 159      /**
 160       * Return the name of the field in the table that has the content
 161       * @return string
 162       */
 163      abstract public function get_fieldname(): string;
 164  
 165      /**
 166       * Return a recordset of the relevant areas for the component/module.
 167       * @param \core\event\base $event
 168       * @return \moodle_recordset|null
 169       */
 170      abstract public function find_relevant_areas(\core\event\base $event): ?\moodle_recordset;
 171  
 172      /**
 173       * Return a recordset of the course areas for the course id.
 174       * @param int $courseid
 175       * @return \moodle_recordset|null
 176       */
 177      abstract public function find_course_areas(int $courseid): ?\moodle_recordset;
 178  
 179      /**
 180       * Return an array of area objects that contain content at the site and system levels only. Override this where necessary.
 181       * @return \moodle_recordset|null
 182       */
 183      public function find_system_areas(): ?\moodle_recordset {
 184          return null;
 185      }
 186  
 187      /**
 188       * The standard Moodle parameter DML parameter substitution doesn't work on all versions of MySQL or Postgres, so we need to use
 189       * inline function substitution to ensure that the left side is a string.
 190       * @return string
 191       */
 192      public function get_standard_area_fields_sql(): string {
 193          return '\'' . $this->get_component() . '\' AS component,
 194              \'' . $this->get_tablename() . '\' AS tablename,
 195              \'' . $this->get_fieldname() . '\' AS fieldorarea, ';
 196      }
 197  
 198      /**
 199       * The standard Moodle parameter DML parameter substitution doesn't work on all versions of MySQL or Postgres, so we need to use
 200       * inline function substitution to ensure that the left side is a string.
 201       * @return string
 202       */
 203      public function get_reftable_field_sql(): string {
 204          return '\'' . $this->get_ref_tablename() . '\' AS reftable, ';
 205      }
 206  
 207      /**
 208       * Processes any sql filtering data. Implement in extensions.
 209       *
 210       * @return null
 211       * @throws \coding_exception
 212       * @throws \dml_exception
 213       */
 214      public function get_courseid_filtering() {
 215          $this->filter = '';
 216          $this->filterparams = [];
 217          return null;
 218      }
 219  }