Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/backup/moodle2/backup_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 backup_activity_task extends backup_task {
00032 
00033     protected $moduleid;
00034     protected $sectionid;
00035     protected $modulename;
00036     protected $activityid;
00037     protected $contextid;
00038 
00042     public function __construct($name, $moduleid, $plan = null) {
00043 
00044         // Check moduleid exists
00045         if (!$coursemodule = get_coursemodule_from_id(false, $moduleid)) {
00046             throw new backup_task_exception('activity_task_coursemodule_not_found', $moduleid);
00047         }
00048         // Check activity supports this moodle2 backup format
00049         if (!plugin_supports('mod', $coursemodule->modname, FEATURE_BACKUP_MOODLE2)) {
00050             throw new backup_task_exception('activity_task_activity_lacks_moodle2_backup_support', $coursemodule->modname);
00051         }
00052 
00053         $this->moduleid   = $moduleid;
00054         $this->sectionid  = $coursemodule->section;
00055         $this->modulename = $coursemodule->modname;
00056         $this->activityid = $coursemodule->instance;
00057         $this->contextid  = get_context_instance(CONTEXT_MODULE, $this->moduleid)->id;
00058 
00059         parent::__construct($name, $plan);
00060     }
00061 
00062     public function get_moduleid() {
00063         return $this->moduleid;
00064     }
00065 
00066     public function get_sectionid() {
00067         return $this->sectionid;
00068     }
00069 
00070     public function get_modulename() {
00071         return $this->modulename;
00072     }
00073 
00074     public function get_activityid() {
00075         return $this->activityid;
00076     }
00077 
00078     public function get_contextid() {
00079         return $this->contextid;
00080     }
00081 
00085     public function get_taskbasepath() {
00086         return $this->get_basepath() . '/activities/' . $this->modulename . '_' . $this->moduleid;
00087     }
00088 
00092     public function build() {
00093 
00094         // If we have decided not to backup activities, prevent anything to be built
00095         if (!$this->get_setting_value('activities')) {
00096             $this->built = true;
00097             return;
00098         }
00099 
00100         // Add some extra settings that related processors are going to need
00101         $this->add_setting(new backup_activity_generic_setting(backup::VAR_MODID, base_setting::IS_INTEGER, $this->moduleid));
00102         $this->add_setting(new backup_activity_generic_setting(backup::VAR_COURSEID, base_setting::IS_INTEGER, $this->get_courseid()));
00103         $this->add_setting(new backup_activity_generic_setting(backup::VAR_SECTIONID, base_setting::IS_INTEGER, $this->sectionid));
00104         $this->add_setting(new backup_activity_generic_setting(backup::VAR_MODNAME, base_setting::IS_FILENAME, $this->modulename));
00105         $this->add_setting(new backup_activity_generic_setting(backup::VAR_ACTIVITYID, base_setting::IS_INTEGER, $this->activityid));
00106         $this->add_setting(new backup_activity_generic_setting(backup::VAR_CONTEXTID, base_setting::IS_INTEGER, $this->contextid));
00107 
00108         // Create the activity directory
00109         $this->add_step(new create_taskbasepath_directory('create_activity_directory'));
00110 
00111         // Generate the module.xml file, containing general information for the
00112         // activity and from its related course_modules record and availability
00113         $this->add_step(new backup_module_structure_step('module_info', 'module.xml'));
00114 
00115         // Annotate the groups used in already annotated groupings
00116         $this->add_step(new backup_annotate_groups_from_groupings('annotate_groups'));
00117 
00118         // Here we add all the common steps for any activity and, in the point of interest
00119         // we call to define_my_steps() is order to get the particular ones inserted in place.
00120         $this->define_my_steps();
00121 
00122         // Generate the roles file (optionally role assignments and always role overrides)
00123         $this->add_step(new backup_roles_structure_step('activity_roles', 'roles.xml'));
00124 
00125         // Generate the filter file (conditionally)
00126         if ($this->get_setting_value('filters')) {
00127             $this->add_step(new backup_filters_structure_step('activity_filters', 'filters.xml'));
00128         }
00129 
00130         // Generate the comments file (conditionally)
00131         if ($this->get_setting_value('comments')) {
00132             $this->add_step(new backup_comments_structure_step('activity_comments', 'comments.xml'));
00133         }
00134 
00135         // Generate the userscompletion file (conditionally)
00136         if ($this->get_setting_value('userscompletion')) {
00137             $this->add_step(new backup_userscompletion_structure_step('activity_userscompletion', 'completion.xml'));
00138         }
00139 
00140         // Generate the logs file (conditionally)
00141         if ($this->get_setting_value('logs')) {
00142             $this->add_step(new backup_activity_logs_structure_step('activity_logs', 'logs.xml'));
00143         }
00144 
00145         // Fetch all the activity grade items and put them to backup_ids
00146         $this->add_step(new backup_activity_grade_items_to_ids('fetch_activity_grade_items'));
00147 
00148         // Generate the grades file
00149         $this->add_step(new backup_activity_grades_structure_step('activity_grades', 'grades.xml'));
00150 
00151         // Generate the grading file (conditionally)
00152         $this->add_step(new backup_activity_grading_structure_step('activity_grading', 'grading.xml'));
00153 
00154         // Annotate the scales used in already annotated outcomes
00155         $this->add_step(new backup_annotate_scales_from_outcomes('annotate_scales'));
00156 
00157         // NOTE: Historical grade information is saved completely at course level only (see 1.9)
00158         // not per activity nor per selected activities (all or nothing).
00159 
00160         // Generate the inforef file (must be after ALL steps gathering annotations of ANY type)
00161         $this->add_step(new backup_inforef_structure_step('activity_inforef', 'inforef.xml'));
00162 
00163         // Migrate the already exported inforef entries to final ones
00164         $this->add_step(new move_inforef_annotations_to_final('migrate_inforef'));
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();
00180         } else { // Setting tells us it's ok to execute
00181             parent::execute();
00182         }
00183     }
00184 
00185 
00191     public function get_setting($name) {
00192         $namewithprefix = $this->modulename . '_' . $this->moduleid . '_' . $name;
00193         $result = null;
00194         foreach ($this->settings as $key => $setting) {
00195             if ($setting->get_name() == $namewithprefix) {
00196                 if ($result != null) {
00197                     throw new base_task_exception('multiple_settings_by_name_found', $namewithprefix);
00198                 } else {
00199                     $result = $setting;
00200                 }
00201             }
00202         }
00203         if ($result) {
00204             return $result;
00205         } else {
00206             // Fallback to parent
00207             return parent::get_setting($name);
00208         }
00209     }
00210 
00211 // Protected API starts here
00212 
00216     protected function define_settings() {
00217 
00218         // All the settings related to this activity will include this prefix
00219         $settingprefix = $this->modulename . '_' . $this->moduleid . '_';
00220 
00221         // All these are common settings to be shared by all activities
00222 
00223         // Define activity_include (to decide if the whole task must be really executed)
00224         // Dependent of:
00225         // - activities root setting
00226         // - section_included setting (if exists)
00227         $settingname = $settingprefix . 'included';
00228         $activity_included = new backup_activity_generic_setting($settingname, base_setting::IS_BOOLEAN, true);
00229         $activity_included->get_ui()->set_icon(new pix_icon('icon', get_string('pluginname', $this->modulename), $this->modulename));
00230         $this->add_setting($activity_included);
00231         // Look for "activities" root setting
00232         $activities = $this->plan->get_setting('activities');
00233         $activities->add_dependency($activity_included);
00234         // Look for "section_included" section setting (if exists)
00235         $settingname = 'section_' . $this->sectionid . '_included';
00236         if ($this->plan->setting_exists($settingname)) {
00237             $section_included = $this->plan->get_setting($settingname);
00238             $section_included->add_dependency($activity_included);
00239         }
00240 
00241         // Define activity_userinfo. Dependent of:
00242         // - users root setting
00243         // - section_userinfo setting (if exists)
00244         // - activity_included setting
00245         $settingname = $settingprefix . 'userinfo';
00246         $activity_userinfo = new backup_activity_userinfo_setting($settingname, base_setting::IS_BOOLEAN, true);
00247         //$activity_userinfo->get_ui()->set_label(get_string('includeuserinfo','backup'));
00248         $activity_userinfo->get_ui()->set_label('-');
00249         $this->add_setting($activity_userinfo);
00250         // Look for "users" root setting
00251         $users = $this->plan->get_setting('users');
00252         $users->add_dependency($activity_userinfo);
00253         // Look for "section_userinfo" section setting (if exists)
00254         $settingname = 'section_' . $this->sectionid . '_userinfo';
00255         if ($this->plan->setting_exists($settingname)) {
00256             $section_userinfo = $this->plan->get_setting($settingname);
00257             $section_userinfo->add_dependency($activity_userinfo);
00258         }
00259         // Look for "activity_included" setting
00260         $activity_included->add_dependency($activity_userinfo);
00261 
00262         // End of common activity settings, let's add the particular ones
00263         $this->define_my_settings();
00264     }
00265 
00269     abstract protected function define_my_settings();
00270 
00274     abstract protected function define_my_steps();
00275 
00280     static public function encode_content_links($content) {
00281         throw new coding_exception('encode_content_links() method needs to be overridden in each subclass of backup_activity_task');
00282     }
00283 
00284 }
 All Data Structures Namespaces Files Functions Variables Enumerations