From 400085c3a4d3af5981da2605b1dd0ca0687e6281 Mon Sep 17 00:00:00 2001
From: Robert Russo <rrusso@lsu.edu>
Date: Tue, 13 Aug 2013 10:06:51 -0500
Subject: [PATCH 1/1] adds configurable course identifier for LTI external tools

---
 mod/lti/edit_form.php   |   10 ++++++++++
 mod/lti/lang/en/lti.php |    3 +++
 mod/lti/locallib.php    |   30 ++++++++++++++++++++++++++++--
 3 files changed, 41 insertions(+), 2 deletions(-)

diff --git a/mod/lti/edit_form.php b/mod/lti/edit_form.php
index cdc801b..f3354dd 100644
--- a/mod/lti/edit_form.php
+++ b/mod/lti/edit_form.php
@@ -104,6 +104,16 @@ class mod_lti_edit_types_form extends moodleform{
         $mform->addHelpButton('lti_launchcontainer', 'default_launch_container', 'lti');
         $mform->setType('lti_launchcontainer', PARAM_INT);
 
+        $courseidoptions=array();
+        $courseidoptions[LTI_LAUNCH_COURSE_ID_SHORTNAME] = get_string('shortname', 'moodle');
+        $courseidoptions[LTI_LAUNCH_COURSE_ID_IDNUMBER]  = get_string('idnumber', 'moodle');
+        $courseidoptions[LTI_LAUNCH_COURSE_ID_ID]        = get_string('courseid', 'lti');
+
+        $mform->addElement('select', 'lti_courseidoptions', get_string('default_launch_course_id', 'lti'), $courseidoptions);
+        $mform->setDefault('lti_courseidoptions', LTI_LAUNCH_COURSE_ID_SHORTNAME);
+        $mform->addHelpButton('lti_courseidoptions', 'default_launch_course_id', 'lti');
+        $mform->setType('lti_courseidoptions', PARAM_TEXT);
+
         // Add privacy preferences fieldset where users choose whether to send their data
         $mform->addElement('header', 'privacy', get_string('privacy', 'lti'));
 
diff --git a/mod/lti/lang/en/lti.php b/mod/lti/lang/en/lti.php
index 337a87f..0a84e9d 100644
--- a/mod/lti/lang/en/lti.php
+++ b/mod/lti/lang/en/lti.php
@@ -121,6 +121,9 @@ real estate to the tool, and others provide a more integrated feel with the Mood
 * **New window** - The tool opens in a new window, occupying all the available space.
         Depending on the browser, it will open in a new tab or a popup window.
         It is possible that browsers will prevent the new window from opening.';
+$string['default_launch_course_id'] = 'Course Identifier';
+$string['default_launch_course_id_help'] = 'Choose which field in the \'course\' database table will be used
+    to map Moodle courses to External Tool course records. Possible values are: id, shortname or idnumber';
 $string['delegate'] = 'Delegate to Teacher';
 $string['delete'] = 'Delete';
 $string['delete_confirmation'] = 'Are you sure you want to delete this external tool configuration?';
diff --git a/mod/lti/locallib.php b/mod/lti/locallib.php
index 81c40fd..7f89343 100644
--- a/mod/lti/locallib.php
+++ b/mod/lti/locallib.php
@@ -62,6 +62,10 @@ define('LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS', 3);
 define('LTI_LAUNCH_CONTAINER_WINDOW', 4);
 define('LTI_LAUNCH_CONTAINER_REPLACE_MOODLE_WINDOW', 5);
 
+define('LTI_LAUNCH_COURSE_ID_SHORTNAME', 0);
+define('LTI_LAUNCH_COURSE_ID_IDNUMBER', 1);
+define('LTI_LAUNCH_COURSE_ID_ID', 2);
+
 define('LTI_TOOL_STATE_ANY', 0);
 define('LTI_TOOL_STATE_CONFIGURED', 1);
 define('LTI_TOOL_STATE_PENDING', 2);
@@ -228,6 +232,22 @@ function lti_build_request($instance, $typeconfig, $course) {
 
     $role = lti_get_ims_role($USER, $instance->cmid, $instance->course);
 
+    if(isset($typeconfig['courseidoptions'])) {
+        switch ($typeconfig['courseidoptions']) {
+            case 0:
+                $label = $course->shortname;
+                break;
+            case 1:
+                $label = $course->idnumber;
+                break;
+            case 2:
+                $label = $course->id;
+                break;
+        }
+    } else {
+        $label = $course->shortname;
+    }
+
     $requestparams = array(
         'resource_link_id' => $instance->id,
         'resource_link_title' => $instance->name,
@@ -235,7 +255,7 @@ function lti_build_request($instance, $typeconfig, $course) {
         'user_id' => $USER->id,
         'roles' => $role,
         'context_id' => $course->id,
-        'context_label' => $course->shortname,
+        'context_label' => $label,
         'context_title' => $course->fullname,
         'launch_presentation_locale' => current_language()
     );
@@ -790,7 +810,6 @@ function lti_get_type_config_from_instance($id) {
     if (isset($config['instructorchoiceallowroster'])) {
         $type->lti_allowroster = $config['instructorchoiceallowroster'];
     }
-
     if (isset($config['instructorcustomparameters'])) {
         $type->lti_allowsetting = $config['instructorcustomparameters'];
     }
@@ -883,6 +902,10 @@ function lti_get_type_type_config($id) {
         $type->lti_module_class_type = $config['module_class_type'];
     }
 
+    if (isset($config['courseidoptions'])) {
+        $type->lti_courseidoptions = $config['courseidoptions'];
+    }
+
     return $type;
 }
 
@@ -899,6 +922,9 @@ function lti_prepare_type_for_save($type, $config) {
 
     $type->timemodified = time();
 
+    if(isset($config->lti_courseidoptions)){
+        $type->courseidoptions = $config->lti_courseidoptions;
+    }
     unset ($config->lti_typename);
     unset ($config->lti_toolurl);
 }
-- 
1.7.1

