remove old readme
[atutor.git] / mods / _standard / tile_search / classes / ResultParser.class.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2010                                              */
6 /* Inclusive Design Institute                                           */
7 /* http://atutor.ca                                                     */
8 /*                                                                      */
9 /* This program is free software. You can redistribute it and/or        */
10 /* modify it under the terms of the GNU General Public License          */
11 /* as published by the Free Software Foundation.                        */
12 /************************************************************************/
13 // $Id$
14
15 /**
16 * ResultParser
17 * Class for parsing XML result returned from transformable search
18 * @access       public
19 * @author       Cindy Qi Li
20 * @package tile_search Module
21 */
22 class ResultParser {
23
24         // all private
25         var $parser; // the XML handler
26         var $result_rows = array(); // the module data
27         var $character_data; // tmp variable for storing the data
28         var $element_path; // array of element paths (basically a stack)
29         var $row_num;
30         var $history_num;
31
32         function ResultParser() {
33                 $this->parser = xml_parser_create(''); 
34
35                 xml_set_object($this->parser, $this);
36                 xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false); /* conform to W3C specs */
37                 xml_parser_set_option($this->parser,XML_OPTION_TARGET_ENCODING, "ISO-8859-1");
38                 xml_set_element_handler($this->parser, 'startElement', 'endElement');
39                 xml_set_character_data_handler($this->parser, 'characterData');
40         }
41
42         // public
43         function parse($xml_data) {
44                 $xml_data = trim($xml_data);
45                 
46                 $this->element_path   = array();
47                 $this->result_rows  = array();
48                 $this->character_data = '';
49                 $this->row_num        = 0;
50                 xml_parse($this->parser, $xml_data, TRUE);
51         }
52
53         // private
54         function startElement($parser, $name, $attributes) 
55         {
56                 array_push($this->element_path, $name);
57    }
58
59         // private
60         /* called when an element ends */
61         /* removed the current element from the $path */
62         function endElement($parser, $name) {
63                 // parse error message
64                 if ($this->element_path == array('errors', 'totalCount')) 
65                 {
66                         $this->result_rows['status'] = 'fail';
67                         $this->result_rows['totalCount'] = trim($this->character_data);
68                 } 
69                 else if ($this->element_path == array('errors', 'error', 'message')) 
70                 {
71                         $this->result_rows['error'][] = trim($this->character_data);
72                 } 
73                 
74                 // parse search results
75                 else if ($this->element_path == array('resultset', 'summary', 'numOfTotalResults')) 
76                 {
77                         $this->result_rows['status'] = 'success';
78                         $this->result_rows['summary']['numOfTotalResults'] = trim($this->character_data);
79                 } 
80                 else if ($this->element_path == array('resultset', 'summary', 'nmOfResults'))
81                 {
82                         $this->result_rows['summary']['nmOfResults'] = trim($this->character_data);
83                 } 
84                 else if ($this->element_path == array('resultset', 'summary', 'lastResultNumber'))
85                 {
86                         $this->result_rows['summary']['lastResultNumber'] = trim($this->character_data);
87                 } 
88                 else if ($this->element_path === array('resultset', 'results', 'result', 'courseID')) 
89                 {
90                         $this->result_rows[$this->row_num]['courseID'] = trim($this->character_data);
91                 } 
92                 else if ($this->element_path === array('resultset', 'results', 'result', 'title')) 
93                 {
94                         $this->result_rows[$this->row_num]['title'] = trim($this->character_data);
95                 } 
96 //              else if ($this->element_path === array('resultset', 'results', 'result', 'authorName')) 
97 //              {
98 //                      $this->result_rows[$this->row_num]['authorName'] = trim($this->character_data);
99 //              } 
100                 else if ($this->element_path === array('resultset', 'results', 'result', 'creationDate')) 
101                 {
102                         $this->result_rows[$this->row_num]['creationDate'] = trim($this->character_data);
103                 } 
104                 else if ($this->element_path === array('resultset', 'results', 'result', 'description')) 
105                 {
106                         $this->result_rows[$this->row_num]['description'] = trim($this->character_data);
107                 } 
108 //              else if ($this->element_path === array('resultset', 'results', 'result', 'detailURL')) 
109 //              {
110 //                      $this->result_rows[$this->row_num]['detailURL'] = trim($this->character_data);
111 //              } 
112 //              else if ($this->element_path === array('resultset', 'results', 'result', 'creativeCommons')) 
113 //              {
114 //                      $this->result_rows[$this->row_num]['creativeCommons'] = trim($this->character_data);
115 //              } 
116                 else if ($this->element_path === array('resultset', 'results', 'result')) 
117                 {
118                         $this->row_num++;
119                 }
120
121                 array_pop($this->element_path);
122                 $this->character_data = '';
123         }
124
125         // private
126         function characterData($parser, $data){
127                 $this->character_data .= $data;
128         }
129
130         // public
131         function getNumOfResults() 
132         {
133                 return count($this->result_rows);
134         }
135
136         // public
137         function getParsedArray() 
138         {
139                 return $this->result_rows;
140         }
141 }
142
143 ?>