// Link to the list of assignments
$search="/(".$base."\/mod\/assignment\/index.php\?id\=)([0-9]+)/";
- $content= preg_replace($search, '$@ASIGNMENTINDEX*$2@$', $content);
+ $content= preg_replace($search, '$@ASSIGNMENTINDEX*$2@$', $content);
// Link to assignment view by moduleid
$search="/(".$base."\/mod\/assignment\/view.php\?id\=)([0-9]+)/";
// Define file annotations
$assignment->annotate_files('mod_assignment', 'intro', null); // This file area hasn't itemid
$submission->annotate_files('mod_assignment', 'submission', 'id');
+ $submission->annotate_files('mod_assignment', 'online_submission', 'id'); // Until MDL-23683 gets decided/implemented
+ $submission->annotate_files('mod_assignment', 'response', 'id');
// Return the root element (assignment), wrapped into standard activity structure
return $this->prepare_activity_structure($assignment);
--- /dev/null
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * @package moodlecore
+ * @subpackage backup-moodle2
+ * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+require_once($CFG->dirroot . '/mod/assignment/backup/moodle2/restore_assignment_stepslib.php'); // Because it exists (must)
+
+/**
+ * assignment restore task that provides all the settings and steps to perform one
+ * complete restore of the activity
+ */
+class restore_assignment_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_assignment_activity_structure_step('assignment_structure', 'assignment.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('assignment', array('intro'), 'assignment');
+
+ 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('ASSIGNMENTVIEWBYID', '/mod/assignment/view.php?id=$1', 'course_module');
+ $rules[] = new restore_decode_rule('ASSIGNMENTINDEX', '/mod/assignment/index.php?id=$1', 'course');
+
+ return $rules;
+
+ }
+}
--- /dev/null
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * @package moodlecore
+ * @subpackage backup-moodle2
+ * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+/**
+ * Define all the restore steps that will be used by the restore_assignment_activity_task
+ */
+
+/**
+ * Structure step to restore one assignment activity
+ */
+class restore_assignment_activity_structure_step extends restore_activity_structure_step {
+
+ protected function define_structure() {
+
+ $paths = array();
+ $userinfo = $this->get_setting_value('userinfo');
+
+ $assignment = new restore_path_element('assignment', '/activity/assignment');
+ $paths[] = $assignment;
+
+ // Apply for 'assignment' subplugins optional paths at assignment level
+ $this->add_subplugin_structure('assignment', $assignment);
+
+ if ($userinfo) {
+ $submission = new restore_path_element('assignment_submission', '/activity/assignment/submissions/submission');
+ $paths[] = $submission;
+ // Apply for 'assignment' subplugins optional stuff at submission level
+ $this->add_subplugin_structure('assignment', $submission);
+ }
+
+ // Return the paths wrapped into standard activity structure
+ return $this->prepare_activity_structure($paths);
+ }
+
+ protected function process_assignment($data) {
+ global $DB;
+
+ $data = (object)$data;
+ $oldid = $data->id;
+ $data->course = $this->get_courseid();
+
+ $data->timedue = $this->apply_date_offset($data->timedue);
+ $data->timeavailable = $this->apply_date_offset($data->timeavailable);
+ $data->timemodified = $this->apply_date_offset($data->timemodified);
+
+ if ($data->grade < 0) { // scale found, get mapping
+ $data->grade = -($this->get_mappingid('scale', abs($data->grade)));
+ }
+
+ // insert the assignment record
+ $newitemid = $DB->insert_record('assignment', $data);
+ // inmediately after inserting "activity" record, call this
+ $this->apply_activity_instance($newitemid);
+ }
+
+ protected function process_assignment_submission($data) {
+ global $DB;
+
+ $data = (object)$data;
+ $oldid = $data->id;
+
+ $data->assignment = $this->get_new_parentid('assignment');
+ $data->timecreated = $this->apply_date_offset($data->timecreated);
+ $data->timemodified = $this->apply_date_offset($data->timemodified);
+ $data->timemarked = $this->apply_date_offset($data->timemarked);
+
+ $data->userid = $this->get_mappingid('user', $data->userid);
+ $data->teacher = $this->get_mappingid('user', $data->teacher);
+
+ $newitemid = $DB->insert_record('assignment_submissions', $data);
+ $this->set_mapping('assignment_submission', $oldid, $newitemid, true); // Going to have files
+ }
+
+ protected function after_execute() {
+ // Add assignment related files, no need to match by itemname (just internally handled context)
+ $this->add_related_files('mod_assignment', 'intro', null);
+ // Add assignment submission files, maching by assignment_submission itemname
+ $this->add_related_files('mod_assignment', 'submission', 'assignment_submission');
+ $this->add_related_files('mod_assignment', 'online_submission', 'assignment_submission'); // Until MDL-23683 gets decided/implemented
+ $this->add_related_files('mod_assignment', 'response', 'assignment_submission');
+ }
+}
+++ /dev/null
-<?php
- //This php script contains all the stuff to backup/restore
- //assignment mods
-
- //This is the "graphical" structure of the assignment mod:
- //
- // assignment
- // (CL,pk->id)
- // |
- // |
- // |
- // assignment_submisions
- // (UL,pk->id, fk->assignment,files)
- //
- // Meaning: pk->primary key field of the table
- // fk->foreign key to link with parent
- // nt->nested field (recursive data)
- // CL->course level info
- // UL->user level info
- // files->table may have files)
- //
- //-----------------------------------------------------------
-
- //This function executes all the backup procedure about this mod
- function assignment_backup_mods($bf,$preferences) {
- global $CFG, $DB;
-
- $status = true;
-
- //Iterate over assignment table
- $assignments = $DB->get_records ("assignment", array("course"=>$preferences->backup_course),"id");
- if ($assignments) {
- foreach ($assignments as $assignment) {
- if (backup_mod_selected($preferences,'assignment',$assignment->id)) {
- $status = assignment_backup_one_mod($bf,$preferences,$assignment);
- // backup files happens in backup_one_mod now too.
- }
- }
- }
- return $status;
- }
-
- function assignment_backup_one_mod($bf,$preferences,$assignment) {
- global $CFG, $DB;
-
- if (is_numeric($assignment)) {
- $assignment = $DB->get_record('assignment', array('id'=>$assignment));
- }
-
- $status = true;
-
- //Start mod
- fwrite ($bf,start_tag("MOD",3,true));
- //Print assignment data
- fwrite ($bf,full_tag("ID",4,false,$assignment->id));
- fwrite ($bf,full_tag("MODTYPE",4,false,"assignment"));
- fwrite ($bf,full_tag("NAME",4,false,$assignment->name));
- fwrite ($bf,full_tag("DESCRIPTION",4,false,$assignment->intro));
- fwrite ($bf,full_tag("FORMAT",4,false,$assignment->introformat));
- fwrite ($bf,full_tag("RESUBMIT",4,false,$assignment->resubmit));
- fwrite ($bf,full_tag("PREVENTLATE",4,false,$assignment->preventlate));
- fwrite ($bf,full_tag("EMAILTEACHERS",4,false,$assignment->emailteachers));
- fwrite ($bf,full_tag("VAR1",4,false,$assignment->var1));
- fwrite ($bf,full_tag("VAR2",4,false,$assignment->var2));
- fwrite ($bf,full_tag("VAR3",4,false,$assignment->var3));
- fwrite ($bf,full_tag("VAR4",4,false,$assignment->var4));
- fwrite ($bf,full_tag("VAR5",4,false,$assignment->var5));
- fwrite ($bf,full_tag("ASSIGNMENTTYPE",4,false,$assignment->assignmenttype));
- fwrite ($bf,full_tag("MAXBYTES",4,false,$assignment->maxbytes));
- fwrite ($bf,full_tag("TIMEDUE",4,false,$assignment->timedue));
- fwrite ($bf,full_tag("TIMEAVAILABLE",4,false,$assignment->timeavailable));
- fwrite ($bf,full_tag("GRADE",4,false,$assignment->grade));
- fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$assignment->timemodified));
-
- $class = 'assignment_' . $assignment->assignmenttype;
- require_once($CFG->dirroot . '/mod/assignment/lib.php');
- require_once($CFG->dirroot . '/mod/assignment/type/' . $assignment->assignmenttype . '/assignment.class.php');
- call_user_func(array($class, 'backup_one_mod'), $bf, $preferences, $assignment);
-
- //if we've selected to backup users info, then execute backup_assignment_submisions and
- //backup_assignment_files_instance
- if (backup_userdata_selected($preferences,'assignment',$assignment->id)) {
- $status = backup_assignment_submissions($bf,$preferences,$assignment);
- if ($status) {
- $status = backup_assignment_files_instance($bf,$preferences,$assignment->id);
- }
- }
- //End mod
- $status =fwrite ($bf,end_tag("MOD",3,true));
-
- return $status;
- }
-
- //Backup assignment_submissions contents (executed from assignment_backup_mods)
- function backup_assignment_submissions ($bf,$preferences,$assignment) {
- global $CFG, $DB;
-
- $status = true;
-
- $assignment_submissions = $DB->get_records("assignment_submissions", array("assignment"=>$assignment->id),"id");
- //If there is submissions
- if ($assignment_submissions) {
- //Write start tag
- $status =fwrite ($bf,start_tag("SUBMISSIONS",4,true));
- //Iterate over each submission
- foreach ($assignment_submissions as $ass_sub) {
- //Start submission
- $status =fwrite ($bf,start_tag("SUBMISSION",5,true));
- //Print submission contents
- fwrite ($bf,full_tag("ID",6,false,$ass_sub->id));
- fwrite ($bf,full_tag("USERID",6,false,$ass_sub->userid));
- fwrite ($bf,full_tag("TIMECREATED",6,false,$ass_sub->timecreated));
- fwrite ($bf,full_tag("TIMEMODIFIED",6,false,$ass_sub->timemodified));
- fwrite ($bf,full_tag("NUMFILES",6,false,$ass_sub->numfiles));
- fwrite ($bf,full_tag("DATA1",6,false,$ass_sub->data1));
- fwrite ($bf,full_tag("DATA2",6,false,$ass_sub->data2));
- fwrite ($bf,full_tag("GRADE",6,false,$ass_sub->grade));
- fwrite ($bf,full_tag("SUBMISSIONCOMMENT",6,false,$ass_sub->submissioncomment));
- fwrite ($bf,full_tag("FORMAT",6,false,$ass_sub->format));
- fwrite ($bf,full_tag("TEACHER",6,false,$ass_sub->teacher));
- fwrite ($bf,full_tag("TIMEMARKED",6,false,$ass_sub->timemarked));
- fwrite ($bf,full_tag("MAILED",6,false,$ass_sub->mailed));
-
- $class = 'assignment_' . $assignment->assignmenttype;
- require_once($CFG->dirroot . '/mod/assignment/lib.php');
- require_once($CFG->dirroot . '/mod/assignment/type/' . $assignment->assignmenttype . '/assignment.class.php');
- call_user_func(array($class, 'backup_one_submission'), $bf, $preferences, $assignment, $ass_sub);
- //End submission
- $status =fwrite ($bf,end_tag("SUBMISSION",5,true));
- }
- //Write end tag
- $status =fwrite ($bf,end_tag("SUBMISSIONS",4,true));
- }
- return $status;
- }
-
- //Backup assignment files because we've selected to backup user info
- //and files are user info's level
- function backup_assignment_files($bf,$preferences) {
- global $CFG, $DB;
-
- $status = true;
-
- //First we check to moddata exists and create it as necessary
- //in temp/backup/$backup_code dir
- $status = check_and_create_moddata_dir($preferences->backup_unique_code);
- //Now copy the assignment dir
- if ($status) {
- //Only if it exists !! Thanks to Daniel Miksik.
- if (is_dir($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/assignment")) {
- $status = backup_copy_file($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/assignment",
- $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/assignment");
- }
- }
-
- return $status;
-
- }
-
- function backup_assignment_files_instance($bf,$preferences,$instanceid) {
- global $CFG, $DB;
-
- $status = true;
-
- //First we check to moddata exists and create it as necessary
- //in temp/backup/$backup_code dir
- $status = check_and_create_moddata_dir($preferences->backup_unique_code);
- $status = check_dir_exists($CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/assignment/",true);
- //Now copy the assignment dir
- if ($status) {
- //Only if it exists !! Thanks to Daniel Miksik.
- if (is_dir($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/assignment/".$instanceid)) {
- $status = backup_copy_file($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/assignment/".$instanceid,
- $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/assignment/".$instanceid);
- }
- }
-
- return $status;
-
- }
-
- //Return an array of info (name,value)
- function assignment_check_backup_mods($course,$user_data=false,$backup_unique_code,$instances=null) {
- if (!empty($instances) && is_array($instances) && count($instances)) {
- $info = array();
- foreach ($instances as $id => $instance) {
- $info += assignment_check_backup_mods_instances($instance,$backup_unique_code);
- }
- return $info;
- }
- //First the course data
- $info[0][0] = get_string("modulenameplural","assignment");
- if ($ids = assignment_ids ($course)) {
- $info[0][1] = count($ids);
- } else {
- $info[0][1] = 0;
- }
-
- //Now, if requested, the user_data
- if ($user_data) {
- $info[1][0] = get_string("submissions","assignment");
- if ($ids = assignment_submission_ids_by_course ($course)) {
- $info[1][1] = count($ids);
- } else {
- $info[1][1] = 0;
- }
- }
- return $info;
- }
-
- //Return an array of info (name,value)
- function assignment_check_backup_mods_instances($instance,$backup_unique_code) {
- $info[$instance->id.'0'][0] = '<b>'.$instance->name.'</b>';
- $info[$instance->id.'0'][1] = '';
- if (!empty($instance->userdata)) {
- $info[$instance->id.'1'][0] = get_string("submissions","assignment");
- if ($ids = assignment_submission_ids_by_instance ($instance->id)) {
- $info[$instance->id.'1'][1] = count($ids);
- } else {
- $info[$instance->id.'1'][1] = 0;
- }
- }
- return $info;
- }
-
- //Return a content encoded to support interactivities linking. Every module
- //should have its own. They are called automatically from the backup procedure.
- function assignment_encode_content_links ($content,$preferences) {
- global $CFG;
-
- $base = preg_quote($CFG->wwwroot,"/");
-
- //Link to the list of assignments
- $buscar="/(".$base."\/mod\/assignment\/index.php\?id\=)([0-9]+)/";
- $result= preg_replace($buscar,'$@ASSIGNMENTINDEX*$2@$',$content);
-
- //Link to assignment view by moduleid
- $buscar="/(".$base."\/mod\/assignment\/view.php\?id\=)([0-9]+)/";
- $result= preg_replace($buscar,'$@ASSIGNMENTVIEWBYID*$2@$',$result);
-
- return $result;
- }
-
- // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
-
- //Returns an array of assignments id
- function assignment_ids ($course) {
- global $CFG, $DB;
-
- return $DB->get_records_sql ("SELECT a.id, a.course
- FROM {assignment} a
- WHERE a.course = ?", array($course));
- }
-
- //Returns an array of assignment_submissions id
- function assignment_submission_ids_by_course ($course) {
- global $CFG, $DB;
-
- return $DB->get_records_sql ("SELECT s.id , s.assignment
- FROM {assignment_submissions} s,
- {assignment} a
- WHERE a.course = ? AND
- s.assignment = a.id", array($course));
- }
-
- //Returns an array of assignment_submissions id
- function assignment_submission_ids_by_instance ($instanceid) {
- global $CFG, $DB;
-
- return $DB->get_records_sql ("SELECT s.id , s.assignment
- FROM {assignment_submissions} s
- WHERE s.assignment = ?", array($instanceid));
- }
-
//
//-----------------------------------------------------------
- //This function executes all the restore procedure about this mod
- function assignment_restore_mods($mod,$restore) {
- global $CFG, $DB;
-
- $status = true;
-
- //Get record from backup_ids
- $data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
-
- if ($data) {
- //Now get completed xmlized object
- $info = $data->info;
- //if necessary, write to restorelog and adjust date/time fields
- if ($restore->course_startdateoffset) {
- restore_log_date_changes('Assignment', $restore, $info['MOD']['#'], array('TIMEDUE', 'TIMEAVAILABLE'));
- }
- //traverse_xmlize($info); //Debug
- //print_object ($GLOBALS['traverse_array']); //Debug
- //$GLOBALS['traverse_array']=""; //Debug
-
- //Now, build the ASSIGNMENT record structure
- $assignment->course = $restore->course_id;
- $assignment->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
- $assignment->intro = backup_todb($info['MOD']['#']['DESCRIPTION']['0']['#']);
- $assignment->introformat = backup_todb($info['MOD']['#']['FORMAT']['0']['#']);
- $assignment->resubmit = backup_todb($info['MOD']['#']['RESUBMIT']['0']['#']);
- $assignment->preventlate = backup_todb($info['MOD']['#']['PREVENTLATE']['0']['#']);
- $assignment->emailteachers = backup_todb($info['MOD']['#']['EMAILTEACHERS']['0']['#']);
- $assignment->var1 = backup_todb($info['MOD']['#']['VAR1']['0']['#']);
- $assignment->var2 = backup_todb($info['MOD']['#']['VAR2']['0']['#']);
- $assignment->var3 = backup_todb($info['MOD']['#']['VAR3']['0']['#']);
- $assignment->var4 = backup_todb($info['MOD']['#']['VAR4']['0']['#']);
- $assignment->var5 = backup_todb($info['MOD']['#']['VAR5']['0']['#']);
- $assignment->type = isset($info['MOD']['#']['TYPE']['0']['#'])?backup_todb($info['MOD']['#']['TYPE']['0']['#']):'';
- $assignment->assignmenttype = backup_todb($info['MOD']['#']['ASSIGNMENTTYPE']['0']['#']);
- $assignment->maxbytes = backup_todb($info['MOD']['#']['MAXBYTES']['0']['#']);
- $assignment->timedue = backup_todb($info['MOD']['#']['TIMEDUE']['0']['#']);
- $assignment->timeavailable = backup_todb($info['MOD']['#']['TIMEAVAILABLE']['0']['#']);
- $assignment->grade = backup_todb($info['MOD']['#']['GRADE']['0']['#']);
- $assignment->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
-
- //We have to recode the grade field if it is <0 (scale)
- if ($assignment->grade < 0) {
- $scale = backup_getid($restore->backup_unique_code,"scale",abs($assignment->grade));
- if ($scale) {
- $assignment->grade = -($scale->new_id);
- }
- }
-
- if (empty($assignment->assignmenttype)) { /// Pre 1.5 assignment
- if ($assignment->type == 1) {
- $assignment->assignmenttype = 'uploadsingle';
- } else {
- $assignment->assignmenttype = 'offline';
- }
- }
-
- // skip restore of plugins that are not installed
- static $plugins;
- if (!isset($plugins)) {
- $plugins = array_keys(get_plugin_list('assignment'));
- }
-
- if (!in_array($assignment->assignmenttype, $plugins)) {
- if (!defined('RESTORE_SILENTLY')) {
- echo "<li><strong>".get_string("modulename","assignment")." \"".format_string($assignment->name,true)."\" - plugin '{$assignment->assignmenttype}' not available!</strong></li>";
- }
- return true; // do not fail the restore
- }
-
- //The structure is equal to the db, so insert the assignment
- $newid = $DB->insert_record ("assignment",$assignment);
-
- //Do some output
- if (!defined('RESTORE_SILENTLY')) {
- echo "<li>".get_string("modulename","assignment")." \"".format_string($assignment->name,true)."\"</li>";
- }
- backup_flush(300);
-
- if ($newid) {
- //We have the newid, update backup_ids
- backup_putid($restore->backup_unique_code,$mod->modtype,
- $mod->id, $newid);
-
- // load up the subtype and see if it wants anything further restored.
- $class = 'assignment_' . $assignment->assignmenttype;
- require_once($CFG->dirroot . '/mod/assignment/lib.php');
- require_once($CFG->dirroot . '/mod/assignment/type/' . $assignment->assignmenttype . '/assignment.class.php');
- call_user_func(array($class, 'restore_one_mod'), $info, $restore, $assignment);
-
- //Now check if want to restore user data and do it.
- if (restore_userdata_selected($restore,'assignment',$mod->id)) {
- //Restore assignmet_submissions
- $status = assignment_submissions_restore_mods($mod->id, $newid,$info,$restore) && $status;
- $status = assignment_submissions_restore_mods($mod->id, $newid,$info,$restore, $assignment) && $status;
- }
- } else {
- $status = false;
- }
- } else {
- $status = false;
- }
-
- return $status;
- }
-
- //This function restores the assignment_submissions
- function assignment_submissions_restore_mods($old_assignment_id, $new_assignment_id,$info,$restore, $assignment) {
- global $CFG, $DB;
-
- $status = true;
-
- //Get the submissions array - it might not be present
- if (isset($info['MOD']['#']['SUBMISSIONS']['0']['#']['SUBMISSION'])) {
- $submissions = $info['MOD']['#']['SUBMISSIONS']['0']['#']['SUBMISSION'];
- } else {
- $submissions = array();
- }
-
- //Iterate over submissions
- for($i = 0; $i < sizeof($submissions); $i++) {
- $sub_info = $submissions[$i];
- //traverse_xmlize($sub_info); //Debug
- //print_object ($GLOBALS['traverse_array']); //Debug
- //$GLOBALS['traverse_array']=""; //Debug
-
- //We'll need this later!!
- $oldid = backup_todb($sub_info['#']['ID']['0']['#']);
- $olduserid = backup_todb($sub_info['#']['USERID']['0']['#']);
-
- //Now, build the ASSIGNMENT_SUBMISSIONS record structure
- $submission->assignment = $new_assignment_id;
- $submission->userid = backup_todb($sub_info['#']['USERID']['0']['#']);
- $submission->timecreated = backup_todb($sub_info['#']['TIMECREATED']['0']['#']);
- $submission->timemodified = backup_todb($sub_info['#']['TIMEMODIFIED']['0']['#']);
- $submission->numfiles = backup_todb($sub_info['#']['NUMFILES']['0']['#']);
- $submission->data1 = backup_todb($sub_info['#']['DATA1']['0']['#']);
- $submission->data2 = backup_todb($sub_info['#']['DATA2']['0']['#']);
- $submission->grade = backup_todb($sub_info['#']['GRADE']['0']['#']);
- if (isset($sub_info['#']['COMMENT']['0']['#'])) {
- $submission->submissioncomment = backup_todb($sub_info['#']['COMMENT']['0']['#']);
- } else {
- $submission->submissioncomment = backup_todb($sub_info['#']['SUBMISSIONCOMMENT']['0']['#']);
- }
- $submission->format = backup_todb($sub_info['#']['FORMAT']['0']['#']);
- $submission->teacher = backup_todb($sub_info['#']['TEACHER']['0']['#']);
- $submission->timemarked = backup_todb($sub_info['#']['TIMEMARKED']['0']['#']);
- $submission->mailed = backup_todb($sub_info['#']['MAILED']['0']['#']);
-
- //We have to recode the userid field
- $user = backup_getid($restore->backup_unique_code,"user",$submission->userid);
- if ($user) {
- $submission->userid = $user->new_id;
- }
-
- //We have to recode the teacher field
- $user = backup_getid($restore->backup_unique_code,"user",$submission->teacher);
- if ($user) {
- $submission->teacher = $user->new_id;
- }
-
- //The structure is equal to the db, so insert the assignment_submission
- $newid = $DB->insert_record ("assignment_submissions",$submission);
-
- //Do some output
- if (($i+1) % 50 == 0) {
- if (!defined('RESTORE_SILENTLY')) {
- echo ".";
- if (($i+1) % 1000 == 0) {
- echo "<br />";
- }
- }
- backup_flush(300);
- }
-
- if ($newid) {
- //We have the newid, update backup_ids
- backup_putid($restore->backup_unique_code,"assignment_submission",$oldid,
- $newid);
-
- //Now copy moddata associated files
- $status = assignment_restore_files ($old_assignment_id, $new_assignment_id,
- $olduserid, $submission->userid, $restore);
-
- $submission->id = $newid;
- $class = 'assignment_' . $assignment->assignmenttype;
- require_once($CFG->dirroot . '/mod/assignment/lib.php');
- require_once($CFG->dirroot . '/mod/assignment/type/' . $assignment->assignmenttype . '/assignment.class.php');
- call_user_func(array($class, 'restore_one_submission'), $sub_info, $restore, $assignment, $submission);
- } else {
- $status = false;
- }
- }
-
- return $status;
- }
-
- //This function copies the assignment related info from backup temp dir to course moddata folder,
- //creating it if needed and recoding everything (assignment id and user id)
- function assignment_restore_files ($oldassid, $newassid, $olduserid, $newuserid, $restore) {
-
- global $CFG;
-
- $status = true;
- $todo = false;
- $moddata_path = "";
- $assignment_path = "";
- $temp_path = "";
-
- //First, we check to "course_id" exists and create is as necessary
- //in CFG->dataroot
- $dest_dir = $CFG->dataroot."/".$restore->course_id;
- $status = check_dir_exists($dest_dir,true);
-
- //Now, locate course's moddata directory
- $moddata_path = $CFG->dataroot."/".$restore->course_id."/".$CFG->moddata;
-
- //Check it exists and create it
- $status = check_dir_exists($moddata_path,true);
-
- //Now, locate assignment directory
- if ($status) {
- $assignment_path = $moddata_path."/assignment";
- //Check it exists and create it
- $status = check_dir_exists($assignment_path,true);
- }
-
- //Now locate the temp dir we are gong to restore
- if ($status) {
- $temp_path = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code.
- "/moddata/assignment/".$oldassid."/".$olduserid;
- //Check it exists
- if (is_dir($temp_path)) {
- $todo = true;
- }
- }
-
- //If todo, we create the neccesary dirs in course moddata/assignment
- if ($status and $todo) {
- //First this assignment id
- $this_assignment_path = $assignment_path."/".$newassid;
- $status = check_dir_exists($this_assignment_path,true);
- //Now this user id
- $user_assignment_path = $this_assignment_path."/".$newuserid;
- //And now, copy temp_path to user_assignment_path
- $status = backup_copy_file($temp_path, $user_assignment_path);
- }
-
- return $status;
- }
-
- //Return a content decoded to support interactivities linking. Every module
- //should have its own. They are called automatically from
- //assignment_decode_content_links_caller() function in each module
- //in the restore process
- function assignment_decode_content_links ($content,$restore) {
-
- global $CFG;
-
- $result = $content;
-
- //Link to the list of assignments
-
- $searchstring='/\$@(ASSIGNMENTINDEX)\*([0-9]+)@\$/';
- //We look for it
- preg_match_all($searchstring,$content,$foundset);
- //If found, then we are going to look for its new id (in backup tables)
- if ($foundset[0]) {
- //print_object($foundset); //Debug
- //Iterate over foundset[2]. They are the old_ids
- foreach($foundset[2] as $old_id) {
- //We get the needed variables here (course id)
- $rec = backup_getid($restore->backup_unique_code,"course",$old_id);
- //Personalize the searchstring
- $searchstring='/\$@(ASSIGNMENTINDEX)\*('.$old_id.')@\$/';
- //If it is a link to this course, update the link to its new location
- if($rec->new_id) {
- //Now replace it
- $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/assignment/index.php?id='.$rec->new_id,$result);
- } else {
- //It's a foreign link so leave it as original
- $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/assignment/index.php?id='.$old_id,$result);
- }
- }
- }
-
- //Link to assignment view by moduleid
-
- $searchstring='/\$@(ASSIGNMENTVIEWBYID)\*([0-9]+)@\$/';
- //We look for it
- preg_match_all($searchstring,$result,$foundset);
- //If found, then we are going to look for its new id (in backup tables)
- if ($foundset[0]) {
- //print_object($foundset); //Debug
- //Iterate over foundset[2]. They are the old_ids
- foreach($foundset[2] as $old_id) {
- //We get the needed variables here (course_modules id)
- $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
- //Personalize the searchstring
- $searchstring='/\$@(ASSIGNMENTVIEWBYID)\*('.$old_id.')@\$/';
- //If it is a link to this course, update the link to its new location
- if($rec->new_id) {
- //Now replace it
- $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/assignment/view.php?id='.$rec->new_id,$result);
- } else {
- //It's a foreign link so leave it as original
- $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/assignment/view.php?id='.$old_id,$result);
- }
- }
- }
-
- return $result;
- }
-
- //This function makes all the necessary calls to xxxx_decode_content_links()
- //function in each module, passing them the desired contents to be decoded
- //from backup format to destination site/course in order to mantain inter-activities
- //working in the backup/restore process. It's called from restore_decode_content_links()
- //function in restore process
- function assignment_decode_content_links_caller($restore) {
- global $CFG, $DB;
- $status = true;
-
- if ($assignments = $DB->get_records('assignment', array('course'=>$restore->course_id), '', "id,description")) {
- //Iterate over each assignment->description
- $i = 0; //Counter to send some output to the browser to avoid timeouts
- foreach ($assignments as $assignment) {
- //Increment counter
- $i++;
- $content = $assignment->description;
- $result = restore_decode_content_links_worker($content,$restore);
- if ($result != $content) {
- //Update record
- $assignment->description = $result;
- $status = $DB->update_record("assignment",$assignment);
- if (debugging()) {
- if (!defined('RESTORE_SILENTLY')) {
- echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
- }
- }
- }
- //Do some output
- if (($i+1) % 5 == 0) {
- if (!defined('RESTORE_SILENTLY')) {
- echo ".";
- if (($i+1) % 100 == 0) {
- echo "<br />";
- }
- }
- backup_flush(300);
- }
- }
- }
- return $status;
- }
-
- //This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for
- //some texts in the module
- function assignment_restore_wiki2markdown ($restore) {
- global $CFG, $DB;
-
- $status = true;
-
- //Convert assignment->description
- if ($records = $DB->get_records_sql ("SELECT a.id, a.intro, a.introformat
- FROM {assignment} a, {backup_ids} b
- WHERE a.course = ? AND
- a.format = ".FORMAT_WIKI. " AND
- b.backup_code = ? AND
- b.table_name = 'assignment' AND
- b.new_id = a.id", array($restore->course_id, $restore->backup_unique_code))) {
- foreach ($records as $record) {
- //Rebuild wiki links
- $record->intro = restore_decode_wiki_content($record->intro, $restore);
- //Convert to Markdown
- $wtm = new WikiToMarkdown();
- $record->intro = $wtm->convert($record->intro, $restore->course_id);
- $record->introformat = FORMAT_MARKDOWN;
- $status = $DB->update_record('assignment', $record);
- //Do some output
- $i++;
- if (($i+1) % 1 == 0) {
- if (!defined('RESTORE_SILENTLY')) {
- echo ".";
- if (($i+1) % 20 == 0) {
- echo "<br />";
- }
- }
- backup_flush(300);
- }
- }
-
- }
- return $status;
- }
//This function returns a log record with all the necessay transformations
//done. It's used by restore_log_module() to restore modules log.