2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2010 */
6 /* Inclusive Design Institute */
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 /************************************************************************/
14 * RESTWebServiceOutput
15 * Class to generate search or error report in REST format
19 if (!defined("TR_INCLUDE_PATH")) die("Error: TR_INCLUDE_PATH is not defined.");
21 class RESTWebServiceOutput {
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
31 '<?xml version="1.0" encoding="ISO-8859-1"?>
34 <numOfTotalResults>{NUMOFTOTALRESULTS}</numOfTotalResults>
35 <numOfResults>{NUMOFRESULTS}</numOfResults>
36 <lastResultNumber>{LASTRESULTNUMBER}</lastResultNumber>
47 <courseID>{COURSEID}</courseID>
48 <title>{TITLE}</title>
49 <description>{DESCRIPTION}</description>
50 <creationDate>{CREATIONDATE}</creationDate>
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
61 function RESTWebServiceOutput($results, $totalCount, $lastRecNumber)
63 $this->results = $results;
64 $this->totalCount = $totalCount;
65 $this->lastRecNumber = $lastRecNumber;
67 $this->generateRESTRpt();
72 * main process to generate report in html format
74 private function generateRESTRpt()
76 if (!is_array($this->results))
79 $results_in_rest = '';
83 $num_of_results = count($this->results);
84 foreach ($this->results as $result)
86 $results_in_rest .= str_replace(array('{COURSEID}',
90 array($result['course_id'],
92 $result['description'],
93 $result['created_date']),
98 // calculate the last record number
100 // generate final output
101 $this->output = str_replace(array('{NUMOFTOTALRESULTS}',
103 '{LASTRESULTNUMBER}',
105 array($this->totalCount,
107 $this->lastRecNumber,
114 * return final web service output
116 * author: Cindy Qi Li
118 public function getWebServiceOutput()
120 return $this->output;
125 * return error report in html
126 * parameters: $errors: errors array
127 * author: Cindy Qi Li
129 public static function generateErrorRpt($errors)
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;
136 // error template in REST format
138 '<?xml version="1.0" encoding="ISO-8859-1"?>
140 <totalCount>{TOTOAL_COUNT}</totalCount>
146 ' <error code="{ERROR_CODE}">
147 <message>{MESSAGE}</message>
150 if (!is_array($errors)) return false;
152 foreach ($errors as $err)
154 $error_detail .= str_replace(array("{ERROR_CODE}", "{MESSAGE}"),
155 array($errorCodes[$err], htmlentities(_AT($err))),
159 return str_replace(array('{TOTOAL_COUNT}', '{ERROR_DETAIL}'),
160 array(count($errors), $error_detail),