Merge branch 'MDL-49216-27' of git://github.com/andrewnicols/moodle into MOODLE_27_STABLE
[moodle.git] / repository / flickr / lib.php
blob8e16011d7d7dd5853adcd2cabc322580a30d1c63
1 <?php
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.
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/>.
17 /**
18 * This plugin is used to access flickr pictures
20 * @since Moodle 2.0
21 * @package repository_flickr
22 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once($CFG->dirroot . '/repository/lib.php');
26 require_once($CFG->libdir.'/flickrlib.php');
28 /**
29 * This plugin is used to access user's private flickr repository
31 * @since Moodle 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
36 class repository_flickr extends repository {
37 private $flickr;
38 public $photos;
40 /**
41 * Stores sizes of images to prevent multiple API call
43 static private $sizes = array();
45 /**
47 * @param int $repositoryid
48 * @param object $context
49 * @param array $options
51 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
52 global $SESSION, $CFG;
53 $options['page'] = optional_param('p', 1, PARAM_INT);
54 parent::__construct($repositoryid, $context, $options);
56 $this->setting = 'flickr_';
58 $this->api_key = $this->get_option('api_key');
59 $this->secret = $this->get_option('secret');
61 $this->token = get_user_preferences($this->setting, '');
62 $this->nsid = get_user_preferences($this->setting.'_nsid', '');
64 $this->flickr = new phpFlickr($this->api_key, $this->secret, $this->token);
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']);
77 /**
79 * @return bool
81 public function check_login() {
82 return !empty($this->token);
85 /**
87 * @return mixed
89 public function logout() {
90 set_user_preference($this->setting, '');
91 set_user_preference($this->setting.'_nsid', '');
92 $this->token = '';
93 $this->nsid = '';
94 return $this->print_login();
97 /**
99 * @param array $options
100 * @return mixed
102 public function set_option($options = array()) {
103 if (!empty($options['api_key'])) {
104 set_config('api_key', trim($options['api_key']), 'flickr');
106 if (!empty($options['secret'])) {
107 set_config('secret', trim($options['secret']), 'flickr');
109 unset($options['api_key']);
110 unset($options['secret']);
111 $ret = parent::set_option($options);
112 return $ret;
117 * @param string $config
118 * @return mixed
120 public function get_option($config = '') {
121 if ($config==='api_key') {
122 return trim(get_config('flickr', 'api_key'));
123 } elseif ($config ==='secret') {
124 return trim(get_config('flickr', 'secret'));
125 } else {
126 $options['api_key'] = trim(get_config('flickr', 'api_key'));
127 $options['secret'] = trim(get_config('flickr', 'secret'));
129 $options = parent::get_option($config);
130 return $options;
135 * @return bool
137 public function global_search() {
138 if (empty($this->token)) {
139 return false;
140 } else {
141 return true;
147 * @return null
149 public function print_login() {
150 if ($this->options['ajax']) {
151 $ret = array();
152 $popup_btn = new stdClass();
153 $popup_btn->type = 'popup';
154 $popup_btn->url = $this->flickr->auth();
155 $ret['login'] = array($popup_btn);
156 return $ret;
157 } else {
158 echo '<a target="_blank" href="'.$this->flickr->auth().'">'.get_string('login', 'repository').'</a>';
163 * Converts result received from phpFlickr::photo_search to Filepicker/repository format
165 * @param mixed $photos
166 * @return array
168 private function build_list($photos) {
169 $photos_url = $this->flickr->urls_getUserPhotos($this->nsid);
170 $ret = array();
171 $ret['manage'] = $photos_url;
172 $ret['list'] = array();
173 $ret['pages'] = $photos['pages'];
174 $ret['total'] = $photos['total'];
175 $ret['perpage'] = $photos['perpage'];
176 $ret['page'] = $photos['page'];
177 if (!empty($photos['photo'])) {
178 foreach ($photos['photo'] as $p) {
179 if(empty($p['title'])) {
180 $p['title'] = get_string('notitle', 'repository_flickr');
182 if (isset($p['originalformat'])) {
183 $format = $p['originalformat'];
184 } else {
185 $format = 'jpg';
187 $format = '.'.$format;
188 // append extensions to the files
189 if (substr($p['title'], strlen($p['title'])-strlen($format)) != $format) {
190 $p['title'] .= $format;
192 $ret['list'][] = array('title'=>$p['title'],'source'=>$p['id'],
193 'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'),
194 'thumbnail_width'=>75, 'thumbnail_height'=>75,
195 'date'=>'', 'size'=>'unknown', 'url'=>$photos_url.$p['id']);
198 return $ret;
203 * @param string $search_text
204 * @param int $page
205 * @return array
207 public function search($search_text, $page = 0) {
208 $photos = $this->flickr->photos_search(array(
209 'user_id'=>$this->nsid,
210 'per_page'=>24,
211 'extras'=>'original_format',
212 'page'=>$page,
213 'text'=>$search_text
215 $ret = $this->build_list($photos);
216 $ret['list'] = array_filter($ret['list'], array($this, 'filter')); // TODO this breaks pagination
217 return $ret;
222 * @param string $path
223 * @param int $page
224 * @return array
226 public function get_listing($path = '', $page = '') {
227 return $this->search('', $page);
231 * Return photo url by given photo id
232 * @param string $photoid
233 * @return string
235 private function build_photo_url($photoid) {
236 $bestsize = $this->get_best_size($photoid);
237 if (!isset($bestsize['source'])) {
238 throw new repository_exception('cannotdownload', 'repository');
240 return $bestsize['source'];
244 * Returns the best size for a photo
246 * @param string $photoid the photo identifier
247 * @return array of information provided by the API
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);
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;
264 return $bestsize;
267 public function get_link($photoid) {
268 return $this->build_photo_url($photoid);
273 * @param string $photoid
274 * @param string $file
275 * @return string
277 public function get_file($photoid, $file = '') {
278 $url = $this->build_photo_url($photoid);
279 return parent::get_file($url, $file);
283 * Add Plugin settings input to Moodle form
284 * @param object $mform
286 public static function type_config_form($mform, $classname = 'repository') {
287 global $CFG;
288 $api_key = get_config('flickr', 'api_key');
289 $secret = get_config('flickr', 'secret');
291 if (empty($api_key)) {
292 $api_key = '';
294 if (empty($secret)) {
295 $secret = '';
298 parent::type_config_form($mform);
300 $strrequired = get_string('required');
301 $mform->addElement('text', 'api_key', get_string('apikey', 'repository_flickr'), array('value'=>$api_key,'size' => '40'));
302 $mform->setType('api_key', PARAM_RAW_TRIMMED);
303 $mform->addElement('text', 'secret', get_string('secret', 'repository_flickr'), array('value'=>$secret,'size' => '40'));
304 $mform->setType('secret', PARAM_RAW_TRIMMED);
306 //retrieve the flickr instances
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);
313 if (empty($instances)) {
314 $callbackurl = get_string('callbackwarning', 'repository_flickr');
315 $mform->addElement('static', null, '', $callbackurl);
316 } else {
317 $instance = array_shift($instances);
318 $callbackurl = $CFG->wwwroot.'/repository/repository_callback.php?repo_id='.$instance->id;
319 $mform->addElement('static', 'callbackurl', '', get_string('callbackurltext', 'repository_flickr', $callbackurl));
322 $mform->addRule('api_key', $strrequired, 'required', null, 'client');
323 $mform->addRule('secret', $strrequired, 'required', null, 'client');
327 * Names of the plugin settings
328 * @return array
330 public static function get_type_option_names() {
331 return array('api_key', 'secret', 'pluginname');
333 public function supported_filetypes() {
334 return array('web_image');
336 public function supported_returntypes() {
337 return (FILE_INTERNAL | FILE_EXTERNAL);
341 * Return the source information
343 * @param string $photoid
344 * @return string|null
346 public function get_file_source_info($photoid) {
347 return $this->build_photo_url($photoid);