Merge branch 'MDL-35026_22' of git://github.com/timhunt/moodle into MOODLE_22_STABLE
[moodle.git] / repository / flickr / lib.php
blobcfc1739c247a65e3e9d1a1745b72adb077c3805e
1 <?php
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.
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/>.
18 /**
19 * repository_flickr class
20 * This plugin is used to access user's private flickr repository
22 * @since 2.0
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
30 require_once($CFG->libdir.'/flickrlib.php');
32 /**
35 class repository_flickr extends repository {
36 private $flickr;
37 public $photos;
39 /**
41 * @param int $repositoryid
42 * @param object $context
43 * @param array $options
45 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
46 global $SESSION, $CFG;
47 $options['page'] = optional_param('p', 1, PARAM_INT);
48 parent::__construct($repositoryid, $context, $options);
50 $this->setting = 'flickr_';
52 $this->api_key = $this->get_option('api_key');
53 $this->secret = $this->get_option('secret');
55 $this->token = get_user_preferences($this->setting, '');
56 $this->nsid = get_user_preferences($this->setting.'_nsid', '');
58 $this->flickr = new phpFlickr($this->api_key, $this->secret, $this->token);
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']);
71 /**
73 * @return bool
75 public function check_login() {
76 return !empty($this->token);
79 /**
81 * @return mixed
83 public function logout() {
84 set_user_preference($this->setting, '');
85 set_user_preference($this->setting.'_nsid', '');
86 $this->token = '';
87 $this->nsid = '';
88 return $this->print_login();
91 /**
93 * @param array $options
94 * @return mixed
96 public function set_option($options = array()) {
97 if (!empty($options['api_key'])) {
98 set_config('api_key', trim($options['api_key']), 'flickr');
100 if (!empty($options['secret'])) {
101 set_config('secret', trim($options['secret']), 'flickr');
103 unset($options['api_key']);
104 unset($options['secret']);
105 $ret = parent::set_option($options);
106 return $ret;
111 * @param string $config
112 * @return mixed
114 public function get_option($config = '') {
115 if ($config==='api_key') {
116 return trim(get_config('flickr', 'api_key'));
117 } elseif ($config ==='secret') {
118 return trim(get_config('flickr', 'secret'));
119 } else {
120 $options['api_key'] = trim(get_config('flickr', 'api_key'));
121 $options['secret'] = trim(get_config('flickr', 'secret'));
123 $options = parent::get_option($config);
124 return $options;
129 * @return bool
131 public function global_search() {
132 if (empty($this->token)) {
133 return false;
134 } else {
135 return true;
141 * @return null
143 public function print_login() {
144 if ($this->options['ajax']) {
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;
151 } else {
152 echo '<a target="_blank" href="'.$this->flickr->auth().'">'.get_string('login', 'repository').'</a>';
157 * Converts result received from phpFlickr::photo_search to Filepicker/repository format
159 * @param mixed $photos
160 * @return array
162 private function build_list($photos) {
163 $photos_url = $this->flickr->urls_getUserPhotos($this->nsid);
164 $ret = array();
165 $ret['manage'] = $photos_url;
166 $ret['list'] = array();
167 $ret['pages'] = $photos['pages'];
168 $ret['total'] = $photos['total'];
169 $ret['perpage'] = $photos['perpage'];
170 $ret['page'] = $photos['page'];
171 if (!empty($photos['photo'])) {
172 foreach ($photos['photo'] as $p) {
173 if(empty($p['title'])) {
174 $p['title'] = get_string('notitle', 'repository_flickr');
176 if (isset($p['originalformat'])) {
177 $format = $p['originalformat'];
178 } else {
179 $format = 'jpg';
181 $format = '.'.$format;
182 // append extensions to the files
183 if (substr($p['title'], strlen($p['title'])-strlen($format)) != $format) {
184 $p['title'] .= $format;
186 $ret['list'][] = array('title'=>$p['title'],'source'=>$p['id'],
187 'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'),
188 'thumbnail_width'=>75, 'thumbnail_height'=>75,
189 'date'=>'', 'size'=>'unknown', 'url'=>$photos_url.$p['id']);
192 return $ret;
197 * @param string $search_text
198 * @param int $page
199 * @return array
201 public function search($search_text, $page = 0) {
202 $photos = $this->flickr->photos_search(array(
203 'user_id'=>$this->nsid,
204 'per_page'=>24,
205 'extras'=>'original_format',
206 'page'=>$page,
207 'text'=>$search_text
209 $ret = $this->build_list($photos);
210 $ret['list'] = array_filter($ret['list'], array($this, 'filter')); // TODO this breaks pagination
211 return $ret;
216 * @param string $path
217 * @param int $page
218 * @return array
220 public function get_listing($path = '', $page = '') {
221 return $this->search('', $page);
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'];
235 return $url;
240 * @param string $photo_id
241 * @param string $file
242 * @return string
244 public function get_file($photo_id, $file = '') {
245 global $CFG;
246 $result = $this->flickr->photos_getSizes($photo_id);
247 $url = '';
248 if(!empty($result[4])) {
249 $url = $result[4]['source'];
250 } elseif(!empty($result[3])) {
251 $url = $result[3]['source'];
252 } elseif(!empty($result[2])) {
253 $url = $result[2]['source'];
255 $path = $this->prepare_file($file);
256 $fp = fopen($path, 'w');
257 $c = new curl;
258 $c->download(array(
259 array('url'=>$url, 'file'=>$fp)
261 fclose($fp);
262 return array('path'=>$path, 'url'=>$url);
266 * Add Plugin settings input to Moodle form
267 * @param object $mform
269 public function type_config_form($mform) {
270 global $CFG;
271 $api_key = get_config('flickr', 'api_key');
272 $secret = get_config('flickr', 'secret');
274 if (empty($api_key)) {
275 $api_key = '';
277 if (empty($secret)) {
278 $secret = '';
281 parent::type_config_form($mform);
283 $strrequired = get_string('required');
284 $mform->addElement('text', 'api_key', get_string('apikey', 'repository_flickr'), array('value'=>$api_key,'size' => '40'));
285 $mform->addElement('text', 'secret', get_string('secret', 'repository_flickr'), array('value'=>$secret,'size' => '40'));
287 //retrieve the flickr instances
288 $params = array();
289 $params['context'] = array();
290 //$params['currentcontext'] = $this->context;
291 $params['onlyvisible'] = false;
292 $params['type'] = 'flickr';
293 $instances = repository::get_instances($params);
294 if (empty($instances)) {
295 $callbackurl = get_string('callbackwarning', 'repository_flickr');
296 $mform->addElement('static', null, '', $callbackurl);
297 } else {
298 $instance = array_shift($instances);
299 $callbackurl = $CFG->wwwroot.'/repository/repository_callback.php?repo_id='.$instance->id;
300 $mform->addElement('static', 'callbackurl', '', get_string('callbackurltext', 'repository_flickr', $callbackurl));
303 $mform->addRule('api_key', $strrequired, 'required', null, 'client');
304 $mform->addRule('secret', $strrequired, 'required', null, 'client');
308 * Names of the plugin settings
309 * @return array
311 public static function get_type_option_names() {
312 return array('api_key', 'secret', 'pluginname');
314 public function supported_filetypes() {
315 return array('web_image');
317 public function supported_returntypes() {
318 return (FILE_INTERNAL | FILE_EXTERNAL);