From 219f956acc7f2eb874956b5fd33e3b2302f3e9eb Mon Sep 17 00:00:00 2001
From: Cameron Ball <cameron@moodle.com>
Date: Tue, 29 Sep 2015 13:41:55 +0800
Subject: [PATCH 1/2] MDL-49143 LTI: Fix missing resource id

---
 mod/lti/locallib.php | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/mod/lti/locallib.php b/mod/lti/locallib.php
index 2803555fb77..1d5941f11b7 100644
--- a/mod/lti/locallib.php
+++ b/mod/lti/locallib.php
@@ -362,6 +362,12 @@ function lti_build_request($instance, $typeconfig, $course, $typeid = null, $isl
         'context_label' => $course->shortname,
         'context_title' => $course->fullname,
     );
+    if (!empty($instance->id)) {
+        $requestparams['resource_link_id'] = $instance->id;
+    }
+    if (!empty($instance->resource_link_id)) {
+        $requestparams['resource_link_id'] = $instance->resource_link_id;
+    }
     if ($course->format == 'site') {
         $requestparams['context_type'] = 'Group';
     } else {
@@ -370,7 +376,7 @@ function lti_build_request($instance, $typeconfig, $course, $typeid = null, $isl
     }
     $placementsecret = $instance->servicesalt;
 
-    if ( isset($placementsecret) && ($islti2 ||
+    if ( !empty($instance->id) && isset($placementsecret) && ($islti2 ||
          $typeconfig['acceptgrades'] == LTI_SETTING_ALWAYS ||
          ($typeconfig['acceptgrades'] == LTI_SETTING_DELEGATE && $instance->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS))) {
 

From 07842a386138a91ea3bac9b86ac7107cd58c8a00 Mon Sep 17 00:00:00 2001
From: "Eloy Lafuente (stronk7)" <stronk7@moodle.org>
Date: Wed, 23 Sep 2015 22:30:39 +0200
Subject: [PATCH 2/2] MDL-49143 LTI: Test resource_link_id in
 lti_build_request()

---
 mod/lti/tests/locallib_test.php | 38 +++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/mod/lti/tests/locallib_test.php b/mod/lti/tests/locallib_test.php
index 18e939c4071..972558ec5e8 100644
--- a/mod/lti/tests/locallib_test.php
+++ b/mod/lti/tests/locallib_test.php
@@ -179,6 +179,44 @@ class mod_lti_locallib_testcase extends advanced_testcase {
         $this->assertEquals('moodle.org//this/is/moodle', lti_get_url_thumbprint('moodle.org/this/is/moodle?foo=bar'));
     }
 
+    /*
+     * Verify that lti_build_request does handle resource_link_id as expected
+     */
+    public function test_lti_buid_request_resource_link_id() {
+        $this->resetAfterTest();
+
+        self::setUser($this->getDataGenerator()->create_user());
+        $course   = $this->getDataGenerator()->create_course();
+        $instance = $this->getDataGenerator()->create_module('lti', array(
+            'intro'       => "<p>This</p>\nhas\r\n<p>some</p>\nnew\n\rlines",
+            'introformat' => FORMAT_HTML,
+            'course'      => $course->id,
+        ));
+
+        $typeconfig = array(
+            'acceptgrades'     => 1,
+            'forcessl'         => 0,
+            'sendname'         => 2,
+            'sendemailaddr'    => 2,
+            'customparameters' => '',
+        );
+
+        // Normal call, we expect $instance->id to be used as resource_link_id.
+        $params = lti_build_request($instance, $typeconfig, $course, null);
+        $this->assertSame($instance->id, $params['resource_link_id']);
+
+        // If there is a resource_link_id set, it gets precedence.
+        $instance->resource_link_id = $instance->id + 99;
+        $params = lti_build_request($instance, $typeconfig, $course, null);
+        $this->assertSame($instance->resource_link_id, $params['resource_link_id']);
+
+        // With none set, resource_link_id is not set either.
+        unset($instance->id);
+        unset($instance->resource_link_id);
+        $params = lti_build_request($instance, $typeconfig, $course, null);
+        $this->assertFalse($params['resource_link_id']);
+    }
+
     /**
      * Test lti_build_request's resource_link_description and ensure
      * that the newlines in the description are correct.