first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,74 @@
<?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/>.
namespace repository_googledocs\local\browser;
use repository_googledocs\googledocs_content;
use repository_googledocs\helper;
/**
* Utility class for browsing content from or within a specified google drive.
*
* This class is responsible for generating the content that would be displayed for a specified drive such as
* 'my drive' or any existing shared drive. It also supports generating data for paths which are located
* within a given drive.
*
* @package repository_googledocs
* @copyright 2021 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class googledocs_drive_content extends googledocs_content {
/**
* Returns all relevant contents based on the given path or search query.
*
* The method fetches all existing content (files and folders) located in a specific folder under a given drive
* through an API call.
*
* @param string $query The search query
* @return array The array containing the contents
*/
protected function get_contents(string $query): array {
$id = str_replace("'", "\'", $query);
// Define the parameters required by the API call.
// Query all files and folders which have not been trashed and located directly in the folder whose ID is $id.
$q = "'{$id}' in parents AND trashed = false";
// The file fields that should be returned in the response.
$fields = 'files(id,name,mimeType,webContentLink,webViewLink,fileExtension,modifiedTime,size,iconLink)';
$params = [
'q' => $q,
'fields' => $fields,
'spaces' => 'drive',
];
// Check whether there are any shared drives.
$response = helper::request($this->service, 'shared_drives_list', []);
if (!empty($response->drives)) {
// To be able to include content from shared drives, we need to enable 'supportsAllDrives' and
// 'includeItemsFromAllDrives'. The Google Drive API requires explicit request for inclusion of content from
// shared drives and also a confirmation that the application is designed to handle files on shared drives.
$params['supportsAllDrives'] = 'true';
$params['includeItemsFromAllDrives'] = 'true';
}
// Request the content through the API call.
$response = helper::request($this->service, 'list', $params);
return $response->files ?? [];
}
}
@@ -0,0 +1,67 @@
<?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/>.
namespace repository_googledocs\local\browser;
use repository_googledocs\googledocs_content;
use repository_googledocs\helper;
/**
* Utility class for browsing the content within the googledocs repository root.
*
* This class is responsible for generating the content that would be displayed in the googledocs repository root.
*
* @package repository_googledocs
* @copyright 2021 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class googledocs_root_content extends googledocs_content {
/**
* Returns all relevant contents based on the given path or search query.
*
* The method predefines the content which will be displayed in the repository root level. Currently,
* only the folders representing 'My drives' and 'Shared drives' will be displayed in the root level.
*
* @param string $query The search query
* @return array The array containing the contents
*/
protected function get_contents(string $query): array {
// Add 'My drive' folder into the displayed contents.
$contents = [
(object)[
'id' => \repository_googledocs::MY_DRIVE_ROOT_ID,
'name' => get_string('mydrive', 'repository_googledocs'),
'mimeType' => 'application/vnd.google-apps.folder',
'modifiedTime' => '',
],
];
// If shared drives exists, include 'Shared drives' folder to the displayed contents.
$response = helper::request($this->service, 'shared_drives_list', []);
if (!empty($response->drives)) {
$contents[] = (object)[
'id' => \repository_googledocs::SHARED_DRIVES_ROOT_ID,
'name' => get_string('shareddrives', 'repository_googledocs'),
'mimeType' => 'application/vnd.google-apps.folder',
'modifiedTime' => '',
];
}
return $contents;
}
}
@@ -0,0 +1,60 @@
<?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/>.
namespace repository_googledocs\local\browser;
use repository_googledocs\googledocs_content;
use repository_googledocs\helper;
/**
* Utility class for browsing the content within the googledocs repository shared drives root.
*
* This class is responsible for generating the content that would be displayed in the googledocs repository
* shared drives root.
*
* @package repository_googledocs
* @copyright 2021 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class googledocs_shared_drives_content extends googledocs_content {
/**
* Returns all relevant contents based on the given path or search query.
*
* The method generates the content which will be displayed in the repository shared drives root level.
* All existing shared drives will be fetched through an API call and presented as folders.
*
* @param string $query The search query
* @return array The array containing the contents
*/
protected function get_contents(string $query): array {
// Make an API request to get all existing shared drives.
$response = helper::request($this->service, 'shared_drives_list', []);
// If shared drives exist, create folder for each shared drive.
if ($shareddrives = $response->drives) {
return array_map(function($shareddrive) {
return (object)[
'id' => $shareddrive->id,
'name' => $shareddrive->name,
'mimeType' => 'application/vnd.google-apps.folder',
'modifiedTime' => '',
];
}, $shareddrives);
}
return [];
}
}
@@ -0,0 +1,210 @@
<?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/>.
namespace repository_googledocs\local\node;
/**
* Class used to represent a file node in the googledocs repository.
*
* @package repository_googledocs
* @copyright 2021 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class file_node implements node {
/** @var string The ID of the file node. */
private $id;
/** @var string|null The title of the file node. */
private $title;
/** @var string The name of the file. */
private $name;
/** @var string|null The file's export format. */
private $exportformat;
/** @var string The external link to the file. */
private $link;
/** @var string The timestamp representing the last modified date. */
private $modified;
/** @var string|null The size of the file. */
private $size;
/** @var string The thumbnail of the file. */
private $thumbnail;
/** @var string|null The type of the Google Doc file (document, presentation, etc.), null if it is a regular file. */
private $googledoctype;
/**
* Constructor.
*
* @param \stdClass $gdfile The Google Drive file object
*/
public function __construct(\stdClass $gdfile) {
$this->id = $gdfile->id;
$this->title = $this->generate_file_title($gdfile);
$this->name = $gdfile->name;
$this->exportformat = $this->generate_file_export_format($gdfile);
$this->link = $this->generate_file_link($gdfile);
$this->modified = ($gdfile->modifiedTime) ? strtotime($gdfile->modifiedTime) : '';
$this->size = !empty($gdfile->size) ? $gdfile->size : null;
// Use iconLink as a file thumbnail if set, otherwise use the default icon depending on the file type.
// Note: The Google Drive API can return a link to a preview thumbnail of the file (via thumbnailLink).
// However, in many cases the Google Drive files are not public and an authorized request is required
// to get the thumbnail which we currently do not support. Therefore, to avoid displaying broken
// thumbnail images in the repository, the icon of the Google Drive file is being used as a thumbnail
// instead as it does not require an authorized request.
// Currently, the returned file icon link points to the 16px version of the icon by default which would result
// in displaying 16px file thumbnails in the repository. To avoid this, the link can be slightly modified in
// order to get a larger version of the icon as there isn't an option to request this through the API call.
$this->thumbnail = !empty($gdfile->iconLink) ? str_replace('/16/', '/64/', $gdfile->iconLink) : '';
$this->googledoctype = !isset($gdfile->fileExtension) ?
str_replace('application/vnd.google-apps.', '', $gdfile->mimeType) : null;
}
/**
* Create a repository file array.
*
* This method returns an array which structure is compatible to represent a file node in the repository.
*
* @return array|null The node array or null if the node could not be created
*/
public function create_node_array(): ?array {
// Cannot create the file node if the file title was not generated or the export format.
// This means that the current file type is invalid or unknown.
if (!$this->title || !$this->exportformat) {
return null;
}
return [
'id' => $this->id,
'title' => $this->title,
'source' => json_encode(
[
'id' => $this->id,
'name' => $this->name,
'link' => $this->link,
'exportformat' => $this->exportformat,
'googledoctype' => $this->googledoctype,
]
),
'date' => $this->modified,
'size' => $this->size,
'thumbnail' => $this->thumbnail,
'thumbnail_height' => 64,
'thumbnail_width' => 64,
];
}
/**
* Generates and returns the title for the file node depending on the type of the Google drive file.
*
* @param \stdClass $gdfile The Google Drive file object
* @return string The file title
*/
private function generate_file_title(\stdClass $gdfile): ?string {
// Determine the file type through the file extension.
if (isset($gdfile->fileExtension)) { // The file is a regular file.
return $gdfile->name;
} else { // The file is probably a Google Doc file.
// We need to generate the name by appending the proper google doc extension.
$type = str_replace('application/vnd.google-apps.', '', $gdfile->mimeType);
if ($type === 'document') {
return "{$gdfile->name}.gdoc";
}
if ($type === 'presentation') {
return "{$gdfile->name}.gslides";
}
if ($type === 'spreadsheet') {
return "{$gdfile->name}.gsheet";
}
if ($type === 'drawing') {
$config = get_config('googledocs');
$ext = $config->drawingformat;
return "{$gdfile->name}.{$ext}";
}
}
return null;
}
/**
* Generates and returns the file export format depending on the type of the Google drive file.
*
* @param \stdClass $gdfile The Google Drive file object
* @return string The file export format
*/
private function generate_file_export_format(\stdClass $gdfile): ?string {
// Determine the file type through the file extension.
if (isset($gdfile->fileExtension)) { // The file is a regular file.
// The file has an extension, therefore we can download it.
return 'download';
} else {
// The file is probably a Google Doc file, we get the corresponding export link.
$type = str_replace('application/vnd.google-apps.', '', $gdfile->mimeType);
$types = get_mimetypes_array();
$config = get_config('googledocs');
if ($type === 'document' && !empty($config->documentformat)) {
$ext = $config->documentformat;
if ($ext === 'rtf') {
// Moodle user 'text/rtf' as the MIME type for RTF files.
// Google uses 'application/rtf' for the same type of file.
// See https://developers.google.com/drive/v3/web/manage-downloads.
return 'application/rtf';
} else {
return $types[$ext]['type'];
}
}
if ($type === 'presentation' && !empty($config->presentationformat)) {
$ext = $config->presentationformat;
return $types[$ext]['type'];
}
if ($type === 'spreadsheet' && !empty($config->spreadsheetformat)) {
$ext = $config->spreadsheetformat;
return $types[$ext]['type'];
}
if ($type === 'drawing' && !empty($config->drawingformat)) {
$ext = $config->drawingformat;
return $types[$ext]['type'];
}
}
return null;
}
/**
* Generates and returns the external link to the file.
*
* @param \stdClass $gdfile The Google Drive file object
* @return string The link to the file
*/
private function generate_file_link(\stdClass $gdfile): string {
// If the google drive file has webViewLink set, use it as an external link.
$link = !empty($gdfile->webViewLink) ? $gdfile->webViewLink : '';
// Otherwise, use webContentLink if set or leave the external link empty.
if (empty($link) && !empty($gdfile->webContentLink)) {
$link = $gdfile->webContentLink;
}
return $link;
}
}
@@ -0,0 +1,76 @@
<?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/>.
namespace repository_googledocs\local\node;
use repository_googledocs\helper;
/**
* Class used to represent a folder node in the googledocs repository.
*
* @package repository_googledocs
* @copyright 2021 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class folder_node implements node {
/** @var string The ID of the folder node. */
private $id;
/** @var string The title of the folder node. */
private $title;
/** @var bool The timestamp representing the last modified date. */
private $modified;
/** @var string The path of the folder node. */
private $path;
/**
* Constructor.
*
* @param \stdClass $gdfolder The Google Drive folder object
* @param string $path The path of the folder node
*/
public function __construct(\stdClass $gdfolder, string $path) {
$this->id = $gdfolder->id;
$this->title = $gdfolder->name;
$this->modified = $gdfolder->modifiedTime ? strtotime($gdfolder->modifiedTime) : '';
$this->path = $path;
}
/**
* Create a repository folder array.
*
* This method returns an array which structure is compatible to represent a folder node in the repository.
*
* @return array|null The node array or null if the node could not be created
*/
public function create_node_array(): ?array {
global $OUTPUT;
return [
'id' => $this->id,
'title' => $this->title,
'path' => helper::build_node_path($this->id, $this->title, $this->path),
'date' => $this->modified,
'thumbnail' => $OUTPUT->image_url(file_folder_icon())->out(false),
'thumbnail_height' => 64,
'thumbnail_width' => 64,
'children' => [],
];
}
}
@@ -0,0 +1,37 @@
<?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/>.
namespace repository_googledocs\local\node;
/**
* The googledocs repository content node interface.
*
* @package repository_googledocs
* @copyright 2021 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
interface node {
/**
* Create a repository node array.
*
* This method returns an array which structure is compatible to represent a content node (file/folder)
* in the repository.
*
* @return array|null The node array or null if the node could not be created
*/
public function create_node_array(): ?array;
}