MDL-67178 mod_forum: Allow users to backup forum grades
authorMathew May <mathewm@hotmail.co.nz>
Fri, 8 Nov 2019 04:08:01 +0000 (12:08 +0800)
committerMathew May <mathewm@hotmail.co.nz>
Wed, 13 Nov 2019 05:25:54 +0000 (13:25 +0800)
mod/forum/backup/moodle2/backup_forum_stepslib.php
mod/forum/backup/moodle2/restore_forum_stepslib.php

index 09498e5..8255dd8 100644 (file)
@@ -44,7 +44,7 @@ class backup_forum_activity_structure_step extends backup_activity_structure_ste
             'maxbytes', 'maxattachments', 'forcesubscribe', 'trackingtype',
             'rsstype', 'rssarticles', 'timemodified', 'warnafter',
             'blockafter', 'blockperiod', 'completiondiscussions', 'completionreplies',
-            'completionposts', 'displaywordcount', 'lockdiscussionafter'));
+            'completionposts', 'displaywordcount', 'lockdiscussionafter', 'grade_forum'));
 
         $discussions = new backup_nested_element('discussions');
 
@@ -96,6 +96,17 @@ class backup_forum_activity_structure_step extends backup_activity_structure_ste
         $track = new backup_nested_element('track', array('id'), array(
             'userid'));
 
+        $grades = new backup_nested_element('grades');
+
+        $grade = new backup_nested_element('grade', ['id'], [
+            'forum',
+            'itemnumber',
+            'userid',
+            'grade',
+            'timecreated',
+            'timemodified',
+        ]);
+
         // Build the tree
 
         $forum->add_child($discussions);
@@ -116,6 +127,9 @@ class backup_forum_activity_structure_step extends backup_activity_structure_ste
         $forum->add_child($tags);
         $tags->add_child($tag);
 
+        $forum->add_child($grades);
+        $grades->add_child($grade);
+
         $discussion->add_child($posts);
         $posts->add_child($post);
 
@@ -166,6 +180,8 @@ class backup_forum_activity_structure_step extends backup_activity_structure_ste
                     backup_helper::is_sqlparam('mod_forum'),
                     backup::VAR_CONTEXTID));
             }
+
+            $grade->set_source_table('forum_grades', array('forum' => backup::VAR_PARENTID));
         }
 
         // Define id annotations
@@ -190,6 +206,9 @@ class backup_forum_activity_structure_step extends backup_activity_structure_ste
 
         $track->annotate_ids('user', 'userid');
 
+        $grade->annotate_ids('userid', 'userid');
+
+        $grade->annotate_ids('forum', 'forum');
         // Define file annotations
 
         $forum->annotate_files('mod_forum', 'intro', null); // This file area hasn't itemid
index 593e353..1da3294 100644 (file)
@@ -47,6 +47,7 @@ class restore_forum_activity_structure_step extends restore_activity_structure_s
             $paths[] = new restore_path_element('forum_digest', '/activity/forum/digests/digest');
             $paths[] = new restore_path_element('forum_read', '/activity/forum/readposts/read');
             $paths[] = new restore_path_element('forum_track', '/activity/forum/trackedprefs/track');
+            $paths[] = new restore_path_element('forum_grade', '/activity/forum/grades/grade');
         }
 
         // Return the paths wrapped into standard activity structure
@@ -214,6 +215,27 @@ class restore_forum_activity_structure_step extends restore_activity_structure_s
         $newitemid = $DB->insert_record('forum_digests', $data);
     }
 
+    protected function process_forum_grade($data) {
+        global $DB;
+
+        $data = (object)$data;
+        $oldid = $data->id;
+
+        $data->forum = $this->get_new_parentid('forum');
+
+        $data->userid = $this->get_mappingid('user', $data->userid);
+
+        // We want to ensure the current user has an ID that we can associate to a grade.
+        if ($data->userid != 0) {
+            $newitemid = $DB->insert_record('forum_grades', $data);
+
+            // Note - the old contextid is required in order to be able to restore files stored in
+            // sub plugin file areas attached to the gradeid.
+            $this->set_mapping('grade', $oldid, $newitemid, false, null, $this->task->get_old_contextid());
+            $this->set_mapping(restore_gradingform_plugin::itemid_mapping('forum'), $oldid, $newitemid);
+        }
+    }
+
     protected function process_forum_read($data) {
         global $DB;