Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]

Nextcloud repository plugin library.

Copyright: 2017 Project seminar (Learnweb, University of Münster)
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or
File Size: 995 lines (40 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class


Class: repository_nextcloud  - X-Ref

Nextcloud repository class.

__construct($repositoryid, $context = SYSCONTEXTID, $options = array()   X-Ref
repository_nextcloud constructor.

param: int $repositoryid
param: bool|int|stdClass $context
param: array $options

get_system_oauth_client()   X-Ref
Get or initialise an oauth client for the system account.

return: false|oauth2_client False if initialisation was unsuccessful, otherwise an initialised client.

get_system_ocs_client()   X-Ref
Get or initialise an ocs client for the system account.

return: null|ocs_client Null if initialisation was unsuccessful, otherwise an initialised client.

initiate_webdavclient()   X-Ref
Initiates the webdav client.


get_file($reference, $title = '')   X-Ref
This function does exactly the same as in the WebDAV repository. The only difference is, that
the nextcloud OAuth2 client uses OAuth2 instead of Basic Authentication.

param: string $reference relative path to the file.
param: string $title title of the file.
return: array|bool returns either the moodle path to the file or false.

get_listing($path='', $page = '')   X-Ref
This function does exactly the same as in the WebDAV repository. The only difference is, that
the nextcloud OAuth2 client uses OAuth2 instead of Basic Authentication.

param: string $path relative path to the directory or file.
param: string $page page number (given multiple pages of elements).
return: array directory properties.

get_link($url)   X-Ref
Use OCS to generate a public share to the requested file.
This method derives a download link from the public share URL.

param: string $url relative path to the chosen file
return: string the generated download link.

get_file_reference($source)   X-Ref
This method does not do any translation of the file source.

param: string $source source of the file, returned by repository as 'source' and received back from user (not cleaned)
return: string file reference, ready to be stored or json encoded string for public link reference

reference_file_selected($reference, $context, $component, $filearea, $itemid)   X-Ref
Called when a file is selected as a "access control 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.
What is done here is transfer ownership to the system user (by copying) then delete the intermediate share
used for that. Finally update the reference to point to new file name.

param: string $reference this reference is generated by repository::get_file_reference()
param: context $context the target context for this new file.
param: string $component the target component for this new file.
param: string $filearea the target filearea for this new file.
param: string $itemid the target itemid for this new file.
return: string updated reference (final one before it's saved to db).

send_file($storedfile, $lifetime=null , $filter=0, $forcedownload=false, array $options = null)   X-Ref
Repository method that serves the referenced file (created e.g. via get_link).
All parameters are there for compatibility with superclass, but they are ignored.

param: stored_file $storedfile
param: int $lifetime (ignored)
param: int $filter (ignored)
param: bool $forcedownload (ignored)
param: array $options additional options affecting the file serving

default_returntype()   X-Ref
Which return type should be selected by default.

return: int

get_type_option_names()   X-Ref
Return names of the general options.
By default: no general option name.

return: array

check_login()   X-Ref
Function which checks whether the user is logged in on the Nextcloud instance.

return: bool false, if no Access Token is set or can be requested.

get_user_oauth_client($overrideurl = false)   X-Ref
Get a cached user authenticated oauth client.

param: bool|moodle_url $overrideurl Use this url instead of the repo callback.
return: \core\oauth2\client

print_login()   X-Ref
Prints a simple Login Button which redirects to an authorization window from Nextcloud.

return: mixed login window properties.

logout()   X-Ref
Deletes the held Access Token and prints the Login window.

return: array login window properties.

callback()   X-Ref
Sets up access token after the redirection from Nextcloud.


create($type, $userid, $context, $params, $readonly=0)   X-Ref
Create an instance for this plug-in

param: string $type the type of the repository
param: int $userid the user id
param: stdClass $context the context
param: array $params the options for this instance
param: int $readonly whether to create it readonly or not (defaults to not)
return: mixed

instance_config_form($mform)   X-Ref
This method adds a select form and additional information to the settings form..

param: \moodleform $mform Moodle form (passed by reference)
return: bool|void

set_option($options = array()   X-Ref
Save settings for repository instance

param: array $options settings
return: bool

get_instance_option_names()   X-Ref
Names of the plugin settings

return: array

supported_returntypes()   X-Ref
Method to define which file-types are supported (hardcoded can not be changed in Admin Menu)

By default FILE_INTERNAL is supported. In case a system account is connected and an issuer exist,
FILE_CONTROLLED_LINK is supported.

FILE_INTERNAL - the file is uploaded/downloaded and stored directly within the Moodle file system.
FILE_CONTROLLED_LINK - creates a copy of the file in Nextcloud from which private shares to permitted users will be
created. The file itself can not be changed any longer by the owner.

return: int return type bitmask supported

get_listing_convert_response($dirpath, $ls)   X-Ref
Take the WebDAV `ls()' output and convert it into a format that Moodle's filepicker understands.

param: string $dirpath Relative (urlencoded) path of the folder of interest.
param: array $ls Output by WebDAV
return: array Moodle-formatted list of directory contents; ready for use as $ret['list'] in get_listings

print_login_popup($attr = null, $embed = false)   X-Ref
Print the login in a popup.

param: array|null $attr Custom attributes to be applied to popup div.

get_listing_prepare_response($path)   X-Ref
Prepare response of get_listing; namely
- defining setting elements,
- filling in the parent path of the currently-viewed directory.

param: string $path Relative path
return: array ret array for use as get_listing's $ret

get_reference_details($reference, $filestatus = 0)   X-Ref
When a controlled link is clicked in the file picker get the human readable info about this file.

param: string $reference
param: int $filestatus
return: string

sync_reference(stored_file $file)   X-Ref
Synchronize the external file if there is an update happened to it.

If the file has been updated in the nextcloud instance, this method
would take care of the file we copy into the moodle file pool.

The call to this method reaches from stored_file::sync_external_file()

param: stored_file $file
return: bool true if synced successfully else false if not ready to sync or reference link not set