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['list'] = $this->skydrive->get_file_list($path);
103 * Downloads a repository file and saves to a path.
105 * @param string $id identifier of file
106 * @param string $filename to save file as
107 * @return array with keys:
108 * path: internal location of the file
109 * url: URL to the source
111 public function get_file($id, $filename = '') {
112 $path = $this->prepare_file($filename);
113 return $this->skydrive->download_file($id, $path);
117 * Return names of the options to display in the repository form
119 * @return array of option names
121 public static function get_type_option_names() {
122 return array('clientid', 'secret', 'pluginname');
126 * Setup repistory form.
128 * @param moodleform $mform Moodle form (passed by reference)
129 * @param string $classname repository class name
131 public static function type_config_form($mform, $classname = 'repository') {
133 $a->callbackurl = microsoft_skydrive::callback_url()->out(false);
134 $mform->addElement('static', null, '', get_string('oauthinfo', 'repository_skydrive', $a));
136 parent::type_config_form($mform);
137 $strrequired = get_string('required');
138 $mform->addElement('text', 'clientid', get_string('clientid', 'repository_skydrive'));
139 $mform->addElement('text', 'secret', get_string('secret', 'repository_skydrive'));
140 $mform->addRule('clientid', $strrequired, 'required', null, 'client');
141 $mform->addRule('secret', $strrequired, 'required', null, 'client');
142 $mform->setType('clientid', PARAM_RAW_TRIMMED);
143 $mform->setType('secret', PARAM_RAW_TRIMMED);
147 * Logout from repository instance and return
150 * @return page to display
152 public function logout() {
153 $this->skydrive->log_out();
154 return $this->print_login();
158 * This repository doesn't support global search.
160 * @return bool if supports global search
162 public function global_search() {
167 * This repoistory supports any filetype.
169 * @return string '*' means this repository support any files
171 public function supported_filetypes() {
176 * This repostiory only supports internal files
178 * @return int return type bitmask supported
180 public function supported_returntypes() {
181 return FILE_INTERNAL;