changeset 2249:30c012132427

Implementation for #21 on PHP
author Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk>
date Tue, 19 Apr 2016 14:58:40 +0100
parents ae2bf6a1693e
children cd1126365aa3 6893f2930849
files php/pool.php php/rel2abs.php tests/pool.xml
diffstat 3 files changed, 210 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/php/pool.php	Tue Apr 19 14:58:40 2016 +0100
@@ -0,0 +1,109 @@
+<?php
+// This works out the pool of pages to force the browser to use from the master pool set by 'modifying' the XML file
+//
+
+// This scripts lists all participated pages and ranks them by lowest occurence first
+// The script then removes from the list any pages which have been completed more times than the lowest occuring test page
+// If the number of pages left is less than the number of pages requested from the poolSize, then those that were
+// selected will have the alwaysInclude attribute set to true to ensure the next iteration will be selected. Then the next
+// least occuring pages are re-added. This continues until number of testPages >= poolSize.
+
+// The reference will always point to the original master XML file.
+
+include 'rel2abs.php';
+
+// MODIFY THE FOLLOWING LINE TO POINT TO YOUR TEST FILE
+$master_file = "../tests/pool.xml";
+// Note this is relative to the PHP location
+
+// First set up the store with all the test page key nodes
+$pages = [];
+$master_xml = simplexml_load_string(file_get_contents($master_file, FILE_TEXT));
+if ($master_xml) {
+    if (!isset($master_xml->setup["poolSize"]))
+    {
+        echo file_get_contents($master_file, FILE_TEXT);
+        return;
+    }
+    $poolSize = $master_xml->setup["poolSize"];
+    foreach($master_xml->page as $pageInstance) {
+        $id = (string)$pageInstance['id'];
+        $pages[$id] = 0;
+    }
+}
+
+$waet_url = rel2abs("pool.php","http://".$_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
+
+$saves = glob("../saves/*.xml");
+if (is_array($saves))
+{
+    foreach($saves as $filename) {
+        $xml_object = simplexml_load_string(file_get_contents($filename, FILE_TEXT));
+        if($xml_object) {
+            // First we must check the saves match the master URL
+            $waet = $xml_object->waet[0];
+            if (urldecode($waet["url"])==$waet_url) {
+                // This save is a save from the master XML
+                // Count which pages have been added
+                foreach($xml_object->page as $page) {
+                    $id = (string)$page['ref'];
+                    $pages[$id] = $pages[$id] + 1;
+                }
+            }
+        }
+    }
+}
+
+// Now we have a list of pages, sorted from low to high
+// Create the new prototype tree
+$orig_doc = new DOMDocument;
+$orig_doc->loadXML(file_get_contents($master_file, FILE_TEXT));
+$orig_doc->schemaValidate("../xml/test-schema.xsd");
+$new_doc = new DOMDocument;
+$new_doc->formatOutput = true;
+//<waet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test-schema.xsd">
+$root = $new_doc->createElement('waet');
+$root->setAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
+$root->setAttribute("xsi:noNamespaceSchemaLocation","test-schema.xsd");
+$root = $new_doc->appendChild($root);
+
+// Copy over the <setup> node
+$dom_setup = $new_doc->importNode(dom_import_simplexml($master_xml->setup),true);
+$root->appendChild($dom_setup);
+
+// We must now extract the number which have been performed the least
+$rot_pages = [];
+foreach($pages as $key => $var)
+    if(array_key_exists($var,$rot_pages)) {
+        array_push($rot_pages[$var],$key);
+    } else {
+        $rot_pages[$var] = [$key];
+    }
+ksort($rot_pages);
+$Keys = array_keys($rot_pages);
+
+// Pages are grouped into an array based on the number of page initiations ($rot_pages)
+// $Keys is an array of the sorted key maps
+$pageList = $new_doc->getElementsByTagName("page");
+$iter = 0;
+while ($pageList->length < $poolSize) {
+    if ($iter > 0) {
+        // We are adding more than one set of pages, make all current always include
+        foreach($pageList as $page) {
+            $page->setAttribute("alwaysInclude","true");
+        }
+    }
+    foreach($rot_pages[$Keys[$iter]] as $pageId) {
+        // We have the pages to add as a $pageId
+        // Now COPY
+        $dom_page = $orig_doc->getElementById($pageId);
+        $dom_page = $new_doc->importNode($dom_page,true);
+        $root->appendChild($dom_page);
+    }
+    $iter++;
+    $pageList = $new_doc->getElementsByTagName("page");
+}
+
+echo $new_doc->saveXML();
+
+?>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/php/rel2abs.php	Tue Apr 19 14:58:40 2016 +0100
@@ -0,0 +1,32 @@
+<?php
+
+//http://stackoverflow.com/questions/4444475/transfrom-relative-path-into-absolute-url-using-php
+function rel2abs($rel, $base)
+{
+    /* return if already absolute URL */
+    if (parse_url($rel, PHP_URL_SCHEME) != '' || substr($rel, 0, 2) == '//') return $rel;
+
+    /* queries and anchors */
+    if ($rel[0]=='#' || $rel[0]=='?') return $base.$rel;
+
+    /* parse base URL and convert to local variables:
+     $scheme, $host, $path */
+    extract(parse_url($base));
+
+    /* remove non-directory element from path */
+    $path = preg_replace('#/[^/]*$#', '', $path);
+
+    /* destroy path if relative url points to root */
+    if ($rel[0] == '/') $path = '';
+
+    /* dirty absolute URL */
+    $abs = "$host$path/$rel";
+
+    /* replace '//' or '/./' or '/foo/../' with '/' */
+    $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
+    for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {}
+
+    /* absolute URL is ready! */
+    return $scheme.'://'.$abs;
+}
+?>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/pool.xml	Tue Apr 19 14:58:40 2016 +0100
@@ -0,0 +1,69 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<waet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test-schema.xsd">
+	<setup interface="likert" projectReturn="save.php" crossFade="3.0" poolSize="3">
+		<metric>
+			<metricenable>testTimer</metricenable>
+			<metricenable>elementTimer</metricenable>
+			<metricenable>elementInitialPosition</metricenable>
+			<metricenable>elementTracker</metricenable>
+			<metricenable>elementFlagListenedTo</metricenable>
+			<metricenable>elementFlagMoved</metricenable>
+			<metricenable>elementListenTracker</metricenable>
+		</metric>
+		<interface>
+			<interfaceoption type="check" name="fragmentMoved"/>
+		</interface>
+	</setup>
+	<page id='test-0' hostURL="media/example/" randomiseOrder='true' repeatCount='4' loop='true' showElementComments='true' loudness="-23">
+		<interface>
+			<scales>
+				<scalelabel position="0">(1) Very Annoying</scalelabel>
+				<scalelabel position="25">(2) Annoying</scalelabel>
+				<scalelabel position="50">(3) Slightly Annoying</scalelabel>
+				<scalelabel position="75">(4) Audible but not Annoying</scalelabel>
+				<scalelabel position="100">(5) Inaudible</scalelabel>
+			</scales>
+		</interface>
+		<audioelement url="0.wav" id="track-1" name="track-0" type="outside-reference"/>
+		<audioelement url="1.wav" id="track-2"/>
+	</page>
+    <page id='test-1' hostURL="media/example/" randomiseOrder='true' repeatCount='4' loop='true' showElementComments='true' loudness="-23">
+		<interface>
+			<scales>
+				<scalelabel position="0">(1) Very Annoying</scalelabel>
+				<scalelabel position="25">(2) Annoying</scalelabel>
+				<scalelabel position="50">(3) Slightly Annoying</scalelabel>
+				<scalelabel position="75">(4) Audible but not Annoying</scalelabel>
+				<scalelabel position="100">(5) Inaudible</scalelabel>
+			</scales>
+		</interface>
+		<audioelement url="0.wav" id="track-3" name="track-0"  type="outside-reference"/>
+		<audioelement url="2.wav" id="track-4"/>
+	</page>
+    <page id='test-2' hostURL="media/example/" randomiseOrder='true' repeatCount='4' loop='true' showElementComments='true' loudness="-23">
+		<interface>
+			<scales>
+				<scalelabel position="0">(1) Very Annoying</scalelabel>
+				<scalelabel position="25">(2) Annoying</scalelabel>
+				<scalelabel position="50">(3) Slightly Annoying</scalelabel>
+				<scalelabel position="75">(4) Audible but not Annoying</scalelabel>
+				<scalelabel position="100">(5) Inaudible</scalelabel>
+			</scales>
+		</interface>
+		<audioelement url="0.wav" id="track-5" name="track-0"  type="outside-reference"/>
+		<audioelement url="3.wav" id="track-6"/>
+	</page>
+    <page id='test-3' hostURL="media/example/" randomiseOrder='true' repeatCount='4' loop='true' showElementComments='true' loudness="-23">
+		<interface>
+			<scales>
+				<scalelabel position="0">(1) Very Annoying</scalelabel>
+				<scalelabel position="25">(2) Annoying</scalelabel>
+				<scalelabel position="50">(3) Slightly Annoying</scalelabel>
+				<scalelabel position="75">(4) Audible but not Annoying</scalelabel>
+				<scalelabel position="100">(5) Inaudible</scalelabel>
+			</scales>
+		</interface>
+		<audioelement url="0.wav" id="track-7" name="track-0"  type="outside-reference"/>
+		<audioelement url="4.wav" id="track-8"/>
+	</page>
+</waet>