diff --git a/repository/googledocs/lib.php b/repository/googledocs/lib.php index 1ce0e1bdc15..09d9518e223 100644 --- a/repository/googledocs/lib.php +++ b/repository/googledocs/lib.php @@ -432,11 +432,17 @@ class repository_googledocs extends repository { throw new repository_exception('cannotdownload', 'repository'); } - $client = $this->get_user_oauth_client(); - $base = 'https://www.googleapis.com/drive/v3'; - $source = json_decode($reference); + $client = null; + if (!empty($source->usesystem)) { + $client = \core\oauth2\api::get_system_oauth_client($this->issuer); + } else { + $client = $this->get_user_oauth_client(); + } + + $base = 'https://www.googleapis.com/drive/v3'; + $newfilename = false; if ($source->exportformat == 'download') { $params = ['alt' => 'media']; @@ -588,7 +594,7 @@ class repository_googledocs extends repository { $storedfile->get_filepath(), $storedfile->get_filename()); - if (empty($options['offline']) && !empty($info) && $info->is_writable()) { + if (empty($options['offline']) && !empty($info) && $info->is_writable() && !empty($source->usesystem)) { // Add the current user as an OAuth writer. $systemauth = \core\oauth2\api::get_system_oauth_client($this->issuer); @@ -945,6 +951,7 @@ class repository_googledocs extends repository { // Update the returned reference so that the stored_file in moodle points to the newly copied file. $source->id = $newsource->id; $source->link = isset($newsource->webViewLink) ? $newsource->webViewLink : ''; + $source->usesystem = true; if (empty($source->link)) { $source->link = isset($newsource->webContentLink) ? $newsource->webContentLink : ''; } @@ -967,6 +974,9 @@ class repository_googledocs extends repository { return get_string('unknownsource', 'repository'); } $source = json_decode($reference); + if (empty($source->usesystem)) { + return ''; + } $systemauth = \core\oauth2\api::get_system_oauth_client($this->issuer); if ($systemauth === false) {