MDL-35101 Repository: Public Flickr fails when the picture has no license
[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
96297ca2 40 /**
41 *
6f2cd52a
DC
42 * @param int $repositoryid
43 * @param object $context
44 * @param array $options
96297ca2 45 */
447c7a19 46 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
d68c527f 47 global $SESSION, $CFG;
48 $options['page'] = optional_param('p', 1, PARAM_INT);
49 parent::__construct($repositoryid, $context, $options);
50
51 $this->setting = 'flickr_';
52
53 $this->api_key = $this->get_option('api_key');
54 $this->secret = $this->get_option('secret');
55
56 $this->token = get_user_preferences($this->setting, '');
57 $this->nsid = get_user_preferences($this->setting.'_nsid', '');
58
59 $this->flickr = new phpFlickr($this->api_key, $this->secret, $this->token);
60
353d5cf3 61 $frob = optional_param('frob', '', PARAM_RAW);
62 if (empty($this->token) && !empty($frob)) {
63 $auth_info = $this->flickr->auth_getToken($frob);
64 $this->token = $auth_info['token'];
65 $this->nsid = $auth_info['user']['nsid'];
66 set_user_preference($this->setting, $auth_info['token']);
67 set_user_preference($this->setting.'_nsid', $auth_info['user']['nsid']);
d68c527f 68 }
69
70 }
96297ca2 71
72 /**
73 *
6f2cd52a 74 * @return bool
96297ca2 75 */
dc0976b8 76 public function check_login() {
d68c527f 77 return !empty($this->token);
78 }
96297ca2 79
80 /**
81 *
6f2cd52a 82 * @return mixed
96297ca2 83 */
dc0976b8 84 public function logout() {
d68c527f 85 set_user_preference($this->setting, '');
86 set_user_preference($this->setting.'_nsid', '');
87 $this->token = '';
88 $this->nsid = '';
89 return $this->print_login();
90 }
96297ca2 91
92 /**
93 *
6f2cd52a
DC
94 * @param array $options
95 * @return mixed
96297ca2 96 */
dc0976b8 97 public function set_option($options = array()) {
27051e43 98 if (!empty($options['api_key'])) {
70d91d90 99 set_config('api_key', trim($options['api_key']), 'flickr');
27051e43 100 }
7fab56b7 101 if (!empty($options['secret'])) {
102 set_config('secret', trim($options['secret']), 'flickr');
103 }
27051e43 104 unset($options['api_key']);
7fab56b7 105 unset($options['secret']);
27051e43 106 $ret = parent::set_option($options);
107 return $ret;
108 }
109
96297ca2 110 /**
111 *
6f2cd52a
DC
112 * @param string $config
113 * @return mixed
96297ca2 114 */
dc0976b8 115 public function get_option($config = '') {
116 if ($config==='api_key') {
70d91d90 117 return trim(get_config('flickr', 'api_key'));
7fab56b7 118 } elseif ($config ==='secret') {
119 return trim(get_config('flickr', 'secret'));
27051e43 120 } else {
70d91d90 121 $options['api_key'] = trim(get_config('flickr', 'api_key'));
7fab56b7 122 $options['secret'] = trim(get_config('flickr', 'secret'));
27051e43 123 }
124 $options = parent::get_option($config);
125 return $options;
126 }
127
96297ca2 128 /**
129 *
6f2cd52a 130 * @return bool
96297ca2 131 */
dc0976b8 132 public function global_search() {
8988c36b 133 if (empty($this->token)) {
455860ce 134 return false;
135 } else {
136 return true;
137 }
138 }
96297ca2 139
140 /**
141 *
6f2cd52a 142 * @return null
96297ca2 143 */
5e98ab96 144 public function print_login() {
145 if ($this->options['ajax']) {
d68c527f 146 $ret = array();
6bdfef5d 147 $popup_btn = new stdClass();
d68c527f 148 $popup_btn->type = 'popup';
149 $popup_btn->url = $this->flickr->auth();
150 $ret['login'] = array($popup_btn);
151 return $ret;
5e98ab96 152 } else {
362a728f 153 echo '<a target="_blank" href="'.$this->flickr->auth().'">'.get_string('login', 'repository').'</a>';
6a391ebf 154 }
36ea9a6c 155 }
96297ca2 156
157 /**
6819c428 158 * Converts result received from phpFlickr::photo_search to Filepicker/repository format
96297ca2 159 *
6f2cd52a 160 * @param mixed $photos
6f2cd52a 161 * @return array
96297ca2 162 */
6819c428 163 private function build_list($photos) {
353d5cf3 164 $photos_url = $this->flickr->urls_getUserPhotos($this->nsid);
d05ef4a3 165 $ret = array();
166 $ret['manage'] = $photos_url;
167 $ret['list'] = array();
168 $ret['pages'] = $photos['pages'];
009a97ce 169 $ret['total'] = $photos['total'];
170 $ret['perpage'] = $photos['perpage'];
6819c428 171 $ret['page'] = $photos['page'];
dc0976b8 172 if (!empty($photos['photo'])) {
8988c36b 173 foreach ($photos['photo'] as $p) {
353d5cf3 174 if(empty($p['title'])) {
8988c36b 175 $p['title'] = get_string('notitle', 'repository_flickr');
176 }
177 if (isset($p['originalformat'])) {
178 $format = $p['originalformat'];
179 } else {
180 $format = 'jpg';
181 }
0e51661b 182 $format = '.'.$format;
183 // append extensions to the files
184 if (substr($p['title'], strlen($p['title'])-strlen($format)) != $format) {
4317f92f 185 $p['title'] .= $format;
0e51661b 186 }
187 $ret['list'][] = array('title'=>$p['title'],'source'=>$p['id'],
353d5cf3 188 'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'),
6819c428 189 'thumbnail_width'=>75, 'thumbnail_height'=>75,
353d5cf3 190 'date'=>'', 'size'=>'unknown', 'url'=>$photos_url.$p['id']);
733cfd80 191 }
6a391ebf 192 }
353d5cf3 193 return $ret;
194 }
96297ca2 195
196 /**
197 *
6f2cd52a 198 * @param string $search_text
6819c428 199 * @param int $page
6f2cd52a 200 * @return array
96297ca2 201 */
68a7c9a6 202 public function search($search_text, $page = 0) {
353d5cf3 203 $photos = $this->flickr->photos_search(array(
204 'user_id'=>$this->nsid,
8a77c949 205 'per_page'=>24,
353d5cf3 206 'extras'=>'original_format',
6819c428 207 'page'=>$page,
353d5cf3 208 'text'=>$search_text
209 ));
c438f190 210 $ret = $this->build_list($photos);
6819c428 211 $ret['list'] = array_filter($ret['list'], array($this, 'filter')); // TODO this breaks pagination
c438f190 212 return $ret;
353d5cf3 213 }
96297ca2 214
215 /**
216 *
5fa358d1 217 * @param string $path
218 * @param int $page
6f2cd52a 219 * @return array
96297ca2 220 */
6819c428
MG
221 public function get_listing($path = '', $page = '') {
222 return $this->search('', $page);
36ea9a6c 223 }
96297ca2 224
d6453211
DC
225 /**
226 * Return photo url by given photo id
227 * @param string $photoid
228 * @return string
229 */
230 private function build_photo_url($photoid) {
231 $result = $this->flickr->photos_getSizes($photoid);
99d52655
DC
232 $url = '';
233 if(!empty($result[4])) {
234 $url = $result[4]['source'];
235 } elseif(!empty($result[3])) {
236 $url = $result[3]['source'];
237 } elseif(!empty($result[2])) {
238 $url = $result[2]['source'];
239 }
240 return $url;
241 }
242
d6453211
DC
243 public function get_link($photoid) {
244 return $this->build_photo_url($photoid);
245 }
246
96297ca2 247 /**
248 *
d6453211 249 * @param string $photoid
6f2cd52a
DC
250 * @param string $file
251 * @return string
96297ca2 252 */
d6453211
DC
253 public function get_file($photoid, $file = '') {
254 $url = $this->build_photo_url($photoid);
72a56555 255 return parent::get_file($url, $file);
6a391ebf 256 }
a6600395 257
96297ca2 258 /**
792ad3aa 259 * Add Plugin settings input to Moodle form
6f2cd52a 260 * @param object $mform
96297ca2 261 */
68a7c9a6 262 public static function type_config_form($mform, $classname = 'repository') {
7fab56b7 263 global $CFG;
336bb44b 264 $api_key = get_config('flickr', 'api_key');
7892948d 265 $secret = get_config('flickr', 'secret');
266
336bb44b 267 if (empty($api_key)) {
268 $api_key = '';
269 }
0868f619 270 if (empty($secret)) {
271 $secret = '';
272 }
7892948d 273
545ca386
FM
274 parent::type_config_form($mform);
275
4a65c39a 276 $strrequired = get_string('required');
0868f619 277 $mform->addElement('text', 'api_key', get_string('apikey', 'repository_flickr'), array('value'=>$api_key,'size' => '40'));
278 $mform->addElement('text', 'secret', get_string('secret', 'repository_flickr'), array('value'=>$secret,'size' => '40'));
e49eaacb 279
280 //retrieve the flickr instances
6f2cd52a
DC
281 $params = array();
282 $params['context'] = array();
283 //$params['currentcontext'] = $this->context;
284 $params['onlyvisible'] = false;
285 $params['type'] = 'flickr';
286 $instances = repository::get_instances($params);
e49eaacb 287 if (empty($instances)) {
6f2cd52a
DC
288 $callbackurl = get_string('callbackwarning', 'repository_flickr');
289 $mform->addElement('static', null, '', $callbackurl);
290 } else {
b38b6132 291 $instance = array_shift($instances);
64f93798 292 $callbackurl = $CFG->wwwroot.'/repository/repository_callback.php?repo_id='.$instance->id;
6f2cd52a 293 $mform->addElement('static', 'callbackurl', '', get_string('callbackurltext', 'repository_flickr', $callbackurl));
e49eaacb 294 }
dbc01944 295
4a65c39a 296 $mform->addRule('api_key', $strrequired, 'required', null, 'client');
0868f619 297 $mform->addRule('secret', $strrequired, 'required', null, 'client');
4a65c39a 298 }
0868f619 299
96297ca2 300 /**
792ad3aa 301 * Names of the plugin settings
6f2cd52a 302 * @return array
96297ca2 303 */
1b79955a 304 public static function get_type_option_names() {
a5adfa26 305 return array('api_key', 'secret', 'pluginname');
4a65c39a 306 }
014c1ca0 307 public function supported_filetypes() {
308 return array('web_image');
309 }
41076c58
DC
310 public function supported_returntypes() {
311 return (FILE_INTERNAL | FILE_EXTERNAL);
312 }
d6453211
DC
313
314 /**
315 * Return the source information
316 *
317 * @param string $photoid
318 * @return string|null
319 */
320 public function get_file_source_info($photoid) {
321 return $this->build_photo_url($photoid);
322 }
8a77c949 323}