MDL-68800 mod_lti: create gbs only for restored activity
[moodle.git] / mod / lti / service / gradebookservices / backup / moodle2 / restore_ltiservice_gradebookservices_subplugin.class.php
index 7d39b59..b11d19e 100644 (file)
@@ -213,25 +213,24 @@ class restore_ltiservice_gradebookservices_subplugin extends restore_subplugin {
                 $DB->update_record('ltiservice_gradebookservices', $gbs);
             }
         }
-        // Pre 3.9 backups did not include a gradebookservices record. We create one here if idnumber is set.
-        $gradeitems = $DB->get_records('grade_items', array('itemtype' => 'mod', 'itemmodule' => 'lti', 'courseid' => $courseid));
-        foreach ($gradeitems as $gi) {
-            if (isset($gi->idnumber) && !empty(trim($gi->idnumber))) {
-                $gbs = $DB->get_records('ltiservice_gradebookservices', ['gradeitemid' => $gi->id]);
-                if (empty($gbs)  && !empty($gi->iteminstance)) {
-                    // We did not find an entry for an LTI grade item with an idnumber, so let's create a gbs entry.
-                    if ($instance = $DB->get_record('lti', array('id' => $gi->iteminstance))) {
-                        if ($tool = lti_get_instance_type($instance)) {
-                            $DB->insert_record('ltiservice_gradebookservices', (object) array(
-                                'gradeitemid' => $gi->id,
-                                'courseid' => $courseid,
-                                'toolproxyid' => $tool->toolproxyid,
-                                'ltilinkid' => $gi->iteminstance,
-                                'typeid' => $tool->id,
-                                'baseurl' => $tool->baseurl,
-                                'resourceid' => $gi->idnumber
-                            ));
-                        }
+        // Pre 3.9 backups did not include a gradebookservices record. Adding one here if missing for the restored instance.
+        $gi = $DB->get_record('grade_items', array('itemtype' => 'mod', 'itemmodule' => 'lti', 'courseid' => $courseid,
+            'iteminstance' => $this->task->get_activityid()));
+        if ($gi) {
+            $gbs = $DB->get_records('ltiservice_gradebookservices', ['gradeitemid' => $gi->id]);
+            if (empty($gbs)) {
+                // The currently restored LTI link has a grade item but no gbs, so let's create a gbs entry.
+                if ($instance = $DB->get_record('lti', array('id' => $gi->iteminstance))) {
+                    if ($tool = lti_get_instance_type($instance)) {
+                        $DB->insert_record('ltiservice_gradebookservices', (object) array(
+                            'gradeitemid' => $gi->id,
+                            'courseid' => $courseid,
+                            'toolproxyid' => $tool->toolproxyid,
+                            'ltilinkid' => $gi->iteminstance,
+                            'typeid' => $tool->id,
+                            'baseurl' => $tool->baseurl,
+                            'resourceid' => $gi->idnumber
+                        ));
                     }
                 }
             }