HOME ➔ SUPPORT ➔ Community ➔ General CourseLab issues ... JavaScript functional sample?
JavaScript functional sample?
  View type:
CourseLab is shaping up to be a superb alternative to high-priced authoring tools, bravo!

I've read every thread related to JavaScript in the Actions, and in particular, how to retrieve object variables. But I'm struggling to understand how I can instantiate local variables, so that I can pass these object properties onto a script.

For example, I'd like to get the scaled score of my "Current Results" object by calling JavaScript in the Actions menu of my test object (called in the "On Test End" event):

//begin code
//create our score object constructor
function Score(objectiveID, rawScore, scaledScore, maxScore) {
this.objectiveID=objectiveID;
this.rawScore=rawScore;
this.scaledScore=scaledScore;
this.maxScore=maxScore;
}

//create our local score object
theScore = new Score(g_arSlideVars['$OBJ_10.objectiveID'], g_arSlideVars["$OBJ_10.rawScore"], g_arSlideVars["$OBJ_10.scaledScore"], g_arSlideVars["$OBJ_10.maxScore"]);

//override the base theScore.toString()
Score.prototype.toString = function scoreToString() {
var ret = this.objectiveID + ", " + this.rawScore + ", " + this.scaledScore + ", " + this.maxScore;
return ret;
}

//display the overridden string value of theScore (instead of returning [object Object])
alert(theScore.toString); //returns score objects
//end code

