changeset 2758:4bf08f7279fb

Merge branch 'master' into Dev_main
author Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk>
date Sat, 15 Apr 2017 11:24:42 +0100
parents 4c76b5e8874b (current diff) 2e1cafe93c78 (diff)
children dfba9f1d316e
files
diffstat 2 files changed, 91 insertions(+), 60 deletions(-) [+]
line wrap: on
line diff
--- a/js/core.js	Fri Apr 14 16:43:55 2017 +0100
+++ b/js/core.js	Sat Apr 15 11:24:42 2017 +0100
@@ -175,7 +175,7 @@
                     gReturnURL = value;
                     break;
                 case "saveFilenamePrefix":
-                    gSaveFilenamePrefix = value;
+                    storage.filenamePrefix = value;
                     break;
             }
         }
@@ -408,51 +408,22 @@
         popup.popupContent.innerHTML = "<span>Please save the file below to give to your test supervisor</span><br>";
         popup.popupContent.appendChild(a);
     } else {
-        var saveUrlSuffix = "";
-        var saveFilenamePrefix = specification.saveFilenamePrefix;
-        if (typeof (saveFilenamePrefix) === "string" && saveFilenamePrefix.length > 0) {
-            saveUrlSuffix = "&saveFilenamePrefix=" + saveFilenamePrefix;
-        }
         var projectReturn = "";
         if (typeof specification.projectReturn == "string") {
             if (specification.projectReturn.substr(0, 4) == "http") {
                 projectReturn = specification.projectReturn;
             }
         }
-        var saveURL = projectReturn + "php/save.php?key=" + storage.SessionKey.key + saveUrlSuffix;
-        var xmlhttp = new XMLHttpRequest();
-        xmlhttp.open("POST", saveURL, true);
-        xmlhttp.setRequestHeader('Content-Type', 'text/xml');
-        xmlhttp.onerror = function () {
-            console.log('Error saving file to server! Presenting download locally');
+        storage.SessionKey.finish().then(function (resolved) {
+            if (typeof specification.returnURL == "string" && specification.returnURL.length > 0) {
+                window.location = specification.returnURL;
+            } else {
+                popup.popupContent.textContent = specification.exitText;
+            }
+        }, function (message) {
+            console.log("Save: Error! " + message.textContent);
             createProjectSave("local");
-        };
-        xmlhttp.onload = function () {
-            console.log(xmlhttp);
-            if (this.status >= 300) {
-                console.log("WARNING - Could not update at this time");
-                createProjectSave("local");
-            } else {
-                var parser = new DOMParser();
-                var xmlDoc = parser.parseFromString(xmlhttp.responseText, "application/xml");
-                var response = xmlDoc.getElementsByTagName('response')[0];
-                if (response.getAttribute("state") == "OK") {
-                    window.onbeforeunload = undefined;
-                    var file = response.getElementsByTagName("file")[0];
-                    console.log("Intermediate save: OK, written " + file.getAttribute("bytes") + "B");
-                    if (typeof specification.returnURL == "string" && specification.returnURL.length > 0) {
-                        window.location = specification.returnURL;
-                    } else {
-                        popup.popupContent.textContent = specification.exitText;
-                    }
-                } else {
-                    var message = response.getElementsByTagName("message");
-                    console.log("Save: Error! " + message.textContent);
-                    createProjectSave("local");
-                }
-            }
-        };
-        xmlhttp.send(file);
+        });
         popup.showPopup();
         popup.popupContent.innerHTML = null;
         popup.popupContent.textContent = "Submitting. Please Wait";
@@ -1213,7 +1184,7 @@
 
 function stateMachine() {
     // Object prototype for tracking and managing the test state
-    
+
     function pickSubPool(pool, numElements) {
         // Assumes each element of pool has function "alwaysInclude"
 
@@ -1227,7 +1198,7 @@
 
         return picked.concat(randomSubArray(pool, numElements - picked.length));
     }
-    
+
     this.stateMap = [];
     this.preTestSurvey = null;
     this.postTestSurvey = null;
@@ -1239,7 +1210,7 @@
 
         // Get the data from Specification
         var pagePool = [];
-        specification.pages.forEach(function(page){
+        specification.pages.forEach(function (page) {
             if (page.position !== null || page.alwaysInclude) {
                 page.alwaysInclude = true;
             }
@@ -1252,7 +1223,7 @@
 
         // Now get the order of pages
         var fixed = [];
-        pagePool.forEach(function(page){
+        pagePool.forEach(function (page) {
             if (page.position !== null) {
                 fixed.push(page);
                 var i = pagePool.indexOf(page);
@@ -1265,7 +1236,7 @@
         }
 
         // Place in the correct order
-        fixed.forEach(function(page) {
+        fixed.forEach(function (page) {
             pagePool.splice(page.position, 0, page);
         });
 
@@ -2585,19 +2556,19 @@
         for (var i = 0; i < optCount; i++) {
             var div = document.createElement('div');
             div.className = "comment-checkbox-inputs-flex";
-            
+
             var span = document.createElement('span');
             span.textContent = commentQuestion.options[i].text;
             span.className = 'comment-radio-span';
             div.appendChild(span);
-            
+
             var input = document.createElement('input');
             input.type = 'radio';
             input.name = commentQuestion.id;
             input.setAttribute('setvalue', commentQuestion.options[i].name);
             input.className = 'comment-radio';
             div.appendChild(input);
-            
+
             this.inputs.appendChild(div);
             this.options.push(input);
         }
@@ -2660,19 +2631,19 @@
         for (var i = 0; i < optCount; i++) {
             var div = document.createElement('div');
             div.className = "comment-checkbox-inputs-flex";
-            
+
             var span = document.createElement('span');
             span.textContent = commentQuestion.options[i].text;
             span.className = 'comment-radio-span';
             div.appendChild(span);
-            
+
             var input = document.createElement('input');
             input.type = 'checkbox';
             input.name = commentQuestion.id;
             input.setAttribute('setvalue', commentQuestion.options[i].name);
             input.className = 'comment-radio';
             div.appendChild(input);
-            
+
             this.inputs.appendChild(div);
             this.options.push(input);
         }
@@ -2840,7 +2811,7 @@
     };
 
     this.playhead = (function () {
-        var playhead ={};
+        var playhead = {};
         playhead.object = document.createElement('div');
         playhead.object.className = 'playhead';
         playhead.object.align = 'left';
@@ -3402,6 +3373,7 @@
     this.document = null;
     this.root = null;
     this.state = 0;
+    var pFilenamePrefix = "";
 
     this.initialise = function (existingStore) {
         if (existingStore === undefined) {
@@ -3480,7 +3452,7 @@
                     returnURL = specification.projectReturn;
                 }
             }
-            xmlhttp.open("POST", returnURL + "php/save.php?key=" + this.key);
+            xmlhttp.open("POST", returnURL + "php/save.php?key=" + this.key + "&saveFilenamePrefix=" + this.parent.filenamePrefix);
             xmlhttp.setRequestHeader('Content-Type', 'text/xml');
             xmlhttp.onerror = function () {
                 console.log('Error updating file to server!');
@@ -3505,6 +3477,42 @@
                 }
             };
             xmlhttp.send([hold.innerHTML]);
+        },
+        finish: function () {
+            // Final upload to complete the test
+            this.parent.finish();
+            var hold = document.createElement("div");
+            var clone = this.parent.root.cloneNode(true);
+            hold.appendChild(clone);
+            return new Promise(function (resolve, reject) {
+                var saveURL = specification.returnURL + "php/save.php?key=" + this.key + "&saveFilenamePrefix=" + this.parent.filenamePrefix;
+                var xmlhttp = new XMLHttpRequest();
+                xmlhttp.open("POST", saveURL);
+                xmlhttp.setRequestHeader('Content-Type', 'text/xml');
+                xmlhttp.onerror = function () {
+                    console.log('Error updating file to server!');
+                    createProjectSave("local");
+                };
+                xmlhttp.onload = function () {
+                    if (this.status >= 300) {
+                        console.log("WARNING - Could not update at this time");
+                        createProjectSave("local");
+                    } else {
+                        var parser = new DOMParser();
+                        var xmlDoc = parser.parseFromString(xmlhttp.responseText, "application/xml");
+                        var response = xmlDoc.getElementsByTagName('response')[0];
+                        if (response.getAttribute("state") == "OK") {
+                            var file = response.getElementsByTagName("file")[0];
+                            console.log("Intermediate save: OK, written " + file.getAttribute("bytes") + "B");
+                            resolve(response);
+                        } else {
+                            var message = response.getElementsByTagName("message");
+                            reject(message);
+                        }
+                    }
+                };
+                xmlhttp.send([hold.innerHTML]);
+            });
         }
     };
 
@@ -3643,13 +3651,25 @@
         this.SessionKey.update();
     };
     this.finish = function () {
-        if (this.state === 0) {
-            this.update();
-        }
         this.state = 1;
         this.root.setAttribute("state", "complete");
         return this.root;
     };
+
+    Object.defineProperties(this, {
+        'filenamePrefix': {
+            'get': function () {
+                return pFilenamePrefix;
+            },
+            'set': function (value) {
+                if (typeof value !== "string") {
+                    value = String(value);
+                }
+                pFilenamePrefix = value;
+                return value;
+            }
+        }
+    })
 }
 
 var window_depedancy_callback;
--- a/python/pythonServer.py	Fri Apr 14 16:43:55 2017 +0100
+++ b/python/pythonServer.py	Sat Apr 15 11:24:42 2017 +0100
@@ -137,16 +137,27 @@
     global curFileName
     global curSaveIndex
     options = self.path.rsplit('?')
-    options = options[1].rsplit('=')
-    key = options[1]
+    options = options[1].rsplit('&')
+    for option in options:
+        optionPair = option.rsplit('=')
+        if optionPair[0] == "key":
+            key = optionPair[1]
+        elif optionPair[0] == "saveFilenamePrefix":
+            prefix = optionPair[1]
+    if key == None:
+        self.send_response(404)
+        return
+    if prefix == None:
+        prefix = "save"
     varLen = int(self.headers['Content-Length'])
     postVars = self.rfile.read(varLen)
     print("Saving file key "+key)
-    file = open('../saves/save-'+key+'.xml','wb')
+    filename = prefix+'-'+key+'.xml'
+    file = open('../saves/'+filename,'wb')
     file.write(postVars)
     file.close()
     try:
-        wbytes = os.path.getsize('../saves/save-'+key+'.xml')
+        wbytes = os.path.getsize('../saves/'+filename)
     except OSError:
         self.send_response(200)
         self.send_header("Content-type", "text/xml")
@@ -155,7 +166,7 @@
     self.send_response(200)
     self.send_header("Content-type", "text/xml")
     self.end_headers()
-    reply = '<response state="OK"><message>OK</message><file bytes="'+str(wbytes)+'">"saves/'+curFileName+'"</file></response>'
+    reply = '<response state="OK"><message>OK</message><file bytes="'+str(wbytes)+'">"saves/'+filename+'"</file></response>'
     if sys.version_info[0] == 2:
         self.wfile.write(reply)
     elif sys.version_info[0] == 3: