Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   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\local\areas\core_course;
  18  
  19  use core\event\course_category_created;
  20  use core\event\course_category_updated;
  21  use tool_brickfield\area_base;
  22  
  23  /**
  24   * Base class for all areas that represent a field from the course_categories table (such as 'intro' or 'name')
  25   *
  26   * @package    tool_brickfield
  27   * @copyright  2020 onward: Brickfield Education Labs, www.brickfield.ie
  28   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   */
  30  abstract class category_base extends area_base {
  31  
  32      /**
  33       * Find recordset of the relevant areas.
  34       * @param \core\event\base $event
  35       * @return \moodle_recordset|null
  36       * @throws \dml_exception
  37       */
  38      public function find_relevant_areas(\core\event\base $event): ?\moodle_recordset {
  39          if ($event instanceof course_category_updated || $event instanceof course_category_created) {
  40              return $this->find_fields_in_course_categories_table(['itemid' => $event->objectid]);
  41          }
  42          return null;
  43      }
  44  
  45      /**
  46       * Find recordset of the course areas.
  47       * @param int $courseid
  48       * @return \moodle_recordset|null
  49       */
  50      public function find_course_areas(int $courseid): ?\moodle_recordset {
  51          return null;
  52      }
  53  
  54      /**
  55       * Return an array of area objects that contain content at the site and system levels only. All category content is considered
  56       * system level.
  57       * @return mixed
  58       */
  59      public function find_system_areas(): ?\moodle_recordset {
  60          return $this->find_fields_in_course_categories_table();
  61      }
  62  
  63      /**
  64       * Helper method that can be used by the classes that define a field in the respective module table
  65       *
  66       * @param array $params
  67       * @return \moodle_recordset
  68       * @throws \dml_exception
  69       */
  70      protected function find_fields_in_course_categories_table(array $params = []): \moodle_recordset {
  71          global $DB;
  72          $where = [];
  73          if (!empty($params['itemid'])) {
  74              $where[] = 't.id = :itemid';
  75          }
  76  
  77          $rs = $DB->get_recordset_sql('SELECT
  78            ' . $this->get_type() . ' AS type,
  79            ctx.id AS contextid,
  80            ' . $this->get_standard_area_fields_sql() . '
  81            t.id AS itemid,
  82            t.id AS categoryid,
  83            t.'.$this->get_fieldname().' AS content
  84          FROM {'.$this->get_tablename().'} t
  85          JOIN {context} ctx ON ctx.instanceid = t.id AND ctx.contextlevel = :pctxlevelcategory '.
  86              ($where ? 'WHERE ' . join(' AND ', $where) : '') . '
  87          ORDER BY t.id',
  88              ['pctxlevelcategory' => CONTEXT_COURSECAT] + $params);
  89  
  90          return $rs;
  91      }
  92  }