1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace core_search\external;
|
|
|
18 |
|
|
|
19 |
use core_external\external_api;
|
|
|
20 |
use core_external\external_function_parameters;
|
|
|
21 |
use core_external\external_single_structure;
|
|
|
22 |
use core_external\external_multiple_structure;
|
|
|
23 |
use moodle_exception;
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* External function for retrieving top search results.
|
|
|
27 |
*
|
|
|
28 |
* @package core_search
|
|
|
29 |
* @copyright 2023 Juan Leyva <juan@moodle.com>
|
|
|
30 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
31 |
* @since Moodle 4.3
|
|
|
32 |
*/
|
|
|
33 |
class get_top_results extends external_api {
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Webservice parameters.
|
|
|
37 |
*
|
|
|
38 |
* @return external_function_parameters
|
|
|
39 |
*/
|
|
|
40 |
public static function execute_parameters(): external_function_parameters {
|
|
|
41 |
|
|
|
42 |
$baseparameters = get_results::execute_parameters();
|
|
|
43 |
|
|
|
44 |
return new external_function_parameters(
|
|
|
45 |
[
|
|
|
46 |
'query' => $baseparameters->keys['query'],
|
|
|
47 |
'filters' => $baseparameters->keys['filters'],
|
|
|
48 |
]
|
|
|
49 |
);
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Gets top search results based on the provided query and filters.
|
|
|
54 |
*
|
|
|
55 |
* @param string $query the search query
|
|
|
56 |
* @param array $filters filters to apply
|
|
|
57 |
* @return array search results
|
|
|
58 |
*/
|
|
|
59 |
public static function execute(string $query, array $filters = []): array {
|
|
|
60 |
global $PAGE;
|
|
|
61 |
|
|
|
62 |
$params = self::validate_parameters(self::execute_parameters(),
|
|
|
63 |
[
|
|
|
64 |
'query' => $query,
|
|
|
65 |
'filters' => $filters,
|
|
|
66 |
]
|
|
|
67 |
);
|
|
|
68 |
|
|
|
69 |
$system = \context_system::instance();
|
|
|
70 |
external_api::validate_context($system);
|
|
|
71 |
|
|
|
72 |
require_capability('moodle/search:query', $system);
|
|
|
73 |
|
|
|
74 |
if (\core_search\manager::is_global_search_enabled() === false) {
|
|
|
75 |
throw new moodle_exception('globalsearchdisabled', 'search');
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
$search = \core_search\manager::instance();
|
|
|
79 |
|
|
|
80 |
$data = new \stdClass();
|
|
|
81 |
// First, mandatory parameters for consistency with web.
|
|
|
82 |
$data->q = $params['query'];
|
|
|
83 |
$data->title = $params['filters']['title'] ?? '';
|
|
|
84 |
$data->timestart = $params['filters']['timestart'] ?? 0;
|
|
|
85 |
$data->timeend = $params['filters']['timeend'] ?? 0;
|
|
|
86 |
$data->areaids = $params['filters']['areaids'] ?? [];
|
|
|
87 |
$data->courseids = $params['filters']['courseids'] ?? [];
|
|
|
88 |
$data->contextids = $params['filters']['contextids'] ?? [];
|
|
|
89 |
$data->userids = $params['filters']['userids'] ?? [];
|
|
|
90 |
$data->groupids = $params['filters']['groupids'] ?? [];
|
|
|
91 |
|
|
|
92 |
$cat = $params['filters']['cat'] ?? '';
|
|
|
93 |
if (\core_search\manager::is_search_area_categories_enabled()) {
|
|
|
94 |
$cat = \core_search\manager::get_search_area_category_by_name($cat);
|
|
|
95 |
}
|
|
|
96 |
if ($cat instanceof \core_search\area_category) {
|
|
|
97 |
$data->cat = $cat->get_name();
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
$docs = $search->search_top($data);
|
|
|
101 |
|
|
|
102 |
$return = [
|
|
|
103 |
'warnings' => [],
|
|
|
104 |
'results' => []
|
|
|
105 |
];
|
|
|
106 |
|
|
|
107 |
// Convert results to simple data structures.
|
|
|
108 |
if ($docs) {
|
|
|
109 |
foreach ($docs as $doc) {
|
|
|
110 |
$return['results'][] = $doc->export_doc($PAGE->get_renderer('core'));
|
|
|
111 |
}
|
|
|
112 |
}
|
|
|
113 |
return $return;
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
/**
|
|
|
117 |
* Webservice returns.
|
|
|
118 |
*
|
|
|
119 |
* @return external_single_structure
|
|
|
120 |
*/
|
|
|
121 |
public static function execute_returns(): external_single_structure {
|
|
|
122 |
|
|
|
123 |
return new external_single_structure(
|
|
|
124 |
[
|
|
|
125 |
'results' => new external_multiple_structure(
|
|
|
126 |
\core_search\external\document_exporter::get_read_structure()
|
|
|
127 |
),
|
|
|
128 |
]
|
|
|
129 |
);
|
|
|
130 |
}
|
|
|
131 |
}
|