Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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   * Abstract class for loading records from the DB.
  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\factories\entity as entity_factory;
  30  use moodle_database;
  31  
  32  /**
  33   * Abstract class for loading records from the DB.
  34   *
  35   * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  abstract class db_table_vault {
  39      /** @var moodle_database $db A moodle database */
  40      private $db;
  41      /** @var entity_factory $entityfactory Entity factory */
  42      private $entityfactory;
  43      /** @var object $legacyfactory Entity->legacy factory */
  44      private $legacyfactory;
  45  
  46      /**
  47       * Constructor.
  48       *
  49       * @param moodle_database $db A moodle database
  50       * @param entity_factory $entityfactory Entity factory
  51       * @param object $legacyfactory Legacy factory
  52       */
  53      public function __construct(
  54          moodle_database $db,
  55          entity_factory $entityfactory,
  56          $legacyfactory
  57      ) {
  58          $this->db = $db;
  59          $this->entityfactory = $entityfactory;
  60          $this->legacyfactory = $legacyfactory;
  61      }
  62  
  63      /**
  64       * Get the table alias.
  65       *
  66       * @return string
  67       */
  68      abstract protected function get_table_alias() : string;
  69  
  70      /**
  71       * Build the SQL to be used in get_records_sql.
  72       *
  73       * @param string|null $wheresql Where conditions for the SQL
  74       * @param string|null $sortsql Order by conditions for the SQL
  75       * @param object|null $user The user object
  76       * @return string
  77       */
  78      abstract protected function generate_get_records_sql(string $wheresql = null, string $sortsql = null,
  79          ?int $userid = null) : string;
  80  
  81      /**
  82       * Convert the DB records into entities. The list of records will have been
  83       * passed through any preprocessors that may be defined before being given to
  84       * this function.
  85       *
  86       * Each result will from the list will be an associative array where the key
  87       * is set to the identifier given to the preprocessor and the result is the value
  88       * generated by the preprocessor.
  89       *
  90       * All results will have a 'record' key who's value is the original DB record.
  91       *
  92       * @param array $results The DB records
  93       */
  94      abstract protected function from_db_records(array $results);
  95  
  96      /**
  97       * Get the list of preprocessors to run on the DB record results. The preprocessors
  98       * should be defined using an associative array. The key used to identify the
  99       * preprocessor in this list will be used to identify the value of that preprocessor
 100       * in the list of results when passed to the from_db_records function.
 101       *
 102       * @return array
 103       */
 104      protected function get_preprocessors() : array {
 105          return [];
 106      }
 107  
 108      /**
 109       * Get the moodle database.
 110       *
 111       * @return moodle_database
 112       */
 113      protected function get_db() : moodle_database {
 114          return $this->db;
 115      }
 116  
 117      /**
 118       * Get the entity factory.
 119       *
 120       * @return entity_factory
 121       */
 122      protected function get_entity_factory() : entity_factory {
 123          return $this->entityfactory;
 124      }
 125  
 126      /**
 127       * Get the legacy factory
 128       *
 129       * @return object
 130       */
 131      protected function get_legacy_factory() {
 132          return $this->legacyfactory;
 133      }
 134  
 135      /**
 136       * Execute the defined preprocessors on the DB record results and then convert
 137       * them into entities.
 138       *
 139       * @param stdClass[] $records List of DB results
 140       * @return array
 141       */
 142      protected function transform_db_records_to_entities(array $records) {
 143          $preprocessors = $this->get_preprocessors();
 144          $result = array_map(function($record) {
 145              return ['record' => $record];
 146          }, $records);
 147  
 148          $result = array_reduce(array_keys($preprocessors), function($carry, $preprocessor) use ($records, $preprocessors) {
 149              $step = $preprocessors[$preprocessor];
 150              $dependencies = $step->execute($records);
 151  
 152              foreach ($dependencies as $index => $dependency) {
 153                  // Add the new dependency to the list.
 154                  $carry[$index] = array_merge($carry[$index], [$preprocessor => $dependency]);
 155              }
 156  
 157              return $carry;
 158          }, $result);
 159  
 160          return $this->from_db_records($result);
 161      }
 162  
 163      /**
 164       * Get the entity for the given id.
 165       *
 166       * @param int $id Identifier for the entity
 167       * @return object|null
 168       */
 169      public function get_from_id(int $id) {
 170          $records = $this->get_from_ids([$id]);
 171          return count($records) ? array_shift($records) : null;
 172      }
 173  
 174      /**
 175       * Get the list of entities for the given ids.
 176       *
 177       * @param int[] $ids Identifiers
 178       * @return array
 179       */
 180      public function get_from_ids(array $ids) {
 181          $alias = $this->get_table_alias();
 182          list($insql, $params) = $this->get_db()->get_in_or_equal($ids);
 183          $wheresql = $alias . '.id ' . $insql;
 184          $sql = $this->generate_get_records_sql($wheresql);
 185          $records = $this->get_db()->get_records_sql($sql, $params);
 186  
 187          return $this->transform_db_records_to_entities($records);
 188      }
 189  }