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 repository_googledocs;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Utility class for displaying google drive content that matched a given search criteria.
|
|
|
21 |
*
|
|
|
22 |
* This class is responsible for generating the content that is returned based on a given search query.
|
|
|
23 |
*
|
|
|
24 |
* @package repository_googledocs
|
|
|
25 |
* @copyright 2021 Mihail Geshoski <mihail@moodle.com>
|
|
|
26 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
27 |
*/
|
|
|
28 |
class googledocs_content_search extends googledocs_content {
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Returns all relevant contents based on the given path and/or search query.
|
|
|
32 |
*
|
|
|
33 |
* The method fetches all content (files) through an API call that matches a given search criteria.
|
|
|
34 |
*
|
|
|
35 |
* @param string $query The search query
|
|
|
36 |
* @return array The array containing the contents
|
|
|
37 |
*/
|
|
|
38 |
protected function get_contents(string $query): array {
|
|
|
39 |
$searchterm = str_replace("'", "\'", $query);
|
|
|
40 |
|
|
|
41 |
// Define the parameters required by the API call.
|
|
|
42 |
// Query all contents which name contains $searchterm and have not been trashed.
|
|
|
43 |
$q = "fullText contains '{$searchterm}' AND trashed = false";
|
|
|
44 |
// The file fields that should be returned in the response.
|
|
|
45 |
$fields = "files(id,name,mimeType,webContentLink,webViewLink,fileExtension,modifiedTime,size,iconLink)";
|
|
|
46 |
|
|
|
47 |
$params = [
|
|
|
48 |
'q' => $q,
|
|
|
49 |
'fields' => $fields,
|
|
|
50 |
'spaces' => 'drive',
|
|
|
51 |
];
|
|
|
52 |
|
|
|
53 |
// If shared drives exist, include the additional required parameters in order to extend the content search
|
|
|
54 |
// into the shared drives area as well.
|
|
|
55 |
$response = helper::request($this->service, 'shared_drives_list', []);
|
|
|
56 |
if (!empty($response->drives)) {
|
|
|
57 |
$params['supportsAllDrives'] = 'true';
|
|
|
58 |
$params['includeItemsFromAllDrives'] = 'true';
|
|
|
59 |
$params['corpora'] = 'allDrives';
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
// Request the content through the API call.
|
|
|
63 |
$response = helper::request($this->service, 'list', $params);
|
|
|
64 |
|
|
|
65 |
return $response->files ?? [];
|
|
|
66 |
}
|
|
|
67 |
}
|