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   * Restore implementation for the (tool_log) logstore_database subplugin.
  19   *
  20   * @package    logstore_database
  21   * @category   backup
  22   * @copyright  2015 Mark Nelson <markn@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  class restore_logstore_database_subplugin extends restore_tool_log_logstore_subplugin {
  29  
  30      /**
  31       * @var moodle_database the external database.
  32       */
  33      private static $extdb = null;
  34  
  35      /**
  36       * @var string the external database table name.
  37       */
  38      private static $extdbtablename = null;
  39  
  40      /**
  41       * The constructor for this logstore.
  42       *
  43       * @param string $subplugintype the subplugin type.
  44       * @param string $subpluginname the subplugin name.
  45       * @param restore_structure_step $step.
  46       */
  47      public function __construct($subplugintype, $subpluginname, $step) {
  48          // Check that the logstore is enabled before setting variables.
  49          $enabledlogstores = explode(',', get_config('tool_log', 'enabled_stores'));
  50          if (in_array('logstore_database', $enabledlogstores)) {
  51              $manager = new \tool_log\log\manager();
  52              $store = new \logstore_database\log\store($manager);
  53              self::$extdb = $store->get_extdb();
  54              self::$extdbtablename = $store->get_config_value('dbtable');
  55          }
  56  
  57          parent::__construct($subplugintype, $subpluginname, $step);
  58      }
  59  
  60      /**
  61       * Returns the subplugin structure to attach to the 'logstore' XML element.
  62       *
  63       * @return restore_path_element[] array of elements to be processed on restore.
  64       */
  65      protected function define_logstore_subplugin_structure() {
  66          // If the logstore is not enabled we don't add structures for it.
  67          $enabledlogstores = explode(',', get_config('tool_log', 'enabled_stores'));
  68          if (!in_array('logstore_database', $enabledlogstores)) {
  69              return array(); // The logstore is not enabled, nothing to restore.
  70          }
  71  
  72          $paths = array();
  73  
  74          $elename = $this->get_namefor('log');
  75          $elepath = $this->get_pathfor('/logstore_database_log');
  76          $paths[] = new restore_path_element($elename, $elepath);
  77  
  78          return $paths;
  79      }
  80  
  81      /**
  82       * Process logstore_database_log entries.
  83       *
  84       * This method proceeds to read, complete, remap and, finally,
  85       * discard or save every log entry.
  86       *
  87       * @param array() $data log entry.
  88       * @return null if we are not restoring the log.
  89       */
  90      public function process_logstore_database_log($data) {
  91          // Do not bother processing if we can not add it to a database.
  92          if (!self::$extdb || !self::$extdbtablename) {
  93              return;
  94          }
  95  
  96          $data = $this->process_log($data, get_config('logstore_database', 'jsonformat'));
  97  
  98          if ($data) {
  99              self::$extdb->insert_record(self::$extdbtablename, $data);
 100          }
 101      }
 102  }