]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-19142: assignment/backup add support for third party subtypes
authormjollnir_ <mjollnir_>
Tue, 11 Aug 2009 12:20:08 +0000 (12:20 +0000)
committermjollnir_ <mjollnir_>
Tue, 11 Aug 2009 12:20:08 +0000 (12:20 +0000)
Now third party assignment type subplugin authors can implement backup/restore

This adds four hooks, backup_one_mod, backup_one_submission, restore_one_mod, restore_one_submission,
all of which are implemented in the assignment_base parent class and documented there.

Merged from MOODLE_19_STABLE.  Backup is broken in HEAD but Eloy asked me to merge it anyway.

mod/assignment/backuplib.php
mod/assignment/lib.php
mod/assignment/restorelib.php

index 06f24a94f5c86d639a097e1e9f718a658c7851c6..a7445e186e48aacc992805287476894d17c29515 100644 (file)
         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));
+
+        assignment_ensure_backup_subtype($assignment, $preferences->backup_course);
+        $assignment->atypeobj->backup_one_mod($bf, $preferences);
         //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->id);
+            $status = backup_assignment_submissions($bf,$preferences,$assignment);
             if ($status) {
                 $status = backup_assignment_files_instance($bf,$preferences,$assignment->id);
             }
@@ -91,7 +94,7 @@
 
         $status = true;
 
-        $assignment_submissions = $DB->get_records("assignment_submissions", array("assignment"=>$assignment),"id");
+        $assignment_submissions = $DB->get_records("assignment_submissions", array("assignment"=>$assignment->id),"id");
         //If there is submissions
         if ($assignment_submissions) {
             //Write start tag
                 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));       
+
+                assignment_ensure_backup_subtype($assignment, $preferences->backup_course);
+                $assignment->atypeobj->backup_one_submission($bf, $preferences, $ass_sub);
                 //End submission
                 $status =fwrite ($bf,end_tag("SUBMISSION",5,true));
             }
                                         FROM {assignment_submissions} s
                                        WHERE s.assignment = ?", array($instanceid));
     }
+
+    /**
+     * small helper function to take the current assignment record
+     * and create the appropriate subtype object and store it
+     *
+     * @param $assignment the assignment db record (passed by reference and modified)
+     * @param $courseid the courseid
+     *
+     * @return void
+     */
+    function assignment_ensure_backup_subtype(&$assignment, $courseid) {
+        global $CFG, $DB;
+        static $course;
+        if (empty($course)) {
+            $course = $DB->get_record('course', array('id' => $courseid));
+        }
+        if (empty($assignment->atypeobj)) {
+            $class = 'assignment_' . $assignment->assignmenttype;
+            require_once($CFG->dirroot . '/mod/assignment/lib.php');
+            require_once($CFG->dirroot . '/mod/assignment/type/' . $assignment->assignmenttype . '/assignment.class.php');
+            $cm = get_coursemodule_from_instance('assignment', $assignment->id, $courseid);
+            $assignment->atypeobj = new $class($cm->id, $assignment, $cm, $course);
+        }
+    }
 ?>
index d75545c4960c45e8ef37f2d3189a507c763bb273..30a9b5700c27e1528a6df0d59b41ec4875048393 100644 (file)
@@ -2012,6 +2012,61 @@ class assignment_base {
     function portfolio_exportable() {
         return false;
     }
+
+    /**
+     * base implementation for backing up subtype specific information
+     * for one single module
+     *
+     * @param filehandle $bf file handle for xml file to write to
+     * @param mixed $preferences the complete backup preference object
+     *
+     * @return boolean
+     */
+    function backup_one_mod($bf, $preferences) {
+        return true;
+    }
+
+    /**
+     * base implementation for backing up subtype specific information
+     * for one single submission
+     *
+     * @param filehandle $bf file handle for xml file to write to
+     * @param mixed $preferences the complete backup preference object
+     * @param object $submission the assignment submission db record
+     *
+     * @return boolean
+     */
+    function backup_one_submission($bf, $preferences, $submission) {
+        return true;
+    }
+
+    /**
+     * base implementation for restoring subtype specific information
+     * for one single module
+     *
+     * @param array  $info the array representing the xml
+     * @param object $restore the restore preferences
+     *
+     * @return boolean
+     */
+    function restore_one_mod($info, $restore) {
+        return true;
+    }
+
+    /**
+     * base implementation for restoring subtype specific information
+     * for one single submission
+     *
+     * @param object $submission the newly created submission
+     * @param array  $info the array representing the xml
+     * @param object $restore the restore preferences
+     *
+     * @return boolean
+     */
+    function restore_one_submission($submission, $info, $restore) {
+        return true;
+    }
+
 } ////// End of the assignment_base class
 
 /**
index c33217458f9e8e1bc6dbf784b52bada20789381b..1764d2a67beb211b7dc32db5c1cb141bace2bfc0 100644 (file)
                 //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');
+                $cmid = backup_getid($restore->backup_unique_code, 'course_modules', $mod->id);
+                $cm = $DB->get_record('course_modules',  array('id' => $cmid->new_id));
+                $cm->instance = $newid;
+                $subtype = new $class($cmid->id, $assignment, $cm);
+                $assignment->id = $newid;
+
+                $subtype->restore_one_mod($info, $restore);
+
                 //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, $subtype) && $status;
                 }
             } else {
                 $status = false;
     }
 
     //This function restores the assignment_submissions
-    function assignment_submissions_restore_mods($old_assignment_id, $new_assignment_id,$info,$restore) {
+    function assignment_submissions_restore_mods($old_assignment_id, $new_assignment_id,$info,$restore, $subtype) {
         global $CFG, $DB;
 
         $status = true;
                 $status = assignment_restore_files ($old_assignment_id, $new_assignment_id, 
                                                     $olduserid, $submission->userid, $restore);
 
+                $submission->id = $newid;
+                $status = $subtype->restore_one_submission($submission, $info, $restore);
             } else {
                 $status = false;
             }