annotate scripts/score_parser.php @ 1940:fffc644a018d

Hotfix: Updated schema. <audioelement>, <surveyelement> and <commentquestion> have optional name attribute.
author Nicholas Jillings <nickjillings@users.noreply.github.com>
date Wed, 09 Mar 2016 11:29:16 +0000
parents 7c846e19725f
children 83b439322229
rev   line source
nickjillings@1939 1 <?php
nickjillings@1939 2 // Value parser for WAET XML
nickjillings@1939 3 // testPage --> axis --> element --> value
nickjillings@1939 4 class nestedObject {
nickjillings@1939 5 function __construct($id) {
nickjillings@1939 6 $this->id = $id;
nickjillings@1939 7 $this->nest = array();
nickjillings@1939 8 $this->type = null;
nickjillings@1939 9 $this->num = 0;
nickjillings@1939 10 }
nickjillings@1939 11 function addNewChild($id) {
nickjillings@1939 12 if ($this->type == null) {
nickjillings@1939 13 $this->type = "nest";
nickjillings@1939 14 }
nickjillings@1939 15 if ($this->type == "nest") {
nickjillings@1939 16 $obj = new nestedObject($id);
nickjillings@1939 17 array_push($this->nest,$obj);
nickjillings@1939 18 $this->num = count($this->nest);
nickjillings@1939 19 return $this->nest[$this->num-1];
nickjillings@1939 20 }
nickjillings@1939 21 return null;
nickjillings@1939 22 }
nickjillings@1939 23 function findChild($checkId) {
nickjillings@1939 24 if ($this->type == "nest"){
nickjillings@1939 25 foreach($this->nest as $child)
nickjillings@1939 26 {
nickjillings@1939 27 if (strcmp($checkId,$child->id) == 0) {
nickjillings@1939 28 return $child;
nickjillings@1939 29 }
nickjillings@1939 30 }
nickjillings@1939 31 }
nickjillings@1939 32 return null;
nickjillings@1939 33 }
nickjillings@1939 34 function addValue($val) {
nickjillings@1939 35 if ($this->type == null) {
nickjillings@1939 36 $this->type = "value";
nickjillings@1939 37 }
nickjillings@1939 38 if ($this->type == "value") {
nickjillings@1939 39 array_push($this->nest,$val);
nickjillings@1939 40 $this->num = count($this->nest);
nickjillings@1939 41 return $this->nest[$this->num-1];
nickjillings@1939 42 }
nickjillings@1939 43 return null;
nickjillings@1939 44 }
nickjillings@1939 45 }
nickjillings@1939 46
nickjillings@1939 47 // Build the root nest object to hold the testPages
nickjillings@1939 48 $root = new nestedObject("root");
nickjillings@1939 49
nickjillings@1939 50 // XML Saves location - assumes it will be saves/
nickjillings@1939 51 $saves = glob("../saves/*.xml");
nickjillings@1939 52 if (is_array($saves))
nickjillings@1939 53 {
nickjillings@1939 54 foreach($saves as $filename) {
nickjillings@1939 55 $xml_string = file_get_contents($filename, FILE_TEXT);
nickjillings@1939 56 $xml_object = simplexml_load_string($xml_string);
nickjillings@1939 57 if ($xml_object == false) {
nickjillings@1939 58 echo "<h1>FATAL</h1> <span>could not parse file ".$filename.": </span>";
nickjillings@1939 59 foreach(libxml_get_errors() as $error) {
nickjillings@1939 60 echo "<br>", $error->message;
nickjillings@1939 61 }
nickjillings@1939 62 } else {
nickjillings@1939 63 // Iterate over each $page node
nickjillings@1939 64 foreach($xml_object->page as $pageInstance)
nickjillings@1939 65 {
nickjillings@1939 66 // Find in the nest
nickjillings@1939 67 $pageInstanceId = $pageInstance['id'];
nickjillings@1939 68 $page_nest = $root->findChild($pageInstanceId);
nickjillings@1939 69 if ($page_nest == null) {
nickjillings@1939 70 $page_nest = $root->addNewChild($pageInstanceId);
nickjillings@1939 71 }
nickjillings@1939 72
nickjillings@1939 73 // Iterate over each $element node
nickjillings@1939 74 foreach($pageInstance->audioelement as $element) {
nickjillings@1939 75 // Find our specific element tag
nickjillings@1939 76 $elementId = $element['id'];
nickjillings@1939 77 $element_nest = $page_nest->findChild($elementId);
nickjillings@1939 78 if ($element_nest == null) {
nickjillings@1939 79 $element_nest = $page_nest->addNewChild($elementId);
nickjillings@1939 80 }
nickjillings@1939 81 // Now get the <value> tags
nickjillings@1939 82 foreach($element->value as $value) {
nickjillings@1939 83 $axis_nest = null;
nickjillings@1939 84 $axisName = "default";
nickjillings@1939 85 if (isset($value['interface-name']))
nickjillings@1939 86 {
nickjillings@1939 87 // Find the axis nest
nickjillings@1939 88 $axisName = $value['interface-name'];
nickjillings@1939 89 }
nickjillings@1939 90
nickjillings@1939 91 $axis_nest = $element_nest->findChild($axisName);
nickjillings@1939 92 if ($axis_nest == null) {
nickjillings@1939 93 $axis_nest = $element_nest->addNewChild($axisName);
nickjillings@1939 94 }
nickjillings@1939 95 // Now push our value
nickjillings@1939 96 $axis_nest->addValue($value);
nickjillings@1939 97 }
nickjillings@1939 98 }
nickjillings@1939 99 }
nickjillings@1939 100 }
nickjillings@1939 101 }
nickjillings@1939 102 // We now have a structure in $root. EXPORT IT
nickjillings@1939 103 switch($_GET['format']) {
nickjillings@1939 104 case "XML":
nickjillings@1939 105 // Convert to XML
nickjillings@1939 106 $doc_root = new SimpleXMLElement('<waetprocess/>');
nickjillings@1939 107 foreach($root->nest as $page) {
nickjillings@1939 108 $doc_page = $doc_root->addChild("page");
nickjillings@1939 109 $doc_page->addAttribute("id",$page->id);
nickjillings@1939 110 foreach($page->nest as $element) {
nickjillings@1939 111 $doc_element = $doc_page->addChild("audioelement");
nickjillings@1939 112 $doc_element->addAttribute("id",$element->id);
nickjillings@1939 113 foreach($element->nest as $axis) {
nickjillings@1939 114 $doc_axis = $doc_element->addChild("interface");
nickjillings@1939 115 $doc_axis->addAttribute("name",$axis->id);
nickjillings@1939 116 foreach($axis->nest as $value) {
nickjillings@1939 117 $doc_axis->addChild("value",$value);
nickjillings@1939 118 }
nickjillings@1939 119 }
nickjillings@1939 120 }
nickjillings@1939 121 }
nickjillings@1939 122 echo $doc_root->asXML();
nickjillings@1939 123 break;
nickjillings@1939 124 case "JSON":
nickjillings@1939 125 // Convert to JSON
nickjillings@1939 126 $doc_root = '{ "pages": [';
nickjillings@1939 127 for ($pageIndex = 0; $pageIndex < $root->num; $pageIndex++)
nickjillings@1939 128 {
nickjillings@1939 129 $page = $root->nest[$pageIndex];
nickjillings@1939 130 $doc_page = '{ "id": "'.$page->id.'", "elements": [';
nickjillings@1939 131 for($elementIndex = 0; $elementIndex < $page->num; $elementIndex++)
nickjillings@1939 132 {
nickjillings@1939 133 $element = $page->nest[$elementIndex];
nickjillings@1939 134 $doc_element = '{ "id": "'.$element->id.'", "axis": [';
nickjillings@1939 135 for($axisIndex = 0; $axisIndex < $element->num; $axisIndex++)
nickjillings@1939 136 {
nickjillings@1939 137 $axis = $element->nest[$axisIndex];
nickjillings@1939 138 $doc_axis = '{ "name": "'.$axis->id.'", "values": [';
nickjillings@1939 139 for ($valueIndex = 0; $valueIndex < $axis->num; $valueIndex++)
nickjillings@1939 140 {
nickjillings@1939 141 $doc_axis = $doc_axis."".strval($axis->nest[$valueIndex]);
nickjillings@1939 142 if ($valueIndex < $axis->num-1) {
nickjillings@1939 143 $doc_axis = $doc_axis.', ';
nickjillings@1939 144 }
nickjillings@1939 145 }
nickjillings@1939 146 $doc_axis = $doc_axis.']}';
nickjillings@1939 147 if ($axisIndex < $element->num-1) {
nickjillings@1939 148 $doc_axis = $doc_axis.', ';
nickjillings@1939 149 }
nickjillings@1939 150 $doc_element = $doc_element.$doc_axis;
nickjillings@1939 151 }
nickjillings@1939 152 $doc_element = $doc_element.']}';
nickjillings@1939 153 if ($elementIndex < $page->num-1) {
nickjillings@1939 154 $doc_element = $doc_element.', ';
nickjillings@1939 155 }
nickjillings@1939 156 $doc_page = $doc_page.$doc_element;
nickjillings@1939 157 }
nickjillings@1939 158 $doc_page = $doc_page.']}';
nickjillings@1939 159 if ($pageIndex < $root->num-1) {
nickjillings@1939 160 $doc_page = $doc_page.', ';
nickjillings@1939 161 }
nickjillings@1939 162 $doc_root = $doc_root.$doc_page;
nickjillings@1939 163 }
nickjillings@1939 164 $doc_root = $doc_root.']}';
nickjillings@1939 165 echo $doc_root;
nickjillings@1939 166 break;
nickjillings@1939 167 case "CSV":
nickjillings@1939 168 // Convert to CSV
nickjillings@1939 169 // CSV Columts: page, axis, element, value
nickjillings@1939 170 $doc_string = "page,element,axis,value"."\r\n";
nickjillings@1939 171 foreach($root->nest as $page){
nickjillings@1939 172 foreach($page->nest as $element) {
nickjillings@1939 173 foreach($element->nest as $axis) {
nickjillings@1939 174 foreach($axis->nest as $value) {
nickjillings@1939 175 $doc_string = $doc_string.$page->id.',';
nickjillings@1939 176 $doc_string = $doc_string.$element->id.',';
nickjillings@1939 177 $doc_string = $doc_string.$axis->id.',';
nickjillings@1939 178 $doc_string = $doc_string.$value;
nickjillings@1939 179 $doc_string = $doc_string."\r\n";
nickjillings@1939 180 }
nickjillings@1939 181 }
nickjillings@1939 182 }
nickjillings@1939 183 }
nickjillings@1939 184 echo $doc_string;
nickjillings@1939 185 }
nickjillings@1939 186 } else {
nickjillings@1939 187 echo "FATAL - No saved XML files discovered";
nickjillings@1939 188 }
nickjillings@1939 189
nickjillings@1939 190 ?>