3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * repository_flickr class
20 * This plugin is used to access user's private flickr repository
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');
35 class repository_flickr
extends repository
{
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']);
75 public function check_login() {
76 return !empty($this->token
);
83 public function logout() {
84 set_user_preference($this->setting
, '');
85 set_user_preference($this->setting
.'_nsid', '');
88 return $this->print_login();
93 * @param array $options
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);
111 * @param string $config
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'));
120 $options['api_key'] = trim(get_config('flickr', 'api_key'));
121 $options['secret'] = trim(get_config('flickr', 'secret'));
123 $options = parent
::get_option($config);
131 public function global_search() {
132 if (empty($this->token
)) {
143 public function print_login() {
144 if ($this->options
['ajax']) {
146 $popup_btn = new stdClass();
147 $popup_btn->type
= 'popup';
148 $popup_btn->url
= $this->flickr
->auth();
149 $ret['login'] = array($popup_btn);
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
162 private function build_list($photos) {
163 $photos_url = $this->flickr
->urls_getUserPhotos($this->nsid
);
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'];
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']);
197 * @param string $search_text
201 public function search($search_text, $page = 0) {
202 $photos = $this->flickr
->photos_search(array(
203 'user_id'=>$this->nsid
,
205 'extras'=>'original_format',
209 $ret = $this->build_list($photos);
210 $ret['list'] = array_filter($ret['list'], array($this, 'filter')); // TODO this breaks pagination
216 * @param string $path
220 public function get_listing($path = '', $page = '') {
221 return $this->search('', $page);
224 public function get_link($photo_id) {
226 $result = $this->flickr
->photos_getSizes($photo_id);
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'];
240 * @param string $photo_id
241 * @param string $file
244 public function get_file($photo_id, $file = '') {
246 $result = $this->flickr
->photos_getSizes($photo_id);
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');
259 array('url'=>$url, 'file'=>$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) {
271 $api_key = get_config('flickr', 'api_key');
272 $secret = get_config('flickr', 'secret');
274 if (empty($api_key)) {
277 if (empty($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
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);
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
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
);