changeset 2566:172c76d9414b

Survey checkbox now supports min and max to define how many options should be selected #17
author Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk>
date Thu, 08 Sep 2016 16:20:18 +0100
parents bc6edd2c8772
children 096bdb64d116
files js/core.js js/specification.js test_create/test_core.js tests/examples/project.xml
diffstat 4 files changed, 69 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/js/core.js	Thu Sep 08 11:05:23 2016 +0100
+++ b/js/core.js	Thu Sep 08 16:20:18 2016 +0100
@@ -915,6 +915,32 @@
 			console.log("Checkbox: "+ node.specification.statement);
 			var inputs = this.popupResponse.getElementsByTagName('input');
 			node.response = [];
+            var numChecked = 0;
+            for (var i=0; i<node.specification.options.length; i++) {
+                if (inputs[i].checked) {numChecked++;}
+            }
+            if (node.specification.min != undefined) {
+                if (node.specification.max == undefined) {
+                    if (numChecked < node.specification.min) {
+                        var msg = "You must select at least "+node.specification.min+" option";
+                        if (node.specification.min > 1) {
+                            msg += "s";
+                        }
+                        interfaceContext.lightbox.post("Error",msg);
+                        return;
+                    }
+                }
+                else {
+                    if (numChecked < node.specification.min || numChecked > node.specification.max) {
+                        if (node.specification.min == node.specification.max) {
+                            interfaceContext.lightbox.post("Error","You must only select "+node.specification.min);
+                        } else {
+                            interfaceContext.lightbox.post("Error","You must select between "+node.specification.min+" and "+node.specification.max);
+                        }
+                        return;
+                    }
+                }
+            }
 			for (var i=0; i<node.specification.options.length; i++) {
 				node.response.push({
 					name: node.specification.options[i].name,
--- a/js/specification.js	Thu Sep 08 11:05:23 2016 +0100
+++ b/js/specification.js	Thu Sep 08 16:20:18 2016 +0100
@@ -281,6 +281,16 @@
                 switch(this.type)
                 {
                     case "checkbox":
+                        if (this.min != undefined) {
+                            node.setAttribute("min", this.min);
+                        } else {
+                            node.setAttribute("min", "0");
+                        }
+                        if (this.max != undefined) {
+                            node.setAttribute("max", this.max);
+                        } else {
+                            node.setAttribute("max","undefined");
+                        }
                     case "radio":
                         for (var i=0; i<this.options.length; i++)
                         {
--- a/test_create/test_core.js	Thu Sep 08 11:05:23 2016 +0100
+++ b/test_create/test_core.js	Thu Sep 08 16:20:18 2016 +0100
@@ -886,6 +886,31 @@
                         this.dynamic.appendChild(url);
                         break;
                     case "checkbox":
+                        var minimum = document.createElement("div");
+                        var minimumEntry = document.createElement("input");
+                        var minimumText = document.createElement("span");
+                        minimumText.textContent = "Minimum: ";
+                        minimum.appendChild(minimumText);
+                        minimum.appendChild(minimumEntry);
+                        minimum.className = "survey-entry-attribute";
+                        minimumEntry.type = "number";
+                        minimumEntry.setAttribute("name","min");
+                        minimumEntry.addEventListener("change",this,false);
+                        minimumEntry.value = this.option.min;
+                        this.dynamic.appendChild(minimum);
+                        
+                        var maximum = document.createElement("div");
+                        var maximumEntry = document.createElement("input");
+                        var maximumText = document.createElement("span");
+                        maximumText.textContent = "Maximum: ";
+                        maximum.appendChild(maximumText);
+                        maximum.appendChild(maximumEntry);
+                        maximum.className = "survey-entry-attribute";
+                        maximumEntry.type = "number";
+                        maximumEntry.setAttribute("name","max");
+                        maximumEntry.addEventListener("change",this,false);
+                        maximumEntry.value = this.option.max;
+                        this.dynamic.appendChild(maximum);
                     case "radio":
                         this.dynamic.appendChild(id);
                         var optionHolder = document.createElement("div");
@@ -1928,8 +1953,14 @@
                     case "checkbox":
                         this.titleDOM.textContent = "Checkbox";
                         var id = convert.convertAttributeToDOM(this.specification,specification.schema.getAllElementsByName("id")[0]);
+                        var min = convert.convertAttributeToDOM(this.specification,specification.schema.getAllElementsByName("min")[0]);
+                        var max = convert.convertAttributeToDOM(this.specification,specification.schema.getAllElementsByName("max")[0]);
                         this.attributeDOM.appendChild(id.holder);
                         this.attributes.push(id);
+                        this.attributeDOM.appendChild(min.holder);
+                        this.attributes.push(min);
+                        this.attributeDOM.appendChild(max.holder);
+                        this.attributes.push(max);
                         break;
                     case "radio":
                         this.titleDOM.textContent = "Radio";
--- a/tests/examples/project.xml	Thu Sep 08 11:05:23 2016 +0100
+++ b/tests/examples/project.xml	Thu Sep 08 16:20:18 2016 +0100
@@ -1,12 +1,12 @@
 <?xml version="1.0" encoding="utf-8"?>
 <waet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test-schema.xsd">
-	<setup interface="APE" projectReturn="save.php" randomiseOrder='true' poolSize="2" loudness="-23" sampleRate="44100" calibration="true">
+	<setup interface="APE" projectReturn="save.php" randomiseOrder='true' poolSize="2" loudness="-23" calibration="true">
 		<survey location="before">
 			<surveyentry type="question" id="sessionId" mandatory="true">
 				<statement>Please enter your name.</statement>
                 <conditional check="equals" value="John" jumpToOnPass="test-intro" jumpToOnFail="checkboxtest"/>
 			</surveyentry>
-			<surveyentry type="checkbox" id="checkboxtest" mandatory="true">
+			<surveyentry type="checkbox" id="checkboxtest" mandatory="true" min="2" max="4">
 				<statement>Please select with which activities you have any experience (example checkbox question)</statement>
 				<option name="musician">Playing a musical instrument</option>
 				<option name="soundengineer">Recording or mixing audio</option>