From a407e23dba41c9a6803ca1f37d367eba9d624376 Mon Sep 17 00:00:00 2001 From: Artem Andreev Date: Sat, 18 Jun 2011 16:42:40 +0400 Subject: [PATCH] Implemented backup/restore functionality --- ...backup_attforblock_activity_task.class.php | 55 ++++++++ .../backup_attforblock_settingslib.php | 12 ++ .../moodle2/backup_attforblock_stepslib.php | 97 +++++++++++++ ...estore_attforblock_activity_task.class.php | 133 ++++++++++++++++++ .../moodle2/restore_attforblock_stepslib.php | 118 ++++++++++++++++ 5 files changed, 415 insertions(+) create mode 100644 backup/moodle2/backup_attforblock_activity_task.class.php create mode 100644 backup/moodle2/backup_attforblock_settingslib.php create mode 100644 backup/moodle2/backup_attforblock_stepslib.php create mode 100644 backup/moodle2/restore_attforblock_activity_task.class.php create mode 100644 backup/moodle2/restore_attforblock_stepslib.php diff --git a/backup/moodle2/backup_attforblock_activity_task.class.php b/backup/moodle2/backup_attforblock_activity_task.class.php new file mode 100644 index 0000000..d8fcf53 --- /dev/null +++ b/backup/moodle2/backup_attforblock_activity_task.class.php @@ -0,0 +1,55 @@ + + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +require_once($CFG->dirroot . '/mod/attforblock/backup/moodle2/backup_attforblock_settingslib.php'); +require_once($CFG->dirroot . '/mod/attforblock/backup/moodle2/backup_attforblock_stepslib.php'); + +/** + * Provides all the settings and steps to perform one complete backup of attforblock activity + */ +class backup_attforblock_activity_task extends backup_activity_task { + + /** + * Define (add) particular settings this activity can have + */ + protected function define_my_settings() { + // No particular settings for this activity + } + + /** + * Define (add) particular steps this activity can have + */ + protected function define_my_steps() { + $this->add_step(new backup_attforblock_activity_structure_step('attforblock_structure', 'attforblock.xml')); + } + + /** + * Code the transformations to perform in the activity in + * order to get transportable (encoded) links + */ + static public function encode_content_links($content) { + global $CFG; + + $base = preg_quote($CFG->wwwroot,"/"); + + //Link to attforblock view by moduleid + $search = "/(" . $base . "\/mod\/attforblock\/view.php\?id\=)([0-9]+)/"; + $content= preg_replace($search, '$@ATTFORBLOCKVIEWBYID*$2@$', $content); + + //Link to attforblock view by moduleid and studentid + $search = "/(" . $base . "\/mod\/attforblock\/view.php\?id\=)([0-9]+)\&studentid\=([0-9]+)/"; + $content= preg_replace($search, '$@ATTFORBLOCKVIEWBYIDSTUD*$2*$3@$', $content); + + return $content; + } +} diff --git a/backup/moodle2/backup_attforblock_settingslib.php b/backup/moodle2/backup_attforblock_settingslib.php new file mode 100644 index 0000000..f3d3197 --- /dev/null +++ b/backup/moodle2/backup_attforblock_settingslib.php @@ -0,0 +1,12 @@ + + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + + // This activity has not particular settings but the inherited from the generic + // backup_activity_task so here there isn't any class definition, like the ones + // existing in /backup/moodle2/backup_settingslib.php (activities section) diff --git a/backup/moodle2/backup_attforblock_stepslib.php b/backup/moodle2/backup_attforblock_stepslib.php new file mode 100644 index 0000000..10a31f3 --- /dev/null +++ b/backup/moodle2/backup_attforblock_stepslib.php @@ -0,0 +1,97 @@ + + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * Defines the complete attforblock structure for backup, with file and id annotations + */ +class backup_attforblock_activity_structure_step extends backup_activity_structure_step { + + protected function define_structure() { + + // are we including userinfo? + $userinfo = $this->get_setting_value('userinfo'); + + //////////////////////////////////////////////////////////////////////// + // XML nodes declaration - non-user data + //////////////////////////////////////////////////////////////////////// + + $attforblock = new backup_nested_element('attforblock', array('id'), array( + 'name', 'grade')); + + $statuses = new backup_nested_element('statuses'); + $status = new backup_nested_element('status', array('id'), array( + 'acronym', 'description', 'grade', 'visible', 'deleted')); + + $sessions = new backup_nested_element('sessions'); + $session = new backup_nested_element('session', array('id'), array( + 'groupid', 'sessdate', 'duration', 'lasttaken', 'lasttakenby', + 'timemodified', 'description', 'descriptionformat')); + + //////////////////////////////////////////////////////////////////////// + // XML nodes declaration - user data + //////////////////////////////////////////////////////////////////////// + + $logs = new backup_nested_element('logs'); + $log = new backup_nested_element('log', array('id'), array( + 'sessionid', 'studentid', 'statusid', 'lasttaken', 'statusset', + 'timetaken', 'takenby', 'remarks')); + + //////////////////////////////////////////////////////////////////////// + // build the tree in the order needed for restore + //////////////////////////////////////////////////////////////////////// + $attforblock->add_child($statuses); + $statuses->add_child($status); + + $attforblock->add_child($sessions); + $sessions->add_child($session); + + $session->add_child($logs); + $logs->add_child($log); + + //////////////////////////////////////////////////////////////////////// + // data sources - non-user data + //////////////////////////////////////////////////////////////////////// + + $attforblock->set_source_table('attforblock', array('id' => backup::VAR_ACTIVITYID)); + + $status->set_source_table('attendance_statuses', array('attendanceid' => backup::VAR_PARENTID)); + + $session->set_source_table('attendance_sessions', array('attendanceid' => backup::VAR_PARENTID)); + + //////////////////////////////////////////////////////////////////////// + // data sources - user related data + //////////////////////////////////////////////////////////////////////// + + if ($userinfo) { + $log->set_source_table('attendance_log', array('sessionid' => backup::VAR_PARENTID)); + } + + //////////////////////////////////////////////////////////////////////// + // id annotations + //////////////////////////////////////////////////////////////////////// + + $session->annotate_ids('user', 'lasttakenby'); + $session->annotate_ids('group', 'groupid'); + $log->annotate_ids('user', 'studentid'); + $log->annotate_ids('user', 'takenby'); + + //////////////////////////////////////////////////////////////////////// + // file annotations + //////////////////////////////////////////////////////////////////////// + + $session->annotate_files('mod_attforblock', 'session', 'id'); + + // return the root element (workshop), wrapped into standard activity structure + return $this->prepare_activity_structure($attforblock); + } +} diff --git a/backup/moodle2/restore_attforblock_activity_task.class.php b/backup/moodle2/restore_attforblock_activity_task.class.php new file mode 100644 index 0000000..3e2124b --- /dev/null +++ b/backup/moodle2/restore_attforblock_activity_task.class.php @@ -0,0 +1,133 @@ + + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +require_once($CFG->dirroot . '/mod/attforblock/backup/moodle2/restore_attforblock_stepslib.php'); + +/** + * attforblock restore task that provides all the settings and steps to perform one + * complete restore of the activity + */ +class restore_attforblock_activity_task extends restore_activity_task { + + /** + * Define (add) particular settings this activity can have + */ + protected function define_my_settings() { + // No particular settings for this activity + } + + /** + * Define (add) particular steps this activity can have + */ + protected function define_my_steps() { + // Choice only has one structure step + $this->add_step(new restore_attforblock_activity_structure_step('attforblock_structure', 'attforblock.xml')); + } + + /** + * Define the contents in the activity that must be + * processed by the link decoder + */ + static public function define_decode_contents() { + $contents = array(); + + $contents[] = new restore_decode_content('attendance_sessions', + array('description'), 'attforblock_session'); + + return $contents; + } + + /** + * Define the decoding rules for links belonging + * to the activity to be executed by the link decoder + */ + static public function define_decode_rules() { + $rules = array(); + + $rules[] = new restore_decode_rule('ATTFORBLOCKVIEWBYID', + '/mod/attforblock/view.php?id=$1', 'course_module'); + $rules[] = new restore_decode_rule('ATTFORBLOCKVIEWBYIDSTUD', + '/mod/attforblock/view.php?id=$1&studentid=$2', array('course_module', 'user')); + + return $rules; + + } + + /** + * Define the restore log rules that will be applied + * by the {@link restore_logs_processor} when restoring + * attforblock logs. It must return one array + * of {@link restore_log_rule} objects + */ + static public function define_restore_log_rules() { + $rules = array(); + + // TODO: log restore + /* + $rules[] = new restore_log_rule('attforblock', 'add', 'view.php?id={course_module}', '{attforblock}'); + $rules[] = new restore_log_rule('attforblock', 'update', 'view.php?id={course_module}', '{attforblock}'); + $rules[] = new restore_log_rule('attforblock', 'view', 'view.php?id={course_module}', '{attforblock}'); + + $rules[] = new restore_log_rule('attforblock', 'add assessment', + 'assessment.php?asid={attforblock_assessment}', '{attforblock_submission}'); + $rules[] = new restore_log_rule('attforblock', 'update assessment', + 'assessment.php?asid={attforblock_assessment}', '{attforblock_submission}'); + + $rules[] = new restore_log_rule('attforblock', 'add reference assessment', + 'exassessment.php?asid={attforblock_referenceassessment}', '{attforblock_examplesubmission}'); + $rules[] = new restore_log_rule('attforblock', 'update reference assessment', + 'exassessment.php?asid={attforblock_referenceassessment}', '{attforblock_examplesubmission}'); + + $rules[] = new restore_log_rule('attforblock', 'add example assessment', + 'exassessment.php?asid={attforblock_exampleassessment}', '{attforblock_examplesubmission}'); + $rules[] = new restore_log_rule('attforblock', 'update example assessment', + 'exassessment.php?asid={attforblock_exampleassessment}', '{attforblock_examplesubmission}'); + + $rules[] = new restore_log_rule('attforblock', 'view submission', + 'submission.php?cmid={course_module}&id={attforblock_submission}', '{attforblock_submission}'); + $rules[] = new restore_log_rule('attforblock', 'add submission', + 'submission.php?cmid={course_module}&id={attforblock_submission}', '{attforblock_submission}'); + $rules[] = new restore_log_rule('attforblock', 'update submission', + 'submission.php?cmid={course_module}&id={attforblock_submission}', '{attforblock_submission}'); + + $rules[] = new restore_log_rule('attforblock', 'view example', + 'exsubmission.php?cmid={course_module}&id={attforblock_examplesubmission}', '{attforblock_examplesubmission}'); + $rules[] = new restore_log_rule('attforblock', 'add example', + 'exsubmission.php?cmid={course_module}&id={attforblock_examplesubmission}', '{attforblock_examplesubmission}'); + $rules[] = new restore_log_rule('attforblock', 'update example', + 'exsubmission.php?cmid={course_module}&id={attforblock_examplesubmission}', '{attforblock_examplesubmission}'); + + $rules[] = new restore_log_rule('attforblock', 'update aggregate grades', 'view.php?id={course_module}', '{attforblock}'); + $rules[] = new restore_log_rule('attforblock', 'update switch phase', 'view.php?id={course_module}', '[phase]'); + $rules[] = new restore_log_rule('attforblock', 'update clear aggregated grades', 'view.php?id={course_module}', '{attforblock}'); + $rules[] = new restore_log_rule('attforblock', 'update clear assessments', 'view.php?id={course_module}', '{attforblock}'); + * + */ + + return $rules; + } + + /** + * Define the restore log rules that will be applied + * by the {@link restore_logs_processor} when restoring + * course logs. It must return one array + * of {@link restore_log_rule} objects + * + * Note this rules are applied when restoring course logs + * by the restore final task, but are defined here at + * activity level. All them are rules not linked to any module instance (cmid = 0) + */ + static public function define_restore_log_rules_for_course() { + $rules = array(); + + return $rules; + } +} diff --git a/backup/moodle2/restore_attforblock_stepslib.php b/backup/moodle2/restore_attforblock_stepslib.php new file mode 100644 index 0000000..c2aa2d5 --- /dev/null +++ b/backup/moodle2/restore_attforblock_stepslib.php @@ -0,0 +1,118 @@ + + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * Define all the restore steps that will be used by the restore_attforblock_activity_task + */ + +/** + * Structure step to restore one attforblock activity + */ +class restore_attforblock_activity_structure_step extends restore_activity_structure_step { + + protected function define_structure() { + + $paths = array(); + + $userinfo = $this->get_setting_value('userinfo'); // are we including userinfo? + + //////////////////////////////////////////////////////////////////////// + // XML interesting paths - non-user data + //////////////////////////////////////////////////////////////////////// + + $paths[] = new restore_path_element('attforblock', '/activity/attforblock'); + + $paths[] = new restore_path_element('attforblock_status', + '/activity/attforblock/statuses/status'); + + $paths[] = new restore_path_element('attforblock_session', + '/activity/attforblock/sessions/session'); + + // End here if no-user data has been selected + if (!$userinfo) { + return $this->prepare_activity_structure($paths); + } + + //////////////////////////////////////////////////////////////////////// + // XML interesting paths - user data + //////////////////////////////////////////////////////////////////////// + + $paths[] = new restore_path_element('attforblock_log', + '/activity/attforblock/sessions/session/logs/log'); + + // Return the paths wrapped into standard activity structure + return $this->prepare_activity_structure($paths); + } + + protected function process_attforblock($data) { + global $DB; + + $data = (object)$data; + $oldid = $data->id; + $data->course = $this->get_courseid(); + + // insert the attforblock record + $newitemid = $DB->insert_record('attforblock', $data); + // immediately after inserting "activity" record, call this + $this->apply_activity_instance($newitemid); + } + + protected function process_attforblock_status($data) { + global $DB; + + $data = (object)$data; + $oldid = $data->id; + + $data->attendanceid = $this->get_new_parentid('attforblock'); + + $newitemid = $DB->insert_record('attendance_statuses', $data); + $this->set_mapping('attforblock_status', $oldid, $newitemid); + } + + protected function process_attforblock_session($data) { + global $DB; + + $data = (object)$data; + $oldid = $data->id; + + $data->attendanceid = $this->get_new_parentid('attforblock'); + $data->groupid = $this->get_mappingid('group', $data->groupid); + $data->sessdate = $this->apply_date_offset($data->sessdate); + $data->lasttaken = $this->apply_date_offset($data->lasttaken); + $data->lasttakenby = $this->get_mappingid('user', $data->lasttakenby); + $data->timemodified = $this->apply_date_offset($data->timemodified); + + $newitemid = $DB->insert_record('attendance_sessions', $data); + $this->set_mapping('attforblock_session', $oldid, $newitemid, true); + } + + protected function process_attforblock_log($data) { + global $DB; + + $data = (object)$data; + $oldid = $data->id; + + $data->sessionid = $this->get_mappingid('attforblock_session', $data->sessionid); + $data->studentid = $this->get_mappingid('user', $data->studentid); + $data->statusid = $this->get_mappingid('attforblock_status', $data->statusid); + $statusset = explode(',', $data->statusset); + foreach ($statusset as $st) $st = $this->get_mappingid('attforblock_status', $st); + $data->statusset = implode(',', $statusset); + $data->timetaken = $this->apply_date_offset($data->timetaken); + $data->takenby = $this->get_mappingid('user', $data->takenby); + + $newitemid = $DB->insert_record('attendance_log', $data); + } + + protected function after_execute() { + $this->add_related_files('mod_attforblock', 'session', 'attforblock_session'); + } +}