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