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 repository_googledocs;
18
 
19
/**
20
 * Base class for presenting the googledocs repository contents.
21
 *
22
 * @package    repository_googledocs
23
 * @copyright  2021 Mihail Geshoski <mihail@moodle.com>
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
26
abstract class googledocs_content {
27
 
28
    /** @var rest The rest API object. */
29
    protected $service;
30
 
31
    /** @var string The current path. */
32
    protected $path;
33
 
34
    /** @var bool Whether sorting should be applied to the fetched content. */
35
    protected $sortcontent;
36
 
37
    /**
38
     * Constructor.
39
     *
40
     * @param rest $service The rest API object
41
     * @param string $path The current path
42
     * @param bool $sortcontent Whether sorting should be applied to the content
43
     */
44
    public function __construct(rest $service, string $path, bool $sortcontent = true) {
45
        $this->service = $service;
46
        $this->path = $path;
47
        $this->sortcontent = $sortcontent;
48
    }
49
 
50
    /**
51
     * Generate and return an array containing all repository node (files and folders) arrays for the existing content
52
     * based on the path or search query.
53
     *
54
     * @param string $query The search query
55
     * @param callable $isaccepted The callback function which determines whether a given file should be displayed
56
     *                             or filtered based on the existing file restrictions
57
     * @return array The array containing the repository content node arrays
58
     */
59
    public function get_content_nodes(string $query, callable $isaccepted): array {
60
        $files = [];
61
        $folders = [];
62
 
63
        foreach ($this->get_contents($query) as $gdcontent) {
64
            $node = helper::get_node($gdcontent, $this->path);
65
            // Create the repository node array.
66
            $nodearray = $node->create_node_array();
67
            // If the repository node array was successfully generated and the type of the content is accepted,
68
            // add it to the repository content nodes array.
69
            if ($nodearray && $isaccepted($nodearray)) {
70
                // Group the content nodes by type (files and folders). Generate unique array keys for each content node
71
                // which will be later used by the sorting function. Note: Using the item id along with the name as key
72
                // of the array because Google Drive allows files and folders with identical names.
73
                if (isset($nodearray['source'])) { // If the content node has a source attribute, it is a file node.
74
                    $files["{$nodearray['title']}{$nodearray['id']}"] = $nodearray;
75
                } else {
76
                    $folders["{$nodearray['title']}{$nodearray['id']}"] = $nodearray;
77
                }
78
            }
79
        }
80
        // If sorting is required, order the results alphabetically by their array keys.
81
        if ($this->sortcontent) {
82
            \core_collator::ksort($files, \core_collator::SORT_STRING);
83
            \core_collator::ksort($folders, \core_collator::SORT_STRING);
84
        }
85
 
86
        return array_merge(array_values($folders), array_values($files));
87
    }
88
 
89
    /**
90
     * Build the navigation (breadcrumb) from a given path.
91
     *
92
     * @return array Array containing name and path of each navigation node
93
     */
94
    public function get_navigation(): array {
95
        $nav = [];
96
        $navtrail = '';
97
        $pathnodes = explode('/', $this->path);
98
 
99
        foreach ($pathnodes as $node) {
100
            list($id, $name) = helper::explode_node_path($node);
101
            $name = empty($name) ? $id : $name;
102
            $nav[] = [
103
                'name' => $name,
104
                'path' => helper::build_node_path($id, $name, $navtrail),
105
            ];
106
            $tmp = end($nav);
107
            $navtrail = $tmp['path'];
108
        }
109
 
110
        return $nav;
111
    }
112
 
113
    /**
114
     * Returns all relevant contents (files and folders) based on the given path or search query.
115
     *
116
     * @param string $query The search query
117
     * @return array The array containing the contents
118
     */
119
    abstract protected function get_contents(string $query): array;
120
}