Mercurial > hg > webaudioevaluationtool
comparison core.js @ 589:055f9763fada Dev_main
Session now performs intermediate saves on each advanceState trigger (page completions, survey completions). Specification projectReturn now "local" for presentation saving
author | Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk> |
---|---|
date | Wed, 09 Mar 2016 12:41:26 +0000 |
parents | 0d6d7618f6da |
children | 5b125fb39142 |
comparison
equal
deleted
inserted
replaced
587:0d6d7618f6da | 589:055f9763fada |
---|---|
339 // Now time to render file locally | 339 // Now time to render file locally |
340 var xmlDoc = interfaceXMLSave(); | 340 var xmlDoc = interfaceXMLSave(); |
341 var parent = document.createElement("div"); | 341 var parent = document.createElement("div"); |
342 parent.appendChild(xmlDoc); | 342 parent.appendChild(xmlDoc); |
343 var file = [parent.innerHTML]; | 343 var file = [parent.innerHTML]; |
344 if (destURL == "null" || destURL == undefined) { | 344 if (destURL == "local") { |
345 var bb = new Blob(file,{type : 'application/xml'}); | 345 var bb = new Blob(file,{type : 'application/xml'}); |
346 var dnlk = window.URL.createObjectURL(bb); | 346 var dnlk = window.URL.createObjectURL(bb); |
347 var a = document.createElement("a"); | 347 var a = document.createElement("a"); |
348 a.hidden = ''; | 348 a.hidden = ''; |
349 a.href = dnlk; | 349 a.href = dnlk; |
359 xmlhttp.setRequestHeader('Content-Type', 'text/xml'); | 359 xmlhttp.setRequestHeader('Content-Type', 'text/xml'); |
360 xmlhttp.onerror = function(){ | 360 xmlhttp.onerror = function(){ |
361 console.log('Error saving file to server! Presenting download locally'); | 361 console.log('Error saving file to server! Presenting download locally'); |
362 createProjectSave(null); | 362 createProjectSave(null); |
363 }; | 363 }; |
364 xmlhttp.onreadystatechange = function() { | 364 xmlhttp.onload = function() { |
365 console.log(xmlhttp.status); | 365 console.log(xmlhttp); |
366 if (xmlhttp.status != 200 && xmlhttp.readyState == 4) { | 366 if (this.status >= 300) { |
367 createProjectSave(null); | 367 console.log("WARNING - Could not update at this time"); |
368 } else { | 368 } else { |
369 var parser = new DOMParser(); | 369 var parser = new DOMParser(); |
370 var xmlDoc = parser.parseFromString(xmlhttp.responseText, "application/xml"); | 370 var xmlDoc = parser.parseFromString(xmlhttp.responseText, "application/xml"); |
371 if (xmlDoc == null) | 371 var response = xmlDoc.getElementsByTagName('response')[0]; |
372 { | 372 if (response.getAttribute("state") == "OK") { |
373 createProjectSave('null'); | 373 var file = response.getElementsByTagName("file")[0]; |
374 } | 374 console.log("Save: OK, written "+file.getAttribute("bytes")+"B"); |
375 var response = xmlDoc.childNodes[0]; | 375 } else { |
376 if (response.getAttribute('state') == "OK") | 376 var message = response.getElementsByTagName("message"); |
377 { | 377 console.log("Save: Error! "+message.textContent); |
378 var file = response.getElementsByTagName('file')[0]; | 378 createProjectSave("local"); |
379 console.log('Save OK: Filename '+file.textContent+','+file.getAttribute('bytes')+'B'); | 379 } |
380 popup.showPopup(); | 380 } |
381 popup.popupContent.innerHTML = null; | 381 }; |
382 popup.popupContent.textContent = "Thank you!"; | |
383 window.onbeforeunload=null; | |
384 } else { | |
385 var message = response.getElementsByTagName('message')[0]; | |
386 errorSessionDump(message.textContent); | |
387 } | |
388 } | |
389 }; | |
390 xmlhttp.send(file); | 382 xmlhttp.send(file); |
391 popup.showPopup(); | 383 popup.showPopup(); |
392 popup.popupContent.innerHTML = null; | 384 popup.popupContent.innerHTML = null; |
393 popup.popupContent.textContent = "Submitting. Please Wait"; | 385 popup.popupContent.textContent = "Submitting. Please Wait"; |
394 popup.hideNextButton(); | 386 popup.hideNextButton(); |
742 this.hidePopup(); | 734 this.hidePopup(); |
743 for (var node of this.popupOptions) | 735 for (var node of this.popupOptions) |
744 { | 736 { |
745 this.store.postResult(node); | 737 this.store.postResult(node); |
746 } | 738 } |
747 this.store.finish(); | |
748 advanceState(); | 739 advanceState(); |
749 } | 740 } |
750 }; | 741 }; |
751 | 742 |
752 this.previousClick = function() { | 743 this.previousClick = function() { |
840 }; | 831 }; |
841 this.advanceState = function(){ | 832 this.advanceState = function(){ |
842 if (this.stateIndex == null) { | 833 if (this.stateIndex == null) { |
843 this.initialise(); | 834 this.initialise(); |
844 } | 835 } |
836 storage.update(); | |
845 if (this.stateIndex == -1) { | 837 if (this.stateIndex == -1) { |
846 this.stateIndex++; | 838 this.stateIndex++; |
847 console.log('Starting test...'); | 839 console.log('Starting test...'); |
848 if (this.preTestSurvey != null) | 840 if (this.preTestSurvey != null) |
849 { | 841 { |
3117 this.root = this.document.childNodes[0]; | 3109 this.root = this.document.childNodes[0]; |
3118 this.state = 0; | 3110 this.state = 0; |
3119 | 3111 |
3120 this.initialise = function(sessionKey) | 3112 this.initialise = function(sessionKey) |
3121 { | 3113 { |
3122 if (specification.preTest != undefined){this.globalPreTest = new this.surveyNode(this,this.root,specification.preTest);} | |
3123 if (specification.postTest != undefined){this.globalPostTest = new this.surveyNode(this,this.root,specification.postTest);} | |
3124 if (sessionKey == undefined) { | 3114 if (sessionKey == undefined) { |
3125 // We need to get the sessionKey | 3115 // We need to get the sessionKey |
3126 this.SessionKey.generateKey(); | 3116 this.SessionKey.generateKey(); |
3117 var projectDocument = specification.projectXML; | |
3118 projectDocument.setAttribute('file-name',url); | |
3119 this.root.appendChild(projectDocument); | |
3120 this.root.appendChild(returnDateNode()); | |
3121 this.root.appendChild(interfaceContext.returnNavigator()); | |
3127 } else { | 3122 } else { |
3128 this.SessionKey.key = sessionKey; | 3123 this.SessionKey.key = sessionKey; |
3129 } | 3124 } |
3125 if (specification.preTest != undefined){this.globalPreTest = new this.surveyNode(this,this.root,specification.preTest);} | |
3126 if (specification.postTest != undefined){this.globalPostTest = new this.surveyNode(this,this.root,specification.postTest);} | |
3130 }; | 3127 }; |
3131 | 3128 |
3132 this.SessionKey = { | 3129 this.SessionKey = { |
3133 key: null, | 3130 key: null, |
3134 request: new XMLHttpRequest(), | 3131 request: new XMLHttpRequest(), |
3136 handleEvent: function() { | 3133 handleEvent: function() { |
3137 var parse = new DOMParser(); | 3134 var parse = new DOMParser(); |
3138 var xml = parse.parseFromString(this.request.response,"text/xml"); | 3135 var xml = parse.parseFromString(this.request.response,"text/xml"); |
3139 if (xml.getAllElementsByTagName("state")[0].textContent == "OK") { | 3136 if (xml.getAllElementsByTagName("state")[0].textContent == "OK") { |
3140 this.key = xml.getAllElementsByTagName("key")[0].textContent; | 3137 this.key = xml.getAllElementsByTagName("key")[0].textContent; |
3138 this.parent.root.setAttribute("key",this.key); | |
3139 this.parent.root.setAttribute("state","empty"); | |
3141 } else { | 3140 } else { |
3142 this.generateKey(); | 3141 this.generateKey(); |
3143 } | 3142 } |
3144 }, | 3143 }, |
3145 generateKey: function() { | 3144 generateKey: function() { |
3146 var temp_key = randomString(32); | 3145 var temp_key = randomString(32); |
3147 this.request.open("GET","keygen.php?key="+temp_key,true); | 3146 this.request.open("GET","keygen.php?key="+temp_key,true); |
3148 this.request.addEventListener("load",this); | 3147 this.request.addEventListener("load",this); |
3149 this.request.send(); | 3148 this.request.send(); |
3150 }, | 3149 }, |
3151 postData: function(nodeName,id,xml) { | 3150 update: function() { |
3152 // nodeName: the node name to find | 3151 this.parent.root.setAttribute("state","update"); |
3153 // id: the id of the node (null if location==root) | 3152 var xmlhttp = new XMLHttpRequest(); |
3154 // xml: the XML node to append | 3153 xmlhttp.open("POST",specification.projectReturn+"?key="+this.key); |
3155 if (this.key != null) { | 3154 xmlhttp.setRequestHeader('Content-Type', 'text/xml'); |
3156 var postXML = new XMLHttpRequest; | 3155 xmlhttp.onerror = function(){ |
3157 postXML.open("POST","intermediate.php?key="+this.key+"&node="+nodeName+"&id="+id); | 3156 console.log('Error updating file to server!'); |
3158 postXML.setRequestHeader('Content-Type','text/xml'); | 3157 }; |
3159 postXML.onerror = function() { | 3158 var hold = document.createElement("div"); |
3160 console.log("Error posting: "+this.responseText); | 3159 var clone = this.parent.root.cloneNode(true); |
3160 hold.appendChild(clone); | |
3161 xmlhttp.onload = function() { | |
3162 if (this.status >= 300) { | |
3163 console.log("WARNING - Could not update at this time"); | |
3164 } else { | |
3165 var parser = new DOMParser(); | |
3166 var xmlDoc = parser.parseFromString(xmlhttp.responseText, "application/xml"); | |
3167 var response = xmlDoc.getElementsByTagName('response')[0]; | |
3168 if (response.getAttribute("state") == "OK") { | |
3169 var file = response.getElementsByTagName("file")[0]; | |
3170 console.log("Intermediate save: OK, written "+file.getAttribute("bytes")+"B"); | |
3171 } else { | |
3172 var message = response.getElementsByTagName("message"); | |
3173 console.log("Intermediate save: Error! "+message.textContent); | |
3174 } | |
3161 } | 3175 } |
3162 var parent = document.createElement("div"); | |
3163 parent.appendChild(xml); | |
3164 postXML.send([parent.innerHTML]); | |
3165 } | 3176 } |
3177 xmlhttp.send([hold.innerHTML]); | |
3166 } | 3178 } |
3167 } | 3179 } |
3168 | 3180 |
3169 this.createTestPageStore = function(specification) | 3181 this.createTestPageStore = function(specification) |
3170 { | 3182 { |
3220 surveyresult.appendChild(checkNode); | 3232 surveyresult.appendChild(checkNode); |
3221 } | 3233 } |
3222 break; | 3234 break; |
3223 } | 3235 } |
3224 }; | 3236 }; |
3225 | |
3226 this.finish = function() { | |
3227 if (this.parent.document != undefined) { | |
3228 this.parent.SessionKey.postData("waetresult",null,this.XMLDOM); | |
3229 } | |
3230 }; | |
3231 }; | 3237 }; |
3232 | 3238 |
3233 this.pageNode = function(parent,specification) | 3239 this.pageNode = function(parent,specification) |
3234 { | 3240 { |
3235 // Create one store per test page | 3241 // Create one store per test page |
3264 aeNode.appendChild(ae_metric); | 3270 aeNode.appendChild(ae_metric); |
3265 this.XMLDOM.appendChild(aeNode); | 3271 this.XMLDOM.appendChild(aeNode); |
3266 } | 3272 } |
3267 | 3273 |
3268 this.parent.root.appendChild(this.XMLDOM); | 3274 this.parent.root.appendChild(this.XMLDOM); |
3269 | 3275 }; |
3270 this.finish = function() { | 3276 this.update = function() { |
3271 this.parent.SessionKey.postData("waetresult",null,this.XMLDOM); | 3277 this.SessionKey.update(); |
3272 } | 3278 } |
3273 }; | |
3274 this.finish = function() | 3279 this.finish = function() |
3275 { | 3280 { |
3276 if (this.state == 0) | 3281 if (this.state == 0) |
3277 { | 3282 { |
3278 var projectDocument = specification.projectXML; | 3283 this.update(); |
3279 projectDocument.setAttribute('file-name',url); | |
3280 this.root.appendChild(projectDocument); | |
3281 this.root.appendChild(returnDateNode()); | |
3282 this.root.appendChild(interfaceContext.returnNavigator()); | |
3283 } | 3284 } |
3284 this.state = 1; | 3285 this.state = 1; |
3285 return this.root; | 3286 return this.root; |
3286 }; | 3287 }; |
3287 } | 3288 } |