mirror of
https://github.com/moodle/moodle.git
synced 2025-01-17 21:49:15 +01:00
MDL-58220 repository: More docs / comments
More docs / comments for the reference_file_selected function used by controlled links.
This commit is contained in:
parent
9165e83831
commit
eb47ad4a6e
@ -840,6 +840,9 @@ class repository_googledocs extends repository {
|
||||
* Called when a file is selected as a "link".
|
||||
* Invoked at MOODLE/repository/repository_ajax.php
|
||||
*
|
||||
* This is called at the point the reference files are being copied from the draft area to the real area
|
||||
* (when the file has really really been selected.
|
||||
*
|
||||
* @param string $reference this reference is generated by
|
||||
* repository::get_file_reference()
|
||||
* @param context $context the target context for this new file.
|
||||
@ -849,32 +852,36 @@ class repository_googledocs extends repository {
|
||||
* @return string updated reference (final one before it's saved to db).
|
||||
*/
|
||||
public function reference_file_selected($reference, $context, $component, $filearea, $itemid) {
|
||||
if (!$this->issuer->get('enabled')) {
|
||||
throw new repository_exception('cannotdownload', 'repository');
|
||||
}
|
||||
// What we need to do here is transfer ownership to the system user (or copy)
|
||||
// then set the permissions so anyone with the share link can view,
|
||||
// finally update the reference to contain the share link if it was not
|
||||
// already there (and point to new file id if we copied).
|
||||
|
||||
|
||||
// Check this issuer is enabled.
|
||||
if (!$this->issuer->get('enabled')) {
|
||||
throw new repository_exception('cannotdownload', 'repository');
|
||||
}
|
||||
|
||||
// Get a system oauth client and a user oauth client.
|
||||
$systemauth = \core\oauth2\api::get_system_oauth_client($this->issuer);
|
||||
|
||||
if ($systemauth === false) {
|
||||
$details = 'Cannot connect as system user';
|
||||
throw new repository_exception('errorwhilecommunicatingwith', 'repository', '', $details);
|
||||
}
|
||||
// Get the system user email so we can share the file with this user.
|
||||
$systemuserinfo = $systemauth->get_userinfo();
|
||||
$systemuseremail = $systemuserinfo['email'];
|
||||
|
||||
$source = json_decode($reference);
|
||||
|
||||
$userauth = $this->get_user_oauth_client();
|
||||
if ($userauth === false) {
|
||||
$details = 'Cannot connect as current user';
|
||||
throw new repository_exception('errorwhilecommunicatingwith', 'repository', '', $details);
|
||||
}
|
||||
$userinfo = $userauth->get_userinfo();
|
||||
$useremail = $userinfo['email'];
|
||||
|
||||
// Get the details from the reference.
|
||||
$source = json_decode($reference);
|
||||
$userservice = new repository_googledocs\rest($userauth);
|
||||
$systemservice = new repository_googledocs\rest($systemauth);
|
||||
|
||||
@ -898,6 +905,8 @@ class repository_googledocs extends repository {
|
||||
$allfolders[] = clean_param($filearea, PARAM_PATH);
|
||||
$allfolders[] = clean_param($itemid, PARAM_PATH);
|
||||
|
||||
// Variable $allfolders is the full path we want to put the file in - so walk it and create each folder.
|
||||
|
||||
foreach ($allfolders as $foldername) {
|
||||
// Make sure a folder exists here.
|
||||
$fullpath .= '/' . $foldername;
|
||||
@ -925,6 +934,7 @@ class repository_googledocs extends repository {
|
||||
$this->set_file_sharing_anyone_with_link_can_read($systemservice, $newsource->id);
|
||||
$this->prevent_writers_from_sharing_file($systemservice, $newsource->id);
|
||||
|
||||
// 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 : '';
|
||||
if (empty($source->link)) {
|
||||
|
@ -1285,7 +1285,13 @@ abstract class repository implements cacheable_object {
|
||||
|
||||
/**
|
||||
* reference_file_selected
|
||||
* Invoked at MOODLE/repository/repository_ajax.php
|
||||
*
|
||||
* This function is called when a controlled link file is selected in a file picker and the form is
|
||||
* saved. The expected behaviour for repositories supporting controlled links is to
|
||||
* - copy the file to the moodle system account
|
||||
* - put it in a folder that reflects the context it is being used
|
||||
* - make sure the sharing permissions are correct (read-only with the link)
|
||||
* - return a new reference string pointing to the newly copied file.
|
||||
*
|
||||
* @param string $reference this reference is generated by
|
||||
* repository::get_file_reference()
|
||||
|
@ -811,6 +811,11 @@ class repository_skydrive extends repository {
|
||||
* Called when a file is selected as a "link".
|
||||
* Invoked at MOODLE/repository/repository_ajax.php
|
||||
*
|
||||
* What should happen here is that the file should be copied to a new file owned by the moodle system user.
|
||||
* It should be organised in a folder based on the file context.
|
||||
* It's sharing permissions should allow read access with the link.
|
||||
* The returned reference should point to the newly copied file - not the original.
|
||||
*
|
||||
* @param string $reference this reference is generated by
|
||||
* repository::get_file_reference()
|
||||
* @param context $context the target context for this new file.
|
||||
@ -824,6 +829,8 @@ class repository_skydrive extends repository {
|
||||
// then set the permissions so anyone with the share link can view,
|
||||
// finally update the reference to contain the share link if it was not
|
||||
// already there (and point to new file id if we copied).
|
||||
|
||||
// Get a system and a user oauth client.
|
||||
$systemauth = \core\oauth2\api::get_system_oauth_client($this->issuer);
|
||||
|
||||
if ($systemauth === false) {
|
||||
@ -883,6 +890,8 @@ class repository_skydrive extends repository {
|
||||
$allfolders[] = urlencode(clean_param($filearea, PARAM_PATH));
|
||||
$allfolders[] = urlencode(clean_param($itemid, PARAM_PATH));
|
||||
|
||||
// Variable $allfolders now has the complete path we want to store the file in.
|
||||
// Create each folder in $allfolders under the system account.
|
||||
foreach ($allfolders as $foldername) {
|
||||
if ($fullpath) {
|
||||
$fullpath .= '/';
|
||||
@ -918,7 +927,9 @@ class repository_skydrive extends repository {
|
||||
$path = $fullpath . '/' . $source->name;
|
||||
$this->delete_file_by_path($systemservice, $path);
|
||||
|
||||
// Copy the file so we have a backup.
|
||||
// Copy the file to the moodle account.
|
||||
// Note this method (copying via a share link) is the only way to copy a file in
|
||||
// office 365 from one user to another.
|
||||
$this->copy_share($systemservice, $sharetoken, $newdrive, $parentid);
|
||||
|
||||
$summary = $this->get_file_summary_by_path($systemservice, $path);
|
||||
|
Loading…
x
Reference in New Issue
Block a user