comparison core.js @ 503:58fd8bcc6620 Dev_main

randomiseOrder a global function. Schema update (hostURL attribute on <pages> not mandatory). Specification node can create XML.
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Wed, 10 Feb 2016 14:46:57 +0000
parents 4658074c6191
children f8920367ec32
comparison
equal deleted inserted replaced
502:4658074c6191 503:58fd8bcc6620
1722 this.interfaces = null; 1722 this.interfaces = null;
1723 this.loudness = null; 1723 this.loudness = null;
1724 this.errors = []; 1724 this.errors = [];
1725 this.schema = null; 1725 this.schema = null;
1726 1726
1727 this.randomiseOrder = function(input)
1728 {
1729 // This takes an array of information and randomises the order
1730 var N = input.length;
1731
1732 var inputSequence = []; // For safety purposes: keep track of randomisation
1733 for (var counter = 0; counter < N; ++counter)
1734 inputSequence.push(counter) // Fill array
1735 var inputSequenceClone = inputSequence.slice(0);
1736
1737 var holdArr = [];
1738 var outputSequence = [];
1739 for (var n=0; n<N; n++)
1740 {
1741 // First pick a random number
1742 var r = Math.random();
1743 // Multiply and floor by the number of elements left
1744 r = Math.floor(r*input.length);
1745 // Pick out that element and delete from the array
1746 holdArr.push(input.splice(r,1)[0]);
1747 // Do the same with sequence
1748 outputSequence.push(inputSequence.splice(r,1)[0]);
1749 }
1750 console.log(inputSequenceClone.toString()); // print original array to console
1751 console.log(outputSequence.toString()); // print randomised array to console
1752 return holdArr;
1753 };
1754
1755 this.processAttribute = function(attribute,schema) 1727 this.processAttribute = function(attribute,schema)
1756 { 1728 {
1757 // attribute is the string returned from getAttribute on the XML 1729 // attribute is the string returned from getAttribute on the XML
1758 // schema is the <xs:attribute> node 1730 // schema is the <xs:attribute> node
1759 if (schema.getAttribute('name') == undefined && schema.getAttribute('ref') != undefined) 1731 if (schema.getAttribute('name') == undefined && schema.getAttribute('ref') != undefined)
1868 } 1840 }
1869 }; 1841 };
1870 1842
1871 this.encode = function() 1843 this.encode = function()
1872 { 1844 {
1873 var root = document.implementation.createDocument(null,"waet"); 1845 var RootDocument = document.implementation.createDocument(null,"waet");
1874 1846 var root = RootDocument.children[0];
1847 root.setAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
1848 root.setAttribute("xsi:noNamespaceSchemaLocation","test-schema.xsd");
1875 // Build setup node 1849 // Build setup node
1876 1850 var setup = RootDocument.createElement("setup");
1877 return root; 1851 var schemaSetup = this.schema.getAllElementsByName('setup')[0];
1852 // First decode the attributes
1853 var attributes = schemaSetup.getAllElementsByTagName('xs:attribute');
1854 for (var i=0; i<attributes.length; i++)
1855 {
1856 var name = attributes[i].getAttribute("name");
1857 if (name == undefined) {
1858 name = attributes[i].getAttribute("ref");
1859 }
1860 if(eval("this."+name+" != undefined") || attributes[i].getAttribute("use") == "required")
1861 {
1862 eval("setup.setAttribute('"+name+"',this."+name+")");
1863 }
1864 }
1865 root.appendChild(setup);
1866 // Survey node
1867 setup.appendChild(this.preTest.encode(RootDocument));
1868 setup.appendChild(this.postTest.encode(RootDocument));
1869 setup.appendChild(this.metrics.encode(RootDocument));
1870 setup.appendChild(this.interfaces.encode(RootDocument));
1871 for (var page of this.pages)
1872 {
1873 root.appendChild(page.encode(RootDocument));
1874 }
1875 return RootDocument;
1878 }; 1876 };
1879 1877
1880 this.surveyNode = function() { 1878 this.surveyNode = function() {
1881 this.location = null; 1879 this.location = null;
1882 this.options = []; 1880 this.options = [];
1932 } 1930 }
1933 } 1931 }
1934 } 1932 }
1935 }; 1933 };
1936 1934
1937 this.exportXML = function(root) 1935 this.exportXML = function(doc)
1938 { 1936 {
1939 var node = root.createElement('surveyelement'); 1937 var node = doc.createElement('surveyelement');
1940 node.setAttribute('type',this.type); 1938 node.setAttribute('type',this.type);
1941 var statement = root.createElement('statement'); 1939 var statement = doc.createElement('statement');
1942 statement.textContent = this.statement; 1940 statement.textContent = this.statement;
1943 node.appendChild(statement); 1941 node.appendChild(statement);
1944 switch(this.type) 1942 switch(this.type)
1945 { 1943 {
1946 case "statement": 1944 case "statement":
1961 case "radio": 1959 case "radio":
1962 node.id = this.id; 1960 node.id = this.id;
1963 for (var i=0; i<this.options.length; i++) 1961 for (var i=0; i<this.options.length; i++)
1964 { 1962 {
1965 var option = this.options[i]; 1963 var option = this.options[i];
1966 var optionNode = root.createElement("option"); 1964 var optionNode = doc.createElement("option");
1967 optionNode.setAttribute("name",option.name); 1965 optionNode.setAttribute("name",option.name);
1968 optionNode.textContent = option.text; 1966 optionNode.textContent = option.text;
1969 node.appendChild(optionNode); 1967 node.appendChild(optionNode);
1970 } 1968 }
1971 break; 1969 break;
1983 var node = new this.OptionNode(); 1981 var node = new this.OptionNode();
1984 node.decode(parent,xml.children[i]); 1982 node.decode(parent,xml.children[i]);
1985 this.options.push(node); 1983 this.options.push(node);
1986 } 1984 }
1987 }; 1985 };
1988 this.encode = function(root) { 1986 this.encode = function(doc) {
1989 var node = root.createElement('survey'); 1987 var node = doc.createElement('survey');
1990 node.setAttribute('location',this.location); 1988 node.setAttribute('location',this.location);
1991 for (var i=0; i<this.options.length; i++) 1989 for (var i=0; i<this.options.length; i++)
1992 { 1990 {
1993 node.appendChild(this.options[i].exportXML()); 1991 node.appendChild(this.options[i].exportXML(doc));
1994 } 1992 }
1995 return node; 1993 return node;
1996 }; 1994 };
1997 }; 1995 };
1998 1996
2050 }); 2048 });
2051 } 2049 }
2052 } 2050 }
2053 }; 2051 };
2054 2052
2055 this.encode = function(root) { 2053 this.encode = function(doc) {
2056 2054 var node = doc.createElement("interface");
2055 if (typeof name == "string")
2056 node.setAttribute("name",this.name);
2057 for (var option of this.options)
2058 {
2059 var child = doc.createElement("interfaceoption");
2060 child.setAttribute("type",option.type);
2061 child.setAttribute("name",option.name);
2062 node.appendChild(child);
2063 }
2064 if (this.scales.length != 0) {
2065 var scales = doc.createElement("scales");
2066 for (var scale of this.scales)
2067 {
2068 var child = doc.createElement("scalelabel");
2069 child.setAttribute("position",scale.position);
2070 child.textContent = scale.text;
2071 scales.appendChild(child);
2072 }
2073 node.appendChild(scales);
2074 }
2075 return node;
2057 }; 2076 };
2058 }; 2077 };
2059 2078
2060 this.metricNode = function() { 2079 this.metricNode = function() {
2061 this.enabled = []; 2080 this.enabled = [];
2064 for (var i in children) { 2083 for (var i in children) {
2065 if (isNaN(Number(i)) == true){break;} 2084 if (isNaN(Number(i)) == true){break;}
2066 this.enabled.push(children[i].textContent); 2085 this.enabled.push(children[i].textContent);
2067 } 2086 }
2068 } 2087 }
2069 this.encode = function(root) { 2088 this.encode = function(doc) {
2070 var node = root.createElement('metric'); 2089 var node = doc.createElement('metric');
2071 for (var i in this.enabled) 2090 for (var i in this.enabled)
2072 { 2091 {
2073 if (isNaN(Number(i)) == true){break;} 2092 if (isNaN(Number(i)) == true){break;}
2074 var child = root.createElement('metricenable'); 2093 var child = doc.createElement('metricenable');
2075 child.textContent = this.enabled[i]; 2094 child.textContent = this.enabled[i];
2076 node.appendChild(child); 2095 node.appendChild(child);
2077 } 2096 }
2078 return node; 2097 return node;
2079 } 2098 }
2172 } 2191 }
2173 }; 2192 };
2174 2193
2175 this.encode = function(root) 2194 this.encode = function(root)
2176 { 2195 {
2177 var AHNode = root.createElement("audioHolder"); 2196 var AHNode = root.createElement("page");
2178 AHNode.id = this.id; 2197 // First decode the attributes
2179 AHNode.setAttribute("hostURL",this.hostURL); 2198 var attributes = this.schema.getAllElementsByTagName('xs:attribute');
2180 AHNode.setAttribute("sampleRate",this.sampleRate); 2199 for (var i=0; i<attributes.length; i++)
2181 AHNode.setAttribute("randomiseOrder",this.randomiseOrder); 2200 {
2182 AHNode.setAttribute("repeatCount",this.repeatCount); 2201 var name = attributes[i].getAttribute("name");
2183 AHNode.setAttribute("loop",this.loop); 2202 if (name == undefined) {
2184 AHNode.setAttribute("elementComments",this.elementComments); 2203 name = attributes[i].getAttribute("ref");
2204 }
2205 if(eval("this."+name+" != undefined") || attributes[i].getAttribute("use") == "required")
2206 {
2207 eval("AHNode.setAttribute('"+name+"',this."+name+")");
2208 }
2209 }
2185 if(this.loudness != null) {AHNode.setAttribute("loudness",this.loudness);} 2210 if(this.loudness != null) {AHNode.setAttribute("loudness",this.loudness);}
2186 if(this.initialPosition != null) { 2211 // <commentboxprefix>
2187 AHNode.setAttribute("loudness",this.initialPosition*100); 2212 var commentboxprefix = root.createElement("commentboxprefix");
2188 } 2213 commentboxprefix.textContent = this.commentBoxPrefix;
2214 AHNode.appendChild(commentboxprefix);
2215
2189 for (var i=0; i<this.interfaces.length; i++) 2216 for (var i=0; i<this.interfaces.length; i++)
2190 { 2217 {
2191 AHNode.appendChild(this.interfaces[i].encode(root)); 2218 AHNode.appendChild(this.interfaces[i].encode(root));
2192 } 2219 }
2193 2220
2195 AHNode.appendChild(this.audioElements[i].encode(root)); 2222 AHNode.appendChild(this.audioElements[i].encode(root));
2196 } 2223 }
2197 // Create <CommentQuestion> 2224 // Create <CommentQuestion>
2198 for (var i=0; i<this.commentQuestions.length; i++) 2225 for (var i=0; i<this.commentQuestions.length; i++)
2199 { 2226 {
2200 AHNode.appendChild(this.commentQuestions[i].exportXML(root)); 2227 AHNode.appendChild(this.commentQuestions[i].encode(root));
2201 } 2228 }
2202 2229
2203 // Create <PreTest> 2230 AHNode.appendChild(this.preTest.encode(root));
2204 var AHPreTest = root.createElement("PreTest"); 2231 AHNode.appendChild(this.postTest.encode(root));
2205 for (var i=0; i<this.preTest.options.length; i++)
2206 {
2207 AHPreTest.appendChild(this.preTest.options[i].exportXML(root));
2208 }
2209
2210 var AHPostTest = root.createElement("PostTest");
2211 for (var i=0; i<this.postTest.options.length; i++)
2212 {
2213 AHPostTest.appendChild(this.postTest.options[i].exportXML(root));
2214 }
2215 AHNode.appendChild(AHPreTest);
2216 AHNode.appendChild(AHPostTest);
2217 return AHNode; 2232 return AHNode;
2218 }; 2233 };
2219 2234
2220 this.commentQuestionNode = function() { 2235 this.commentQuestionNode = function() {
2221 this.id = null; 2236 this.id = null;
2239 } 2254 }
2240 }; 2255 };
2241 2256
2242 this.encode = function(root) 2257 this.encode = function(root)
2243 { 2258 {
2244 2259 var node = root.createElement("commentquestion");
2260 node.id = this.id;
2261 node.setAttribute("type",this.type);
2262 var statement = root.createElement("statement");
2263 statement.textContent = this.statement;
2264 node.appendChild(statement);
2265 for (var option of this.options)
2266 {
2267 var child = root.createElement("option");
2268 child.setAttribute("name",option.name);
2269 child.textContent = option.text;
2270 node.appendChild(child);
2271 }
2272 return node;
2245 }; 2273 };
2246 }; 2274 };
2247 2275
2248 this.audioElementNode = function() { 2276 this.audioElementNode = function() {
2249 this.url = null; 2277 this.url = null;
2277 } 2305 }
2278 2306
2279 }; 2307 };
2280 this.encode = function(root) 2308 this.encode = function(root)
2281 { 2309 {
2282 var AENode = root.createElement("audioElements"); 2310 var AENode = root.createElement("audioelement");
2283 AENode.id = this.id; 2311 var attributes = this.schema.getAllElementsByTagName('xs:attribute');
2284 AENode.setAttribute("url",this.url); 2312 for (var i=0; i<attributes.length; i++)
2285 AENode.setAttribute("type",this.type); 2313 {
2286 AENode.setAttribute("gain",linearToDecibel(this.gain)); 2314 var name = attributes[i].getAttribute("name");
2287 if (this.marker != false) 2315 if (name == undefined) {
2288 { 2316 name = attributes[i].getAttribute("ref");
2289 AENode.setAttribute("marker",this.marker*100); 2317 }
2290 } 2318 if(eval("this."+name+" != undefined") || attributes[i].getAttribute("use") == "required")
2319 {
2320 eval("AENode.setAttribute('"+name+"',this."+name+")");
2321 }
2322 }
2291 return AENode; 2323 return AENode;
2292 }; 2324 };
2293 }; 2325 };
2294 }; 2326 };
2295 } 2327 }