Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/backup/util/helper/restore_logs_processor.class.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00039 class restore_logs_processor {
00040 
00041     private static $instance; // The current instance of restore_logs_processor
00042     private static $task;     // The current restore_task instance this processor belongs to
00043     private $rules;           // Array of restore_log_rule rules (module-action being keys), supports multiple per key
00044 
00045     private function __construct($values) { // Private constructor
00046 
00047         // Constructor has been called, so we need to reload everything
00048         // Process rules
00049         $this->rules = array();
00050         $rules = call_user_func(array(self::$task, 'define_restore_log_rules'));
00051         foreach ($rules as $rule) {
00052             // TODO: Check it is one restore_log_rule
00053 
00054             // Set rule restoreid
00055             $rule->set_restoreid(self::$task->get_restoreid());
00056             // Set rule fixed values if needed
00057             if (is_array($values) and !empty($values)) {
00058                 $rule->set_fixed_values($values);
00059             }
00060             // Add the rule to the associative array
00061             if (array_key_exists($rule->get_key_name(), $this->rules)) {
00062                 $this->rules[$rule->get_key_name()][] = $rule;
00063             } else {
00064                 $this->rules[$rule->get_key_name()] = array($rule);
00065             }
00066         }
00067     }
00068 
00069     public static function get_instance($task, $values) {
00070         // If the singleton isn't set or if the task is another one, create new instance
00071         if (!isset(self::$instance) || self::$task !== $task) {
00072             self::$task = $task;
00073             self::$instance = new restore_logs_processor($values);
00074         }
00075         return self::$instance;
00076     }
00077 
00078     public function process_log_record($log) {
00079         // Check we have one restore_log_rule for this log record
00080         $keyname = $log->module . '-' . $log->action;
00081         if (array_key_exists($keyname, $this->rules)) {
00082             // Try it for each rule available
00083             foreach ($this->rules[$keyname] as $rule) {
00084                 $newlog = $rule->process($log);
00085                 // Some rule has been able to perform the conversion, exit from loop
00086                 if (!empty($newlog)) {
00087                     break;
00088                 }
00089             }
00090             // Arrived here log is empty, no rule was able to perform the conversion, log the problem
00091             if (empty($newlog)) {
00092                 self::$task->log('Log module-action "' . $keyname . ' process problem. Not restored', backup::LOG_DEBUG);
00093             }
00094 
00095         } else { // Action not found log the problem
00096             self::$task->log('Log module-action "' . $keyname . ' unknown. Not restored', backup::LOG_DEBUG);
00097             $newlog = false;
00098 
00099         }
00100         return $newlog;
00101     }
00102 
00108     public static function register_log_rules_for_course() {
00109         $tasks = array(); // To get the list of tasks having log rules for course
00110         $rules = array(); // To accumulate rules for course
00111 
00112         // Add the module tasks
00113         $mods = get_plugin_list('mod');
00114         foreach ($mods as $mod => $moddir) {
00115             if (class_exists('restore_' . $mod . '_activity_task')) {
00116                 $tasks[$mod] = 'restore_' . $mod . '_activity_task';
00117             }
00118         }
00119 
00120         foreach ($tasks as $mod => $classname) {
00121             if (!method_exists($classname, 'define_restore_log_rules_for_course')) {
00122                 continue; // This method is optional
00123             }
00124             // Get restore_log_rule array and accumulate
00125             $taskrules = call_user_func(array($classname, 'define_restore_log_rules_for_course'));
00126             if (!is_array($taskrules)) {
00127                 throw new restore_logs_processor_exception('define_restore_log_rules_for_course_not_array', $classname);
00128             }
00129             $rules = array_merge($rules, $taskrules);
00130         }
00131         return $rules;
00132     }
00133 }
00134 
00135 /*
00136  * Exception class used by all the @restore_logs_processor stuff
00137  */
00138 class restore_logs_processor_exception extends backup_exception {
00139 
00140     public function __construct($errorcode, $a=NULL, $debuginfo=null) {
00141         return parent::__construct($errorcode, $a, $debuginfo);
00142     }
00143 }
 All Data Structures Namespaces Files Functions Variables Enumerations