[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
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', backup::LOG_DEBUG); 93 } 94 95 } else { // Action not found log the problem 96 self::$task->log('Log module-action "' . $keyname . '" unknown. Not restored', backup::LOG_DEBUG); 97 $newlog = false; 98 99 } 100 return $newlog; 101 } 102 103 /** 104 * Adds all the activity {@link restore_log_rule} rules 105 * defined in activity task but corresponding to log 106 * records at course level (cmid = 0). 107 */ 108 public static function register_log_rules_for_course() { 109 $tasks = array(); // To get the list of tasks having log rules for course 110 $rules = array(); // To accumulate rules for course 111 112 // Add the module tasks 113 $mods = core_component::get_plugin_list('mod'); 114 foreach ($mods as $mod => $moddir) { 115 if (class_exists('restore_' . $mod . '_activity_task')) { 116 $tasks[$mod] = 'restore_' . $mod . '_activity_task'; 117 } 118 } 119 120 foreach ($tasks as $mod => $classname) { 121 if (!method_exists($classname, 'define_restore_log_rules_for_course')) { 122 continue; // This method is optional 123 } 124 // Get restore_log_rule array and accumulate 125 $taskrules = call_user_func(array($classname, 'define_restore_log_rules_for_course')); 126 if (!is_array($taskrules)) { 127 throw new restore_logs_processor_exception('define_restore_log_rules_for_course_not_array', $classname); 128 } 129 $rules = array_merge($rules, $taskrules); 130 } 131 return $rules; 132 } 133 } 134 135 /* 136 * Exception class used by all the @restore_logs_processor stuff 137 */ 138 class restore_logs_processor_exception extends backup_exception { 139 140 public function __construct($errorcode, $a=NULL, $debuginfo=null) { 141 return parent::__construct($errorcode, $a, $debuginfo); 142 } 143 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |