MDL-28557 Calendar filters: show all group events in course to teacher
[moodle.git] / repository / dropbox / lib.php
blob2c1003c5c2c822a37610af5f68778ee44e8a4a6f
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * repository_dropbox class
20 * This plugin is used to access user's dropbox files
22 * @since 2.0
23 * @package repository
24 * @subpackage dropbox
25 * @copyright 2010 Dongsheng Cai
26 * @author Dongsheng Cai <dongsheng@moodle.com>
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 require_once(dirname(__FILE__).'/locallib.php');
32 class repository_dropbox extends repository {
33 private $dropbox;
34 public $files;
35 public $logged=false;
37 /**
38 * Constructor of dropbox plugin
39 * @param int $repositoryid
40 * @param object $context
41 * @param array $options
43 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
44 global $SESSION, $CFG;
45 $options['page'] = optional_param('p', 1, PARAM_INT);
46 parent::__construct($repositoryid, $context, $options);
48 $this->setting = 'dropbox_';
50 $this->dropbox_key = $this->get_option('dropbox_key');
51 $this->dropbox_secret = $this->get_option('dropbox_secret');
53 $this->access_key = get_user_preferences($this->setting.'_access_key', '');
54 $this->access_secret = get_user_preferences($this->setting.'_access_secret', '');
56 if (!empty($this->access_key) && !empty($this->access_secret)) {
57 $this->logged = true;
60 $this->callback = new moodle_url($CFG->wwwroot.'/repository/repository_callback.php', array(
61 'callback'=>'yes',
62 'repo_id'=>$repositoryid
63 ));
65 $args = array(
66 'oauth_consumer_key'=>$this->dropbox_key,
67 'oauth_consumer_secret'=>$this->dropbox_secret,
68 'oauth_callback' => $this->callback->out(false),
69 'api_root' => 'https://www.dropbox.com/1/oauth',
72 $this->dropbox = new dropbox($args);
75 /**
76 * Check if moodle has got access token and secret
77 * @return bool
79 public function check_login() {
80 return !empty($this->logged);
83 /**
84 * Generate dropbox login url
85 * @return array
87 public function print_login() {
88 $result = $this->dropbox->request_token();
89 set_user_preference($this->setting.'_request_secret', $result['oauth_token_secret']);
90 $url = $result['authorize_url'];
91 if ($this->options['ajax']) {
92 $ret = array();
93 $popup_btn = new stdClass();
94 $popup_btn->type = 'popup';
95 $popup_btn->url = $url;
96 $ret['login'] = array($popup_btn);
97 return $ret;
98 } else {
99 echo '<a target="_blank" href="'.$url.'">'.get_string('login', 'repository').'</a>';
104 * Request access token
105 * @return array
107 public function callback() {
108 $token = optional_param('oauth_token', '', PARAM_TEXT);
109 $secret = get_user_preferences($this->setting.'_request_secret', '');
110 $access_token = $this->dropbox->get_access_token($token, $secret);
111 set_user_preference($this->setting.'_access_key', $access_token['oauth_token']);
112 set_user_preference($this->setting.'_access_secret', $access_token['oauth_token_secret']);
116 * Get dropbox files
117 * @param string $path
118 * @param int $page
119 * @return array
121 public function get_listing($path = '', $page = '1') {
122 global $OUTPUT;
123 if (empty($path) || $path=='/') {
124 $path = '/';
125 } else {
126 $path = file_correct_filepath($path);
128 $encoded_path = str_replace("%2F", "/", rawurlencode($path));
130 $list = array();
131 $list['list'] = array();
132 $list['manage'] = false;
133 $list['dynload'] = true;
134 $list['nosearch'] = true;
135 // process breadcrumb trail
136 $list['path'] = array(
137 array('name'=>get_string('dropbox', 'repository_dropbox'), 'path'=>'/')
140 $result = $this->dropbox->get_listing($encoded_path, $this->access_key, $this->access_secret);
142 if (!is_object($result) || empty($result)) {
143 return $list;
145 if (empty($result->path)) {
146 $current_path = '/';
147 } else {
148 $current_path = file_correct_filepath($result->path);
151 $trail = '';
152 if (!empty($path)) {
153 $parts = explode('/', $path);
154 if (count($parts) > 1) {
155 foreach ($parts as $part) {
156 if (!empty($part)) {
157 $trail .= ('/'.$part);
158 $list['path'][] = array('name'=>$part, 'path'=>$trail);
161 } else {
162 $list['path'][] = array('name'=>$path, 'path'=>$path);
166 if (!empty($result->error)) {
167 // reset access key
168 set_user_preference($this->setting.'_access_key', '');
169 set_user_preference($this->setting.'_access_secret', '');
170 throw new repository_exception('repositoryerror', 'repository', '', $result->error);
172 if (empty($result->contents) or !is_array($result->contents)) {
173 return $list;
175 $files = $result->contents;
176 foreach ($files as $file) {
177 if ($file->is_dir) {
178 $list['list'][] = array(
179 'title' => substr($file->path, strpos($file->path, $current_path)+strlen($current_path)),
180 'path' => file_correct_filepath($file->path),
181 'size' => $file->size,
182 'date' => $file->modified,
183 'thumbnail' => $OUTPUT->pix_url('f/folder-32')->out(false),
184 'children' => array(),
186 } else {
187 $list['list'][] = array(
188 'title' => substr($file->path, strpos($file->path, $current_path)+strlen($current_path)),
189 'source' => $file->path,
190 'size' => $file->size,
191 'date' => $file->modified,
192 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file->path, 32))->out(false)
196 return $list;
199 * Logout from dropbox
200 * @return array
202 public function logout() {
203 set_user_preference($this->setting.'_access_key', '');
204 set_user_preference($this->setting.'_access_secret', '');
205 $this->access_key = '';
206 $this->access_secret = '';
207 return $this->print_login();
211 * Set dropbox option
212 * @param array $options
213 * @return mixed
215 public function set_option($options = array()) {
216 if (!empty($options['dropbox_key'])) {
217 set_config('dropbox_key', trim($options['dropbox_key']), 'dropbox');
219 if (!empty($options['dropbox_secret'])) {
220 set_config('dropbox_secret', trim($options['dropbox_secret']), 'dropbox');
222 unset($options['dropbox_key']);
223 unset($options['dropbox_secret']);
224 $ret = parent::set_option($options);
225 return $ret;
229 * Get dropbox options
230 * @param string $config
231 * @return mixed
233 public function get_option($config = '') {
234 if ($config==='dropbox_key') {
235 return trim(get_config('dropbox', 'dropbox_key'));
236 } elseif ($config==='dropbox_secret') {
237 return trim(get_config('dropbox', 'dropbox_secret'));
238 } else {
239 $options['dropbox_key'] = trim(get_config('dropbox', 'dropbox_key'));
240 $options['dropbox_secret'] = trim(get_config('dropbox', 'dropbox_secret'));
242 $options = parent::get_option($config);
243 return $options;
248 * @param string $photo_id
249 * @param string $file
250 * @return string
252 public function get_file($filepath, $saveas = '') {
253 $this->dropbox->set_access_token($this->access_key, $this->access_secret);
254 $saveas = $this->prepare_file($saveas);
255 return $this->dropbox->get_file($filepath, $saveas);
258 * Add Plugin settings input to Moodle form
259 * @param object $mform
261 public static function type_config_form($mform, $classname = 'repository') {
262 global $CFG;
263 parent::type_config_form($mform);
264 $key = get_config('dropbox', 'dropbox_key');
265 $secret = get_config('dropbox', 'dropbox_secret');
267 if (empty($key)) {
268 $key = '';
270 if (empty($secret)) {
271 $secret = '';
274 $strrequired = get_string('required');
276 $mform->addElement('text', 'dropbox_key', get_string('apikey', 'repository_dropbox'), array('value'=>$key,'size' => '40'));
277 $mform->addElement('text', 'dropbox_secret', get_string('secret', 'repository_dropbox'), array('value'=>$secret,'size' => '40'));
279 $mform->addRule('dropbox_key', $strrequired, 'required', null, 'client');
280 $mform->addRule('dropbox_secret', $strrequired, 'required', null, 'client');
281 $str_getkey = get_string('instruction', 'repository_dropbox');
282 $mform->addElement('static', null, '', $str_getkey);
286 * Option names of dropbox plugin
287 * @return array
289 public static function get_type_option_names() {
290 return array('dropbox_key', 'dropbox_secret', 'pluginname');
294 * Dropbox plugin supports all kinds of files
295 * @return array
297 public function supported_filetypes() {
298 return '*';
302 * User cannot use the external link to dropbox
303 * @return int
305 public function supported_returntypes() {
306 return FILE_INTERNAL;