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.
   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_section_created;
  20  use core\event\course_section_updated;
  21  use tool_brickfield\area_base;
  22  
  23  /**
  24   * Course section name observer.
  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  class sectionname extends area_base {
  31  
  32      /**
  33       * Get table name.
  34       * @return string
  35       */
  36      public function get_tablename(): string {
  37          return 'course_sections';
  38      }
  39  
  40      /**
  41       * Get field name.
  42       * @return string
  43       */
  44      public function get_fieldname(): string {
  45          return 'name';
  46      }
  47  
  48      /**
  49       * Get table name reference.
  50       * @return string
  51       */
  52      public function get_ref_tablename(): string {
  53          return 'course';
  54      }
  55  
  56      /**
  57       * Find recordset of the relevant areas.
  58       * @param \core\event\base $event
  59       * @return \moodle_recordset|null
  60       * @throws \coding_exception
  61       * @throws \dml_exception
  62       */
  63      public function find_relevant_areas(\core\event\base $event): ?\moodle_recordset {
  64          if ($event instanceof course_section_created) {
  65              return $this->find_fields_in_course_sections_table(['courseid' => $event->courseid, 'sectionid' => $event->objectid]);
  66          } else if ($event instanceof course_section_updated) {
  67              return $this->find_fields_in_course_sections_table(['courseid' => $event->courseid, 'sectionid' => $event->objectid]);
  68          }
  69          return null;
  70      }
  71  
  72      /**
  73       * Find recordset of the course areas.
  74       * @param int $courseid
  75       * @return \moodle_recordset
  76       * @throws \coding_exception
  77       * @throws \dml_exception
  78       */
  79      public function find_course_areas(int $courseid): ?\moodle_recordset {
  80          return $this->find_fields_in_course_sections_table(['courseid' => $courseid]);
  81      }
  82  
  83      /**
  84       * Helper method that can be used by the classes that define a field in the 'course_sections' table
  85       *
  86       * @param array $params
  87       * @return \moodle_recordset
  88       * @throws \coding_exception
  89       * @throws \dml_exception
  90       */
  91      protected function find_fields_in_course_sections_table(array $params = []): \moodle_recordset {
  92          global $DB;
  93          $where = [];
  94          if (!empty($params['courseid'])) {
  95              $where[] = 't.course = :courseid';
  96          }
  97          if (!empty($params['sectionid'])) {
  98              $where[] = 't.id = :sectionid';
  99          }
 100  
 101          // Filter against approved / non-approved course category listings.
 102          $this->filterfieldname = 'co.id';
 103          $this->get_courseid_filtering();
 104          if ($this->filter != '') {
 105              $params = $params + $this->filterparams;
 106          }
 107  
 108          $rs = $DB->get_recordset_sql('SELECT
 109              ' . $this->get_type() . ' AS type,
 110              ctx.id AS contextid,
 111              ' . $this->get_standard_area_fields_sql() . '
 112              t.id AS itemid,
 113              ' . $this->get_reftable_field_sql() . '
 114              t.course AS refid,
 115              t.course AS courseid,
 116              t.'.$this->get_fieldname().' AS content
 117          FROM {'.$this->get_tablename().'} t
 118          JOIN {course} co ON co.id = t.course
 119          JOIN {context} ctx ON ctx.instanceid = co.id AND ctx.contextlevel = :pctxlevelcourse '.
 120              ($where ? 'WHERE ' . join(' AND ', $where) : '') . $this->filter . '
 121          ORDER BY t.course',
 122              ['pctxlevelcourse' => CONTEXT_COURSE] + $params);
 123  
 124          return $rs;
 125      }
 126  }