Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/backup/moodle2/restore_activity_task.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 
00031 abstract class restore_activity_task extends restore_task {
00032 
00033     protected $info; // info related to activity gathered from backup file
00034     protected $modulename;  // name of the module
00035     protected $moduleid;    // new (target) id of the course module
00036     protected $oldmoduleid; // old (original) id of the course module
00037     protected $oldmoduleversion; // old (original) version of the module
00038     protected $contextid;   // new (target) context of the activity
00039     protected $oldcontextid;// old (original) context of the activity
00040     protected $activityid;  // new (target) id of the activity
00041     protected $oldactivityid;// old (original) id of the activity
00042 
00046     public function __construct($name, $info, $plan = null) {
00047         $this->info = $info;
00048         $this->modulename = $this->info->modulename;
00049         $this->moduleid = 0;
00050         $this->oldmoduleid = $this->info->moduleid;
00051         $this->oldmoduleversion = 0;
00052         $this->contextid = 0;
00053         $this->oldcontextid = 0;
00054         $this->activityid = 0;
00055         $this->oldactivityid = 0;
00056         parent::__construct($name, $plan);
00057     }
00058 
00062     public function get_taskbasepath() {
00063         return $this->get_basepath() . '/' . $this->info->directory;
00064     }
00065 
00066     public function set_moduleid($moduleid) {
00067         $this->moduleid = $moduleid;
00068     }
00069 
00070     public function set_old_moduleversion($oldmoduleversion) {
00071         $this->oldmoduleversion = $oldmoduleversion;
00072     }
00073 
00074     public function set_activityid($activityid) {
00075         $this->activityid = $activityid;
00076     }
00077 
00078     public function set_old_activityid($activityid) {
00079         $this->oldactivityid = $activityid;
00080     }
00081 
00082     public function set_contextid($contextid) {
00083         $this->contextid = $contextid;
00084     }
00085 
00086     public function set_old_contextid($contextid) {
00087         $this->oldcontextid = $contextid;
00088     }
00089 
00090     public function get_modulename() {
00091         return $this->modulename;
00092     }
00093 
00094     public function get_moduleid() {
00095         return $this->moduleid;
00096     }
00097 
00098     public function get_old_moduleversion() {
00099         return $this->oldmoduleversion;
00100     }
00101 
00102     public function get_activityid() {
00103         return $this->activityid;
00104     }
00105 
00106     public function get_old_activityid() {
00107         return $this->oldactivityid;
00108     }
00109 
00110     public function get_contextid() {
00111         return $this->contextid;
00112     }
00113 
00114     public function get_old_contextid() {
00115         return $this->oldcontextid;
00116     }
00117 
00121     public function build() {
00122 
00123         // If we have decided not to restore activities, prevent anything to be built
00124         if (!$this->get_setting_value('activities')) {
00125             $this->built = true;
00126             return;
00127         }
00128 
00129         // Load he course_module estructure, generating it (with instance = 0)
00130         // but allowing the creation of the target context needed in following steps
00131         $this->add_step(new restore_module_structure_step('module_info', 'module.xml'));
00132 
00133         // Here we add all the common steps for any activity and, in the point of interest
00134         // we call to define_my_steps() is order to get the particular ones inserted in place.
00135         $this->define_my_steps();
00136 
00137         // Roles (optionally role assignments and always role overrides)
00138         $this->add_step(new restore_ras_and_caps_structure_step('course_ras_and_caps', 'roles.xml'));
00139 
00140         // Filters (conditionally)
00141         if ($this->get_setting_value('filters')) {
00142             $this->add_step(new restore_filters_structure_step('activity_filters', 'filters.xml'));
00143         }
00144 
00145         // Comments (conditionally)
00146         if ($this->get_setting_value('comments')) {
00147             $this->add_step(new restore_comments_structure_step('activity_comments', 'comments.xml'));
00148         }
00149 
00150         // Grades (module-related, rest of gradebook is restored later if possible: cats, calculations...)
00151         $this->add_step(new restore_activity_grades_structure_step('activity_grades', 'grades.xml'));
00152 
00153         // Advanced grading methods attached to the module
00154         $this->add_step(new restore_activity_grading_structure_step('activity_grading', 'grading.xml'));
00155 
00156         // Userscompletion (conditionally)
00157         if ($this->get_setting_value('userscompletion')) {
00158             $this->add_step(new restore_userscompletion_structure_step('activity_userscompletion', 'completion.xml'));
00159         }
00160 
00161         // Logs (conditionally)
00162         if ($this->get_setting_value('logs')) {
00163             $this->add_step(new restore_activity_logs_structure_step('activity_logs', 'logs.xml'));
00164         }
00165 
00166         // At the end, mark it as built
00167         $this->built = true;
00168     }
00169 
00174     public function execute() {
00175 
00176         // Find activity_included_setting
00177         if (!$this->get_setting_value('included')) {
00178             $this->log('activity skipped by _included setting', backup::LOG_DEBUG, $this->name);
00179             $this->plan->set_excluding_activities(); // Inform plan we are excluding actvities
00180 
00181         } else { // Setting tells us it's ok to execute
00182             parent::execute();
00183         }
00184     }
00185 
00186 
00192     public function get_setting($name) {
00193         $namewithprefix = $this->info->modulename . '_' . $this->info->moduleid . '_' . $name;
00194         $result = null;
00195         foreach ($this->settings as $key => $setting) {
00196             if ($setting->get_name() == $namewithprefix) {
00197                 if ($result != null) {
00198                     throw new base_task_exception('multiple_settings_by_name_found', $namewithprefix);
00199                 } else {
00200                     $result = $setting;
00201                 }
00202             }
00203         }
00204         if ($result) {
00205             return $result;
00206         } else {
00207             // Fallback to parent
00208             return parent::get_setting($name);
00209         }
00210     }
00211 
00218     public function get_comment_mapping_itemname($commentarea) {
00219         return $commentarea;
00220     }
00221 
00225     abstract protected function define_my_steps();
00226 
00231     static public function define_decode_contents() {
00232         throw new coding_exception('define_decode_contents() method needs to be overridden in each subclass of restore_activity_task');
00233     }
00234 
00239     static public function define_decode_rules() {
00240         throw new coding_exception('define_decode_rules() method needs to be overridden in each subclass of restore_activity_task');
00241     }
00242 
00249     static public function define_restore_log_rules() {
00250         throw new coding_exception('define_restore_log_rules() method needs to be overridden in each subclass of restore_activity_task');
00251     }
00252 
00253 // Protected API starts here
00254 
00258     protected function define_settings() {
00259 
00260         // All the settings related to this activity will include this prefix
00261         $settingprefix = $this->info->modulename . '_' . $this->info->moduleid . '_';
00262 
00263         // All these are common settings to be shared by all activities
00264 
00265         // Define activity_include (to decide if the whole task must be really executed)
00266         // Dependent of:
00267         // - activities root setting
00268         // - section_included setting (if exists)
00269         $settingname = $settingprefix . 'included';
00270         $activity_included = new restore_activity_generic_setting($settingname, base_setting::IS_BOOLEAN, true);
00271         $activity_included->get_ui()->set_icon(new pix_icon('icon', get_string('pluginname', $this->modulename), $this->modulename));
00272         $this->add_setting($activity_included);
00273         // Look for "activities" root setting
00274         $activities = $this->plan->get_setting('activities');
00275         $activities->add_dependency($activity_included);
00276         // Look for "section_included" section setting (if exists)
00277         $settingname = 'section_' . $this->info->sectionid . '_included';
00278         if ($this->plan->setting_exists($settingname)) {
00279             $section_included = $this->plan->get_setting($settingname);
00280             $section_included->add_dependency($activity_included);
00281         }
00282 
00283         // Define activity_userinfo. Dependent of:
00284         // - users root setting
00285         // - section_userinfo setting (if exists)
00286         // - activity_included setting
00287         $settingname = $settingprefix . 'userinfo';
00288         $selectvalues = array(0=>get_string('no')); // Safer options
00289         $defaultvalue = false;                      // Safer default
00290         if (isset($this->info->settings[$settingname]) && $this->info->settings[$settingname]) { // Only enabled when available
00291             $selectvalues = array(1=>get_string('yes'), 0=>get_string('no'));
00292             $defaultvalue = true;
00293         }
00294         $activity_userinfo = new restore_activity_userinfo_setting($settingname, base_setting::IS_BOOLEAN, $defaultvalue);
00295         $activity_userinfo->set_ui(new backup_setting_ui_select($activity_userinfo, get_string('includeuserinfo','backup'), $selectvalues));
00296         $this->add_setting($activity_userinfo);
00297         // Look for "users" root setting
00298         $users = $this->plan->get_setting('users');
00299         $users->add_dependency($activity_userinfo);
00300         // Look for "section_userinfo" section setting (if exists)
00301         $settingname = 'section_' . $this->info->sectionid . '_userinfo';
00302         if ($this->plan->setting_exists($settingname)) {
00303             $section_userinfo = $this->plan->get_setting($settingname);
00304             $section_userinfo->add_dependency($activity_userinfo);
00305         }
00306         // Look for "activity_included" setting
00307         $activity_included->add_dependency($activity_userinfo);
00308 
00309         // End of common activity settings, let's add the particular ones
00310         $this->define_my_settings();
00311     }
00312 
00316     abstract protected function define_my_settings();
00317 }
 All Data Structures Namespaces Files Functions Variables Enumerations