Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are 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  /**
  18   * Forum vault class.
  19   *
  20   * @package    mod_forum
  21   * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace mod_forum\local\vaults;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use mod_forum\local\entities\forum as forum_entity;
  30  use mod_forum\local\vaults\preprocessors\extract_context as extract_context_preprocessor;
  31  use mod_forum\local\vaults\preprocessors\extract_record as extract_record_preprocessor;
  32  use core\dml\table as dml_table;
  33  use context_helper;
  34  
  35  /**
  36   * Forum vault class.
  37   *
  38   * This should be the only place that accessed the database.
  39   *
  40   * This uses the repository pattern. See:
  41   * https://designpatternsphp.readthedocs.io/en/latest/More/Repository/README.html
  42   *
  43   * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
  44   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  45   */
  46  class forum extends db_table_vault {
  47      /** The table for this vault */
  48      private const TABLE = 'forum';
  49  
  50      /**
  51       * Get the table alias.
  52       *
  53       * @return string
  54       */
  55      protected function get_table_alias() : string {
  56          return 'f';
  57      }
  58  
  59      /**
  60       * Build the SQL to be used in get_records_sql.
  61       *
  62       * @param string|null $wheresql Where conditions for the SQL
  63       * @param string|null $sortsql Order by conditions for the SQL
  64       * @param int|null $userid The user ID
  65       * @return string
  66       */
  67      protected function generate_get_records_sql(string $wheresql = null, string $sortsql = null, ?int $userid = null) : string {
  68          $db = $this->get_db();
  69          $alias = $this->get_table_alias();
  70  
  71          $thistable = new dml_table(self::TABLE, $alias, $alias);
  72          $cmtable = new dml_table('course_modules', 'cm', 'cm_');
  73          $coursetable = new dml_table('course', 'c', 'c_');
  74  
  75          $fields = implode(', ', [
  76              $thistable->get_field_select(),
  77              context_helper::get_preload_record_columns_sql('ctx'),
  78              $cmtable->get_field_select(),
  79              $coursetable->get_field_select(),
  80          ]);
  81  
  82          $tables = $thistable->get_from_sql();
  83          $tables .= " JOIN {modules} m ON m.name = 'forum'";
  84          $tables .= " JOIN " . $cmtable->get_from_sql() . " ON cm.module = m.id AND cm.instance = {$alias}.id";
  85          $tables .= ' JOIN {context} ctx ON ctx.contextlevel = ' . CONTEXT_MODULE .  ' AND ctx.instanceid = cm.id';
  86          $tables .= " JOIN " . $coursetable->get_from_sql() . " ON c.id = {$alias}.course";
  87  
  88          $selectsql = 'SELECT ' . $fields . ' FROM ' . $tables;
  89          $selectsql .= $wheresql ? ' WHERE ' . $wheresql : '';
  90          $selectsql .= $sortsql ? ' ORDER BY ' . $sortsql : '';
  91  
  92          return $selectsql;
  93      }
  94  
  95      /**
  96       * Get a list of preprocessors to execute on the DB results before being converted
  97       * into entities.
  98       *
  99       * @return array
 100       */
 101      protected function get_preprocessors() : array {
 102          return array_merge(
 103              parent::get_preprocessors(),
 104              [
 105                  'forum' => new extract_record_preprocessor(self::TABLE, $this->get_table_alias()),
 106                  'course_module' => new extract_record_preprocessor('course_modules', 'cm_'),
 107                  'course' => new extract_record_preprocessor('course', 'c_'),
 108                  'context' => new extract_context_preprocessor(),
 109              ]
 110          );
 111      }
 112  
 113      /**
 114       * Convert the DB records into forum entities.
 115       *
 116       * @param array $results The DB records
 117       * @return forum_entity[]
 118       */
 119      protected function from_db_records(array $results) : array {
 120          $entityfactory = $this->get_entity_factory();
 121  
 122          return array_map(function(array $result) use ($entityfactory) {
 123              [
 124                  'forum' => $forumrecord,
 125                  'course_module' => $coursemodule,
 126                  'course' => $course,
 127                  'context' => $context,
 128              ] = $result;
 129              return $entityfactory->get_forum_from_stdclass($forumrecord, $context, $coursemodule, $course);
 130          }, $results);
 131      }
 132  
 133      /**
 134       * Get the forum for the given course module id.
 135       *
 136       * @param int $id The course module id
 137       * @return forum_entity|null
 138       */
 139      public function get_from_course_module_id(int $id) : ?forum_entity {
 140          $records = $this->get_from_course_module_ids([$id]);
 141          return count($records) ? array_shift($records) : null;
 142      }
 143  
 144      /**
 145       * Get the forums for the given course module ids
 146       *
 147       * @param int[] $ids The course module ids
 148       * @return forum_entity[]
 149       */
 150      public function get_from_course_module_ids(array $ids) : array {
 151          $alias = $this->get_table_alias();
 152          list($insql, $params) = $this->get_db()->get_in_or_equal($ids);
 153          $wheresql = 'cm.id ' . $insql;
 154          $sql = $this->generate_get_records_sql($wheresql);
 155          $records = $this->get_db()->get_records_sql($sql, $params);
 156  
 157          return $this->transform_db_records_to_entities($records);
 158      }
 159  
 160      /**
 161       * Get the forum entity for the given post id.
 162       *
 163       * @param int $id The course module id
 164       * @return forum_entity|null
 165       */
 166      public function get_from_post_id(int $id) : ?forum_entity {
 167          $alias = $this->get_table_alias();
 168          $thistable = new dml_table(self::TABLE, $alias, $alias);
 169          $coursemoduletable = new dml_table('course_modules', 'cm', 'cm_');
 170          $coursetable = new dml_table('course', 'c', 'c_');
 171  
 172          $tablefields = $thistable->get_field_select();
 173          $coursemodulefields = $coursemoduletable->get_field_select();
 174          $coursefields = $coursetable->get_field_select();
 175  
 176          $fields = implode(', ', [
 177              $tablefields,
 178              context_helper::get_preload_record_columns_sql('ctx'),
 179              $coursemodulefields,
 180              $coursefields,
 181          ]);
 182  
 183          $tables = "{forum_posts} p";
 184          $tables .= " JOIN {forum_discussions} d ON d.id = p.discussion";
 185          $tables .= ' JOIN {' . self::TABLE . "} {$alias} ON {$alias}.id = d.forum";
 186          $tables .= " JOIN {modules} m ON m.name = 'forum'";
 187          $tables .= " JOIN {course_modules} cm ON cm.module = m.id AND cm.instance = {$alias}.id";
 188          $tables .= ' JOIN {context} ctx ON ctx.contextlevel = ' . CONTEXT_MODULE .  ' AND ctx.instanceid = cm.id';
 189          $tables .= " JOIN {course} c ON c.id = {$alias}.course";
 190  
 191          $sql = "SELECT {$fields} FROM {$tables} WHERE p.id = :postid";
 192          $records = $this->get_db()->get_records_sql($sql, [
 193              'postid' => $id,
 194          ]);
 195  
 196          $records = $this->transform_db_records_to_entities($records);
 197          return count($records) ? array_shift($records) : null;
 198      }
 199  }