annotate scripts/comment_parser.php @ 1105:d2afd2ee8684

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