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!
This is the code i use and works flawless.
// when published as Scorm on a LMS
let player = GetPlayer();
let lmsAPI = findLMSAPI(this);
let myName = lmsAPI.GetStudentName();
let array = myName.split(',');
let newName = array[1] + ' ' + array[0];
player.SetVar("newUser", newName);
console.log("newUser: "+myName);
//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);
}
}
- willbogen3 months agoCommunity Member
This code worked a charm! Thank you so much :) The only two things I still need are to have the URL have a suffix populate the newUser variable into it, and we strongly prefer to receive the Canvas UserID (which is a 5 digit code) instead of the student's first and last name.
Is it possible to, upon clicking a button with a url, have the user ID populate in the url and also the variable in storyline?- MathNotermans-93 months agoCommunity Member
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.
- willbogen3 months agoCommunity Member
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!