@@ -29,27 +29,31 @@ export default class Quizly extends RunestoneBase {
2929 if ( DEBUG ) console . log ( "DEBUG: Quizly constructor, opts=" + JSON . stringify ( opts ) ) ;
3030 this . origElem = orig ;
3131 this . divid = orig . id ;
32+ if ( DEBUG ) console . log ( "DEBUG: Quizly constructor, divid= " + this . divid ) ;
3233 this . resultsViewer = $ ( orig ) . data ( "results" ) ;
33- this . getIFrame ( ) ;
34+ this . getIFrameAndQuizname ( ) ;
3435 this . renderQuizly ( ) ; //generates HTML
3536 // Checks localStorage to see if this quizly has already been completed by this user.
3637 // this.checkQuizlyStorage();
3738 this . caption = "Quizly" ;
3839 this . addCaption ( "runestone" ) ;
39- // if (DEBUG) console.log("DEBUG: Quizly constructor, this=" + JSON.stringify(this));
4040 }
4141
4242 // The main content of the quizly node is the iframe.
43- getIFrame ( ) {
43+ getIFrameAndQuizname ( ) {
4444 var html = $ ( this . origElem ) . html ( ) ;
4545 var p1 = html . search ( '<iframe' ) ;
4646 var p2 = html . search ( '</iframe>' ) ;
4747 this . iframe = html . slice ( p1 , p2 + 8 ) ;
48- if ( DEBUG ) console . log ( "DEBUG: getQuestionText() html = " + html ) ;
49- if ( DEBUG ) console . log ( "DEBUG: getQuestionText() iframe = " + this . iframe ) ;
48+ if ( DEBUG ) console . log ( "DEBUG: getIFrame() html = " + html ) ;
49+ if ( DEBUG ) console . log ( "DEBUG: getIFrame() iframe = " + this . iframe ) ;
50+ p1 = html . search ( 'quizname=' ) ;
51+ p2 = html . search ( 'hints' ) ;
52+ this . quizname = html . slice ( p1 + 9 , p2 - 5 ) ; // Grab the quizname from iframe
53+ if ( DEBUG ) console . log ( "DEBUG quizname= " , this . quizname ) ;
5054 }
5155
52- //generates the HTML that the user interacts with
56+ // Generates the HTML that the user interacts with
5357 renderQuizly ( ) {
5458 var _this = this ;
5559 if ( DEBUG ) console . log ( "DEBUG: renderQuizly()" ) ;
@@ -62,21 +66,18 @@ export default class Quizly extends RunestoneBase {
6266 if ( DEBUG ) console . log ( "DEBUG: renderQuizly(), this = " + JSON . stringify ( this ) ) ;
6367 }
6468
65- // This is what an mchoice event looks like
66- // logging event {"event":"mChoice","act":"answer:0:correct","answer":"0","correct":"T","div_id":"mc1","course":"quizly","clientLoginStatus":false,"timezoneoffset":4,"percent":1}
67-
68- // Called when user clicks submit button
69+ // Called from the exercise when user clicks submit button
6970 // Checks for the result, sets localstorage and submits to the server
70- submitQuizly ( result ) {
71+ submitQuizly ( result ) {
7172 if ( DEBUG ) console . log ( "DEBUG: submitQuizly result = " + JSON . stringify ( result ) ) ;
7273 var answer = result [ "xml" ] ;
7374 var correct = ( result [ "result" ] == true ? "T" : "F" ) ;
7475 var loganswer = "answer:" + ( correct == "T" ? "correct" : "no" ) ; // backward compatible
7576 var eventInfo = { event : "quizly" , act :loganswer , answer :answer , correct :correct , div_id : this . divid } ;
76- // log the response to the database
77+ // Log the response to the database
7778 this . logBookEvent ( eventInfo ) ; // in bookfuncs.js
7879 if ( DEBUG ) console . log ( "DEBUG: submitquizly logbookevent = " + JSON . stringify ( eventInfo ) ) ;
79- // log the fact that the user has attempted this quizly exercise to local storage
80+ // Log the fact that the user has attempted this quizly exercise to local storage
8081 localStorage . setItem ( this . divid , "true" ) ;
8182 }
8283}
@@ -87,11 +88,11 @@ export default class Quizly extends RunestoneBase {
8788=================================*/
8889 $ ( document ) . bind ( "runestone:login-complete" , function ( ) {
8990 $ ( "[data-component=quizly" ) . each ( function ( index ) {
90- if ( DEBUG ) console . log ( "DEBUG: Quizly rendering" ) ;
9191 try {
9292 var quizly = new Quizly ( { orig : this } ) ;
9393 quizlyList [ this . id ] = quizly ;
94- setupCallback ( quizly ) ;
94+ if ( DEBUG ) console . log ( "DEBUG: Quizly rendering, this.id = " + this . id ) ;
95+ setupCallback ( quizly , quizly . quizname ) ;
9596 } catch ( err ) {
9697 console . log ( `Error rendering Quizly Exercise ${ this . id }
9798 Details: ${ err } ` ) ;
@@ -100,16 +101,14 @@ export default class Quizly extends RunestoneBase {
100101 } ) ;
101102 } ) ;
102103
103- // Sets up a call back function on the window containing the quizly component
104- // We need to pass a reference to this quizly object so that it can be use during callback.
105- function setupCallback ( quizly ) {
104+ // Sets up a unique callback function on the window containing the quizly component
105+ // The quizly param is a reference to this quizly object so that it can be use during callback.
106+ // The quizname param is used to construct a unique callback function name
107+ function setupCallback ( quizly , quizname ) {
106108 if ( typeof window . component_factory === "undefined" ) {
107109 window . component_factory = { } ;
108110 }
109- window . component_factory . quizly = function ( result ) {
110- if ( DEBUG ) console . log ( "DEBUG: Quizly component factory = " + JSON . stringify ( window . component_factory ) ) ;
111- if ( DEBUG ) console . log ( "DEBUG: Quizly component factory quizly " + JSON . stringify ( result ) ) ;
112- quizly . submitQuizly ( result ) ;
113- }
111+ var fn_name = "quizly_" + quizname ; // Unique function name
112+ window . component_factory [ fn_name ] = function ( result ) { quizly . submitQuizly ( result ) ; }
114113}
115114
0 commit comments