annotate php/get_tests.php @ 2438:02df8de58e7b

Merge branch 'master' of https://github.com/BrechtDeMan/WebAudioEvaluationTool
author www-data <www-data@sucuk.dcs.qmul.ac.uk>
date Thu, 02 Jun 2016 17:20:55 +0100
parents 169f08dc9634
children d26623bd65e0
rev   line source
nicholas@2224 1 <?php
nicholas@2224 2 /*
nicholas@2224 3 Get Tests
nicholas@2224 4
nicholas@2224 5 This script returns the XML test names available, plus the number of tests
nicholas@2224 6 */
nicholas@2224 7
nicholas@2283 8 include "rel2abs.php";
nicholas@2224 9
nicholas@2224 10 // XML Saves location - assumes it will be saves/
nicholas@2283 11 $data = array();
nicholas@2224 12 $saves = glob("../saves/*.xml");
nicholas@2224 13 if (is_array($saves))
nicholas@2224 14 {
nicholas@2224 15 foreach($saves as $filename) {
nicholas@2224 16 $xml_string = file_get_contents($filename, FILE_TEXT);
nicholas@2224 17 $xml_object = simplexml_load_string($xml_string);
nicholas@2224 18 if ($xml_object) {
nicholas@2224 19 $filename = rel2abs($filename,"http://".$_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
nicholas@2224 20 $waet = $xml_object->waet[0];
nicholas@2224 21 $testName = urldecode($waet["url"]);
nicholas@2224 22 if(array_key_exists($testName,$data)) {
nicholas@2224 23 // Key exists
nicholas@2224 24 array_push($data[$testName],$filename);
nicholas@2224 25 } else {
nicholas@2224 26 // Key does not exist
nicholas@2283 27 $data[$testName] = array($filename);
nicholas@2224 28 }
nicholas@2224 29 }
nicholas@2224 30 }
nicholas@2224 31 }
nicholas@2224 32
nicholas@2224 33 // Now read the format response
nicholas@2224 34 $format = "JSON";
nicholas@2224 35 if (array_key_exists("format",$_GET)) {
nicholas@2224 36 $format = $_GET["format"];
nicholas@2224 37 }
nicholas@2224 38 switch($format) {
nicholas@2224 39 case "JSON":
nicholas@2224 40 // Return JSON
nicholas@2224 41 $doc_root = '{"tests": [';
nicholas@2224 42 $keys = array_keys($data);
nicholas@2224 43 $numTests = count($data);
nicholas@2224 44 for ($testIndex = 0; $testIndex < $numTests; $testIndex++) {
nicholas@2224 45 $test_root = '{"testName": "'.$keys[$testIndex].'", "files": [';
nicholas@2224 46 $numFiles = count($data[$keys[$testIndex]]);
nicholas@2224 47 for ($countIndex=0; $countIndex < $numFiles; $countIndex++) {
nicholas@2224 48 $test_root = $test_root.'"'.$data[$keys[$testIndex]][$countIndex].'"';
nicholas@2224 49 if ($countIndex == $numFiles-1) {
nicholas@2224 50 $test_root = $test_root.']}';
nicholas@2224 51 } else {
nicholas@2224 52 $test_root = $test_root.',';
nicholas@2224 53 }
nicholas@2224 54 }
nicholas@2224 55 $doc_root = $doc_root.$test_root;
nicholas@2224 56 if ($testIndex == $numTests-1) {
nicholas@2224 57 $doc_root = $doc_root.']}';
nicholas@2224 58 } else {
nicholas@2224 59 $doc_root = $doc_root.',';
nicholas@2224 60 }
nicholas@2224 61 }
nicholas@2224 62 echo $doc_root;
nicholas@2224 63 break;
nicholas@2224 64 default:
nicholas@2224 65 echo '{"error": "format can only be JSON"}';
nicholas@2224 66 }
nicholas@2224 67
nicholas@2224 68 ?>