forked from atutor/AContent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.php
86 lines (71 loc) · 3.02 KB
/
search.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
/************************************************************************/
/* AContent */
/************************************************************************/
/* Copyright (c) 2010 */
/* Inclusive Design Institute */
/* */
/* This program is free software. You can redistribute it and/or */
/* modify it under the terms of the GNU General Public License */
/* as published by the Free Software Foundation. */
/************************************************************************/
/*
* This is the web service interface to search AContent and return
* search results in REST
* Expected parameters:
* id: to identify the user. must be given
* keywords: The keywords to search for. must be given
* start: start receiving from this record number, 0 if not specified
* maxResults: Number of results desired, 10 if not specified
*/
define('TR_INCLUDE_PATH', 'include/');
include(TR_INCLUDE_PATH.'vitals.inc.php');
include_once(TR_INCLUDE_PATH. 'classes/Utility.class.php');
include_once(TR_INCLUDE_PATH. 'classes/DAO/UsersDAO.class.php');
include_once(TR_INCLUDE_PATH. 'classes/DAO/CoursesDAO.class.php');
include_once(TR_INCLUDE_PATH. 'classes/RESTWebServiceOutput.class.php');
$keywords = trim(urldecode($_REQUEST['keywords']));
$web_service_id = trim($_REQUEST['id']);
$start = intval(trim(strtolower($_REQUEST['start'])));
$maxResults = intval(trim(strtolower($_REQUEST['maxResults'])));
if ($maxResults == 0) $maxResults = 10; // default
// validate parameters
if ($keywords == '')
{
$errors[] = 'TR_ERROR_EMPTY_KEYWORDS';
}
if ($web_service_id == '')
{
$errors[] = 'TR_ERROR_EMPTY_WEB_SERVICE_ID';
}
else
{ // validate web service id
$usersDAO = new UsersDAO();
$user_row = $usersDAO->getUserByWebServiceID($web_service_id);
if (!$user_row) $errors[] = 'TR_ERROR_INVALID_WEB_SERVICE_ID';
$user_id = $user_row['user_id'];
}
// return errors
if (is_array($errors))
{
echo RESTWebServiceOutput::generateErrorRpt($errors);
exit;
}
$coursesDAO = new CoursesDAO();
$results = $coursesDAO->getSearchResult($addslashes($keywords), '', $start, $maxResults);
// get total number of search results regardless of $maxResults
$all_results = $coursesDAO->getSearchResult($addslashes($keywords));
if (is_array($all_results)) $total_num = count($all_results);
else $total_num = 0;
// calculate the last record number
if (is_array($results))
{
$num_of_results = count($results);
if ($maxResults > $num_of_results) $last_rec_number = $start + $num_of_results;
else $last_rec_number = $start + $maxResults;
}
else $last_rec_number = $total_num;
//debug($results);exit;
$restWebServiceOutput = new RESTWebServiceOutput($results, $total_num, $last_rec_number);
echo $restWebServiceOutput->getWebServiceOutput();
?>