2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Microsoft Live Skydrive Repository Plugin
20 * @package repository_skydrive
21 * @copyright 2012 Lancaster University Network Services Ltd
22 * @author Dan Poltawski <dan.poltawski@luns.net.uk>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
28 require_once('microsoftliveapi.php');
31 * Microsoft skydrive repository plugin.
33 * @package repository_skydrive
34 * @copyright 2012 Lancaster University Network Services Ltd
35 * @author Dan Poltawski <dan.poltawski@luns.net.uk>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class repository_skydrive
extends repository
{
39 /** @var microsoft_skydrive skydrive oauth2 api helper object */
40 private $skydrive = null;
45 * @param int $repositoryid repository instance id.
46 * @param int|stdClass $context a context id or context object.
47 * @param array $options repository options.
49 public function __construct($repositoryid, $context = SYSCONTEXTID
, $options = array()) {
50 parent
::__construct($repositoryid, $context, $options);
52 $clientid = get_config('skydrive', 'clientid');
53 $secret = get_config('skydrive', 'secret');
54 $returnurl = new moodle_url('/repository/repository_callback.php');
55 $returnurl->param('callback', 'yes');
56 $returnurl->param('repo_id', $this->id
);
57 $returnurl->param('sesskey', sesskey());
59 $this->skydrive
= new microsoft_skydrive($clientid, $secret, $returnurl);
64 * Checks whether the user is logged in or not.
66 * @return bool true when logged in
68 public function check_login() {
69 return $this->skydrive
->is_logged_in();
73 * Print the login form, if required
75 * @return array of login options
77 public function print_login() {
78 $popup = new stdClass();
79 $popup->type
= 'popup';
80 $url = $this->skydrive
->get_login_url();
81 $popup->url
= $url->out(false);
82 return array('login' => array($popup));
86 * Given a path, and perhaps a search, get a list of files.
88 * See details on {@link http://docs.moodle.org/dev/Repository_plugins}
90 * @param string $path identifier for current path
91 * @param string $page the page number of file list
92 * @return array list of files including meta information as specified by parent.
94 public function get_listing($path='', $page = '') {
96 $ret['dynload'] = true;
97 $ret['nosearch'] = true;
98 $ret['manage'] = 'https://skydrive.live.com/';
100 $fileslist = $this->skydrive
->get_file_list($path);
101 // Filter list for accepted types. Hopefully this will be done by core some day.
102 $fileslist = array_filter($fileslist, array($this, 'filter'));
103 $ret['list'] = $fileslist;
105 // Generate path bar, always start with the plugin name.
106 $ret['path'] = array();
107 $ret['path'][] = array('name'=> $this->name
, 'path'=>'');
109 // Now add each level folder.
112 $parts = explode('/', $path);
113 foreach ($parts as $folderid) {
114 if (!empty($folderid)) {
115 $trail .= ('/'.$folderid);
116 $ret['path'][] = array('name' => $this->skydrive
->get_folder_name($folderid),
126 * Downloads a repository file and saves to a path.
128 * @param string $id identifier of file
129 * @param string $filename to save file as
130 * @return array with keys:
131 * path: internal location of the file
132 * url: URL to the source
134 public function get_file($id, $filename = '') {
135 $path = $this->prepare_file($filename);
136 return $this->skydrive
->download_file($id, $path);
140 * Return names of the options to display in the repository form
142 * @return array of option names
144 public static function get_type_option_names() {
145 return array('clientid', 'secret', 'pluginname');
149 * Setup repistory form.
151 * @param moodleform $mform Moodle form (passed by reference)
152 * @param string $classname repository class name
154 public static function type_config_form($mform, $classname = 'repository') {
156 $a->callbackurl
= microsoft_skydrive
::callback_url()->out(false);
157 $mform->addElement('static', null, '', get_string('oauthinfo', 'repository_skydrive', $a));
159 parent
::type_config_form($mform);
160 $strrequired = get_string('required');
161 $mform->addElement('text', 'clientid', get_string('clientid', 'repository_skydrive'));
162 $mform->addElement('text', 'secret', get_string('secret', 'repository_skydrive'));
163 $mform->addRule('clientid', $strrequired, 'required', null, 'client');
164 $mform->addRule('secret', $strrequired, 'required', null, 'client');
165 $mform->setType('clientid', PARAM_RAW_TRIMMED
);
166 $mform->setType('secret', PARAM_RAW_TRIMMED
);
170 * Logout from repository instance and return
173 * @return page to display
175 public function logout() {
176 $this->skydrive
->log_out();
177 return $this->print_login();
181 * This repository doesn't support global search.
183 * @return bool if supports global search
185 public function global_search() {
190 * This repoistory supports any filetype.
192 * @return string '*' means this repository support any files
194 public function supported_filetypes() {
199 * This repostiory only supports internal files
201 * @return int return type bitmask supported
203 public function supported_returntypes() {
204 return FILE_INTERNAL
;