MDL-38852 repository_flickr: fix missing setType calls
[moodle.git] / repository / flickr / lib.php
CommitLineData
36ea9a6c 1<?php
10d53fd3
DC
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
36ea9a6c 17/**
67233725 18 * This plugin is used to access flickr pictures
36ea9a6c 19 *
10d53fd3 20 * @since 2.0
67233725
DC
21 * @package repository_flickr
22 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
d078f6d3 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36ea9a6c 24 */
67233725 25require_once($CFG->dirroot . '/repository/lib.php');
539b3d13 26require_once($CFG->libdir.'/flickrlib.php');
6a391ebf 27
96297ca2 28/**
67233725 29 * This plugin is used to access user's private flickr repository
96297ca2 30 *
67233725
DC
31 * @since 2.0
32 * @package repository_flickr
33 * @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
96297ca2 35 */
dc0976b8 36class repository_flickr extends repository {
6a391ebf 37 private $flickr;
38 public $photos;
19add4c0 39
b2245b7e
FM
40 /**
41 * Stores sizes of images to prevent multiple API call
42 */
43 static private $sizes = array();
44
96297ca2 45 /**
46 *
6f2cd52a
DC
47 * @param int $repositoryid
48 * @param object $context
49 * @param array $options
96297ca2 50 */
447c7a19 51 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
d68c527f 52 global $SESSION, $CFG;
53 $options['page'] = optional_param('p', 1, PARAM_INT);
54 parent::__construct($repositoryid, $context, $options);
55
56 $this->setting = 'flickr_';
57
58 $this->api_key = $this->get_option('api_key');
59 $this->secret = $this->get_option('secret');
60
61 $this->token = get_user_preferences($this->setting, '');
62 $this->nsid = get_user_preferences($this->setting.'_nsid', '');
63
64 $this->flickr = new phpFlickr($this->api_key, $this->secret, $this->token);
65
353d5cf3 66 $frob = optional_param('frob', '', PARAM_RAW);
67 if (empty($this->token) && !empty($frob)) {
68 $auth_info = $this->flickr->auth_getToken($frob);
69 $this->token = $auth_info['token'];
70 $this->nsid = $auth_info['user']['nsid'];
71 set_user_preference($this->setting, $auth_info['token']);
72 set_user_preference($this->setting.'_nsid', $auth_info['user']['nsid']);
d68c527f 73 }
74
75 }
96297ca2 76
77 /**
78 *
6f2cd52a 79 * @return bool
96297ca2 80 */
dc0976b8 81 public function check_login() {
d68c527f 82 return !empty($this->token);
83 }
96297ca2 84
85 /**
86 *
6f2cd52a 87 * @return mixed
96297ca2 88 */
dc0976b8 89 public function logout() {
d68c527f 90 set_user_preference($this->setting, '');
91 set_user_preference($this->setting.'_nsid', '');
92 $this->token = '';
93 $this->nsid = '';
94 return $this->print_login();
95 }
96297ca2 96
97 /**
98 *
6f2cd52a
DC
99 * @param array $options
100 * @return mixed
96297ca2 101 */
dc0976b8 102 public function set_option($options = array()) {
27051e43 103 if (!empty($options['api_key'])) {
70d91d90 104 set_config('api_key', trim($options['api_key']), 'flickr');
27051e43 105 }
7fab56b7 106 if (!empty($options['secret'])) {
107 set_config('secret', trim($options['secret']), 'flickr');
108 }
27051e43 109 unset($options['api_key']);
7fab56b7 110 unset($options['secret']);
27051e43 111 $ret = parent::set_option($options);
112 return $ret;
113 }
114
96297ca2 115 /**
116 *
6f2cd52a
DC
117 * @param string $config
118 * @return mixed
96297ca2 119 */
dc0976b8 120 public function get_option($config = '') {
121 if ($config==='api_key') {
70d91d90 122 return trim(get_config('flickr', 'api_key'));
7fab56b7 123 } elseif ($config ==='secret') {
124 return trim(get_config('flickr', 'secret'));
27051e43 125 } else {
70d91d90 126 $options['api_key'] = trim(get_config('flickr', 'api_key'));
7fab56b7 127 $options['secret'] = trim(get_config('flickr', 'secret'));
27051e43 128 }
129 $options = parent::get_option($config);
130 return $options;
131 }
132
96297ca2 133 /**
134 *
6f2cd52a 135 * @return bool
96297ca2 136 */
dc0976b8 137 public function global_search() {
8988c36b 138 if (empty($this->token)) {
455860ce 139 return false;
140 } else {
141 return true;
142 }
143 }
96297ca2 144
145 /**
146 *
6f2cd52a 147 * @return null
96297ca2 148 */
5e98ab96 149 public function print_login() {
150 if ($this->options['ajax']) {
d68c527f 151 $ret = array();
6bdfef5d 152 $popup_btn = new stdClass();
d68c527f 153 $popup_btn->type = 'popup';
154 $popup_btn->url = $this->flickr->auth();
155 $ret['login'] = array($popup_btn);
156 return $ret;
5e98ab96 157 } else {
362a728f 158 echo '<a target="_blank" href="'.$this->flickr->auth().'">'.get_string('login', 'repository').'</a>';
6a391ebf 159 }
36ea9a6c 160 }
96297ca2 161
162 /**
6819c428 163 * Converts result received from phpFlickr::photo_search to Filepicker/repository format
96297ca2 164 *
6f2cd52a 165 * @param mixed $photos
6f2cd52a 166 * @return array
96297ca2 167 */
6819c428 168 private function build_list($photos) {
353d5cf3 169 $photos_url = $this->flickr->urls_getUserPhotos($this->nsid);
d05ef4a3 170 $ret = array();
171 $ret['manage'] = $photos_url;
172 $ret['list'] = array();
173 $ret['pages'] = $photos['pages'];
009a97ce 174 $ret['total'] = $photos['total'];
175 $ret['perpage'] = $photos['perpage'];
6819c428 176 $ret['page'] = $photos['page'];
dc0976b8 177 if (!empty($photos['photo'])) {
8988c36b 178 foreach ($photos['photo'] as $p) {
353d5cf3 179 if(empty($p['title'])) {
8988c36b 180 $p['title'] = get_string('notitle', 'repository_flickr');
181 }
182 if (isset($p['originalformat'])) {
183 $format = $p['originalformat'];
184 } else {
185 $format = 'jpg';
186 }
0e51661b 187 $format = '.'.$format;
188 // append extensions to the files
189 if (substr($p['title'], strlen($p['title'])-strlen($format)) != $format) {
4317f92f 190 $p['title'] .= $format;
0e51661b 191 }
192 $ret['list'][] = array('title'=>$p['title'],'source'=>$p['id'],
353d5cf3 193 'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'),
6819c428 194 'thumbnail_width'=>75, 'thumbnail_height'=>75,
353d5cf3 195 'date'=>'', 'size'=>'unknown', 'url'=>$photos_url.$p['id']);
733cfd80 196 }
6a391ebf 197 }
353d5cf3 198 return $ret;
199 }
96297ca2 200
201 /**
202 *
6f2cd52a 203 * @param string $search_text
6819c428 204 * @param int $page
6f2cd52a 205 * @return array
96297ca2 206 */
68a7c9a6 207 public function search($search_text, $page = 0) {
353d5cf3 208 $photos = $this->flickr->photos_search(array(
209 'user_id'=>$this->nsid,
8a77c949 210 'per_page'=>24,
353d5cf3 211 'extras'=>'original_format',
6819c428 212 'page'=>$page,
353d5cf3 213 'text'=>$search_text
214 ));
c438f190 215 $ret = $this->build_list($photos);
6819c428 216 $ret['list'] = array_filter($ret['list'], array($this, 'filter')); // TODO this breaks pagination
c438f190 217 return $ret;
353d5cf3 218 }
96297ca2 219
220 /**
221 *
5fa358d1 222 * @param string $path
223 * @param int $page
6f2cd52a 224 * @return array
96297ca2 225 */
6819c428
MG
226 public function get_listing($path = '', $page = '') {
227 return $this->search('', $page);
36ea9a6c 228 }
96297ca2 229
d6453211
DC
230 /**
231 * Return photo url by given photo id
232 * @param string $photoid
233 * @return string
234 */
235 private function build_photo_url($photoid) {
b2245b7e
FM
236 $bestsize = $this->get_best_size($photoid);
237 if (!isset($bestsize['source'])) {
238 throw new repository_exception('cannotdownload', 'repository');
239 }
240 return $bestsize['source'];
241 }
242
243 /**
244 * Returns the best size for a photo
245 *
246 * @param string $photoid the photo identifier
247 * @return array of information provided by the API
248 */
249 protected function get_best_size($photoid) {
250 if (!isset(self::$sizes[$photoid])) {
251 // Sizes are returned from smallest to greatest.
252 self::$sizes[$photoid] = $this->flickr->photos_getSizes($photoid);
253 }
254 $sizes = self::$sizes[$photoid];
255 $bestsize = array();
256 if (is_array($sizes)) {
257 while ($bestsize = array_pop($sizes)) {
258 // Make sure the source is set. Exit the loop if found.
259 if (isset($bestsize['source'])) {
260 break;
261 }
262 }
99d52655 263 }
b2245b7e 264 return $bestsize;
99d52655
DC
265 }
266
d6453211
DC
267 public function get_link($photoid) {
268 return $this->build_photo_url($photoid);
269 }
270
96297ca2 271 /**
272 *
d6453211 273 * @param string $photoid
6f2cd52a
DC
274 * @param string $file
275 * @return string
96297ca2 276 */
d6453211
DC
277 public function get_file($photoid, $file = '') {
278 $url = $this->build_photo_url($photoid);
72a56555 279 return parent::get_file($url, $file);
6a391ebf 280 }
a6600395 281
96297ca2 282 /**
792ad3aa 283 * Add Plugin settings input to Moodle form
6f2cd52a 284 * @param object $mform
96297ca2 285 */
68a7c9a6 286 public static function type_config_form($mform, $classname = 'repository') {
7fab56b7 287 global $CFG;
336bb44b 288 $api_key = get_config('flickr', 'api_key');
7892948d 289 $secret = get_config('flickr', 'secret');
290
336bb44b 291 if (empty($api_key)) {
292 $api_key = '';
293 }
0868f619 294 if (empty($secret)) {
295 $secret = '';
296 }
7892948d 297
545ca386
FM
298 parent::type_config_form($mform);
299
4a65c39a 300 $strrequired = get_string('required');
0868f619 301 $mform->addElement('text', 'api_key', get_string('apikey', 'repository_flickr'), array('value'=>$api_key,'size' => '40'));
5077e858 302 $mform->setType('api_key', PARAM_RAW_TRIMMED);
0868f619 303 $mform->addElement('text', 'secret', get_string('secret', 'repository_flickr'), array('value'=>$secret,'size' => '40'));
5077e858 304 $mform->setType('secret', PARAM_RAW_TRIMMED);
e49eaacb 305
306 //retrieve the flickr instances
6f2cd52a
DC
307 $params = array();
308 $params['context'] = array();
309 //$params['currentcontext'] = $this->context;
310 $params['onlyvisible'] = false;
311 $params['type'] = 'flickr';
312 $instances = repository::get_instances($params);
e49eaacb 313 if (empty($instances)) {
6f2cd52a
DC
314 $callbackurl = get_string('callbackwarning', 'repository_flickr');
315 $mform->addElement('static', null, '', $callbackurl);
316 } else {
b38b6132 317 $instance = array_shift($instances);
64f93798 318 $callbackurl = $CFG->wwwroot.'/repository/repository_callback.php?repo_id='.$instance->id;
6f2cd52a 319 $mform->addElement('static', 'callbackurl', '', get_string('callbackurltext', 'repository_flickr', $callbackurl));
e49eaacb 320 }
dbc01944 321
4a65c39a 322 $mform->addRule('api_key', $strrequired, 'required', null, 'client');
0868f619 323 $mform->addRule('secret', $strrequired, 'required', null, 'client');
4a65c39a 324 }
0868f619 325
96297ca2 326 /**
792ad3aa 327 * Names of the plugin settings
6f2cd52a 328 * @return array
96297ca2 329 */
1b79955a 330 public static function get_type_option_names() {
a5adfa26 331 return array('api_key', 'secret', 'pluginname');
4a65c39a 332 }
014c1ca0 333 public function supported_filetypes() {
334 return array('web_image');
335 }
41076c58
DC
336 public function supported_returntypes() {
337 return (FILE_INTERNAL | FILE_EXTERNAL);
338 }
d6453211
DC
339
340 /**
341 * Return the source information
342 *
343 * @param string $photoid
344 * @return string|null
345 */
346 public function get_file_source_info($photoid) {
347 return $this->build_photo_url($photoid);
348 }
8a77c949 349}