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 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_book;
  18  
  19  use tool_brickfield\area_base;
  20  
  21  /**
  22   * Book chapter content observer.
  23   *
  24   * @package    tool_brickfield
  25   * @copyright  2020 onward: Brickfield Education Labs, www.brickfield.ie
  26   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  class chaptercontent extends area_base {
  29  
  30      /**
  31       * Get table name.
  32       * @return string
  33       */
  34      public function get_tablename(): string {
  35          return 'book_chapters';
  36      }
  37  
  38      /**
  39       * Get field name.
  40       * @return string
  41       */
  42      public function get_fieldname(): string {
  43          return 'content';
  44      }
  45  
  46      /**
  47       * Get table name reference.
  48       * @return string
  49       */
  50      public function get_ref_tablename(): string {
  51          return 'book';
  52      }
  53  
  54      /**
  55       * Find recordset of the relevant areas.
  56       * @param \core\event\base $event
  57       * @return \moodle_recordset|null
  58       * @throws \coding_exception
  59       * @throws \dml_exception
  60       */
  61      public function find_relevant_areas(\core\event\base $event): ?\moodle_recordset {
  62          if ($event->eventname == '\mod_book\event\chapter_updated' || $event->eventname == '\mod_book\event\chapter_created') {
  63              return $this->find_areas(['contextid' => $event->contextid, 'objectid' => $event->objectid]);
  64          }
  65          return null;
  66      }
  67  
  68      /**
  69       * Find recordset of the course areas.
  70       * @param int $courseid
  71       * @return \moodle_recordset
  72       * @throws \coding_exception
  73       * @throws \dml_exception
  74       */
  75      public function find_course_areas(int $courseid): ?\moodle_recordset {
  76          return $this->find_areas(['courseid' => $courseid]);
  77      }
  78  
  79      /**
  80       * Find recordset of areas.
  81       * @param array $params
  82       * @return \moodle_recordset
  83       * @throws \coding_exception
  84       * @throws \dml_exception
  85       */
  86      protected function find_areas(array $params = []): \moodle_recordset {
  87          global $DB;
  88  
  89          $where = [];
  90          if (!empty($params['contextid'])) {
  91              $where[] = 'ctx.id = :contextid';
  92              $where[] = 'co.id = :objectid';
  93          }
  94          if (!empty($params['courseid'])) {
  95              $where[] = 'cm.course = :courseid';
  96          }
  97  
  98          // Filter against approved / non-approved course category listings.
  99          $this->filterfieldname = 'cm.course';
 100          $this->get_courseid_filtering();
 101          if ($this->filter != '') {
 102              $params = $params + $this->filterparams;
 103          }
 104  
 105          $rs = $DB->get_recordset_sql('SELECT
 106            ' . $this->get_type() . ' AS type,
 107            ctx.id AS contextid,
 108            ' . $this->get_standard_area_fields_sql() . '
 109            co.id AS itemid,
 110            ' . $this->get_reftable_field_sql() . '
 111            t.id AS refid,
 112            cm.id AS cmid,
 113            cm.course AS courseid,
 114            co.'.$this->get_fieldname().' AS content
 115          FROM {book} t
 116          JOIN {course_modules} cm ON cm.instance = t.id
 117          JOIN {modules} m ON m.id = cm.module AND m.name = :preftablename2
 118          JOIN {context} ctx ON ctx.instanceid = cm.id AND ctx.contextlevel = :pctxlevelmodule
 119          JOIN {'.$this->get_tablename().'} co ON co.bookid = t.id ' .
 120          ($where ? 'WHERE ' . join(' AND ', $where) : '') . $this->filter . '
 121          ORDER BY t.id, co.id',
 122              ['pctxlevelmodule' => CONTEXT_MODULE,
 123                  'preftablename2' => $this->get_ref_tablename(),
 124              ] + $params);
 125  
 126          return $rs;
 127      }
 128  }