From e34a301fd02b9ce0a024a7730bc5d686ef603d36 Mon Sep 17 00:00:00 2001 From: Mihail Geshoski Date: Tue, 5 Apr 2022 00:20:55 +0800 Subject: [PATCH 1/2] MDL-74424 repository_googledocs: Respect default import formats Makes sure that the correct file extension is attributed when importing a file using the Google Drive repository based on the set configuration values for the default import format settings. --- .../classes/local/node/file_node.php | 6 ++++++ repository/googledocs/lib.php | 20 ++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/repository/googledocs/classes/local/node/file_node.php b/repository/googledocs/classes/local/node/file_node.php index 142714c9469..e023edca2c7 100644 --- a/repository/googledocs/classes/local/node/file_node.php +++ b/repository/googledocs/classes/local/node/file_node.php @@ -49,6 +49,9 @@ class file_node implements node { /** @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. * @@ -72,6 +75,8 @@ class file_node implements node { // 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; } /** @@ -97,6 +102,7 @@ class file_node implements node { 'name' => $this->name, 'link' => $this->link, 'exportformat' => $this->exportformat, + 'googledoctype' => $this->googledoctype, ] ), 'date' => $this->modified, diff --git a/repository/googledocs/lib.php b/repository/googledocs/lib.php index aeae746fdee..3c2435877d3 100644 --- a/repository/googledocs/lib.php +++ b/repository/googledocs/lib.php @@ -528,8 +528,26 @@ class repository_googledocs extends repository { if ($checktype == 'application/rtf') { $checktype = 'text/rtf'; } + // Determine the relevant default import format config for the given file. + switch ($source->googledoctype) { + case 'document': + $importformatconfig = get_config('googledocs', 'documentformat'); + break; + case 'presentation': + $importformatconfig = get_config('googledocs', 'presentationformat'); + break; + case 'spreadsheet': + $importformatconfig = get_config('googledocs', 'spreadsheetformat'); + break; + case 'drawing': + $importformatconfig = get_config('googledocs', 'drawingformat'); + break; + default: + $importformatconfig = null; + } + foreach ($types as $extension => $info) { - if ($info['type'] == $checktype) { + if ($info['type'] == $checktype && $extension === $importformatconfig) { $newfilename = $source->name . '.' . $extension; break; } From 64bc864e2e142c59c16aefa925bc7a72a4a5fc9d Mon Sep 17 00:00:00 2001 From: Mihail Geshoski Date: Wed, 6 Apr 2022 01:42:09 +0800 Subject: [PATCH 2/2] MDL-74424 repository_googledocs: Update phpunit tests --- repository/googledocs/tests/local/node/file_node_test.php | 2 +- .../googledocs/tests/repository_googledocs_testcase.php | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/repository/googledocs/tests/local/node/file_node_test.php b/repository/googledocs/tests/local/node/file_node_test.php index 7e5d3bbf099..188341c15d9 100644 --- a/repository/googledocs/tests/local/node/file_node_test.php +++ b/repository/googledocs/tests/local/node/file_node_test.php @@ -100,7 +100,7 @@ class file_node_test extends \repository_googledocs_testcase { ], $this->create_file_content_node_array('d85b21c0f86cb0', 'File', 'File.gdoc', '', '', 'https://googleusercontent.com/type/application/vnd.google-apps.document', '', - 'application/rtf'), + 'application/rtf', 'document'), ], 'Google Drive file without an extension (Google presentation file; presentationformat config not set).' => [ diff --git a/repository/googledocs/tests/repository_googledocs_testcase.php b/repository/googledocs/tests/repository_googledocs_testcase.php index 85be8eadfb3..35fd15a01d7 100644 --- a/repository/googledocs/tests/repository_googledocs_testcase.php +++ b/repository/googledocs/tests/repository_googledocs_testcase.php @@ -60,10 +60,12 @@ abstract class repository_googledocs_testcase extends \advanced_testcase { * @param string $thumbnail The thumbnail of the file * @param string|null $link The external link to the file * @param string|null $exportformat The export format of the file + * @param string|null $googledoctype The type of the Google Doc file (if applicable) * @return array The repository file node array */ protected function create_file_content_node_array(string $id, string $name, string $title, ?string $size = null, - string $modified = '', string $thumbnail = '' , string $link = '', string $exportformat = ''): array { + string $modified = '', string $thumbnail = '' , string $link = '', string $exportformat = '', + ?string $googledoctype = null): array { return [ 'id' => $id, @@ -72,7 +74,8 @@ abstract class repository_googledocs_testcase extends \advanced_testcase { 'id' => $id, 'name' => $name, 'link' => $link, - 'exportformat' => $exportformat + 'exportformat' => $exportformat, + 'googledoctype' => $googledoctype ]), 'date' => $modified, 'size' => $size,