MDL-15350: format code (mostly white spaces) + skeleton for documentation
[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>
7 * @version $Id$
36ea9a6c 8 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9 */
10
539b3d13 11require_once($CFG->libdir.'/flickrlib.php');
6a391ebf 12
dc0976b8 13/**
14 *
15 */
16class repository_flickr extends repository {
6a391ebf 17 private $flickr;
18 public $photos;
19add4c0 19
dc0976b8 20 /**
21 *
22 * @global <type> $SESSION
23 * @global <type> $CFG
24 * @param <type> $repositoryid
25 * @param <type> $context
26 * @param <type> $options
27 */
28 public function __construct($repositoryid, $context = SITEID, $options = array()) {
d68c527f 29 global $SESSION, $CFG;
dc0976b8 30
d68c527f 31 $options['page'] = optional_param('p', 1, PARAM_INT);
32 parent::__construct($repositoryid, $context, $options);
33
34 $this->setting = 'flickr_';
35
36 $this->api_key = $this->get_option('api_key');
37 $this->secret = $this->get_option('secret');
38
39 $this->token = get_user_preferences($this->setting, '');
40 $this->nsid = get_user_preferences($this->setting.'_nsid', '');
41
42 $this->flickr = new phpFlickr($this->api_key, $this->secret, $this->token);
43
dc0976b8 44 if (empty($this->token)) {
d68c527f 45 $frob = optional_param('frob', '', PARAM_RAW);
dc0976b8 46 if (!empty($frob)) {
d68c527f 47 $auth_info = $this->flickr->auth_getToken($frob);
48 $this->token = $auth_info['token'];
49 $this->nsid = $auth_info['user']['nsid'];
50 set_user_preference($this->setting, $auth_info['token']);
51 set_user_preference($this->setting.'_nsid', $auth_info['user']['nsid']);
52 $this->perm = $auth_info['token'];
53 }
54 }
55
56 }
dc0976b8 57
58 /**
59 *
60 * @return <type>
61 */
62 public function check_login() {
d68c527f 63 return !empty($this->token);
64 }
dc0976b8 65
66 /**
67 *
68 * @return <type>
69 */
70 public function logout() {
d68c527f 71 set_user_preference($this->setting, '');
72 set_user_preference($this->setting.'_nsid', '');
73 $this->token = '';
74 $this->nsid = '';
75 return $this->print_login();
76 }
dc0976b8 77
78 /**
79 *
80 * @param <type> $options
81 * @return <type>
82 */
83 public function set_option($options = array()) {
27051e43 84 if (!empty($options['api_key'])) {
70d91d90 85 set_config('api_key', trim($options['api_key']), 'flickr');
27051e43 86 }
7fab56b7 87 if (!empty($options['secret'])) {
88 set_config('secret', trim($options['secret']), 'flickr');
89 }
27051e43 90 unset($options['api_key']);
7fab56b7 91 unset($options['secret']);
27051e43 92 $ret = parent::set_option($options);
93 return $ret;
94 }
95
dc0976b8 96 /**
97 *
98 * @param <type> $config
99 * @return <type>
100 */
101 public function get_option($config = '') {
102 if ($config==='api_key') {
70d91d90 103 return trim(get_config('flickr', 'api_key'));
7fab56b7 104 } elseif ($config ==='secret') {
105 return trim(get_config('flickr', 'secret'));
27051e43 106 } else {
70d91d90 107 $options['api_key'] = trim(get_config('flickr', 'api_key'));
7fab56b7 108 $options['secret'] = trim(get_config('flickr', 'secret'));
27051e43 109 }
110 $options = parent::get_option($config);
111 return $options;
112 }
113
dc0976b8 114 /**
115 *
116 * @global <type> $SESSION
117 * @return <type>
118 */
119 public function global_search() {
455860ce 120 global $SESSION;
dc0976b8 121
8988c36b 122 if (empty($this->token)) {
455860ce 123 return false;
124 } else {
125 return true;
126 }
127 }
dc0976b8 128
129 /**
130 *
131 * @global <type> $SESSION
132 * @param <type> $ajax
133 * @return <type>
134 */
135 public function print_login($ajax = true) {
36ea9a6c 136 global $SESSION;
dc0976b8 137
d68c527f 138 if($ajax){
139 $ret = array();
140 $popup_btn = new stdclass;
141 $popup_btn->type = 'popup';
142 $popup_btn->url = $this->flickr->auth();
143 $ret['login'] = array($popup_btn);
144 return $ret;
6a391ebf 145 }
36ea9a6c 146 }
dc0976b8 147
148 /**
149 *
150 * @global <type> $SESSION
151 * @param <type> $path
152 * @param <type> $search
153 * @return <type>
154 */
155 public function get_listing($path = '1', $search = '') {
36ea9a6c 156 global $SESSION;
dc0976b8 157
2b9feb5f 158 $nsid = get_user_preferences($this->setting.'_nsid');
8988c36b 159 $photos_url = $this->flickr->urls_getUserPhotos($nsid);
6a391ebf 160
dc0976b8 161 if (!empty($search)) {
8988c36b 162 $photos = $this->flickr->photos_search(array(
2b9feb5f 163 'user_id'=>$nsid,
8988c36b 164 'per_page'=>25,
165 'page'=>$path,
166 'extras'=>'original_format',
167 'text'=>$search
168 ));
dc0976b8 169 } elseif (!empty($path) && empty($search)) {
8988c36b 170 $photos = $this->flickr->photos_search(array(
171 'user_id'=>$nsid,
172 'per_page'=>25,
173 'page'=>$path,
174 'extras'=>'original_format'
175 ));
6a391ebf 176 }
d05ef4a3 177 $ret = array();
178 $ret['manage'] = $photos_url;
179 $ret['list'] = array();
180 $ret['pages'] = $photos['pages'];
dc0976b8 181 if (is_int($path) && $path <= $ret['pages']) {
d05ef4a3 182 $ret['page'] = $path;
6a391ebf 183 } else {
d05ef4a3 184 $ret['page'] = 1;
6a391ebf 185 }
dc0976b8 186 if (!empty($photos['photo'])) {
8988c36b 187 foreach ($photos['photo'] as $p) {
dc0976b8 188 if (empty($p['title'])) {
8988c36b 189 $p['title'] = get_string('notitle', 'repository_flickr');
190 }
191 if (isset($p['originalformat'])) {
192 $format = $p['originalformat'];
193 } else {
194 $format = 'jpg';
195 }
196 $ret['list'][] =
197 array('title'=>$p['title'].'.'.$format,'source'=>$p['id'],'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'), 'date'=>'', 'size'=>'unknown', 'url'=>$photos_url.$p['id']);
733cfd80 198 }
6a391ebf 199 }
dc0976b8 200 if (empty($ret)) {
6a391ebf 201 throw new repository_exception('nullphotolist', 'repository_flickr');
bf1fccf0 202 } else {
203 return $ret;
204 }
36ea9a6c 205 }
dc0976b8 206
207 /**
208 *
209 * @return <type>
210 */
211 public function print_listing() {
539b3d13 212 return false;
36ea9a6c 213 }
dc0976b8 214
215 /**
216 *
217 * @return <type>
218 */
219 public function print_search() {
2b9feb5f 220 echo '<input name="s" value="" />';
221 parent::print_search();
222 return true;
36ea9a6c 223 }
dc0976b8 224
225 /**
226 *
227 * @global <type> $CFG
228 * @param <type> $photo_id
229 * @param <type> $file
230 * @return <type>
231 */
232 public function get_file($photo_id, $file = '') {
6a391ebf 233 global $CFG;
dc0976b8 234
6a391ebf 235 $result = $this->flickr->photos_getSizes($photo_id);
236 $url = '';
dc0976b8 237 if (!empty($result[4])) {
6a391ebf 238 $url = $result[4]['source'];
dc0976b8 239 } elseif (!empty($result[3])) {
6a391ebf 240 $url = $result[3]['source'];
dc0976b8 241 } elseif (!empty($result[2])) {
6a391ebf 242 $url = $result[2]['source'];
243 }
244 if (!file_exists($CFG->dataroot.'/repository/download')) {
245 mkdir($CFG->dataroot.'/repository/download/', 0777, true);
246 }
247 if(is_dir($CFG->dataroot.'/repository/download')) {
248 $dir = $CFG->dataroot.'/repository/download/';
249 }
250
dc0976b8 251 if (empty($file)) {
6a391ebf 252 $file = $photo_id.'_'.time().'.jpg';
253 }
dc0976b8 254 if (file_exists($dir.$file)) {
6a391ebf 255 $file = uniqid('m').$file;
256 }
257 $fp = fopen($dir.$file, 'w');
258 $c = new curl;
259 $c->download(array(
260 array('url'=>$url, 'file'=>$fp)
261 ));
262 return $dir.$file;
263 }
dc0976b8 264
265 /**
266 *
267 * @return <type>
268 */
4a65c39a 269 public static function has_admin_config() {
270 return true;
271 }
a6600395 272
dc0976b8 273 /**
274 *
275 * @return <type>
276 */
faaa613d 277 public static function has_multiple_instances() {
7fab56b7 278 return false;
faaa613d 279 }
280
dc0976b8 281 /**
282 *
283 * @return <type>
284 */
faaa613d 285 public static function has_instance_config() {
7892948d 286 return false;
0868f619 287 }
288
dc0976b8 289 /**
290 *
291 * @param <type> $
292 */
0868f619 293 public function instance_config_form(&$mform) {
294 $mform->addElement('text', 'email_address', get_string('emailaddress', 'repository_flickr'));
3701fd35 295 $mform->addRule('email_address', get_string('required'), 'required', null, 'client');
0868f619 296 }
297
dc0976b8 298 /**
299 *
300 * @return <type>
301 */
302 public static function get_instance_option_names() {
0868f619 303 return array('email_address');
faaa613d 304 }
305
dc0976b8 306 /**
307 *
308 * @global <type> $CFG
309 * @param <type> $
310 */
4a65c39a 311 public function admin_config_form(&$mform) {
7fab56b7 312 global $CFG;
dc0976b8 313
336bb44b 314 $api_key = get_config('flickr', 'api_key');
7892948d 315 $secret = get_config('flickr', 'secret');
316
317 //retrieve the flickr instances
318 $instances = repository_get_instances(array(),null,false,"flickr");
dc0976b8 319 if (empty($instances)) {
7892948d 320 $callbackurl = get_string("callbackwarning","repository_flickr");
321 }
322 else {
323 $callbackurl = $CFG->wwwroot.'/repository/ws.php?callback=yes&repo_id='.$instances[0]->id;
324 }
325
336bb44b 326 if (empty($api_key)) {
327 $api_key = '';
328 }
0868f619 329 if (empty($secret)) {
330 $secret = '';
331 }
7892948d 332
4a65c39a 333 $strrequired = get_string('required');
0868f619 334 $mform->addElement('text', 'api_key', get_string('apikey', 'repository_flickr'), array('value'=>$api_key,'size' => '40'));
335 $mform->addElement('text', 'secret', get_string('secret', 'repository_flickr'), array('value'=>$secret,'size' => '40'));
7fab56b7 336 $mform->addElement('static', 'callbackurl', get_string('callbackurl', 'repository_flickr'), $callbackurl);
0868f619 337
4a65c39a 338 $mform->addRule('api_key', $strrequired, 'required', null, 'client');
0868f619 339 $mform->addRule('secret', $strrequired, 'required', null, 'client');
4a65c39a 340 }
0868f619 341
dc0976b8 342 /**
343 *
344 * @return <type>
345 */
346 public static function get_admin_option_names() {
7fab56b7 347 return array('api_key', 'secret');
4a65c39a 348 }
a6600395 349
36ea9a6c 350}