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