Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.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\mod_choice;
  18  
  19  use core\event\course_module_created;
  20  use core\event\course_module_updated;
  21  use tool_brickfield\area_base;
  22  
  23  /**
  24   * Choice option 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 option extends area_base {
  31  
  32      /**
  33       * Get table name.
  34       * @return string
  35       */
  36      public function get_tablename(): string {
  37          return 'choice_options';
  38      }
  39  
  40      /**
  41       * Get field name.
  42       * @return string
  43       */
  44      public function get_fieldname(): string {
  45          return 'text';
  46      }
  47  
  48      /**
  49       * Get table name reference.
  50       * @return string
  51       */
  52      public function get_ref_tablename(): string {
  53          return 'choice';
  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_module_updated || $event instanceof course_module_created) {
  65              if ($event->other['modulename'] === 'choice') {
  66                  return $this->find_areas(['refid' => $event->other['instanceid']]);
  67              }
  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_areas(['courseid' => $courseid]);
  81      }
  82  
  83      /**
  84       * Find recordset of areas.
  85       * @param array $params
  86       * @return \moodle_recordset
  87       * @throws \coding_exception
  88       * @throws \dml_exception
  89       */
  90      protected function find_areas(array $params = []): \moodle_recordset {
  91          global $DB;
  92  
  93          $where = [];
  94          if (!empty($params['refid'])) {
  95              $where[] = 't.id = :refid';
  96          }
  97          if (!empty($params['courseid'])) {
  98              $where[] = 'cm.course = :courseid';
  99          }
 100  
 101          // Filter against approved / non-approved course category listings.
 102          $this->filterfieldname = 'cm.course';
 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            co.id AS itemid,
 113            ' . $this->get_reftable_field_sql() . '
 114            t.id AS refid,
 115            cm.id AS cmid,
 116            cm.course AS courseid,
 117            co.'.$this->get_fieldname().' AS content
 118          FROM {choice} t
 119          JOIN {course_modules} cm ON cm.instance = t.id
 120          JOIN {modules} m ON m.id = cm.module AND m.name = :preftablename2
 121          JOIN {context} ctx ON ctx.instanceid = cm.id AND ctx.contextlevel = :pctxlevelmodule
 122          JOIN {'.$this->get_tablename().'} co ON co.choiceid = t.id ' .
 123          ($where ? 'WHERE ' . join(' AND ', $where) : '') . $this->filter . '
 124          ORDER BY t.id, co.id',
 125              ['pctxlevelmodule' => CONTEXT_MODULE,
 126                  'preftablename2' => $this->get_ref_tablename(),
 127              ] + $params);
 128  
 129          return $rs;
 130      }
 131  }