Mercurial > hg > webaudioevaluationtool
comparison ape.js @ 938:c3fe29d33643
Currently playing sample red.
author | Brecht De Man <BrechtDeMan@users.noreply.github.com> |
---|---|
date | Sat, 16 May 2015 17:52:51 +0100 |
parents | |
children | c2fe27b91359 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 938:c3fe29d33643 |
---|---|
1 /** | |
2 * ape.js | |
3 * Create the APE interface | |
4 */ | |
5 | |
6 var currentState; // Keep track of the current state (pre/post test, which test, final test? first test?) | |
7 // preTest - In preTest state | |
8 // testRun-ID - In test running, test Id number at the end 'testRun-2' | |
9 // testRunPost-ID - Post test of test ID | |
10 // testRunPre-ID - Pre-test of test ID | |
11 // postTest - End of test, final submission! | |
12 | |
13 | |
14 // Once this is loaded and parsed, begin execution | |
15 loadInterface(projectXML); | |
16 | |
17 function loadInterface(xmlDoc) { | |
18 | |
19 // Get the dimensions of the screen available to the page | |
20 var width = window.innerWidth; | |
21 var height = window.innerHeight; | |
22 | |
23 // The injection point into the HTML page | |
24 var insertPoint = document.getElementById("topLevelBody"); | |
25 var testContent = document.createElement('div'); | |
26 testContent.id = 'testContent'; | |
27 | |
28 | |
29 // Decode parts of the xmlDoc that are needed | |
30 // xmlDoc MUST already be parsed by jQuery! | |
31 var xmlSetup = xmlDoc.find('setup'); | |
32 // Should put in an error function here incase of malprocessed or malformed XML | |
33 | |
34 // Extract the different test XML DOM trees | |
35 var audioHolders = xmlDoc.find('audioHolder'); | |
36 audioHolders.each(function(index,element) { | |
37 var repeatN = element.attributes['repeatCount'].value; | |
38 for (var r=0; r<=repeatN; r++) { | |
39 testXMLSetups[testXMLSetups.length] = element; | |
40 } | |
41 }); | |
42 | |
43 // New check if we need to randomise the test order | |
44 var randomise = xmlSetup[0].attributes['randomiseOrder']; | |
45 if (randomise != undefined) { | |
46 randomise = Boolean(randomise.value); | |
47 } else { | |
48 randomise = false; | |
49 } | |
50 if (randomise) | |
51 { | |
52 testXMLSetups = randomiseOrder(testXMLSetups); | |
53 } | |
54 | |
55 // Obtain the metrics enabled | |
56 var metricNode = xmlSetup.find('Metric'); | |
57 var metricNode = metricNode.find('metricEnable'); | |
58 metricNode.each(function(index,node){ | |
59 var enabled = node.textContent; | |
60 switch(enabled) | |
61 { | |
62 case 'testTimer': | |
63 sessionMetrics.prototype.enableTestTimer = true; | |
64 break; | |
65 case 'elementTimer': | |
66 sessionMetrics.prototype.enableElementTimer = true; | |
67 break; | |
68 case 'elementTracker': | |
69 sessionMetrics.prototype.enableElementTracker = true; | |
70 break; | |
71 case 'elementInitalPosition': | |
72 sessionMetrics.prototype.enableElementInitialPosition = true; | |
73 break; | |
74 case 'elementFlagListenedTo': | |
75 sessionMetrics.prototype.enableFlagListenedTo = true; | |
76 break; | |
77 case 'elementFlagMoved': | |
78 sessionMetrics.prototype.enableFlagMoved = true; | |
79 break; | |
80 case 'elementFlagComments': | |
81 sessionMetrics.prototype.enableFlagComments = true; | |
82 break; | |
83 } | |
84 }); | |
85 | |
86 // Create APE specific metric functions | |
87 audioEngineContext.metric.initialiseTest = function() | |
88 { | |
89 var sliders = document.getElementsByClassName('track-slider'); | |
90 for (var i=0; i<sliders.length; i++) | |
91 { | |
92 audioEngineContext.audioObjects[i].metric.initialised(convSliderPosToRate(i)); | |
93 } | |
94 }; | |
95 | |
96 audioEngineContext.metric.sliderMoveStart = function(id) | |
97 { | |
98 if (this.data == -1) | |
99 { | |
100 this.data = id; | |
101 } else { | |
102 console.log('ERROR: Metric tracker detecting two moves!'); | |
103 this.data = -1; | |
104 } | |
105 }; | |
106 audioEngineContext.metric.sliderMoved = function() | |
107 { | |
108 var time = audioEngineContext.timer.getTestTime(); | |
109 var id = this.data; | |
110 this.data = -1; | |
111 var position = convSliderPosToRate(id); | |
112 if (audioEngineContext.timer.testStarted) | |
113 { | |
114 audioEngineContext.audioObjects[id].metric.moved(time,position); | |
115 } | |
116 }; | |
117 | |
118 audioEngineContext.metric.sliderPlayed = function(id) | |
119 { | |
120 var time = audioEngineContext.timer.getTestTime(); | |
121 if (audioEngineContext.timer.testStarted) | |
122 { | |
123 if (this.lastClicked >= 0) | |
124 { | |
125 audioEngineContext.audioObjects[this.lastClicked].metric.listening(time); | |
126 } | |
127 this.lastClicked = id; | |
128 audioEngineContext.audioObjects[id].metric.listening(time); | |
129 } | |
130 }; | |
131 | |
132 // Create the top div for the Title element | |
133 var titleAttr = xmlSetup[0].attributes['title']; | |
134 var title = document.createElement('div'); | |
135 title.className = "title"; | |
136 title.align = "center"; | |
137 var titleSpan = document.createElement('span'); | |
138 | |
139 // Set title to that defined in XML, else set to default | |
140 if (titleAttr != undefined) { | |
141 titleSpan.innerHTML = titleAttr.value; | |
142 } else { | |
143 titleSpan.innerHTML = 'Listening test'; | |
144 } | |
145 // Insert the titleSpan element into the title div element. | |
146 title.appendChild(titleSpan); | |
147 | |
148 var pagetitle = document.createElement('div'); | |
149 pagetitle.className = "pageTitle"; | |
150 pagetitle.align = "center"; | |
151 var titleSpan = document.createElement('span'); | |
152 titleSpan.id = "pageTitle"; | |
153 pagetitle.appendChild(titleSpan); | |
154 | |
155 // Store the return URL path in global projectReturn | |
156 projectReturn = xmlSetup[0].attributes['projectReturn'].value; | |
157 | |
158 // Create Interface buttons! | |
159 var interfaceButtons = document.createElement('div'); | |
160 interfaceButtons.id = 'interface-buttons'; | |
161 | |
162 // MANUAL DOWNLOAD POINT | |
163 // If project return is null, this MUST be specified as the location to create the download link | |
164 var downloadPoint = document.createElement('div'); | |
165 downloadPoint.id = 'download-point'; | |
166 | |
167 // Create playback start/stop points | |
168 var playback = document.createElement("button"); | |
169 playback.innerHTML = 'Start'; | |
170 playback.id = 'playback-button'; | |
171 // onclick function. Check if it is playing or not, call the correct function in the | |
172 // audioEngine, change the button text to reflect the next state. | |
173 playback.onclick = function() { | |
174 if (audioEngineContext.status == 0) { | |
175 audioEngineContext.play(); | |
176 this.innerHTML = 'Stop'; | |
177 } else { | |
178 audioEngineContext.stop(); | |
179 this.innerHTML = 'Start'; | |
180 } | |
181 }; | |
182 // Create Submit (save) button | |
183 var submit = document.createElement("button"); | |
184 submit.innerHTML = 'Submit'; | |
185 submit.onclick = buttonSubmitClick; | |
186 submit.id = 'submit-button'; | |
187 // Append the interface buttons into the interfaceButtons object. | |
188 interfaceButtons.appendChild(playback); | |
189 interfaceButtons.appendChild(submit); | |
190 interfaceButtons.appendChild(downloadPoint); | |
191 | |
192 // Now create the slider and HTML5 canvas boxes | |
193 | |
194 // Create the div box to center align | |
195 var sliderBox = document.createElement('div'); | |
196 sliderBox.className = 'sliderCanvasDiv'; | |
197 sliderBox.id = 'sliderCanvasHolder'; | |
198 sliderBox.align = 'center'; | |
199 | |
200 // Create the slider box to hold the slider elements | |
201 var canvas = document.createElement('div'); | |
202 canvas.id = 'slider'; | |
203 // Must have a known EXACT width, as this is used later to determine the ratings | |
204 canvas.style.width = width - 100 +"px"; | |
205 canvas.align = "left"; | |
206 sliderBox.appendChild(canvas); | |
207 | |
208 // Create the div to hold any scale objects | |
209 var scale = document.createElement('div'); | |
210 scale.className = 'sliderScale'; | |
211 scale.id = 'sliderScaleHolder'; | |
212 scale.align = 'left'; | |
213 sliderBox.appendChild(scale); | |
214 | |
215 // Global parent for the comment boxes on the page | |
216 var feedbackHolder = document.createElement('div'); | |
217 feedbackHolder.id = 'feedbackHolder'; | |
218 | |
219 testContent.style.zIndex = 1; | |
220 insertPoint.innerHTML = null; // Clear the current schema | |
221 | |
222 // Create pre and post test questions | |
223 var blank = document.createElement('div'); | |
224 blank.className = 'testHalt'; | |
225 | |
226 var popupHolder = document.createElement('div'); | |
227 popupHolder.id = 'popupHolder'; | |
228 popupHolder.className = 'popupHolder'; | |
229 popupHolder.style.position = 'absolute'; | |
230 popupHolder.style.left = (window.innerWidth/2)-250 + 'px'; | |
231 popupHolder.style.top = (window.innerHeight/2)-125 + 'px'; | |
232 insertPoint.appendChild(popupHolder); | |
233 insertPoint.appendChild(blank); | |
234 hidePopup(); | |
235 | |
236 var preTest = xmlSetup.find('PreTest'); | |
237 var postTest = xmlSetup.find('PostTest'); | |
238 preTest = preTest[0]; | |
239 postTest = postTest[0]; | |
240 | |
241 currentState = 'preTest'; | |
242 | |
243 // Create Pre-Test Box | |
244 if (preTest != undefined && preTest.children.length >= 1) | |
245 { | |
246 showPopup(); | |
247 preTestPopupStart(preTest); | |
248 } | |
249 | |
250 // Inject into HTML | |
251 testContent.appendChild(title); // Insert the title | |
252 testContent.appendChild(pagetitle); | |
253 testContent.appendChild(interfaceButtons); | |
254 testContent.appendChild(sliderBox); | |
255 testContent.appendChild(feedbackHolder); | |
256 insertPoint.appendChild(testContent); | |
257 | |
258 // Load the full interface | |
259 | |
260 } | |
261 | |
262 function loadTest(id) | |
263 { | |
264 // Used to load a specific test page | |
265 var textXML = testXMLSetups[id]; | |
266 | |
267 var feedbackHolder = document.getElementById('feedbackHolder'); | |
268 var canvas = document.getElementById('slider'); | |
269 feedbackHolder.innerHTML = null; | |
270 canvas.innerHTML = null; | |
271 | |
272 // Setup question title | |
273 var interfaceObj = $(textXML).find('interface'); | |
274 var titleNode = interfaceObj.find('title'); | |
275 if (titleNode[0] != undefined) | |
276 { | |
277 document.getElementById('pageTitle').textContent = titleNode[0].textContent; | |
278 } | |
279 var positionScale = canvas.style.width.substr(0,canvas.style.width.length-2); | |
280 var offset = 50-8; // Half the offset of the slider (window width -100) minus the body padding of 8 | |
281 // TODO: AUTOMATE ABOVE!! | |
282 var scale = document.getElementById('sliderScaleHolder'); | |
283 scale.innerHTML = null; | |
284 interfaceObj.find('scale').each(function(index,scaleObj){ | |
285 var position = Number(scaleObj.attributes['position'].value)*0.01; | |
286 var pixelPosition = (position*positionScale)+offset; | |
287 var scaleDOM = document.createElement('span'); | |
288 scaleDOM.textContent = scaleObj.textContent; | |
289 scale.appendChild(scaleDOM); | |
290 scaleDOM.style.left = Math.floor((pixelPosition-($(scaleDOM).width()/2)))+'px'; | |
291 }); | |
292 | |
293 // Extract the hostURL attribute. If not set, create an empty string. | |
294 var hostURL = textXML.attributes['hostURL']; | |
295 if (hostURL == undefined) { | |
296 hostURL = ""; | |
297 } else { | |
298 hostURL = hostURL.value; | |
299 } | |
300 // Extract the sampleRate. If set, convert the string to a Number. | |
301 var hostFs = textXML.attributes['sampleRate']; | |
302 if (hostFs != undefined) { | |
303 hostFs = Number(hostFs.value); | |
304 } | |
305 | |
306 /// CHECK FOR SAMPLE RATE COMPATIBILITY | |
307 if (hostFs != undefined) { | |
308 if (Number(hostFs) != audioContext.sampleRate) { | |
309 var errStr = 'Sample rates do not match! Requested '+Number(hostFs)+', got '+audioContext.sampleRate+'. Please set the sample rate to match before completing this test.'; | |
310 alert(errStr); | |
311 return; | |
312 } | |
313 } | |
314 | |
315 var commentShow = textXML.attributes['elementComments']; | |
316 if (commentShow != undefined) { | |
317 if (commentShow.value == 'false') {commentShow = false;} | |
318 else {commentShow = true;} | |
319 } else {commentShow = true;} | |
320 | |
321 var loopPlayback = textXML.attributes['loop']; | |
322 if (loopPlayback != undefined) | |
323 { | |
324 loopPlayback = loopPlayback.value; | |
325 if (loopPlayback == 'true') { | |
326 loopPlayback = true; | |
327 } else { | |
328 loopPlayback = false; | |
329 } | |
330 } else { | |
331 loopPlayback = false; | |
332 } | |
333 audioEngineContext.loopPlayback = loopPlayback; | |
334 | |
335 // Create AudioEngine bindings for playback | |
336 if (loopPlayback) { | |
337 audioEngineContext.play = function() { | |
338 // Send play command to all playback buffers for synchronised start | |
339 // Also start timer callbacks to detect if playback has finished | |
340 if (this.status == 0) { | |
341 this.timer.startTest(); | |
342 // First get current clock | |
343 var timer = audioContext.currentTime; | |
344 // Add 3 seconds | |
345 timer += 3.0; | |
346 // Send play to all tracks | |
347 for (var i=0; i<this.audioObjects.length; i++) | |
348 { | |
349 this.audioObjects[i].play(timer); | |
350 } | |
351 this.status = 1; | |
352 } | |
353 }; | |
354 | |
355 audioEngineContext.stop = function() { | |
356 // Send stop and reset command to all playback buffers | |
357 if (this.status == 1) { | |
358 if (this.loopPlayback) { | |
359 for (var i=0; i<this.audioObjects.length; i++) | |
360 { | |
361 this.audioObjects[i].stop(); | |
362 } | |
363 } | |
364 this.status = 0; | |
365 } | |
366 }; | |
367 | |
368 audioEngineContext.selectedTrack = function(id) { | |
369 for (var i=0; i<this.audioObjects.length; i++) | |
370 { | |
371 if (id == i) { | |
372 this.audioObjects[i].outputGain.gain.value = 1.0; | |
373 } else { | |
374 this.audioObjects[i].outputGain.gain.value = 0.0; | |
375 } | |
376 } | |
377 }; | |
378 } else { | |
379 audioEngineContext.play = function() { | |
380 // Send play command to all playback buffers for synchronised start | |
381 // Also start timer callbacks to detect if playback has finished | |
382 if (this.status == 0) { | |
383 this.timer.startTest(); | |
384 this.status = 1; | |
385 } | |
386 }; | |
387 | |
388 audioEngineContext.stop = function() { | |
389 // Send stop and reset command to all playback buffers | |
390 if (this.status == 1) { | |
391 if (this.loopPlayback) { | |
392 for (var i=0; i<this.audioObjects.length; i++) | |
393 { | |
394 this.audioObjects[i].stop(); | |
395 } | |
396 } | |
397 this.status = 0; | |
398 } | |
399 }; | |
400 | |
401 audioEngineContext.selectedTrack = function(id) { | |
402 for (var i=0; i<this.audioObjects.length; i++) | |
403 { | |
404 if (id == i) { | |
405 this.audioObjects[i].outputGain.gain.value = 1.0; | |
406 this.audioObjects[i].play(audioContext.currentTime+0.01); | |
407 } else { | |
408 this.audioObjects[i].outputGain.gain.value = 0.0; | |
409 this.audioObjects[i].stop(); | |
410 } | |
411 } | |
412 }; | |
413 } | |
414 | |
415 currentTestHolder = document.createElement('audioHolder'); | |
416 currentTestHolder.id = textXML.id; | |
417 currentTestHolder.repeatCount = textXML.attributes['repeatCount'].value; | |
418 var currentPreTestHolder = document.createElement('preTest'); | |
419 var currentPostTestHolder = document.createElement('postTest'); | |
420 currentTestHolder.appendChild(currentPreTestHolder); | |
421 currentTestHolder.appendChild(currentPostTestHolder); | |
422 | |
423 var randomise = textXML.attributes['randomiseOrder']; | |
424 if (randomise != undefined) {randomise = randomise.value;} | |
425 else {randomise = false;} | |
426 | |
427 var audioElements = $(textXML).find('audioElements'); | |
428 currentTrackOrder = []; | |
429 audioElements.each(function(index,element){ | |
430 // Find any blind-repeats | |
431 // Not implemented yet, but just in case | |
432 currentTrackOrder[index] = element; | |
433 }); | |
434 if (randomise) { | |
435 currentTrackOrder = randomiseOrder(currentTrackOrder); | |
436 } | |
437 | |
438 // Delete any previous audioObjects associated with the audioEngine | |
439 audioEngineContext.audioObjects = []; | |
440 | |
441 // Find all the audioElements from the audioHolder | |
442 $(currentTrackOrder).each(function(index,element){ | |
443 // Find URL of track | |
444 // In this jQuery loop, variable 'this' holds the current audioElement. | |
445 | |
446 // Now load each audio sample. First create the new track by passing the full URL | |
447 var trackURL = hostURL + this.attributes['url'].value; | |
448 audioEngineContext.newTrack(trackURL); | |
449 | |
450 if (commentShow) { | |
451 // Create document objects to hold the comment boxes | |
452 var trackComment = document.createElement('div'); | |
453 trackComment.className = 'comment-div'; | |
454 // Create a string next to each comment asking for a comment | |
455 var trackString = document.createElement('span'); | |
456 trackString.innerHTML = 'Comment on track '+index; | |
457 // Create the HTML5 comment box 'textarea' | |
458 var trackCommentBox = document.createElement('textarea'); | |
459 trackCommentBox.rows = '4'; | |
460 trackCommentBox.cols = '100'; | |
461 trackCommentBox.name = 'trackComment'+index; | |
462 trackCommentBox.className = 'trackComment'; | |
463 var br = document.createElement('br'); | |
464 // Add to the holder. | |
465 trackComment.appendChild(trackString); | |
466 trackComment.appendChild(br); | |
467 trackComment.appendChild(trackCommentBox); | |
468 feedbackHolder.appendChild(trackComment); | |
469 } | |
470 | |
471 // Create a slider per track | |
472 | |
473 var trackSliderObj = document.createElement('div'); | |
474 trackSliderObj.className = 'track-slider'; | |
475 trackSliderObj.id = 'track-slider-'+index; | |
476 // Distribute it randomnly | |
477 var w = window.innerWidth - 100; | |
478 w = Math.random()*w; | |
479 trackSliderObj.style.left = Math.floor(w)+50+'px'; | |
480 trackSliderObj.innerHTML = '<span>'+index+'</span>'; | |
481 trackSliderObj.draggable = true; | |
482 trackSliderObj.ondragend = dragEnd; | |
483 trackSliderObj.ondragstart = function() | |
484 { | |
485 var id = Number(this.id.substr(13,2)); // Maximum theoretical tracks is 99! | |
486 audioEngineContext.metric.sliderMoveStart(id); | |
487 }; | |
488 | |
489 // Onclick, switch playback to that track | |
490 trackSliderObj.onclick = function() { | |
491 // Get the track ID from the object ID | |
492 var id = Number(this.id.substr(13,2)); // Maximum theoretical tracks is 99! | |
493 audioEngineContext.metric.sliderPlayed(id); | |
494 audioEngineContext.selectedTrack(id); | |
495 // Currently playing track red, rest green | |
496 document.getElementById('track-slider-'+index).style.backgroundColor = "#FF0000"; | |
497 for (var i = 0; i<$(currentTrackOrder).length; i++) | |
498 { | |
499 if (i!=index) | |
500 { | |
501 document.getElementById('track-slider-'+i).style.backgroundColor = "rgb(100,200,100)"; | |
502 } | |
503 | |
504 } | |
505 }; | |
506 | |
507 canvas.appendChild(trackSliderObj); | |
508 }); | |
509 | |
510 // Append any commentQuestion boxes | |
511 var commentQuestions = $(textXML).find('CommentQuestion'); | |
512 $(commentQuestions).each(function(index,element) { | |
513 // Create document objects to hold the comment boxes | |
514 var trackComment = document.createElement('div'); | |
515 trackComment.className = 'comment-div commentQuestion'; | |
516 trackComment.id = element.attributes['id'].value; | |
517 // Create a string next to each comment asking for a comment | |
518 var trackString = document.createElement('span'); | |
519 trackString.innerHTML = element.textContent; | |
520 // Create the HTML5 comment box 'textarea' | |
521 var trackCommentBox = document.createElement('textarea'); | |
522 trackCommentBox.rows = '4'; | |
523 trackCommentBox.cols = '100'; | |
524 trackCommentBox.name = 'commentQuestion'+index; | |
525 trackCommentBox.className = 'trackComment'; | |
526 var br = document.createElement('br'); | |
527 // Add to the holder. | |
528 trackComment.appendChild(trackString); | |
529 trackComment.appendChild(br); | |
530 trackComment.appendChild(trackCommentBox); | |
531 feedbackHolder.appendChild(trackComment); | |
532 }); | |
533 | |
534 // Now process any pre-test commands | |
535 | |
536 var preTest = $(testXMLSetups[id]).find('PreTest')[0]; | |
537 if (preTest.children.length > 0) | |
538 { | |
539 currentState = 'testRunPre-'+id; | |
540 preTestPopupStart(preTest); | |
541 showPopup(); | |
542 } else { | |
543 currentState = 'testRun-'+id; | |
544 } | |
545 } | |
546 | |
547 function preTestPopupStart(preTest) | |
548 { | |
549 var popupHolder = document.getElementById('popupHolder'); | |
550 popupHolder.innerHTML = null; | |
551 // Parse the first box | |
552 var preTestOption = document.createElement('div'); | |
553 preTestOption.id = 'preTest'; | |
554 preTestOption.style.marginTop = '25px'; | |
555 preTestOption.align = "center"; | |
556 var child = preTest.children[0]; | |
557 if (child.nodeName == 'statement') | |
558 { | |
559 preTestOption.innerHTML = '<span>'+child.innerHTML+'</span>'; | |
560 } else if (child.nodeName == 'question') | |
561 { | |
562 var questionId = child.attributes['id'].value; | |
563 var textHold = document.createElement('span'); | |
564 textHold.innerHTML = child.innerHTML; | |
565 textHold.id = questionId + 'response'; | |
566 var textEnter = document.createElement('textarea'); | |
567 preTestOption.appendChild(textHold); | |
568 preTestOption.appendChild(textEnter); | |
569 } | |
570 var nextButton = document.createElement('button'); | |
571 nextButton.className = 'popupButton'; | |
572 nextButton.value = '0'; | |
573 nextButton.innerHTML = 'Next'; | |
574 nextButton.onclick = popupButtonClick; | |
575 | |
576 popupHolder.appendChild(preTestOption); | |
577 popupHolder.appendChild(nextButton); | |
578 } | |
579 | |
580 function popupButtonClick() | |
581 { | |
582 // Global call from the 'Next' button click | |
583 if (currentState == 'preTest') | |
584 { | |
585 // At the start of the preTest routine! | |
586 var xmlTree = projectXML.find('setup'); | |
587 var preTest = xmlTree.find('PreTest')[0]; | |
588 this.value = preTestButtonClick(preTest,this.value); | |
589 } else if (currentState.substr(0,10) == 'testRunPre') | |
590 { | |
591 //Specific test pre-test | |
592 var testId = currentState.substr(11,currentState.length-10); | |
593 var preTest = $(testXMLSetups[testId]).find('PreTest')[0]; | |
594 this.value = preTestButtonClick(preTest,this.value); | |
595 } else if (currentState.substr(0,11) == 'testRunPost') | |
596 { | |
597 // Specific test post-test | |
598 var testId = currentState.substr(12,currentState.length-11); | |
599 var preTest = $(testXMLSetups[testId]).find('PostTest')[0]; | |
600 this.value = preTestButtonClick(preTest,this.value); | |
601 } else if (currentState == 'postTest') | |
602 { | |
603 // At the end of the test, running global post test | |
604 var xmlTree = projectXML.find('setup'); | |
605 var PostTest = xmlTree.find('PostTest')[0]; | |
606 this.value = preTestButtonClick(PostTest,this.value); | |
607 } | |
608 } | |
609 | |
610 function preTestButtonClick(preTest,index) | |
611 { | |
612 // Called on click of pre-test button | |
613 // Need to find and parse preTest again! | |
614 var preTestOption = document.getElementById('preTest'); | |
615 // Check if current state is a question! | |
616 if (preTest.children[index].nodeName == 'question') { | |
617 var questionId = preTest.children[index].attributes['id'].value; | |
618 var questionHold = document.createElement('comment'); | |
619 var questionResponse = document.getElementById(questionId + 'response'); | |
620 var mandatory = preTest.children[index].attributes['mandatory']; | |
621 if (mandatory != undefined){ | |
622 if (mandatory.value == 'true') {mandatory = true;} | |
623 else {mandatory = false;} | |
624 } else {mandatory = false;} | |
625 if (mandatory == true && questionResponse.value.length == 0) { | |
626 return index; | |
627 } | |
628 questionHold.id = questionId; | |
629 questionHold.innerHTML = questionResponse.value; | |
630 postPopupResponse(questionHold); | |
631 } | |
632 index++; | |
633 if (index < preTest.children.length) | |
634 { | |
635 // More to process | |
636 var child = preTest.children[index]; | |
637 if (child.nodeName == 'statement') | |
638 { | |
639 preTestOption.innerHTML = '<span>'+child.innerHTML+'</span>'; | |
640 } else if (child.nodeName == 'question') | |
641 { | |
642 var textHold = document.createElement('span'); | |
643 textHold.innerHTML = child.innerHTML; | |
644 var textEnter = document.createElement('textarea'); | |
645 textEnter.id = child.attributes['id'].value + 'response'; | |
646 var br = document.createElement('br'); | |
647 preTestOption.innerHTML = null; | |
648 preTestOption.appendChild(textHold); | |
649 preTestOption.appendChild(br); | |
650 preTestOption.appendChild(textEnter); | |
651 } | |
652 } else { | |
653 // Time to clear | |
654 preTestOption.innerHTML = null; | |
655 if (currentState != 'postTest') { | |
656 hidePopup(); | |
657 // Progress the state! | |
658 advanceState(); | |
659 } else { | |
660 a = createProjectSave(projectReturn); | |
661 preTestOption.appendChild(a); | |
662 } | |
663 } | |
664 return index; | |
665 } | |
666 | |
667 function postPopupResponse(response) | |
668 { | |
669 if (currentState == 'preTest') { | |
670 preTestQuestions.appendChild(response); | |
671 } else if (currentState == 'postTest') { | |
672 postTestQuestions.appendChild(response); | |
673 } else { | |
674 // Inside a specific test | |
675 if (currentState.substr(0,10) == 'testRunPre') { | |
676 // Pre Test | |
677 var store = $(currentTestHolder).find('preTest'); | |
678 } else { | |
679 // Post Test | |
680 var store = $(currentTestHolder).find('postTest'); | |
681 } | |
682 store[0].appendChild(response); | |
683 } | |
684 } | |
685 | |
686 function showPopup() | |
687 { | |
688 var popupHolder = document.getElementById('popupHolder'); | |
689 popupHolder.style.zIndex = 3; | |
690 popupHolder.style.visibility = 'visible'; | |
691 var blank = document.getElementsByClassName('testHalt')[0]; | |
692 blank.style.zIndex = 2; | |
693 blank.style.visibility = 'visible'; | |
694 } | |
695 | |
696 function hidePopup() | |
697 { | |
698 var popupHolder = document.getElementById('popupHolder'); | |
699 popupHolder.style.zIndex = -1; | |
700 popupHolder.style.visibility = 'hidden'; | |
701 var blank = document.getElementsByClassName('testHalt')[0]; | |
702 blank.style.zIndex = -2; | |
703 blank.style.visibility = 'hidden'; | |
704 } | |
705 | |
706 function dragEnd(ev) { | |
707 // Function call when a div has been dropped | |
708 var slider = document.getElementById('slider'); | |
709 var w = slider.style.width; | |
710 w = Number(w.substr(0,w.length-2)); | |
711 var x = ev.x; | |
712 if (x >= 42 && x < w+42) { | |
713 this.style.left = (x)+'px'; | |
714 } else { | |
715 if (x<42) { | |
716 this.style.left = '42px'; | |
717 } else { | |
718 this.style.left = (w+42) + 'px'; | |
719 } | |
720 } | |
721 audioEngineContext.metric.sliderMoved(); | |
722 } | |
723 | |
724 function advanceState() | |
725 { | |
726 console.log(currentState); | |
727 if (currentState == 'preTest') | |
728 { | |
729 // End of pre-test, begin the test | |
730 loadTest(0); | |
731 } else if (currentState.substr(0,10) == 'testRunPre') | |
732 { | |
733 // Start the test | |
734 var testId = currentState.substr(11,currentState.length-10); | |
735 currentState = 'testRun-'+testId; | |
736 } else if (currentState.substr(0,11) == 'testRunPost') | |
737 { | |
738 var testId = currentState.substr(12,currentState.length-11); | |
739 testEnded(testId); | |
740 } else if (currentState.substr(0,7) == 'testRun') | |
741 { | |
742 var testId = currentState.substr(8,currentState.length-7); | |
743 // Check if we have any post tests to perform | |
744 var postXML = $(testXMLSetups[testId]).find('PostTest')[0]; | |
745 if (postXML == undefined) { | |
746 testEnded(testId); | |
747 } | |
748 else if (postXML.children.length > 0) | |
749 { | |
750 currentState = 'testRunPost-'+testId; | |
751 showPopup(); | |
752 preTestPopupStart(postXML); | |
753 } | |
754 else { | |
755 | |
756 | |
757 // No post tests, check if we have another test to perform instead | |
758 | |
759 } | |
760 } | |
761 console.log(currentState); | |
762 } | |
763 | |
764 function testEnded(testId) | |
765 { | |
766 pageXMLSave(testId); | |
767 if (testXMLSetups.length-1 > testId) | |
768 { | |
769 // Yes we have another test to perform | |
770 testId = (Number(testId)+1); | |
771 currentState = 'testRun-'+testId; | |
772 loadTest(testId); | |
773 } else { | |
774 console.log('Testing Completed!'); | |
775 currentState = 'postTest'; | |
776 // Check for any post tests | |
777 var xmlSetup = projectXML.find('setup'); | |
778 var postTest = xmlSetup.find('PostTest')[0]; | |
779 showPopup(); | |
780 preTestPopupStart(postTest); | |
781 } | |
782 } | |
783 | |
784 function buttonSubmitClick() | |
785 { | |
786 if (audioEngineContext.status == 1) { | |
787 var playback = document.getElementById('playback-button'); | |
788 playback.click(); | |
789 // This function is called when the submit button is clicked. Will check for any further tests to perform, or any post-test options | |
790 } else | |
791 { | |
792 if (audioEngineContext.timer.testStarted == false) | |
793 { | |
794 alert('You have not started the test! Please press start to begin the test!'); | |
795 return; | |
796 } | |
797 } | |
798 if (currentState.substr(0,7) == 'testRun') | |
799 { | |
800 audioEngineContext.timer.stopTest(); | |
801 advanceState(); | |
802 } | |
803 } | |
804 | |
805 function convSliderPosToRate(id) | |
806 { | |
807 var w = document.getElementById('slider').style.width; | |
808 var maxPix = w.substr(0,w.length-2); | |
809 var slider = document.getElementsByClassName('track-slider')[id]; | |
810 var pix = slider.style.left; | |
811 pix = pix.substr(0,pix.length-2); | |
812 var rate = (pix-42)/maxPix; | |
813 return rate; | |
814 } | |
815 | |
816 function pageXMLSave(testId) | |
817 { | |
818 // Saves a specific test page | |
819 var xmlDoc = currentTestHolder; | |
820 // Check if any session wide metrics are enabled | |
821 | |
822 var commentShow = testXMLSetups[testId].attributes['elementComments']; | |
823 if (commentShow != undefined) { | |
824 if (commentShow.value == 'false') {commentShow = false;} | |
825 else {commentShow = true;} | |
826 } else {commentShow = true;} | |
827 | |
828 var metric = document.createElement('metric'); | |
829 if (audioEngineContext.metric.enableTestTimer) | |
830 { | |
831 var testTime = document.createElement('metricResult'); | |
832 testTime.id = 'testTime'; | |
833 testTime.textContent = audioEngineContext.timer.testDuration; | |
834 metric.appendChild(testTime); | |
835 } | |
836 xmlDoc.appendChild(metric); | |
837 var trackSliderObjects = document.getElementsByClassName('track-slider'); | |
838 var commentObjects = document.getElementsByClassName('comment-div'); | |
839 for (var i=0; i<trackSliderObjects.length; i++) | |
840 { | |
841 var audioElement = document.createElement('audioElement'); | |
842 audioElement.id = currentTrackOrder[i].attributes['id'].value; | |
843 audioElement.url = currentTrackOrder[i].attributes['url'].value; | |
844 var value = document.createElement('value'); | |
845 value.innerHTML = convSliderPosToRate(i); | |
846 if (commentShow) { | |
847 var comment = document.createElement("comment"); | |
848 var question = document.createElement("question"); | |
849 var response = document.createElement("response"); | |
850 question.textContent = commentObjects[i].children[0].textContent; | |
851 response.textContent = commentObjects[i].children[2].value; | |
852 comment.appendChild(question); | |
853 comment.appendChild(response); | |
854 audioElement.appendChild(comment); | |
855 } | |
856 audioElement.appendChild(value); | |
857 // Check for any per element metrics | |
858 var metric = document.createElement('metric'); | |
859 var elementMetric = audioEngineContext.audioObjects[i].metric; | |
860 if (audioEngineContext.metric.enableElementTimer) { | |
861 var elementTimer = document.createElement('metricResult'); | |
862 elementTimer.id = 'elementTimer'; | |
863 elementTimer.textContent = elementMetric.listenedTimer; | |
864 metric.appendChild(elementTimer); | |
865 } | |
866 if (audioEngineContext.metric.enableElementTracker) { | |
867 var elementTrackerFull = document.createElement('metricResult'); | |
868 elementTrackerFull.id = 'elementTrackerFull'; | |
869 var data = elementMetric.movementTracker; | |
870 for (var k=0; k<data.length; k++) | |
871 { | |
872 var timePos = document.createElement('timePos'); | |
873 timePos.id = k; | |
874 var time = document.createElement('time'); | |
875 time.textContent = data[k][0]; | |
876 var position = document.createElement('position'); | |
877 position.textContent = data[k][1]; | |
878 timePos.appendChild(time); | |
879 timePos.appendChild(position); | |
880 elementTrackerFull.appendChild(timePos); | |
881 } | |
882 metric.appendChild(elementTrackerFull); | |
883 } | |
884 if (audioEngineContext.metric.enableElementInitialPosition) { | |
885 var elementInitial = document.createElement('metricResult'); | |
886 elementInitial.id = 'elementInitialPosition'; | |
887 elementInitial.textContent = elementMetric.initialPosition; | |
888 metric.appendChild(elementInitial); | |
889 } | |
890 if (audioEngineContext.metric.enableFlagListenedTo) { | |
891 var flagListenedTo = document.createElement('metricResult'); | |
892 flagListenedTo.id = 'elementFlagListenedTo'; | |
893 flagListenedTo.textContent = elementMetric.wasListenedTo; | |
894 metric.appendChild(flagListenedTo); | |
895 } | |
896 if (audioEngineContext.metric.enableFlagMoved) { | |
897 var flagMoved = document.createElement('metricResult'); | |
898 flagMoved.id = 'elementFlagMoved'; | |
899 flagMoved.textContent = elementMetric.wasMoved; | |
900 metric.appendChild(flagMoved); | |
901 } | |
902 if (audioEngineContext.metric.enableFlagComments) { | |
903 var flagComments = document.createElement('metricResult'); | |
904 flagComments.id = 'elementFlagComments'; | |
905 if (response.textContent.length == 0) {flag.textContent = 'false';} | |
906 else {flag.textContet = 'true';} | |
907 metric.appendChild(flagComments); | |
908 } | |
909 audioElement.appendChild(metric); | |
910 xmlDoc.appendChild(audioElement); | |
911 } | |
912 var commentQuestion = document.getElementsByClassName('commentQuestion'); | |
913 for (var i=0; i<commentQuestion.length; i++) | |
914 { | |
915 var cqHolder = document.createElement('CommentQuestion'); | |
916 var comment = document.createElement('comment'); | |
917 var question = document.createElement('question'); | |
918 cqHolder.id = commentQuestion[i].id; | |
919 comment.textContent = commentQuestion[i].children[2].value; | |
920 question.textContent = commentQuestion[i].children[0].textContent; | |
921 cqHolder.appendChild(question); | |
922 cqHolder.appendChild(comment); | |
923 xmlDoc.appendChild(cqHolder); | |
924 } | |
925 testResultsHolders[testId] = xmlDoc; | |
926 } | |
927 | |
928 // Only other global function which must be defined in the interface class. Determines how to create the XML document. | |
929 function interfaceXMLSave(){ | |
930 // Create the XML string to be exported with results | |
931 var xmlDoc = document.createElement("BrowserEvaluationResult"); | |
932 for (var i=0; i<testResultsHolders.length; i++) | |
933 { | |
934 xmlDoc.appendChild(testResultsHolders[i]); | |
935 } | |
936 // Append Pre/Post Questions | |
937 xmlDoc.appendChild(preTestQuestions); | |
938 xmlDoc.appendChild(postTestQuestions); | |
939 | |
940 return xmlDoc; | |
941 } |