diff --git a/lang/en/repository.php b/lang/en/repository.php index d76784a5b7b79..16f77ddf476d9 100644 --- a/lang/en/repository.php +++ b/lang/en/repository.php @@ -232,6 +232,7 @@ $string['upload'] = 'Upload this file'; $string['uploading'] = 'Uploading...'; $string['uploadsucc'] = 'The file has been uploaded successfully'; +$string['unknownsource'] = 'Unknown source'; $string['undisclosedsource'] = '(Undisclosed)'; $string['undisclosedreference'] = '(Undisclosed)'; $string['uselatestfile'] = 'Use latest file'; diff --git a/repository/googledocs/lang/en/repository_googledocs.php b/repository/googledocs/lang/en/repository_googledocs.php index e64dabcb73e09..cd4c4af3a599f 100644 --- a/repository/googledocs/lang/en/repository_googledocs.php +++ b/repository/googledocs/lang/en/repository_googledocs.php @@ -41,6 +41,7 @@ $string['supportedreturntypes'] = 'Supported files'; $string['defaultreturntype'] = 'Default return type'; $string['fileoptions'] = 'The types and defaults for returned files is configurable here. Note that all files linked externally will be updated so that the owner is the Moodle system account.'; +$string['owner'] = 'Owned by: {$a}'; // Deprecated since Moodle 3.3. $string['oauthinfo'] = '

To use this plugin, you must register your site with Google, as described in the documentation Google OAuth 2.0 setup.

As part of the registration process, you will need to enter the following URL as \'Authorized Redirect URIs\':

{$a->callbackurl}

Once registered, you will be provided with a client ID and secret which can be used to configure all Google Drive and Picasa plugins.

Please also note that you will have to enable the service \'Drive API\'.

'; diff --git a/repository/googledocs/lib.php b/repository/googledocs/lib.php index bfffc736f92de..14fdef85a9b9c 100644 --- a/repository/googledocs/lib.php +++ b/repository/googledocs/lib.php @@ -299,7 +299,10 @@ protected function query($q, $path = null, $page = 0) { ); } else { // This is a file. - $link = isset($gfile->webContentLink) ? $gfile->webContentLink : ''; + $link = isset($gfile->webViewLink) ? $gfile->webViewLink : ''; + if (empty($link)) { + $link = isset($gfile->webContentLink) ? $gfile->webContentLink : ''; + } if (isset($gfile->fileExtension)) { // The file has an extension, therefore we can download it. $source = json_encode(['id' => $gfile->id, 'exportformat' => 'download', 'link' => $link]); @@ -521,7 +524,6 @@ public function callback() { * @param array $options additional options affecting the file serving */ public function send_file($storedfile, $lifetime=null , $filter=0, $forcedownload=false, array $options = null) { - // TODO. $source = json_decode($storedfile->get_reference()); if ($source->link) { @@ -605,6 +607,23 @@ protected function get_file_capabilities(\repository_googledocs\rest $client, $f return $client->call('get', $params); } + /** + * Get simple file info for humans. + * + * @param \core\oauth2\client $client Authenticated client. + * @param string $fileid The file we are querying. + * + * @return stdClass + */ + protected function get_file_summary(\repository_googledocs\rest $client, $fileid) { + $fields = "id,name,owners"; + $params = [ + 'fileid' => $fileid, + 'fields' => $fields + ]; + return $client->call('get', $params); + } + /** * Update file owner. * @@ -665,7 +684,7 @@ protected function add_writer_to_file($client, $fileid, $email) { 'role' => 'writer', 'type' => 'user' ]; - $params = ['fileid' => $fileid]; + $params = ['fileid' => $fileid, 'sendNotificationEmail' => 'false']; $response = $client->call('create_permission', $params, json_encode($updateeditor)); if (empty($response->id)) { $details = 'Cannot add user ' . $email . ' as a writer for document: ' . $fileid; @@ -783,12 +802,6 @@ public function reference_file_selected($reference, $context) { $readshareupdaterequired = true; $ownerupdaterequired = true; foreach ($permissions->permissions as $permission) { - if ($permission->type == 'user' && - $permission->role == 'owner' && - isset($permission->emailAddress) && - $permission->emailAddress == $systemuseremail) { - $ownerupdaterequired = false; - } if ($permission->id == 'anyoneWithLink' && $permission->type == 'anyone' && $permission->role == 'reader' && @@ -797,12 +810,17 @@ public function reference_file_selected($reference, $context) { } } + // Add Moodle as writer. + $this->add_writer_to_file($userservice, $source->id, $systemuseremail); + // Now move it to a sensible folder. $contextlist = array_reverse($context->get_parent_contexts(true)); $parentid = 'root'; foreach ($contextlist as $context) { // Make sure a folder exists here. + $foldername = $context->get_context_name(); + $folderid = $this->folder_exists_in_folder($systemservice, $foldername, $parentid); if ($folderid !== false) { $parentid = $folderid; @@ -812,38 +830,43 @@ public function reference_file_selected($reference, $context) { } } - // See if we have edit capability before the copy. - $fileinfo = $this->get_file_capabilities($userservice, $source->id); - $canedit = !empty($fileinfo->capabilities->canEdit); - $writerscanshare = !empty($fileinfo->writersCanShare); - - // The owner was not the system user so we have to update the file. - if ($ownerupdaterequired) { + $this->move_file_from_root_to_folder($systemservice, $source->id, $parentid); - $worked = $this->update_file_owner($userservice, $source->id, $systemuseremail); - if (!$worked) { - // Updating the owner only works for "google files" like documents etc. For binary - // files we will get here. - $source = $this->copy_file($systemservice, $source->id); - - $readshareupdaterequired = true; - $writerscanshare = true; + if ($readshareupdaterequired) { + $this->set_file_sharing_anyone_with_link_can_read($systemservice, $source->id); + } - if ($canedit) { - $this->add_writer_to_file($systemservice, $source->id, $useremail); - } - } + // We did not update the reference at all. + return $reference; + } - $this->move_file_from_root_to_folder($systemservice, $source->id, $parentid); + /** + * Get human readable file info from a the reference. + * + * @param string $reference + * @param int $filestatus + */ + public function get_reference_details($reference, $filestatus = 0) { + if (empty($reference)) { + return get_string('unknownsource', 'repository'); } + $source = json_decode($reference); + $systemauth = \core\oauth2\api::get_system_oauth_client($this->issuer); - if ($writerscanshare) { - // We don't want anyone but Moodle to change the sharing settings. - $this->prevent_writers_from_sharing_file($systemservice, $source->id); + if ($systemauth === false) { + return ''; } + $systemservice = new repository_googledocs\rest($systemauth); + $info = $this->get_file_summary($systemservice, $source->id); - if ($readshareupdaterequired) { - $this->set_file_sharing_anyone_with_link_can_read($systemservice, $source->id); + $owner = ''; + if (!empty($info->owners[0]->displayName)) { + $owner = $info->owners[0]->displayName; + } + if ($owner) { + return get_string('owner', 'repository_googledocs', $owner); + } else { + return $info->name; } }