annotate scripts/comment_parser.php @ 640:828d6b96af57 Dev_main

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