Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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 core_external\external_value;
24
use core_external\external_warnings;
25
use moodle_exception;
26
 
27
/**
28
 * External function for trigger view search results event.
29
 *
30
 * @package    core_search
31
 * @copyright  2023 Juan Leyva <juan@moodle.com>
32
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 * @since      Moodle 4.3
34
 */
35
class view_results extends external_api {
36
 
37
    /**
38
     * Webservice parameters.
39
     *
40
     * @return external_function_parameters
41
     */
42
    public static function execute_parameters(): external_function_parameters {
43
        return new external_function_parameters(
44
            [
45
                'query' => new external_value(PARAM_NOTAGS, 'the search query'),
46
                'filters' => new external_single_structure(
47
                    [
48
                        'title' => new external_value(PARAM_NOTAGS, 'result title', VALUE_OPTIONAL),
49
                        'areaids' => new external_multiple_structure(
50
                            new external_value(PARAM_RAW, 'areaid'), 'restrict results to these areas', VALUE_DEFAULT, []
51
                        ),
52
                        'courseids' => new external_multiple_structure(
53
                            new external_value(PARAM_INT, 'courseid'), 'restrict results to these courses', VALUE_DEFAULT, []
54
                        ),
55
                        'timestart' => new external_value(PARAM_INT, 'docs modified after this date', VALUE_DEFAULT, 0),
56
                        'timeend' => new external_value(PARAM_INT, 'docs modified before this date', VALUE_DEFAULT, 0)
57
                    ], 'filters to apply', VALUE_DEFAULT, []
58
                ),
59
                'page' => new external_value(PARAM_INT, 'results page number starting from 0, defaults to the first page',
60
                    VALUE_DEFAULT, 0)
61
            ]
62
        );
63
    }
64
 
65
    /**
66
     * Trigger view results event.
67
     *
68
     * @param string $query the search query
69
     * @param array $filters filters to apply
70
     * @param int $page results page
71
     * @return array status and warnings
72
     */
73
    public static function execute(string $query, array $filters = [], int $page = 0): array {
74
 
75
        $params = self::validate_parameters(self::execute_parameters(),
76
            [
77
                'query' => $query,
78
                'filters' => $filters,
79
                'page' => $page,
80
            ]
81
        );
82
 
83
        $system = \context_system::instance();
84
        external_api::validate_context($system);
85
 
86
        require_capability('moodle/search:query', $system);
87
 
88
        if (\core_search\manager::is_global_search_enabled() === false) {
89
            throw new moodle_exception('globalsearchdisabled', 'search');
90
        }
91
 
92
        $filters = new \stdClass();
93
        $filters->title = $params['filters']['title'] ?? '';
94
        $filters->timestart = $params['filters']['timestart'] ?? 0;
95
        $filters->timeend = $params['filters']['timeend'] ?? 0;
96
        $filters->areaids = $params['filters']['areaids'] ?? [];
97
        $filters->courseids = $params['filters']['courseids'] ?? [];
98
 
99
        \core_search\manager::trigger_search_results_viewed([
100
            'q' => $params['query'],
101
            'page' => $params['page'],
102
            'title' => !empty($filters->title) ? $filters->title : '',
103
            'areaids' => !empty($filters->areaids) ? $filters->areaids : [],
104
            'courseids' => !empty($filters->courseids) ? $filters->courseids : [],
105
            'timestart' => isset($filters->timestart) ? $filters->timestart : 0,
106
            'timeend' => isset($filters->timeend) ? $filters->timeend : 0
107
        ]);
108
 
109
        return ['status' => true, 'warnings' => []];
110
    }
111
 
112
    /**
113
     * Webservice returns.
114
     *
115
     * @return external_single_structure
116
     */
117
    public static function execute_returns(): external_single_structure {
118
        return new external_single_structure(
119
            [
120
                'status' => new external_value(PARAM_BOOL, 'status: true if success'),
121
                'warnings' => new external_warnings()
122
            ]
123
        );
124
    }
125
}