annotate scripts/comment_parser.php @ 1103:2051868b21f0

Hotfix: Updated schema. <audioelement>, <surveyelement> and <commentquestion> have optional name attribute.
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Wed, 09 Mar 2016 11:29:16 +0000
parents b5bf2f57187c
children 9ee921c8cdd3
rev   line source
n@1102 1 <?php
n@1102 2 // Comment Parser for PHP
n@1102 3 class audioElement {
n@1102 4 function __construct($id) {
n@1102 5 $this->id = $id;
n@1102 6 $this->comments = array();
n@1102 7 }
n@1102 8 function addComment($str) {
n@1102 9 array_push($this->comments,$str);
n@1102 10 }
n@1102 11 }
n@1102 12
n@1102 13 class testPage {
n@1102 14 function __construct($id) {
n@1102 15 $this->id = $id;
n@1102 16 $this->elements = array();
n@1102 17 }
n@1102 18 }
n@1102 19 // XML Saves location - assumes it will be saves/
n@1102 20 $saves = glob("../saves/*.xml");
n@1102 21 $comment_struct = array();
n@1102 22 if (is_array($saves))
n@1102 23 {
n@1102 24 foreach($saves as $filename) {
n@1102 25 $xml_string = file_get_contents($filename, FILE_TEXT);
n@1102 26 $xml_object = simplexml_load_string($xml_string);
n@1102 27 if ($xml_object == false) {
n@1102 28 echo "<h1>FATAL</h1> <span>could not parse file ".$filename.": </span>";
n@1102 29 foreach(libxml_get_errors() as $error) {
n@1102 30 echo "<br>", $error->message;
n@1102 31 }
n@1102 32 } else {
n@1102 33 // Iterate over each audioHolder node
n@1102 34 foreach($xml_object->page as $pageInstance)
n@1102 35 {
n@1102 36 // Find the page in the comment_struct
n@1102 37 $page_struct = null;
n@1102 38 foreach($comment_struct as $comment_struct_page)
n@1102 39 {
n@1102 40 if ($pageInstance['id'] == $comment_struct_page->id)
n@1102 41 {
n@1102 42 $page_struct = $comment_struct_page;
n@1102 43 break;
n@1102 44 }
n@1102 45 }
n@1102 46 if ($page_struct == null) {
n@1102 47 array_push($comment_struct,new testPage($pageInstance['id']));
n@1102 48 $page_struct = $comment_struct[count($comment_struct)-1];
n@1102 49 }
n@1102 50 // Get the audioelements of the page
n@1102 51 foreach($pageInstance->audioelement as $fragment)
n@1102 52 {
n@1102 53 // Find the page in the comment_struct
n@1102 54 $element_struct = null;
n@1102 55 foreach($page_struct->elements as $page_struct_element)
n@1102 56 {
n@1102 57 if ($fragment['id'] == $page_struct_element->id)
n@1102 58 {
n@1102 59 $element_struct = $page_struct_element;
n@1102 60 break;
n@1102 61 }
n@1102 62 }
n@1102 63 if ($element_struct == null) {
n@1102 64 array_push($page_struct->elements,new audioElement($fragment['id']));
n@1102 65 $element_struct = $page_struct->elements[count($page_struct->elements)-1];
n@1102 66 }
n@1102 67 $element_struct->addComment($fragment->comment->response);
n@1102 68 }
n@1102 69 }
n@1102 70 }
n@1102 71 }
n@1102 72 // Now we have a sub <xml> containing all comment data
n@1102 73 switch($_GET['format']) {
n@1102 74 case "XML":
n@1102 75 // Convert to an XML
n@1102 76 $doc_struct = new SimpleXMLElement('<waetprocess/>');
n@1102 77 foreach($comment_struct as $page_struct)
n@1102 78 {
n@1102 79 $doc_page = $doc_struct->addChild("page");
n@1102 80 $doc_page->addAttribute("id",$page_struct->id);
n@1102 81 foreach($page_struct->elements as $element_struct)
n@1102 82 {
n@1102 83 $doc_element = $doc_page->addChild("audioelement");
n@1102 84 $doc_element->addAttribute("id",$element_struct->id);
n@1102 85 foreach($element_struct->comments as $comment)
n@1102 86 {
n@1102 87 $doc_comment = $doc_element->addChild("comment",$comment);
n@1102 88 }
n@1102 89 }
n@1102 90 }
n@1102 91 echo $doc_struct->asXML();
n@1102 92 break;
n@1102 93 case "JSON":
n@1102 94 // Convert to JSON
n@1102 95 $doc_string = '{ "pages": [';
n@1102 96 for($page_index = 0; $page_index < count($comment_struct); $page_index++ )
n@1102 97 {
n@1102 98 $page_struct = $comment_struct[$page_index];
n@1102 99 $doc_page = '{"id": "'.$page_struct->id.'", "elements": [';
n@1102 100 for($element_index = 0; $element_index < count($page_struct->elements); $element_index++ )
n@1102 101 {
n@1102 102 $element_struct = $page_struct->elements[$element_index];
n@1102 103 $doc_element = '{"id": "'.$element_struct->id.'", "comments": [';
n@1102 104 for($comment_index = 0; $comment_index < count($element_struct->comments); $comment_index++ )
n@1102 105 {
n@1102 106 $doc_comment = '"'.$element_struct->comments[$comment_index].'"';
n@1102 107 if ($comment_index < count($element_struct->comments)-1) {
n@1102 108 $doc_comment = $doc_comment.',';
n@1102 109 }
n@1102 110 $doc_element = $doc_element.$doc_comment;
n@1102 111 }
n@1102 112 $doc_element = $doc_element.']}';
n@1102 113 if ($element_index < count($page_struct->elements)-1) {
n@1102 114 $doc_element = $doc_element.',';
n@1102 115 }
n@1102 116 $doc_page = $doc_page.$doc_element;
n@1102 117 }
n@1102 118 $doc_page = $doc_page.']}';
n@1102 119 if ($page_index < count($comment_struct)-1) {
n@1102 120 $doc_page = $doc_page.',';
n@1102 121 }
n@1102 122 $doc_string = $doc_string.$doc_page;
n@1102 123 }
n@1102 124 $doc_string = $doc_string."]}";
n@1102 125 echo $doc_string;
n@1102 126 break;
n@1102 127 case "CSV":
n@1102 128 // Conver to CSV
n@1102 129 // The CSV has three columns: page, element, comment
n@1102 130 $doc_string = "page,element,comment"."\r\n";
n@1102 131 foreach($comment_struct as $page_struct)
n@1102 132 {
n@1102 133 foreach($page_struct->elements as $element_struct)
n@1102 134 {
n@1102 135 foreach($element_struct->comments as $comment)
n@1102 136 {
n@1102 137 $doc_string = $doc_string.$page_struct->id.",".$element_struct->id.",".$comment."\r\n";
n@1102 138 }
n@1102 139 }
n@1102 140 }
n@1102 141 echo $doc_string;
n@1102 142 }
n@1102 143 } else {
n@1102 144 echo "FATAL - No saved XML files discovered";
n@1102 145 }
n@1102 146 ?>