MDL-53716 competency: Backup and restore course competency user data
[moodle.git] / backup / moodle2 / restore_stepslib.php
index 2de6165..b6733e4 100644 (file)
@@ -3066,10 +3066,15 @@ class restore_course_competencies_structure_step extends restore_structure_step
      * @return array
      */
     protected function define_structure() {
+        $userinfo = $this->get_setting_value('users');
         $paths = array(
             new restore_path_element('course_competency', '/course_competencies/competencies/competency'),
-            new restore_path_element('course_competency_settings', '/course_competencies/settings')
+            new restore_path_element('course_competency_settings', '/course_competencies/settings'),
         );
+        if ($userinfo) {
+            $paths[] = new restore_path_element('user_competency_course',
+                '/course_competencies/user_competencies/user_competency');
+        }
         return $paths;
     }
 
@@ -3080,22 +3085,27 @@ class restore_course_competencies_structure_step extends restore_structure_step
      */
     public function process_course_competency_settings($data) {
         global $DB;
-
         $data = (object) $data;
+
+        // We do not restore the course settings during merge.
+        $target = $this->get_task()->get_target();
+        if ($target == backup::TARGET_CURRENT_ADDING || $target == backup::TARGET_EXISTING_ADDING) {
+            return;
+        }
+
         $courseid = $this->task->get_courseid();
-        $exists = \core_competency\course_competency_settings::get_record(array('courseid' => $courseid));
+        $exists = \core_competency\course_competency_settings::record_exists_select('courseid = :courseid',
+            array('courseid' => $courseid));
 
-        // Now update or insert.
+        // Strangely the course settings already exist, let's just leave them as is then.
         if ($exists) {
-            $settings = $exists;
-            $settings->set_pushratingstouserplans($data->pushratingstouserplans);
-            return $settings->update();
-        } else {
-            $data = (object) array('courseid' => $courseid, 'pushratingstouserplans' => $data->pushratingstouserplans);
-            $settings = new \core_competency\course_competency_settings(0, $data);
-            $result = $settings->create();
-            return !empty($result);
+            $this->log('Course competency settings not restored, existing settings have been found.', backup::LOG_WARNING);
+            return;
         }
+
+        $data = (object) array('courseid' => $courseid, 'pushratingstouserplans' => $data->pushratingstouserplans);
+        $settings = new \core_competency\course_competency_settings(0, $data);
+        $settings->create();
     }
 
     /**
@@ -3116,6 +3126,7 @@ class restore_course_competencies_structure_step extends restore_structure_step
         if (!$competency) {
             return;
         }
+        $this->set_mapping(\core_competency\competency::TABLE, $data->id, $competency->get_id());
 
         $params = array(
             'competencyid' => $competency->get_id(),
@@ -3133,6 +3144,41 @@ class restore_course_competencies_structure_step extends restore_structure_step
         }
     }
 
+    /**
+     * Process the user competency course.
+     *
+     * @param array $data The data.
+     */
+    public function process_user_competency_course($data) {
+        global $USER, $DB;
+        $data = (object) $data;
+
+        $data->competencyid = $this->get_mappingid(\core_competency\competency::TABLE, $data->competencyid);
+        if (!$data->competencyid) {
+            // This is strange, the competency does not belong to the course.
+            return;
+        } else if ($data->grade === null) {
+            // We do not need to do anything when there is no grade.
+            return;
+        }
+
+        $data->userid = $this->get_mappingid('user', $data->userid);
+        $shortname = $DB->get_field('course', 'shortname', array('id' => $this->task->get_courseid()), MUST_EXIST);
+
+        // The method add_evidence also sets the course rating.
+        \core_competency\api::add_evidence($data->userid,
+                                           $data->competencyid,
+                                           $this->task->get_contextid(),
+                                           \core_competency\evidence::ACTION_OVERRIDE,
+                                           'evidence_courserestored',
+                                           'core_competency',
+                                           $shortname,
+                                           false,
+                                           null,
+                                           $data->grade,
+                                           $USER->id);
+    }
+
     /**
      * Execute conditions.
      *