n@519: id = $id;
n@522: $this->comments = array();
n@522: }
n@522: function addComment($str) {
n@522: array_push($this->comments,$str);
n@522: }
n@522: }
n@519:
n@522: class testPage {
n@522: function __construct($id) {
n@522: $this->id = $id;
n@522: $this->elements = array();
n@522: }
n@522: }
n@519: // XML Saves location - assumes it will be saves/
n@519: $saves = glob("../saves/*.xml");
n@522: $comment_struct = array();
n@519: if (is_array($saves))
n@519: {
n@519: foreach($saves as $filename) {
n@519: $xml_string = file_get_contents($filename, FILE_TEXT);
n@519: $xml_object = simplexml_load_string($xml_string);
n@519: if ($xml_object == false) {
n@519: echo "
FATAL
could not parse file ".$filename.": ";
n@519: foreach(libxml_get_errors() as $error) {
n@519: echo "
", $error->message;
n@519: }
n@519: } else {
n@519: // Iterate over each audioHolder node
n@519: foreach($xml_object->page as $pageInstance)
n@519: {
n@522: // Find the page in the comment_struct
n@522: $page_struct = null;
n@522: foreach($comment_struct as $comment_struct_page)
n@522: {
n@522: if ($pageInstance['id'] == $comment_struct_page->id)
n@522: {
n@522: $page_struct = $comment_struct_page;
n@522: break;
n@522: }
n@522: }
n@522: if ($page_struct == null) {
n@522: array_push($comment_struct,new testPage($pageInstance['id']));
n@522: $page_struct = $comment_struct[count($comment_struct)-1];
n@522: }
n@519: // Get the audioelements of the page
n@519: foreach($pageInstance->audioelement as $fragment)
n@519: {
n@522: // Find the page in the comment_struct
n@522: $element_struct = null;
n@522: foreach($page_struct->elements as $page_struct_element)
n@522: {
n@522: if ($fragment['id'] == $page_struct_element->id)
n@522: {
n@522: $element_struct = $page_struct_element;
n@522: break;
n@522: }
n@522: }
n@522: if ($element_struct == null) {
n@522: array_push($page_struct->elements,new audioElement($fragment['id']));
n@522: $element_struct = $page_struct->elements[count($page_struct->elements)-1];
n@522: }
n@522: $element_struct->addComment($fragment->comment->response);
n@519: }
n@519: }
n@519: }
n@519: }
n@519: // Now we have a sub containing all comment data
n@522: switch($_GET['format']) {
n@522: case "XML":
n@522: // Convert to an XML
n@522: $doc_struct = new SimpleXMLElement('');
n@522: foreach($comment_struct as $page_struct)
n@522: {
n@522: $doc_page = $doc_struct->addChild("page");
n@522: $doc_page->addAttribute("id",$page_struct->id);
n@522: foreach($page_struct->elements as $element_struct)
n@522: {
n@522: $doc_element = $doc_page->addChild("audioelement");
n@522: $doc_element->addAttribute("id",$element_struct->id);
n@522: foreach($element_struct->comments as $comment)
n@522: {
n@522: $doc_comment = $doc_element->addChild("comment",$comment);
n@522: }
n@522: }
n@522: }
n@522: echo $doc_struct->asXML();
n@522: break;
n@522: case "JSON":
n@522: // Convert to JSON
n@522: $doc_string = '{ "pages": [';
n@522: for($page_index = 0; $page_index < count($comment_struct); $page_index++ )
n@522: {
n@522: $page_struct = $comment_struct[$page_index];
n@522: $doc_page = '{"id": "'.$page_struct->id.'", "elements": [';
n@522: for($element_index = 0; $element_index < count($page_struct->elements); $element_index++ )
n@522: {
n@522: $element_struct = $page_struct->elements[$element_index];
n@522: $doc_element = '{"id": "'.$element_struct->id.'", "comments": [';
n@522: for($comment_index = 0; $comment_index < count($element_struct->comments); $comment_index++ )
n@522: {
n@522: $doc_comment = '"'.$element_struct->comments[$comment_index].'"';
n@522: if ($comment_index < count($element_struct->comments)-1) {
n@522: $doc_comment = $doc_comment.',';
n@522: }
n@522: $doc_element = $doc_element.$doc_comment;
n@522: }
n@522: $doc_element = $doc_element.']}';
n@522: if ($element_index < count($page_struct->elements)-1) {
n@522: $doc_element = $doc_element.',';
n@522: }
n@522: $doc_page = $doc_page.$doc_element;
n@522: }
n@522: $doc_page = $doc_page.']}';
n@522: if ($page_index < count($comment_struct)-1) {
n@522: $doc_page = $doc_page.',';
n@522: }
n@522: $doc_string = $doc_string.$doc_page;
n@522: }
n@522: $doc_string = $doc_string."]}";
n@522: echo $doc_string;
n@522: break;
n@522: case "CSV":
n@522: // Conver to CSV
n@522: // The CSV has three columns: page, element, comment
n@522: $doc_string = "page,element,comment"."\r\n";
n@522: foreach($comment_struct as $page_struct)
n@522: {
n@522: foreach($page_struct->elements as $element_struct)
n@522: {
n@522: foreach($element_struct->comments as $comment)
n@522: {
n@522: $doc_string = $doc_string.$page_struct->id.",".$element_struct->id.",".$comment."\r\n";
n@522: }
n@522: }
n@522: }
n@522: echo $doc_string;
n@522: }
n@519: } else {
n@519: echo "FATAL - No saved XML files discovered";
n@519: }
n@519: ?>