This returns "undefinded, undefined, undefined, undefined." If I change “alert(theScore.toString);” to “alert(theScore.toString());” I get function scoreToString () {
var ret = this.objectiveID + ", " + this.rawScore + ", " + this.scaledScore + ", " + this.maxScore;
return ret;”

Can you please provide a working example?

I already have functional JavaScript code, which is capable of sending information to a SharePoint list (via a SOAP enclosure). Despite the availability of SharePoint Learning Kit, I am not in a position to install a server template, but I would like to track simple, internal variables without the need for an LMS or near-LMS. Once I can get this final piece of information, I'll be happy to publish the code for all those who may be intereste
 
The easiest way to use this snippet is to:
(1) create a new course;
(2) add a new page;
(3) add a test object and a quiz results object;
(4) name the quiz results object "OBJ_10";
(5) create an action on the quiz object's "onTestEnd" event;
(6) close the project and open 1.xml with your text editor;
(7) find the following node "<onTestEnd id="" type="actions" custom="true">";
(8) paste the following snippet in between the <onTestEnd></onTestEnd> nodes:

//code start
<METHOD pid="OBJ_10" method="Refresh"></METHOD>
<VARIABLE name="theScore" value="$OBJ_10.rawScore" global="0"/>

<JAVASCRIPT

var xmlDoc = "";
var strListName = "Scores";
var soapRequest = "";
var theScore;

xmlDoc += "<Batch>";
xmlDoc += "<Method ID='1' Cmd='New'>";
xmlDoc += "<Field Name='Title'>" + document.title + "</Field>";
xmlDoc += "<Field Name='Score'>" + g_arSlideVars["theScore"] + "</Field>";
xmlDoc += "</Method>";
xmlDoc += "</Batch>";

soapRequest += "<?xml version='1.0' encoding='utf-8'?>";
soapRequest += "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>";
soapRequest += " <soap:Body>";
soapRequest += " <UpdateListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'>";
soapRequest += " <listName>" + strListName +"</listName>"; //Change here the name of the list to update
soapRequest += " <updates>" + xmlDoc + "</updates>"; //Just specify your Batch xml variable created before
soapRequest += " </UpdateListItems>";
soapRequest += " </soap:Body>";
soapRequest += "</soap:Envelope>";

var xmlHttpRequest = new ActiveXObject("MSXML2.XMLHTTP.5.0");
//Send datas to Sharepoint. Here we're specifying headers. Content-Length is not mandatory.
xmlHttpRequest.open("POST", "http://yoursharepointsite.com/sites/_vti_bin/Lists.asmx", false); //Just specify here the complete address of the list service
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length",1);
xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/UpdateListItems");
xmlHttpRequest.send(soapRequest);"/>
//code end

(9) rename "http://yoursharepointsite.com" to your SharePoint site; (10) create a list library named "Scores" --- now close the text file, re-open your CL project file and view the course. Provided that your SharePoint site is enabled with the appropriate services, this test will create a new list item each time the onTestEnd event fires. The SharePoint list code is not my original code.
 
 
Here is a direct copy from my version (I tried to convert the escaped characters - bad idea):

<METHOD pid="OBJ_10" method="Refresh"></METHOD>

<VARIABLE name="theScore" value="$OBJ_10.rawScore" global="0"/>

<JAVASCRIPT text="var xmlDoc = &quot;&quot;;

var strListName = &quot;Scores&quot;;

var soapRequest = &quot;&quot;;

var theScore;



xmlDoc += &quot;&lt;Batch&gt;&quot;;

xmlDoc += &quot;&lt;Method ID=&apos;1&apos; Cmd=&apos;New&apos;&gt;&quot;;

xmlDoc += &quot;&lt;Field Name=&apos;Title&apos;&gt;&quot; + document.title + &quot;&lt;/Field&gt;&quot;;

xmlDoc += &quot;&lt;Field Name=&apos;Score&apos;&gt;&quot; + g_arSlideVars[&quot;theScore&quot;] + &quot;&lt;/Field&gt;&quot;;

xmlDoc += &quot;&lt;/Method&gt;&quot;;

xmlDoc += &quot;&lt;/Batch&gt;&quot;;



soapRequest += &quot;&lt;?xml version=&apos;1.0&apos; encoding=&apos;utf-8&apos;?&gt;&quot;;

soapRequest += &quot;&lt;soap:Envelope xmlns:xsi=&apos;http://www.w3.org/2001/XMLSchema-instance&apos; xmlns:xsd=&apos;http://www.w3.org/2001/XMLSchema&apos; xmlns:soap=&apos;http://schemas.xmlsoap.org/soap/envelope/&apos;&gt;&quot;;

soapRequest += &quot; &lt;soap:Body&gt;&quot;;

soapRequest += &quot; &lt;UpdateListItems xmlns=&apos;http://schemas.microsoft.com/sharepoint/soap/&apos;&gt;&quot;;

soapRequest += &quot; &lt;listName&gt;&quot; + strListName +&quot;&lt;/listName&gt;&quot;; //Change here the name of the list to update

soapRequest += &quot; &lt;updates&gt;&quot; + xmlDoc + &quot;&lt;/updates&gt;&quot;; //Just specify your Batch xml variable created before

soapRequest += &quot; &lt;/UpdateListItems&gt;&quot;;

soapRequest += &quot; &lt;/soap:Body&gt;&quot;;

soapRequest += &quot;&lt;/soap:Envelope&gt;&quot;;





var xmlHttpRequest = new ActiveXObject(&quot;MSXML2.XMLHTTP.5.0&quot;);





//Send datas to Sharepoint. Here we&apos;re specifying headers. Content-Length is not mandatory.

xmlHttpRequest.open(&quot;POST&quot;, &quot;http://yoursharepointsite.com/sites/securityreview/_vti_bin/Lists.asmx&quot;, false); //Just specify here the complete address of the list service

xmlHttpRequest.setRequestHeader(&quot;Content-Type&quot;, &quot;text/xml; charset=utf-8&quot;);

xmlHttpRequest.setRequestHeader(&quot;Content-Length&quot;,1);

xmlHttpRequest.setRequestHeader(&quot;SOAPAction&quot;, &quot;http://schemas.microsoft.com/sharepoint/soap/UpdateListItems&quot;);

xmlHttpRequest.send(soapRequest);"/>
 
 
 
Pete,
You are a star my man!!
I'll set up a small subsite on sharepont and play with this, looks to be very useful indeed.
This would be an exceptionally useful option to have built into Courselab given the wide use of sharepoint.
 
 
 
 
Nick,

If you have any questions about the code, please ask.

Regards,
Pete
Subject:
Message options
No additional options