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