Forum Discussion
My custom javascript cannot pull the Canvas User ID while used in Canvas, but can in SCORM cloud
- 3 months ago
Actually, late last night we got it all working pretty good! Based on your initial code, we made the following script to run on the button when it is clicked:
// when published as Scorm on a LMS let player = GetPlayer(); let lmsAPI = findLMSAPI(this); let myName = lmsAPI.GetStudentID(); //player.SetVar("newUser", myName); //console.log("newUser: "+myName); window.open("https://s3.us-east-2.amazonaws.com/contentplayer****/Samantha/v2/index.html?context="+myName, "samantha"); //Function to connect with LMSAPI function findLMSAPI(win) { if (win.hasOwnProperty("GetStudentID")) { return win; }else if (win.parent == win) { return null; }else { return findLMSAPI(win.parent); } }
This fit our goal of retrieving the user ID, and it works totally on its own with no other scripts or required button presses. It does collect all the extra stuff you showed me, but it did collect the 5 digit code from Canvas that I was looking for.
Thank you so much for the help, I really really appreciate the assistance!
Im not sure whether Scorm has the Canvas UserID. With the Canvas API ( and in Canvas itself ) you for sure can get that ID. As im working on getting Canvas username from the API to use it directly in blocks of HTML ( so no Scorm needed ) that for sure will work in getting the userID.
Actually checking my userObject in Canvas the id is a bit lengthier then 5 ;-)
"id":122477700023471280 ( this ID is random non-existent ;- )
On the Canvas LIVE APi you can test any API calls.
Issue using this directly in Scorm ( or HTML ) on Canvas is CORS security offcourse. Trying to get that fixed now.
Actually, late last night we got it all working pretty good! Based on your initial code, we made the following script to run on the button when it is clicked:
// when published as Scorm on a LMS
let player = GetPlayer();
let lmsAPI = findLMSAPI(this);
let myName = lmsAPI.GetStudentID();
//player.SetVar("newUser", myName);
//console.log("newUser: "+myName);
window.open("https://s3.us-east-2.amazonaws.com/contentplayer****/Samantha/v2/index.html?context="+myName, "samantha");
//Function to connect with LMSAPI
function findLMSAPI(win) {
if (win.hasOwnProperty("GetStudentID")) {
return win;
}else if (win.parent == win) {
return null;
}else {
return findLMSAPI(win.parent);
}
}
This fit our goal of retrieving the user ID, and it works totally on its own with no other scripts or required button presses. It does collect all the extra stuff you showed me, but it did collect the 5 digit code from Canvas that I was looking for.
Thank you so much for the help, I really really appreciate the assistance!