changed git call from https to git readonly
[atutor.git] / mods / merlot / classes / MerlotResultParser.class.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2008 by Greg Gay, Joel Kronenberg & Heidi Hazelton*/
6 /* Adaptive Technology Resource Centre / University of Toronto          */
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: MerlotResultParser.class.php 7208 2008-02-08 16:07:24Z cindy $
14
15 /**
16 * MerlotResultParser
17 * Class for parsing XML result returned from merlot search
18 * @access       public
19 * @author       Cindy Qi Li
20 * @package Merlot Module
21 */
22 class MerlotResultParser {
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 MerlotResultParser() {
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_set_element_handler($this->parser, 'startElement', 'endElement');
38                 xml_set_character_data_handler($this->parser, 'characterData');
39         }
40
41         // public
42         function parse($xml_data) {
43                 $this->element_path   = array();
44                 $this->result_rows  = array();
45                 $this->character_data = '';
46                 $this->row_num        = 0;
47                 xml_parse($this->parser, $xml_data, TRUE);
48         }
49
50         // private
51         function startElement($parser, $name, $attributes) 
52         {
53                 array_push($this->element_path, $name);
54    }
55
56         // private
57         /* called when an element ends */
58         /* removed the current element from the $path */
59         function endElement($parser, $name) {
60                 if ($this->element_path == array('merlotMaterialSearchWebService', 'status')) 
61                 {
62                         $this->result_rows['status'] = trim($this->character_data);
63                 } 
64                 else if ($this->element_path == array('merlotMaterialSearchWebService', 'error', 'message')) 
65                 {
66                         $this->result_rows['error'] = trim($this->character_data);
67                 } 
68                 else if ($this->element_path == array('merlotMaterialSearchWebService', 'summary', 'totalCount')) 
69                 {
70                         $this->result_rows['summary']['totalCount'] = trim($this->character_data);
71                 } 
72                 else if ($this->element_path == array('merlotMaterialSearchWebService', 'summary', 'resultCount'))
73                 {
74                         $this->result_rows['summary']['resultCount'] = trim($this->character_data);
75                 } 
76                 else if ($this->element_path == array('merlotMaterialSearchWebService', 'summary', 'lastRecNumber'))
77                 {
78                         $this->result_rows['summary']['lastRecNumber'] = trim($this->character_data);
79                 } 
80                 else if ($this->element_path === array('merlotMaterialSearchWebService', 'results', 'material', 'title')) 
81                 {
82                         $this->result_rows[$this->row_num]['title'] = trim($this->character_data);
83                 } 
84                 else if ($this->element_path === array('merlotMaterialSearchWebService', 'results', 'material', 'URL')) 
85                 {
86                         $this->result_rows[$this->row_num]['URL'] = trim($this->character_data);
87                 } 
88                 else if ($this->element_path === array('merlotMaterialSearchWebService', 'results', 'material', 'authorName')) 
89                 {
90                         $this->result_rows[$this->row_num]['authorName'] = trim($this->character_data);
91                 } 
92                 else if ($this->element_path === array('merlotMaterialSearchWebService', 'results', 'material', 'creationDate')) 
93                 {
94                         $this->result_rows[$this->row_num]['creationDate'] = trim($this->character_data);
95                 } 
96                 else if ($this->element_path === array('merlotMaterialSearchWebService', 'results', 'material', 'description')) 
97                 {
98                         $this->result_rows[$this->row_num]['description'] = trim($this->character_data);
99                 } 
100                 else if ($this->element_path === array('merlotMaterialSearchWebService', 'results', 'material', 'detailURL')) 
101                 {
102                         $this->result_rows[$this->row_num]['detailURL'] = trim($this->character_data);
103                 } 
104                 else if ($this->element_path === array('merlotMaterialSearchWebService', 'results', 'material', 'creativeCommons')) 
105                 {
106                         $this->result_rows[$this->row_num]['creativeCommons'] = trim($this->character_data);
107                 } 
108                 else if ($this->element_path === array('merlotMaterialSearchWebService', 'results', 'material')) 
109                 {
110                         $this->row_num++;
111                 }
112
113                 array_pop($this->element_path);
114                 $this->character_data = '';
115         }
116
117         // private
118         function characterData($parser, $data){
119                 $this->character_data .= $data;
120         }
121
122         // public
123         function getNumOfResults() 
124         {
125                 return count($this->result_rows);
126         }
127
128         // public
129         function getParsedArray() 
130         {
131                 return $this->result_rows;
132         }
133 }
134
135 ?>