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 the Query submission plugin
   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_created;
  20  use core\event\course_updated;
  21  use core\event\course_restored;
  22  use tool_brickfield\area_base;
  23  
  24  /**
  25   * Base class for various course-related areas
  26   *
  27   * This is an abstract class so it will be skipped by manager when it finds all areas
  28   *
  29   * @package    tool_brickfield
  30   * @copyright  2020 onward: Brickfield Education Labs, www.brickfield.ie
  31   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  abstract class base extends area_base {
  34  
  35      /**
  36       * Find recordset of the relevant areas.
  37       * @param \core\event\base $event
  38       * @return \moodle_recordset|null
  39       * @throws \coding_exception
  40       * @throws \dml_exception
  41       */
  42      public function find_relevant_areas(\core\event\base $event): ?\moodle_recordset {
  43          if ($event instanceof course_created) {
  44              return $this->find_fields_in_course_table(['courseid' => $event->courseid]);
  45          } else if ($event instanceof course_restored) {
  46              return $this->find_fields_in_course_table(['courseid' => $event->courseid]);
  47          } else if ($event instanceof course_updated) {
  48              return $this->find_fields_in_course_table(['courseid' => $event->courseid]);
  49          }
  50          return null;
  51      }
  52  
  53      /**
  54       * Find recordset of the course areas.
  55       * @param int $courseid
  56       * @return \moodle_recordset
  57       * @throws \coding_exception
  58       * @throws \dml_exception
  59       */
  60      public function find_course_areas(int $courseid): ?\moodle_recordset {
  61          return $this->find_fields_in_course_table(['courseid' => $courseid]);
  62      }
  63  
  64      /**
  65       * Return an array of area objects that contain content at the site and system levels only.
  66       * @return mixed
  67       */
  68      public function find_system_areas(): ?\moodle_recordset {
  69          return null;
  70      }
  71  
  72      /**
  73       * Helper method that can be used by the classes that define a field in the 'course' table
  74       *
  75       * @param array $params
  76       * @return \moodle_recordset
  77       * @throws \coding_exception
  78       * @throws \dml_exception
  79       */
  80      protected function find_fields_in_course_table(array $params = []): \moodle_recordset {
  81          global $DB;
  82          $where = [];
  83  
  84          if (!empty($params['courseid'])) {
  85              $where[] = 't.id = :courseid';
  86          }
  87  
  88          $rs = $DB->get_recordset_sql('SELECT
  89            ' . $this->get_type() . ' AS type,
  90            ctx.id AS contextid,
  91            ' . $this->get_standard_area_fields_sql() . '
  92            t.id AS itemid,
  93            t.id AS courseid,
  94            t.'.$this->get_fieldname().' AS content
  95          FROM {'.$this->get_tablename().'} t
  96          JOIN {context} ctx ON ctx.instanceid = t.id AND ctx.contextlevel = :pctxlevelcourse '.
  97              ($where ? 'WHERE ' . join(' AND ', $where) : '') . '
  98          ORDER BY t.id',
  99              ['pctxlevelcourse' => CONTEXT_COURSE] + $params);
 100  
 101          return $rs;
 102      }
 103  
 104      /**
 105       * Returns the moodle_url of the page to edit the error.
 106       * @param \stdClass $componentinfo
 107       * @return \moodle_url
 108       */
 109      public static function get_edit_url(\stdClass $componentinfo): \moodle_url {
 110          if ($componentinfo->tablename == 'course_sections') {
 111              return new \moodle_url('/course/editsection.php', ['id' => $componentinfo->itemid, 'sr' => '']);
 112          } else if ($componentinfo->tablename == 'course_categories') {
 113              return new \moodle_url('/course/editcategory.php', ['id' => $componentinfo->itemid]);
 114          } else {
 115              return new \moodle_url('/course/edit.php', ['id' => $componentinfo->courseid]);
 116          }
 117      }
 118  }