MDL-65208 admin: Add upgrade cli --is-pending option
[moodle.git] / repository / skydrive / lib.php
blob0343f97eb5606e9c77e1174388bbdad3bfe7ff8a
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
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');
30 /**
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;
42 /**
43 * Constructor
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);
60 $this->check_login();
63 /**
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();
72 /**
73 * Print the login form, if required
75 * @return array of login options
77 public function print_login() {
78 $url = $this->skydrive->get_login_url();
80 if ($this->options['ajax']) {
81 $popup = new stdClass();
82 $popup->type = 'popup';
83 $popup->url = $url->out(false);
84 return array('login' => array($popup));
85 } else {
86 echo '<a target="_blank" href="'.$url->out(false).'">'.get_string('login', 'repository').'</a>';
90 /**
91 * Given a path, and perhaps a search, get a list of files.
93 * See details on {@link http://docs.moodle.org/dev/Repository_plugins}
95 * @param string $path identifier for current path
96 * @param string $page the page number of file list
97 * @return array list of files including meta information as specified by parent.
99 public function get_listing($path='', $page = '') {
100 $ret = array();
101 $ret['dynload'] = true;
102 $ret['nosearch'] = true;
103 $ret['manage'] = 'https://skydrive.live.com/';
105 $fileslist = $this->skydrive->get_file_list($path);
106 // Filter list for accepted types. Hopefully this will be done by core some day.
107 $fileslist = array_filter($fileslist, array($this, 'filter'));
108 $ret['list'] = $fileslist;
110 // Generate path bar, always start with the plugin name.
111 $ret['path'] = array();
112 $ret['path'][] = array('name'=> $this->name, 'path'=>'');
114 // Now add each level folder.
115 $trail = '';
116 if (!empty($path)) {
117 $parts = explode('/', $path);
118 foreach ($parts as $folderid) {
119 if (!empty($folderid)) {
120 $trail .= ('/'.$folderid);
121 $ret['path'][] = array('name' => $this->skydrive->get_folder_name($folderid),
122 'path' => $trail);
127 return $ret;
131 * Downloads a repository file and saves to a path.
133 * @param string $id identifier of file
134 * @param string $filename to save file as
135 * @return array with keys:
136 * path: internal location of the file
137 * url: URL to the source
139 public function get_file($id, $filename = '') {
140 $path = $this->prepare_file($filename);
141 return $this->skydrive->download_file($id, $path);
145 * Return names of the options to display in the repository form
147 * @return array of option names
149 public static function get_type_option_names() {
150 return array('clientid', 'secret', 'pluginname');
154 * Setup repistory form.
156 * @param moodleform $mform Moodle form (passed by reference)
157 * @param string $classname repository class name
159 public static function type_config_form($mform, $classname = 'repository') {
160 global $OUTPUT;
162 $a = new stdClass;
163 $a->callbackurl = microsoft_skydrive::callback_url()->out(false);
164 $mform->addElement('static', null, '', get_string('oauthinfo', 'repository_skydrive', $a));
166 $mform->addElement('static', null, '', $OUTPUT->notification(get_string('deprecatedwarning', 'repository_skydrive', $a)));
168 parent::type_config_form($mform);
169 $strrequired = get_string('required');
170 $mform->addElement('text', 'clientid', get_string('clientid', 'repository_skydrive'));
171 $mform->addElement('text', 'secret', get_string('secret', 'repository_skydrive'));
172 $mform->addRule('clientid', $strrequired, 'required', null, 'client');
173 $mform->addRule('secret', $strrequired, 'required', null, 'client');
174 $mform->setType('clientid', PARAM_RAW_TRIMMED);
175 $mform->setType('secret', PARAM_RAW_TRIMMED);
179 * Logout from repository instance and return
180 * login form.
182 * @return page to display
184 public function logout() {
185 $this->skydrive->log_out();
186 return $this->print_login();
190 * This repository doesn't support global search.
192 * @return bool if supports global search
194 public function global_search() {
195 return false;
199 * This repoistory supports any filetype.
201 * @return string '*' means this repository support any files
203 public function supported_filetypes() {
204 return '*';
208 * This repostiory only supports internal files
210 * @return int return type bitmask supported
212 public function supported_returntypes() {
213 return FILE_INTERNAL;