Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * @package    moodlecore
  20   * @subpackage backup-helper
  21   * @copyright  2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   *
  24   * TODO: Finish phpdocs
  25   */
  26  
  27  /**
  28   * This class is one varying singleton that, for all the logs corresponding to
  29   * one task, is in charge of storing all its {@link restore_log_rule} rules,
  30   * dispatching to the correct one and insert/log the resulting information.
  31   *
  32   * Each time the task getting the instance changes, the rules are completely
  33   * reloaded with the ones in the new task. And all rules are informed with
  34   * new fixed values if explicity set.
  35   *
  36   * This class adopts the singleton pattern to be able to provide some persistency
  37   * of rules along the restore of all the logs corresponding to one restore_task
  38   */
  39  class restore_logs_processor {
  40  
  41      private static $instance; // The current instance of restore_logs_processor
  42      private static $task;     // The current restore_task instance this processor belongs to
  43      private $rules;           // Array of restore_log_rule rules (module-action being keys), supports multiple per key
  44  
  45      private function __construct($values) { // Private constructor
  46  
  47          // Constructor has been called, so we need to reload everything
  48          // Process rules
  49          $this->rules = array();
  50          $rules = call_user_func(array(self::$task, 'define_restore_log_rules'));
  51          foreach ($rules as $rule) {
  52              // TODO: Check it is one restore_log_rule
  53  
  54              // Set rule restoreid
  55              $rule->set_restoreid(self::$task->get_restoreid());
  56              // Set rule fixed values if needed
  57              if (is_array($values) and !empty($values)) {
  58                  $rule->set_fixed_values($values);
  59              }
  60              // Add the rule to the associative array
  61              if (array_key_exists($rule->get_key_name(), $this->rules)) {
  62                  $this->rules[$rule->get_key_name()][] = $rule;
  63              } else {
  64                  $this->rules[$rule->get_key_name()] = array($rule);
  65              }
  66          }
  67      }
  68  
  69      public static function get_instance($task, $values) {
  70          // If the singleton isn't set or if the task is another one, create new instance
  71          if (!isset(self::$instance) || self::$task !== $task) {
  72              self::$task = $task;
  73              self::$instance = new restore_logs_processor($values);
  74          }
  75          return self::$instance;
  76      }
  77  
  78      public function process_log_record($log) {
  79          // Check we have one restore_log_rule for this log record
  80          $keyname = $log->module . '-' . $log->action;
  81          if (array_key_exists($keyname, $this->rules)) {
  82              // Try it for each rule available
  83              foreach ($this->rules[$keyname] as $rule) {
  84                  $newlog = $rule->process($log);
  85                  // Some rule has been able to perform the conversion, exit from loop
  86                  if (!empty($newlog)) {
  87                      break;
  88                  }
  89              }
  90              // Arrived here log is empty, no rule was able to perform the conversion, log the problem
  91              if (empty($newlog)) {
  92                  self::$task->log('Log module-action "' . $keyname . '" process problem. Not restored. ' .
  93                      json_encode($log), backup::LOG_DEBUG);
  94              }
  95  
  96          } else { // Action not found log the problem
  97              self::$task->log('Log module-action "' . $keyname . '" unknown. Not restored. '.json_encode($log), backup::LOG_DEBUG);
  98              $newlog = false;
  99  
 100          }
 101          return $newlog;
 102      }
 103  
 104      /**
 105       * Adds all the activity {@link restore_log_rule} rules
 106       * defined in activity task but corresponding to log
 107       * records at course level (cmid = 0).
 108       */
 109      public static function register_log_rules_for_course() {
 110          $tasks = array(); // To get the list of tasks having log rules for course
 111          $rules = array(); // To accumulate rules for course
 112  
 113          // Add the module tasks
 114          $mods = core_component::get_plugin_list('mod');
 115          foreach ($mods as $mod => $moddir) {
 116              if (class_exists('restore_' . $mod . '_activity_task')) {
 117                  $tasks[$mod] = 'restore_' . $mod . '_activity_task';
 118              }
 119          }
 120  
 121          foreach ($tasks as $mod => $classname) {
 122              if (!method_exists($classname, 'define_restore_log_rules_for_course')) {
 123                  continue; // This method is optional
 124              }
 125              // Get restore_log_rule array and accumulate
 126              $taskrules = call_user_func(array($classname, 'define_restore_log_rules_for_course'));
 127              if (!is_array($taskrules)) {
 128                  throw new restore_logs_processor_exception('define_restore_log_rules_for_course_not_array', $classname);
 129              }
 130              $rules = array_merge($rules, $taskrules);
 131          }
 132          return $rules;
 133      }
 134  }
 135  
 136  /*
 137   * Exception class used by all the @restore_logs_processor stuff
 138   */
 139  class restore_logs_processor_exception extends backup_exception {
 140  
 141      public function __construct($errorcode, $a=NULL, $debuginfo=null) {
 142          return parent::__construct($errorcode, $a, $debuginfo);
 143      }
 144  }