changeset 1882:4c616e2ef71a

WIP. Adding intermediate save options. Will require mass editing of save engine as it stands, so still WIP (note: this version will not work on python server).
author Nicholas Jillings <nickjillings@users.noreply.github.com>
date Wed, 09 Mar 2016 11:12:06 +0000
parents 584efb7ec6b3
children b7db6a837242
files core.js intermediate.php keygen.php
diffstat 3 files changed, 72 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/core.js	Tue Mar 08 18:12:14 2016 +0000
+++ b/core.js	Wed Mar 09 11:12:06 2016 +0000
@@ -213,20 +213,20 @@
             }
             return;
         }
-        
+        // Build the specification
+	   specification.decode(projectXML);
         // Generate the session-key
         storage.initialise();
         
     } else if (responseDocument.children[0].nodeName == "waetresult") {
         // document is a result
         projectXML = responseDocument.getElementsByTagName('waet')[0];
+        // Build the specification
+	    specification.decode(projectXML);
         // Use the session-key
         var sessionKey = responseDocument.children[0].getAttribute(key);
         storage.initialise(sessionKey);
     }
-	
-	// Build the specification
-	specification.decode(projectXML);
 	/// CHECK FOR SAMPLE RATE COMPATIBILITY
 	if (specification.sampleRate != undefined) {
 		if (Number(specification.sampleRate) != audioContext.sampleRate) {
@@ -744,6 +744,7 @@
 			{
 				this.store.postResult(node);
 			}
+            this.store.finish();
 			advanceState();
 		}
 	};
@@ -822,6 +823,7 @@
 		{
 			if (specification.testPages <= i && specification.testPages != 0) {break;}
 			this.stateMap.push(pageHolder[i]);
+            storage.createTestPageStore(pageHolder[i]);
 		}
         
 		if (specification.preTest != null) {this.preTestSurvey = specification.preTest;}
@@ -872,7 +874,7 @@
 				{
 					this.currentStateMap.audioElements = randomiseOrder(this.currentStateMap.audioElements);
 				}
-                this.currentStore = storage.createTestPageStore(this.currentStateMap);
+                this.currentStore = storage.testPages[this.stateIndex];
 				if (this.currentStateMap.preTest != null)
 				{
 					this.currentStatePosition = 'pre';
@@ -903,6 +905,7 @@
 			case 'post':
 				this.stateIndex++;
 				this.currentStateMap = null;
+                this.currentStore.finish();
 				this.advanceState();
 				break;
 			};
@@ -3144,6 +3147,22 @@
             this.request.open("GET","keygen.php?key="+temp_key,true);
             this.request.addEventListener("load",this);
             this.request.send();
+        },
+        postData: function(nodeName,id,xml) {
+            // nodeName: the node name to find
+            // id: the id of the node (null if location==root)
+            // xml: the XML node to append
+            if (this.key != null) {
+                var postXML = new XMLHttpRequest;
+                postXML.open("POST","intermediate.php?key="+this.key+"&node="+nodeName+"&id="+id);
+                postXML.setRequestHeader('Content-Type','text/xml');
+                postXML.onerror = function() {
+                    console.log("Error posting: "+this.responseText);
+                }
+                var parent = document.createElement("div");
+                parent.appendChild(xml);
+                postXML.send([parent.innerHTML]);
+            }
         }
     }
 	
@@ -3203,6 +3222,12 @@
 				break;
 			}
 		};
+        
+        this.finish = function() {
+            if (this.parent.document != undefined) {
+                this.parent.SessionKey.postData("waetresult",null,this.XMLDOM);
+            }
+        };
 	};
 	
 	this.pageNode = function(parent,specification)
@@ -3241,6 +3266,10 @@
 		}
 		
 		this.parent.root.appendChild(this.XMLDOM);
+        
+        this.finish = function() {
+            this.parent.SessionKey.postData("waetresult",null,this.XMLDOM);
+        }
 	};
 	this.finish = function()
 	{
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/intermediate.php	Wed Mar 09 11:12:06 2016 +0000
@@ -0,0 +1,33 @@
+<?php
+// This script manages the intermediate saves
+
+//http://stackoverflow.com/questions/4778865/php-simplexml-addchild-with-another-simplexmlelement
+function sxml_append(SimpleXMLElement $to, SimpleXMLElement $from) {
+    $toDom = dom_import_simplexml($to);
+    $fromDom = dom_import_simplexml($from);
+    $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));
+}
+
+$file_key = $_GET['key'];
+$nodeName = $_GET['node'];
+$id = $_GET['id'];
+$filename = "saves/save-".$file_key.".xml";
+$save_ok = '<response state="OK"><message>OK</message><file>'.$filename.'</file></response>';
+$save_error = '<response state="error"><message>Could not update the file</message><file>'.$filename.'</file></response>';
+$node = file_get_contents('php://input');
+$inject_xml = simplexml_load_string($node);
+$xml_string = file_get_contents($filename, FILE_TEXT);
+$xml_object = simplexml_load_string($xml_string);
+
+if ($nodeName == "waetresult") {
+    sxml_append($xml_object, $inject_xml);
+} else {
+    if ($id == "undefined") {
+        return;
+    } else {
+        $result = $xml_object->xpath("".$nodeName."[@id='".$id."']");
+        sxml_append($result[0], $inject_xml);
+    }
+}
+$xml_object->asXML($filename)
+?>
\ No newline at end of file
--- a/keygen.php	Tue Mar 08 18:12:14 2016 +0000
+++ b/keygen.php	Wed Mar 09 11:12:06 2016 +0000
@@ -1,7 +1,7 @@
 <?php
 // This checks the key sent by the JavaScript against the current bunch of saves
 // XML Saves location - assumes it will be saves/
-$saves = glob("../saves/*.xml");
+$saves = glob("saves/*.xml");
 
 $key_requested = $_GET['key'];
 
@@ -26,8 +26,12 @@
     echo $xml_good;
     // TODO:
     //  Generate the XML Base file and save it
+    $doc_struct = new SimpleXMLElement('<waetresult/>');
+    $doc_struct->addAttribute("key",$key_requested);
     //  Add start time
     //  Add IP Address information
+    //  Save the file
+    $doc_struct->asXML("saves/save-".$key_requested.".xml");
     return;
 } else {
     echo $xml_error;