Skip to content

Instantly share code, notes, and snippets.

@ztraboo
Last active August 3, 2018 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ztraboo/4b0d589917f4ec7e25dadca2a5d0bfb6 to your computer and use it in GitHub Desktop.
Save ztraboo/4b0d589917f4ec7e25dadca2a5d0bfb6 to your computer and use it in GitHub Desktop.
Open edX (pdfbook link) - LTI Integration
/*
Author: Zachary Trabookis
Date: 2018-08-02
Description: Update the relative course paths according to LTI usage or not.
*/
function getCoursePathLTI(dataLink) {
var isInIframe = (window.location != window.parent.location) ? true : false;
console.log("In LTI = " + isInIframe);
var coursePath = "";
/* Divide up the LTI URL and rebuild the LMS course path.
* Only do this when serving content over LTI since this content will be within an iframe.
* */
if (isInIframe) {
/* Find the LMS domain path
*
* Course ID (Examples of different LMS course URLS)
* https://edx.readthedocs.io/projects/open-edx-building-and-running-a-course/en/latest/course_features/lti/lti_address_content.html
*
* {key type}:{org}+{course}+{run}, for example, course-v1:edX+DemoX+2014
* https://example.com/lti_provider/courses/course-v1:CUCWD+FAA-PART-147-SGOS101+DEVELOPMENT/block-v1:CUCWD+ID201+DEVELOPMENT+type@sequential+block@5cec8fcf28364c32aa277ab1432mod04
*
* {org}/{course}/{run}, for example, edX/DemoX/2014
* https://example.com/lti_provider/courses/CUCWD/MET101/2014_Spring/i4x://CUCWD/ID101/sequential/061b306f1796481fb2e3008038da1c3c
* */
/* Split based on 'lti_provider' since that's unique to the LTI URL path then assign course path to the URL domain to start. */
var locationHREF = $(location).attr('href').split("lti_provider");
coursePath = locationHREF[0];
/* Find the course id based on different versions (i4x - prior version, course-id - new version) */
var coursePathSubPath = locationHREF[1].split("/");
(locationHREF[1].includes("i4x")) ?
coursePath += coursePathSubPath[1] + "/" + coursePathSubPath[2] + "/" + coursePathSubPath[3] + "/" + coursePathSubPath[4] + "/":
coursePath += coursePathSubPath[1] + "/" + coursePathSubPath[2] + "/";
}
else {
/* When we're not using LTI we need this path to be relative to the current course. */
coursePath = "../../../";
}
console.log("getCoursePathLTI = " + coursePath + dataLink);
return coursePath + dataLink;
}
/*
<!-- Example include for the HTML Compoment. -->
<p>Please visit the <a href="#" class="eBook" data-link="pdfbook/0/chapter/1/1" target="_blank">eBook</a> for more information.</p>
<!-- Include LTI path override in top most HTML Component only. -->
<script src="/static/lti-path.js"></script>
<script>// <![CDATA[
$(document).ready(function () {
// Rewrite the pdfbook link because of LTI
$('a.eBook').attr("href", function () {
return getCoursePathLTI( $(this).attr("data-link") );
});
});
// ]]></script>
*/
<!--
This code updates the 'href' for the anchor tag that's associated with the class "eBook".
Important when you need this to link to continue to work with external LMS through LTI and Open edX instance.
-->
<ul>
<li><a href="#" target="_blank" class="eBook" data-link="pdfbook/0/chapter/1/1">&nbsp;&nbsp;&nbsp;</a></li>
</ul>
<script>// <![CDATA[
$(document).ready(function () {
var isInIframe = (window.location != window.parent.location) ? true : false;
console.log("In LTI = " + isInIframe);
var coursePath = "";
/* Divide up the LTI URL and rebuild the LMS course path.
* Only do this when serving content over LTI since this content will be within an iframe.
* */
if (isInIframe) {
/* Find the LMS domain path
*
* Course ID (Examples of different LMS course URLS)
* https://edx.readthedocs.io/projects/open-edx-building-and-running-a-course/en/latest/course_features/lti/lti_address_content.html
*
* {key type}:{org}+{course}+{run}, for example, course-v1:edX+DemoX+2014
* https://example.com/lti_provider/courses/course-v1:CUCWD+FAA-PART-147-SGOS101+DEVELOPMENT/block-v1:CUCWD+ID201+DEVELOPMENT+type@sequential+block@5cec8fcf28364c32aa277ab1432mod04
*
* {org}/{course}/{run}, for example, edX/DemoX/2014
* https://example.com/lti_provider/courses/CUCWD/MET101/2014_Spring/i4x://CUCWD/ID101/sequential/061b306f1796481fb2e3008038da1c3c
* */
/* Split based on 'lti_provider' since that's unique to the LTI URL path then assign course path to the URL domain to start. */
var locationHREF = $(location).attr('href').split("lti_provider");
coursePath = locationHREF[0];
/* Find the course id based on different versions (i4x - prior version, course-id - new version) */
var coursePathSubPath = locationHREF[1].split("/");
(locationHREF[1].includes("i4x")) ?
coursePath += coursePathSubPath[1] + "/" + coursePathSubPath[2] + "/" + coursePathSubPath[3] + "/" + coursePathSubPath[4] + "/":
coursePath += coursePathSubPath[1] + "/" + coursePathSubPath[2] + "/";
}
else {
/* When we're not using LTI we need this path to be relative to the current course. */
coursePath = "../../../";
}
console.log("coursePath = " + coursePath);
$('a.eBook').attr("href", function () {
return coursePath + $(this).attr("data-link");
});
});
// ]]></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment