comparison test_create/test_create.html @ 1372:25a33f2489e7

randomiseOrder a global function. Schema update (hostURL attribute on <pages> not mandatory). Specification node can create XML.
author Nicholas Jillings <nickjillings@users.noreply.github.com>
date Wed, 10 Feb 2016 14:46:57 +0000
parents f329347e8918
children d4348ae0bc02
comparison
equal deleted inserted replaced
1371:70f479d47a39 1372:25a33f2489e7
15 this.metrics = null; 15 this.metrics = null;
16 this.interfaces = null; 16 this.interfaces = null;
17 this.loudness = null; 17 this.loudness = null;
18 this.errors = []; 18 this.errors = [];
19 this.schema = null; 19 this.schema = null;
20
21 this.randomiseOrder = function(input)
22 {
23 // This takes an array of information and randomises the order
24 var N = input.length;
25
26 var inputSequence = []; // For safety purposes: keep track of randomisation
27 for (var counter = 0; counter < N; ++counter)
28 inputSequence.push(counter) // Fill array
29 var inputSequenceClone = inputSequence.slice(0);
30
31 var holdArr = [];
32 var outputSequence = [];
33 for (var n=0; n<N; n++)
34 {
35 // First pick a random number
36 var r = Math.random();
37 // Multiply and floor by the number of elements left
38 r = Math.floor(r*input.length);
39 // Pick out that element and delete from the array
40 holdArr.push(input.splice(r,1)[0]);
41 // Do the same with sequence
42 outputSequence.push(inputSequence.splice(r,1)[0]);
43 }
44 console.log(inputSequenceClone.toString()); // print original array to console
45 console.log(outputSequence.toString()); // print randomised array to console
46 return holdArr;
47 };
48 20
49 this.processAttribute = function(attribute,schema) 21 this.processAttribute = function(attribute,schema)
50 { 22 {
51 // attribute is the string returned from getAttribute on the XML 23 // attribute is the string returned from getAttribute on the XML
52 // schema is the <xs:attribute> node 24 // schema is the <xs:attribute> node
162 } 134 }
163 }; 135 };
164 136
165 this.encode = function() 137 this.encode = function()
166 { 138 {
167 var root = document.implementation.createDocument(null,"waet"); 139 var RootDocument = document.implementation.createDocument(null,"waet");
168 140 var root = RootDocument.children[0];
141 root.setAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
142 root.setAttribute("xsi:noNamespaceSchemaLocation","test-schema.xsd");
169 // Build setup node 143 // Build setup node
170 144 var setup = RootDocument.createElement("setup");
171 return root; 145 var schemaSetup = this.schema.getAllElementsByName('setup')[0];
146 // First decode the attributes
147 var attributes = schemaSetup.getAllElementsByTagName('xs:attribute');
148 for (var i=0; i<attributes.length; i++)
149 {
150 var name = attributes[i].getAttribute("name");
151 if (name == undefined) {
152 name = attributes[i].getAttribute("ref");
153 }
154 if(eval("this."+name+" != undefined") || attributes[i].getAttribute("use") == "required")
155 {
156 eval("setup.setAttribute('"+name+"',this."+name+")");
157 }
158 }
159 root.appendChild(setup);
160 // Survey node
161 setup.appendChild(this.preTest.encode(RootDocument));
162 setup.appendChild(this.postTest.encode(RootDocument));
163 setup.appendChild(this.metrics.encode(RootDocument));
164 setup.appendChild(this.interfaces.encode(RootDocument));
165 for (var page of this.pages)
166 {
167 root.appendChild(page.encode(RootDocument));
168 }
169 return RootDocument;
172 }; 170 };
173 171
174 this.surveyNode = function() { 172 this.surveyNode = function() {
175 this.location = null; 173 this.location = null;
176 this.options = []; 174 this.options = [];
226 } 224 }
227 } 225 }
228 } 226 }
229 }; 227 };
230 228
231 this.exportXML = function(root) 229 this.exportXML = function(doc)
232 { 230 {
233 var node = root.createElement('surveyelement'); 231 var node = doc.createElement('surveyelement');
234 node.setAttribute('type',this.type); 232 node.setAttribute('type',this.type);
235 var statement = root.createElement('statement'); 233 var statement = doc.createElement('statement');
236 statement.textContent = this.statement; 234 statement.textContent = this.statement;
237 node.appendChild(statement); 235 node.appendChild(statement);
238 switch(this.type) 236 switch(this.type)
239 { 237 {
240 case "statement": 238 case "statement":
255 case "radio": 253 case "radio":
256 node.id = this.id; 254 node.id = this.id;
257 for (var i=0; i<this.options.length; i++) 255 for (var i=0; i<this.options.length; i++)
258 { 256 {
259 var option = this.options[i]; 257 var option = this.options[i];
260 var optionNode = root.createElement("option"); 258 var optionNode = doc.createElement("option");
261 optionNode.setAttribute("name",option.name); 259 optionNode.setAttribute("name",option.name);
262 optionNode.textContent = option.text; 260 optionNode.textContent = option.text;
263 node.appendChild(optionNode); 261 node.appendChild(optionNode);
264 } 262 }
265 break; 263 break;
277 var node = new this.OptionNode(); 275 var node = new this.OptionNode();
278 node.decode(parent,xml.children[i]); 276 node.decode(parent,xml.children[i]);
279 this.options.push(node); 277 this.options.push(node);
280 } 278 }
281 }; 279 };
282 this.encode = function(root) { 280 this.encode = function(doc) {
283 var node = root.createElement('survey'); 281 var node = doc.createElement('survey');
284 node.setAttribute('location',this.location); 282 node.setAttribute('location',this.location);
285 for (var i=0; i<this.options.length; i++) 283 for (var i=0; i<this.options.length; i++)
286 { 284 {
287 node.appendChild(this.options[i].exportXML()); 285 node.appendChild(this.options[i].exportXML(doc));
288 } 286 }
289 return node; 287 return node;
290 }; 288 };
291 }; 289 };
292 290
344 }); 342 });
345 } 343 }
346 } 344 }
347 }; 345 };
348 346
349 this.encode = function(root) { 347 this.encode = function(doc) {
350 348 var node = doc.createElement("interface");
349 if (typeof name == "string")
350 node.setAttribute("name",this.name);
351 for (var option of this.options)
352 {
353 var child = doc.createElement("interfaceoption");
354 child.setAttribute("type",option.type);
355 child.setAttribute("name",option.name);
356 node.appendChild(child);
357 }
358 if (this.scales.length != 0) {
359 var scales = doc.createElement("scales");
360 for (var scale of this.scales)
361 {
362 var child = doc.createElement("scalelabel");
363 child.setAttribute("position",scale.position);
364 child.textContent = scale.text;
365 scales.appendChild(child);
366 }
367 node.appendChild(scales);
368 }
369 return node;
351 }; 370 };
352 }; 371 };
353 372
354 this.metricNode = function() { 373 this.metricNode = function() {
355 this.enabled = []; 374 this.enabled = [];
358 for (var i in children) { 377 for (var i in children) {
359 if (isNaN(Number(i)) == true){break;} 378 if (isNaN(Number(i)) == true){break;}
360 this.enabled.push(children[i].textContent); 379 this.enabled.push(children[i].textContent);
361 } 380 }
362 } 381 }
363 this.encode = function(root) { 382 this.encode = function(doc) {
364 var node = root.createElement('metric'); 383 var node = doc.createElement('metric');
365 for (var i in this.enabled) 384 for (var i in this.enabled)
366 { 385 {
367 if (isNaN(Number(i)) == true){break;} 386 if (isNaN(Number(i)) == true){break;}
368 var child = root.createElement('metricenable'); 387 var child = doc.createElement('metricenable');
369 child.textContent = this.enabled[i]; 388 child.textContent = this.enabled[i];
370 node.appendChild(child); 389 node.appendChild(child);
371 } 390 }
372 return node; 391 return node;
373 } 392 }
466 } 485 }
467 }; 486 };
468 487
469 this.encode = function(root) 488 this.encode = function(root)
470 { 489 {
471 var AHNode = root.createElement("audioHolder"); 490 var AHNode = root.createElement("page");
472 AHNode.id = this.id; 491 // First decode the attributes
473 AHNode.setAttribute("hostURL",this.hostURL); 492 var attributes = this.schema.getAllElementsByTagName('xs:attribute');
474 AHNode.setAttribute("sampleRate",this.sampleRate); 493 for (var i=0; i<attributes.length; i++)
475 AHNode.setAttribute("randomiseOrder",this.randomiseOrder); 494 {
476 AHNode.setAttribute("repeatCount",this.repeatCount); 495 var name = attributes[i].getAttribute("name");
477 AHNode.setAttribute("loop",this.loop); 496 if (name == undefined) {
478 AHNode.setAttribute("elementComments",this.elementComments); 497 name = attributes[i].getAttribute("ref");
498 }
499 if(eval("this."+name+" != undefined") || attributes[i].getAttribute("use") == "required")
500 {
501 eval("AHNode.setAttribute('"+name+"',this."+name+")");
502 }
503 }
479 if(this.loudness != null) {AHNode.setAttribute("loudness",this.loudness);} 504 if(this.loudness != null) {AHNode.setAttribute("loudness",this.loudness);}
480 if(this.initialPosition != null) { 505 // <commentboxprefix>
481 AHNode.setAttribute("loudness",this.initialPosition*100); 506 var commentboxprefix = root.createElement("commentboxprefix");
482 } 507 commentboxprefix.textContent = this.commentBoxPrefix;
508 AHNode.appendChild(commentboxprefix);
509
483 for (var i=0; i<this.interfaces.length; i++) 510 for (var i=0; i<this.interfaces.length; i++)
484 { 511 {
485 AHNode.appendChild(this.interfaces[i].encode(root)); 512 AHNode.appendChild(this.interfaces[i].encode(root));
486 } 513 }
487 514
489 AHNode.appendChild(this.audioElements[i].encode(root)); 516 AHNode.appendChild(this.audioElements[i].encode(root));
490 } 517 }
491 // Create <CommentQuestion> 518 // Create <CommentQuestion>
492 for (var i=0; i<this.commentQuestions.length; i++) 519 for (var i=0; i<this.commentQuestions.length; i++)
493 { 520 {
494 AHNode.appendChild(this.commentQuestions[i].exportXML(root)); 521 AHNode.appendChild(this.commentQuestions[i].encode(root));
495 } 522 }
496 523
497 // Create <PreTest> 524 AHNode.appendChild(this.preTest.encode(root));
498 var AHPreTest = root.createElement("PreTest"); 525 AHNode.appendChild(this.postTest.encode(root));
499 for (var i=0; i<this.preTest.options.length; i++)
500 {
501 AHPreTest.appendChild(this.preTest.options[i].exportXML(root));
502 }
503
504 var AHPostTest = root.createElement("PostTest");
505 for (var i=0; i<this.postTest.options.length; i++)
506 {
507 AHPostTest.appendChild(this.postTest.options[i].exportXML(root));
508 }
509 AHNode.appendChild(AHPreTest);
510 AHNode.appendChild(AHPostTest);
511 return AHNode; 526 return AHNode;
512 }; 527 };
513 528
514 this.commentQuestionNode = function() { 529 this.commentQuestionNode = function() {
515 this.id = null; 530 this.id = null;
533 } 548 }
534 }; 549 };
535 550
536 this.encode = function(root) 551 this.encode = function(root)
537 { 552 {
538 553 var node = root.createElement("commentquestion");
554 node.id = this.id;
555 node.setAttribute("type",this.type);
556 var statement = root.createElement("statement");
557 statement.textContent = this.statement;
558 node.appendChild(statement);
559 for (var option of this.options)
560 {
561 var child = root.createElement("option");
562 child.setAttribute("name",option.name);
563 child.textContent = option.text;
564 node.appendChild(child);
565 }
566 return node;
539 }; 567 };
540 }; 568 };
541 569
542 this.audioElementNode = function() { 570 this.audioElementNode = function() {
543 this.url = null; 571 this.url = null;
571 } 599 }
572 600
573 }; 601 };
574 this.encode = function(root) 602 this.encode = function(root)
575 { 603 {
576 var AENode = root.createElement("audioElements"); 604 var AENode = root.createElement("audioelement");
577 AENode.id = this.id; 605 var attributes = this.schema.getAllElementsByTagName('xs:attribute');
578 AENode.setAttribute("url",this.url); 606 for (var i=0; i<attributes.length; i++)
579 AENode.setAttribute("type",this.type); 607 {
580 AENode.setAttribute("gain",linearToDecibel(this.gain)); 608 var name = attributes[i].getAttribute("name");
581 if (this.marker != false) 609 if (name == undefined) {
582 { 610 name = attributes[i].getAttribute("ref");
583 AENode.setAttribute("marker",this.marker*100); 611 }
612 if(eval("this."+name+" != undefined") || attributes[i].getAttribute("use") == "required")
613 {
614 eval("AENode.setAttribute('"+name+"',this."+name+")");
615 }
584 } 616 }
585 return AENode; 617 return AENode;
586 }; 618 };
587 }; 619 };
588 }; 620 };
589 } 621 }
622
590 </script> 623 </script>
591 <script type="text/javascript" src="test_core.js"/> 624 <script type="text/javascript" src="test_core.js"/>
592 <script type="text/javascript"> 625 <script type="text/javascript">
593 626
594 </script> 627 </script>