annotate scripts/comment_parser.php @ 1274:f22a6240fb51

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