MDL-30740 skydrive: Add filepicker navigation bar
[moodle.git] / repository / skydrive / lib.php
CommitLineData
74462841
DP
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.
13//
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/>.
16
17/**
18 * Microsoft Live Skydrive Repository Plugin
19 *
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
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28require_once('microsoftliveapi.php');
29
30/**
31 * Microsoft skydrive repository plugin.
32 *
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
37 */
38class repository_skydrive extends repository {
39 /** @var microsoft_skydrive skydrive oauth2 api helper object */
40 private $skydrive = null;
41
42 /**
43 * Constructor
44 *
45 * @param int $repositoryid repository instance id.
46 * @param int|stdClass $context a context id or context object.
47 * @param array $options repository options.
48 */
49 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
50 parent::__construct($repositoryid, $context, $options);
51
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());
58
59 $this->skydrive = new microsoft_skydrive($clientid, $secret, $returnurl);
60 $this->check_login();
61 }
62
63 /**
64 * Checks whether the user is logged in or not.
65 *
66 * @return bool true when logged in
67 */
68 public function check_login() {
69 return $this->skydrive->is_logged_in();
70 }
71
72 /**
73 * Print the login form, if required
74 *
75 * @return array of login options
76 */
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));
83 }
84
85 /**
86 * Given a path, and perhaps a search, get a list of files.
87 *
88 * See details on {@link http://docs.moodle.org/dev/Repository_plugins}
89 *
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.
93 */
94 public function get_listing($path='', $page = '') {
95 $ret = array();
96 $ret['dynload'] = true;
97 $ret['nosearch'] = true;
98 $ret['list'] = $this->skydrive->get_file_list($path);
d4fd856b
DP
99
100 // Generate path bar, always start with the plugin name.
101 $ret['path'] = array();
102 $ret['path'][] = array('name'=> $this->name, 'path'=>'');
103
104 // Now add each level folder.
105 $trail = '';
106 if (!empty($path)) {
107 $parts = explode('/', $path);
108 foreach ($parts as $folderid) {
109 if (!empty($folderid)) {
110 $trail .= ('/'.$folderid);
111 $ret['path'][] = array('name' => $this->skydrive->get_folder_name($folderid),
112 'path' => $trail);
113 }
114 }
115 }
116
74462841
DP
117 return $ret;
118 }
119
120 /**
121 * Downloads a repository file and saves to a path.
122 *
123 * @param string $id identifier of file
124 * @param string $filename to save file as
125 * @return array with keys:
126 * path: internal location of the file
127 * url: URL to the source
128 */
129 public function get_file($id, $filename = '') {
130 $path = $this->prepare_file($filename);
131 return $this->skydrive->download_file($id, $path);
132 }
133
134 /**
135 * Return names of the options to display in the repository form
136 *
137 * @return array of option names
138 */
139 public static function get_type_option_names() {
140 return array('clientid', 'secret', 'pluginname');
141 }
142
143 /**
144 * Setup repistory form.
145 *
146 * @param moodleform $mform Moodle form (passed by reference)
147 * @param string $classname repository class name
148 */
149 public static function type_config_form($mform, $classname = 'repository') {
150 $a = new stdClass;
151 $a->callbackurl = microsoft_skydrive::callback_url()->out(false);
152 $mform->addElement('static', null, '', get_string('oauthinfo', 'repository_skydrive', $a));
153
154 parent::type_config_form($mform);
155 $strrequired = get_string('required');
156 $mform->addElement('text', 'clientid', get_string('clientid', 'repository_skydrive'));
157 $mform->addElement('text', 'secret', get_string('secret', 'repository_skydrive'));
158 $mform->addRule('clientid', $strrequired, 'required', null, 'client');
159 $mform->addRule('secret', $strrequired, 'required', null, 'client');
160 $mform->setType('clientid', PARAM_RAW_TRIMMED);
161 $mform->setType('secret', PARAM_RAW_TRIMMED);
162 }
163
164 /**
165 * Logout from repository instance and return
166 * login form.
167 *
168 * @return page to display
169 */
170 public function logout() {
171 $this->skydrive->log_out();
172 return $this->print_login();
173 }
174
175 /**
176 * This repository doesn't support global search.
177 *
178 * @return bool if supports global search
179 */
180 public function global_search() {
181 return false;
182 }
183
184 /**
185 * This repoistory supports any filetype.
186 *
187 * @return string '*' means this repository support any files
188 */
189 public function supported_filetypes() {
190 return '*';
191 }
192
193 /**
194 * This repostiory only supports internal files
195 *
196 * @return int return type bitmask supported
197 */
198 public function supported_returntypes() {
199 return FILE_INTERNAL;
200 }
201}