AC_4897, AC_4898, AC_4899: Multifile uploader fixes.
[acontent.git] / include / classes / RESTWebServiceOutput.class.php
1 <?php
2 /************************************************************************/
3 /* AContent                                                             */
4 /************************************************************************/
5 /* Copyright (c) 2010                                                   */
6 /* Inclusive Design Institute                                           */
7 /*                                                                      */
8 /* This program is free software. You can redistribute it and/or        */
9 /* modify it under the terms of the GNU General Public License          */
10 /* as published by the Free Software Foundation.                        */
11 /************************************************************************/
12
13 /**
14 * RESTWebServiceOutput
15 * Class to generate search or error report in REST format 
16 * @access       public
17 * @author       Cindy Qi Li
18 */
19 if (!defined("TR_INCLUDE_PATH")) die("Error: TR_INCLUDE_PATH is not defined.");
20
21 class RESTWebServiceOutput {
22
23         // all private
24         var $results;                    // constructor parameter. array of errors
25         var $totalCount;                 // constructor parameter. total number of search results regardless of the maxResults
26         var $lastRecNumber;              // constructor parameter. number of the last record in the <results> element
27         var $output;                     // final web service output
28         
29         // REST templates
30         var $rest_main =
31 '<?xml version="1.0" encoding="ISO-8859-1"?>
32 <resultset>
33   <summary>
34     <numOfTotalResults>{NUMOFTOTALRESULTS}</numOfTotalResults>
35     <numOfResults>{NUMOFRESULTS}</numOfResults>
36     <lastResultNumber>{LASTRESULTNUMBER}</lastResultNumber>
37   </summary>
38
39   <results>
40 {RESULTS}
41   </results>
42 </resultset>
43 ';
44         
45         var $rest_result = 
46 '    <result>
47       <courseID>{COURSEID}</courseID>
48       <title>{TITLE}</title>
49       <description>{DESCRIPTION}</description>
50       <creationDate>{CREATIONDATE}</creationDate>
51     </result> 
52 ';
53         
54         /**
55         * class constructor
56         * @access public
57         * @param  $results: an array of search results
58         *         $totalCount: total number of all search results
59         *         $lastRecNumber: number of the last record in the <results> element
60         */
61         function RESTWebServiceOutput($results, $totalCount, $lastRecNumber)
62         {
63                 $this->results = $results;
64                 $this->totalCount = $totalCount;
65                 $this->lastRecNumber = $lastRecNumber;
66                 
67                 $this->generateRESTRpt();
68         }
69         
70         /**
71         * private
72         * main process to generate report in html format
73         */
74         private function generateRESTRpt()
75         {
76                 if (!is_array($this->results))
77                 {
78                         $num_of_results = 0;
79                         $results_in_rest = '';
80                 }
81                 else
82                 {
83                         $num_of_results = count($this->results);
84                         foreach ($this->results as $result)
85                         {
86                                 $results_in_rest .= str_replace(array('{COURSEID}', 
87                                                              '{TITLE}',
88                                                              '{DESCRIPTION}', 
89                                                              '{CREATIONDATE}'),
90                                                       array($result['course_id'], 
91                                                             $result['title'], 
92                                                             $result['description'], 
93                                                             $result['created_date']),
94                                                       $this->rest_result);
95                         }
96                 }
97                 
98                 // calculate the last record number
99                 
100                 // generate final output
101                 $this->output = str_replace(array('{NUMOFTOTALRESULTS}', 
102                                                   '{NUMOFRESULTS}', 
103                                                           '{LASTRESULTNUMBER}', 
104                                                   '{RESULTS}'),
105                                             array($this->totalCount,
106                                                   $num_of_results,
107                                                   $this->lastRecNumber,
108                                                   $results_in_rest), 
109                                             $this->rest_main);
110         }
111         
112         /** 
113         * public
114         * return final web service output
115         * parameters: none
116         * author: Cindy Qi Li
117         */
118         public function getWebServiceOutput()
119         {
120                 return $this->output;
121         }
122         
123         /** 
124         * public
125         * return error report in html
126         * parameters: $errors: errors array
127         * author: Cindy Qi Li
128         */
129         public static function generateErrorRpt($errors)
130         {
131                 // initialize error codes. Note that all errors reported in REST need to be defined here.
132                 $errorCodes['TR_ERROR_EMPTY_KEYWORDS'] = 401;
133                 $errorCodes['TR_ERROR_EMPTY_WEB_SERVICE_ID'] = 402;
134                 $errorCodes['TR_ERROR_INVALID_WEB_SERVICE_ID'] = 403;
135                 
136                 // error template in REST format
137                 $rest_error = 
138 '<?xml version="1.0" encoding="ISO-8859-1"?>
139 <errors>
140   <totalCount>{TOTOAL_COUNT}</totalCount>
141 {ERROR_DETAIL}
142 </errors>
143 ';
144         
145                 $rest_error_detail = 
146 '  <error code="{ERROR_CODE}">
147     <message>{MESSAGE}</message>
148   </error>
149 ';
150                 if (!is_array($errors)) return false;
151                 
152                 foreach ($errors as $err)
153                 {
154                         $error_detail .= str_replace(array("{ERROR_CODE}", "{MESSAGE}"), 
155                                                      array($errorCodes[$err], htmlentities(_AT($err))), 
156                                                      $rest_error_detail); 
157                 }
158                                                     
159                 return str_replace(array('{TOTOAL_COUNT}', '{ERROR_DETAIL}'), 
160                                    array(count($errors), $error_detail),
161                                    $rest_error);
162         }
163 }
164 ?>