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  /**
  18   * Helpers and methods relating to DML tables.
  19   *
  20   * @since      Moodle 3.7
  21   * @package    core
  22   * @category   dml
  23   * @copyright  2019 Andrew Nicols <andrew@nicols.co.uk>
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  namespace core\dml;
  28  
  29  use stdClass;
  30  
  31  defined('MOODLE_INTERNAL') || die();
  32  
  33  /**
  34   * Helpers and methods relating to DML tables.
  35   *
  36   * @copyright  2019 Andrew Nicols <andrew@nicols.co.uk>
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class table {
  40  
  41      /** @var string Name of the table that this class represents */
  42      protected $tablename;
  43  
  44      /** @var string Table alias */
  45      protected $tablealias;
  46  
  47      /** @var string Prefix to place before each field */
  48      protected $fieldprefix;
  49  
  50      /** @var array List of fields */
  51      protected $fields;
  52  
  53      /**
  54       * Constructor for the table class.
  55       *
  56       * @param   string  $tablename The name of the table that this instance represents.
  57       * @param   string  $tablealias The alias to use when selecting the table
  58       * @param   string  $fieldprefix The prefix to use when selecting fields.
  59       */
  60      public function __construct(string $tablename, string $tablealias, string $fieldprefix) {
  61          $this->tablename = $tablename;
  62          $this->tablealias = $tablealias;
  63          $this->fieldprefix = $fieldprefix;
  64      }
  65  
  66      /**
  67       * Get the from TABLE ALIAS part of the FROM/JOIN string.
  68       *
  69       * @return  string
  70       */
  71      public function get_from_sql() : string {
  72          return "{{$this->tablename}} {$this->tablealias}";
  73      }
  74  
  75      /**
  76       * Get the list of fields in a table for use in preloading fields.
  77       *
  78       * @return  array       The list of columns in a table. The array key is the column name with an applied prefix.
  79       */
  80      protected function get_fieldlist() : array {
  81          global $DB;
  82  
  83          if (null === $this->fields) {
  84              $fields = [];
  85              foreach (array_keys($DB->get_columns($this->tablename)) as $fieldname) {
  86                  $fields["{$this->fieldprefix}{$fieldname}"] = $fieldname;
  87              }
  88  
  89              $this->fields = $fields;
  90          }
  91  
  92          return $this->fields;
  93      }
  94  
  95      /**
  96       * Get the SELECT SQL to select a set of columns for this table.
  97       *
  98       * This function is intended to be used in combination with extract_from_result().
  99       *
 100       * @return  string      The SQL to use in the SELECT
 101       */
 102      public function get_field_select() : string {
 103          $fieldlist = $this->get_fieldlist();
 104  
 105          return implode(', ', array_map(function($fieldname, $fieldalias) {
 106              return "{$this->tablealias}.{$fieldname} AS {$fieldalias}";
 107          }, $fieldlist, array_keys($fieldlist)));
 108      }
 109  
 110      /**
 111       * Extract fields from the specified result. The fields are removed from the original object.
 112       *
 113       * This function is intended to be used in combination with get_field_select().
 114       *
 115       * @param   stdClass    $result The result retrieved from the database with fields to be extracted
 116       * @return  stdClass    The extracted result
 117       */
 118      public function extract_from_result(stdClass $result) : stdClass {
 119          $record = new stdClass();
 120  
 121          $fieldlist = $this->get_fieldlist();
 122          foreach ($fieldlist as $fieldalias => $fieldname) {
 123              if (property_exists($result, $fieldalias)) {
 124                  $record->$fieldname = $result->$fieldalias;
 125                  unset($result->$fieldalias);
 126              } else {
 127                  debugging("Field '{$fieldname}' not found", DEBUG_DEVELOPER);
 128              }
 129          }
 130  
 131          return $record;
 132      }
 133  }