add_to_log in javascript

Re: add_to_log in javascript

by Roddy Boyd -
Number of replies: 0

incase anyone needs this for their site i have a solution that works 80% of the time.

I ended up using AJAX to call my addToLog.php file

so on the '<body>' of the index.html page for my scorm course (could be any course) i changed it to '<body onunload="trackClose();">' I ended up using onunload as it seemed to work better in IE6

then in my template footer i added

"<script type="text/javascript">

var needToConfirm = true;

function trackClose(){   

function getUrlVars(){ //This function gets the variables from the url, because i have a scorm object i need the 'a' and 'scoid' so it shows the course info in the logs which is found in the addToLog.php file
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}

var hash = getUrlVars();
ajaxFunction(hash['a'], hash['scoid']);   
}

function ajaxFunction(a, scoid){
var ajaxRequest;  // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the url
var passData = 'a='+a+'&scoid='+scoid;
ajaxRequest.open("POST", "/mod/scorm/addToLog.php", false); //the "false" var here doesn't let the page close until the request is finished
ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
if (ajaxRequest.readyState==4 || ajaxRequest.readyState=="complete") {
callback(ajaxRequest.responseText, ajaxRequest.status);
alert('Log sent to DB');
}   
ajaxRequest.send(passData);
}

</script>"

and then finally i have my php file that adds the browser close to the moodle reports, which i have attached

 

Hope this helps someone out in the future, need to remember that this was written specifically for scorm objects, if you have a different object you would have to edit the addToLog and the javascript (above) to get the relevant information from the LMS