Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.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   * Backup implementation for the (tool_log) logstore_database nested element.
  19   *
  20   * @package    logstore_database
  21   * @category   backup
  22   * @copyright  2015 Damyon Wiese <damyon@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * Custom subclass of backup_nested_element that iterates over an external DB connection.
  30   *
  31   * @package    logstore_database
  32   * @category   backup
  33   * @copyright  2015 Damyon Wiese <damyon@moodle.com>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class backup_logstore_database_nested_element extends backup_nested_element {
  37  
  38      /**
  39       * @var \moodle_database $sourcedb
  40       */
  41      protected $sourcedb;
  42  
  43      /**
  44       * Constructor - instantiates one backup_nested_element, specifying its basic info.
  45       *
  46       * @param string $name name of the element
  47       * @param array  $attributes attributes this element will handle (optional, defaults to null)
  48       * @param array  $finalelements this element will handle (optional, defaults to null)
  49       */
  50      public function __construct($name, $attributes = null, $finalelements = null) {
  51          global $DB;
  52  
  53          parent::__construct($name, $attributes, $finalelements);
  54          $this->sourcedb = $DB;
  55      }
  56  
  57      /**
  58       * For sql or table datasources, this will iterate over the "external" DB connection
  59       * stored in this class instead of the default $DB. All other cases use the parent default.
  60       * @param object $processor the processor
  61       */
  62      protected function get_iterator($processor) {
  63          if ($this->get_source_table() !== null) { // It's one table, return recordset iterator.
  64              return $this->get_source_db()->get_recordset(
  65                  $this->get_source_table(),
  66                  backup_structure_dbops::convert_params_to_values($this->procparams, $processor),
  67                  $this->get_source_table_sortby()
  68              );
  69  
  70          } else if ($this->get_source_sql() !== null) { // It's one sql, return recordset iterator.
  71              return $this->get_source_db()->get_recordset_sql(
  72                  $this->get_source_sql(),
  73                  backup_structure_dbops::convert_params_to_values($this->procparams, $processor)
  74              );
  75          }
  76  
  77          return parent::get_iterator($processor);
  78      }
  79  
  80      /**
  81       * Set the database we want to use.
  82       *
  83       * @param \moodle_database $db
  84       */
  85      public function set_source_db($db) {
  86          $this->sourcedb = $db;
  87      }
  88  
  89      /**
  90       * Get the database we want to use.
  91       *
  92       * @return \moodle_database $db
  93       */
  94      public function get_source_db() {
  95          return $this->sourcedb;
  96      }
  97  
  98  }