-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathexternallib.php
191 lines (169 loc) · 7.59 KB
/
externallib.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Elasticsearch Web Service
*
* @package search_elastic
* @copyright 2017 Matt Porritt <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core_search\manager;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_multiple_structure;
use core_external\external_single_structure;
use core_external\external_value;
/**
* Elasticsearch Web Service
*
* @package search_elastic
* @copyright 2017 Matt Porritt <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class search_elastic_external extends external_api {
/**
* Returns description of method parameters
* @return external_function_parameters
*/
public static function search_parameters() {
return new external_function_parameters ( array (
'q' => new external_value(PARAM_TEXT, 'The search query', VALUE_DEFAULT, '*'),
'timestart' => new external_value(PARAM_INT,
'Return results newer than this. Value in seconds since Epoch', VALUE_DEFAULT, 0 ),
'timeend' => new external_value(PARAM_INT,
'Return results older than this. Value in seconds since Epoch', VALUE_DEFAULT, 0 ),
'title' => new external_value(PARAM_TEXT, 'Show results that match this title', VALUE_DEFAULT, ''),
'limit' => new external_value(PARAM_TEXT, 'Limit results to this number', VALUE_DEFAULT, '100'),
'courseids' => new external_multiple_structure(
new external_value(PARAM_INT, 'Course ids'),
'List of course ids. If empty return all courses.', VALUE_OPTIONAL),
'areaids' => new external_multiple_structure(
new external_value(PARAM_TEXT, 'Area ids'),
'List of area ids. If empty return all areas.', VALUE_OPTIONAL ),
) );
}
/**
* Returns search results
*
* @param string $q The search string.
* @param integer $timestart Return results newer than this.
* @param integer $timeend Return results older than this.
* @param string $title Show results that match this title.
* @param integer $limit Limit results to this number.
* @param array $courseids Course ids.
* @param array $areaids Searcharea ids.
* @throws moodle_exception
* @return array $docs The search results
*/
public static function search($q, $timestart, $timeend, $title, $limit, $courseids=array(), $areaids=array()) {
global $USER;
// Parameter validation.
// This feels dumb and the docs are vague, buy it is required.
$params = self::validate_parameters(self::search_parameters(),
array('q' => $q,
'timestart' => $timestart,
'timeend' => $timeend,
'title' => $title,
'limit' => $limit,
'courseids' => $courseids,
'areaids' => $areaids,
));
// Context validation.
$context = context_user::instance($USER->id);
self::validate_context($context);
// Capability checking.
if (!has_capability('moodle/search:query', $context)) {
throw new moodle_exception('cannot_search');
}
// Execute search.
$search = \core_search\manager::instance();
$results = $search->search((object)$params, $params['limit']);
// Process the results.
$docs = array();
foreach ($results as $result) {
$docs[] = $result->export_for_webservice();
}
return $docs;
}
/**
* Returns description of method result value
* @return external_description
*/
public static function search_returns() {
return new external_multiple_structure(
new external_single_structure(
array(
'componentname' => new external_value(PARAM_TEXT, 'The name of the document'),
'areaname' => new external_value(PARAM_TEXT, 'the search area the document is associated with'),
'courseurl' => new external_value(PARAM_RAW, 'URL of course associated with result'),
'coursefullname' => new external_value(PARAM_TEXT, 'Full name of course associated with result'),
'modified' => new external_value(PARAM_TEXT, 'Time document was last modified'),
'title' => new external_value(PARAM_TEXT, 'The tile of the result document'),
'docurl' => new external_value(PARAM_RAW, 'The direct link to the document resource'),
'content' => new external_value(PARAM_RAW, 'The content of the result'),
'contexturl' => new external_value(PARAM_RAW, 'URL of the result context'),
'description1' => new external_value(PARAM_TEXT, 'Extra data fields for result'),
'description2' => new external_value(PARAM_TEXT, 'Extra data fields for result'),
)
)
);
}
/**
* Returns description of method parameters
* @return external_function_parameters
*/
public static function search_areas_parameters() {
return new external_function_parameters ( array (
'enabled' => new external_value(PARAM_BOOL, 'Return only enabled search areas', VALUE_DEFAULT, false)
) );
}
/**
* Returns search area ids.
*
* @param bool $enabled return only enabled search areas
* @throws moodle_exception
* @return \core_search\base[] $results The search area ids
*/
public static function search_areas($enabled) {
global $USER;
// Parameter validation.
// This feels dumb and the docs are vague, buy it is required.
$params = self::validate_parameters(self::search_areas_parameters(),
array('enabled' => $enabled));
// Context validation.
$context = context_user::instance($USER->id);
self::validate_context($context);
// Capability checking.
if (!has_capability('moodle/search:query', $context)) {
throw new moodle_exception('cannot_search');
}
// Execute search.
$search = \core_search\manager::instance();
$results = $search->get_search_areas_list($params['enabled']);
return $results;
}
/**
* Returns description of method result value
* @return external_description
*/
public static function search_areas_returns() {
return new external_multiple_structure(
new external_single_structure(
array('areaid' => new external_value(PARAM_TEXT, 'The search area ID'))
)
);
}
}