-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlti-tools.js
124 lines (85 loc) · 4.97 KB
/
lti-tools.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
(function(){
// ****************************************************************************************************************************************************
// writes out a hyperlink to site followed by link to the LTI page for the tool in question in site (which will launch the tool)
// ****************************************************************************************************************************************************
// ******************************************************
// Assumes only one LTI tool per Sakai page
// ******************************************************
// get tool id for the attribute data-lti-tool-id
var toolId;
var div;
$(document).ready(function(){
toolId = $("#lti-tools").attr("data-lti-tool-id");
div = $(document.getElementById("lti-tools"));
});
var ul = $("<ul class='recorded-lectures-list'/>");
$.getJSON( "/direct/membership.json", function( data ) {
var mems = data.membership_collection;
$.each( mems, function( key, val ) {
// site URL
var url = "/portal"+val.locationReference;
// Site ID
var siteId = url.substring(13);
if (siteId == "!admin") { // ignore
}
else {
// now get details of lti tools on a site,
// NB we need to handle 403 errors as it's possible a second factor of auth may be needed to get a contents listing from some sites
var siteLtiToolsJsonUrl = "/direct/lti/"+siteId+"/contents.json";
try {
$.getJSON( siteLtiToolsJsonUrl, function( data ) {
var listItems = data.list;
// loop over each list element
$.each( listItems, function( key, val ) {
if (val.tool_id == toolId) {
// grab placement ID from JSON
var placementId = val.placement;
// now get launch URL
var pagesJsonUrl = "/direct/site/"+siteId+"/pages.json";
$.getJSON( pagesJsonUrl, function( data ) {
// declare the LTI url
var replayLtiUrl = "/URL/goes/here";
// declare the page name
var pageName;
// for breaking when we've written out the ink to the LTI tool
var haveFoundThePage = false;
// loop over each page, get details of tools on the page
for (i = 0; i < data.length; i++) {
if (haveFoundThePage) { break; }
var tools = data[i].tools;
// if tools is empty, return
if (null == tools) { console.log("No tools on site: "+siteId); return; }
var toolNum = 0;
// ** Optimisation **
// we're assuming there's only 1 (LTI) tool per page so we just get placementId of first tool on site, ie, tools[0]
if (placementId == data[i].tools[0].placementId) {
haveFoundThePage = true;
replayLtiUrl = data[i].tools[0].url;
pageName = data[i].title;
// get site name
var siteTitle;
// If there's a siteId of "info" then this is the guidance site, NB, '/direct/site/info.json' is a reserved URL
if ("info" == siteId) {
siteTitle = "Guidance Site";
ul.append("<li><a href=\"" + url + "\" title=\""+siteTitle+"\">" + siteTitle + "</a> <a href=\"" + replayLtiUrl + "\" title=\""+siteTitle + " (" + pageName +")\">("+pageName+")</a></li>" );
}
else {
var siteJsonUrl = "/direct/site/"+siteId+".json";
$.getJSON( siteJsonUrl, function( data ) {
siteTitle = data.title;
ul.append("<li><a href=\"" + url + "\" title=\""+siteTitle+"\">" + siteTitle + "</a> <a href=\"" + replayLtiUrl + "\" title=\""+siteTitle + " (" + pageName +")\">("+pageName+")</a></li>" );
}); // end get siteJsonUrl
} // end if siteId = info
return; // we've written out the link to the LTI tool, NB, we are assuming there's only one LTI tool on a Sakai page
} // end of if placement id
} // for loop over data
}); // end of page URL get JSON
} // end of if( toolId = )
}); // end of foreach list item
})} catch (err) { console.log("Error getting site contents; "+err.message);}; // end of get site contents / lti tools URL
}
}); // end of for each mems
$('#spinner').hide();
div .append(ul);
}); // end of get JSON memberships
})();