weekly release 2.3dev
[moodle.git] / repository / flickr / lib.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 22 * @since 2.0
d078f6d3
PS
23 * @package repository
24 * @subpackage flickr
25 * @copyright 2009 Dongsheng Cai
26 * @author Dongsheng Cai <dongsheng@moodle.com>
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 */
447c7a19 45 public function __construct($repositoryid, $context = SYSCONTEXTID, $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();
6bdfef5d 146 $popup_btn = new stdClass();
d68c527f 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 /**
6819c428 157 * Converts result received from phpFlickr::photo_search to Filepicker/repository format
96297ca2 158 *
6f2cd52a 159 * @param mixed $photos
6f2cd52a 160 * @return array
96297ca2 161 */
6819c428 162 private function build_list($photos) {
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'];
6819c428 170 $ret['page'] = $photos['page'];
dc0976b8 171 if (!empty($photos['photo'])) {
8988c36b 172 foreach ($photos['photo'] as $p) {
353d5cf3 173 if(empty($p['title'])) {
8988c36b 174 $p['title'] = get_string('notitle', 'repository_flickr');
175 }
176 if (isset($p['originalformat'])) {
177 $format = $p['originalformat'];
178 } else {
179 $format = 'jpg';
180 }
0e51661b 181 $format = '.'.$format;
182 // append extensions to the files
183 if (substr($p['title'], strlen($p['title'])-strlen($format)) != $format) {
4317f92f 184 $p['title'] .= $format;
0e51661b 185 }
186 $ret['list'][] = array('title'=>$p['title'],'source'=>$p['id'],
353d5cf3 187 'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'),
6819c428 188 'thumbnail_width'=>75, 'thumbnail_height'=>75,
353d5cf3 189 'date'=>'', 'size'=>'unknown', 'url'=>$photos_url.$p['id']);
733cfd80 190 }
6a391ebf 191 }
353d5cf3 192 return $ret;
193 }
96297ca2 194
195 /**
196 *
6f2cd52a 197 * @param string $search_text
6819c428 198 * @param int $page
6f2cd52a 199 * @return array
96297ca2 200 */
68a7c9a6 201 public function search($search_text, $page = 0) {
353d5cf3 202 $photos = $this->flickr->photos_search(array(
203 'user_id'=>$this->nsid,
8a77c949 204 'per_page'=>24,
353d5cf3 205 'extras'=>'original_format',
6819c428 206 'page'=>$page,
353d5cf3 207 'text'=>$search_text
208 ));
c438f190 209 $ret = $this->build_list($photos);
6819c428 210 $ret['list'] = array_filter($ret['list'], array($this, 'filter')); // TODO this breaks pagination
c438f190 211 return $ret;
353d5cf3 212 }
96297ca2 213
214 /**
215 *
5fa358d1 216 * @param string $path
217 * @param int $page
6f2cd52a 218 * @return array
96297ca2 219 */
6819c428
MG
220 public function get_listing($path = '', $page = '') {
221 return $this->search('', $page);
36ea9a6c 222 }
96297ca2 223
99d52655
DC
224 public function get_link($photo_id) {
225 global $CFG;
226 $result = $this->flickr->photos_getSizes($photo_id);
227 $url = '';
228 if(!empty($result[4])) {
229 $url = $result[4]['source'];
230 } elseif(!empty($result[3])) {
231 $url = $result[3]['source'];
232 } elseif(!empty($result[2])) {
233 $url = $result[2]['source'];
234 }
235 return $url;
236 }
237
96297ca2 238 /**
239 *
6f2cd52a
DC
240 * @param string $photo_id
241 * @param string $file
242 * @return string
96297ca2 243 */
dc0976b8 244 public function get_file($photo_id, $file = '') {
6a391ebf 245 global $CFG;
246 $result = $this->flickr->photos_getSizes($photo_id);
247 $url = '';
353d5cf3 248 if(!empty($result[4])) {
6a391ebf 249 $url = $result[4]['source'];
353d5cf3 250 } elseif(!empty($result[3])) {
6a391ebf 251 $url = $result[3]['source'];
353d5cf3 252 } elseif(!empty($result[2])) {
6a391ebf 253 $url = $result[2]['source'];
254 }
a53d4f45 255 $path = $this->prepare_file($file);
256 $fp = fopen($path, 'w');
6a391ebf 257 $c = new curl;
258 $c->download(array(
259 array('url'=>$url, 'file'=>$fp)
260 ));
1dce6261 261 return array('path'=>$path, 'url'=>$url);
6a391ebf 262 }
a6600395 263
96297ca2 264 /**
792ad3aa 265 * Add Plugin settings input to Moodle form
6f2cd52a 266 * @param object $mform
96297ca2 267 */
68a7c9a6 268 public static function type_config_form($mform, $classname = 'repository') {
7fab56b7 269 global $CFG;
336bb44b 270 $api_key = get_config('flickr', 'api_key');
7892948d 271 $secret = get_config('flickr', 'secret');
272
336bb44b 273 if (empty($api_key)) {
274 $api_key = '';
275 }
0868f619 276 if (empty($secret)) {
277 $secret = '';
278 }
7892948d 279
4a65c39a 280 $strrequired = get_string('required');
0868f619 281 $mform->addElement('text', 'api_key', get_string('apikey', 'repository_flickr'), array('value'=>$api_key,'size' => '40'));
282 $mform->addElement('text', 'secret', get_string('secret', 'repository_flickr'), array('value'=>$secret,'size' => '40'));
e49eaacb 283
284 //retrieve the flickr instances
6f2cd52a
DC
285 $params = array();
286 $params['context'] = array();
287 //$params['currentcontext'] = $this->context;
288 $params['onlyvisible'] = false;
289 $params['type'] = 'flickr';
290 $instances = repository::get_instances($params);
e49eaacb 291 if (empty($instances)) {
6f2cd52a
DC
292 $callbackurl = get_string('callbackwarning', 'repository_flickr');
293 $mform->addElement('static', null, '', $callbackurl);
294 } else {
b38b6132 295 $instance = array_shift($instances);
64f93798 296 $callbackurl = $CFG->wwwroot.'/repository/repository_callback.php?repo_id='.$instance->id;
6f2cd52a 297 $mform->addElement('static', 'callbackurl', '', get_string('callbackurltext', 'repository_flickr', $callbackurl));
e49eaacb 298 }
dbc01944 299
4a65c39a 300 $mform->addRule('api_key', $strrequired, 'required', null, 'client');
0868f619 301 $mform->addRule('secret', $strrequired, 'required', null, 'client');
4a65c39a 302 }
0868f619 303
96297ca2 304 /**
792ad3aa 305 * Names of the plugin settings
6f2cd52a 306 * @return array
96297ca2 307 */
1b79955a 308 public static function get_type_option_names() {
a5adfa26 309 return array('api_key', 'secret', 'pluginname');
4a65c39a 310 }
014c1ca0 311 public function supported_filetypes() {
312 return array('web_image');
313 }
41076c58
DC
314 public function supported_returntypes() {
315 return (FILE_INTERNAL | FILE_EXTERNAL);
316 }
8a77c949 317}