portfolio/lib MDL-15995 Added a missing global declaration for $CFG that broke cron
[moodle.git] / repository / flickr_public / repository.class.php
CommitLineData
539b3d13 1<?php
2/**
3 * repository_flickr_public class
4 * This one is used to create public repository
5 * You can set up a public account in admin page, so everyone can
6 * access photos in this public account
7 *
8 * @author Dongsheng Cai <dongsheng@moodle.com>
539b3d13 9 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
10 */
11
12require_once($CFG->libdir.'/flickrlib.php');
5fdc39c7 13require_once(dirname(__FILE__) . '/image.php');
539b3d13 14
96297ca2 15/**
16 *
17 */
dc0976b8 18class repository_flickr_public extends repository {
539b3d13 19 private $flickr;
20 public $photos;
21
96297ca2 22 /**
6b2a6c90 23 * save api_key in config table
24 * @param array $options
25 * @return boolean
96297ca2 26 */
dc0976b8 27 public function set_option($options = array()) {
539b3d13 28 if (!empty($options['api_key'])) {
29 set_config('api_key', trim($options['api_key']), 'flickr_public');
30 }
31 unset($options['api_key']);
32 $ret = parent::set_option($options);
33 return $ret;
34 }
35
96297ca2 36 /**
6b2a6c90 37 * get api_key from config table
38 * @param string $config
39 * @return mixed
96297ca2 40 */
dc0976b8 41 public function get_option($config = '') {
42 if ($config==='api_key') {
539b3d13 43 return trim(get_config('flickr_public', 'api_key'));
44 } else {
45 $options['api_key'] = trim(get_config('flickr_public', 'api_key'));
46 }
47 $options = parent::get_option($config);
48 return $options;
49 }
50
96297ca2 51 /**
6b2a6c90 52 * is global_search available?
53 * @return boolean
96297ca2 54 */
dc0976b8 55 public function global_search() {
539b3d13 56 if (empty($this->flickr_account)) {
57 return false;
58 } else {
59 return true;
60 }
61 }
62
96297ca2 63 /**
64 *
6b2a6c90 65 * @global object $CFG
66 * @param int $repositoryid
67 * @param int $context
68 * @param array $options
69 * @param boolean $readonly
96297ca2 70 */
948c2860 71 public function __construct($repositoryid, $context = SITEID, $options = array(), $readonly=0) {
6b2a6c90 72 global $CFG, $SESSION;
948c2860 73 parent::__construct($repositoryid, $context, $options,$readonly);
539b3d13 74 $this->api_key = $this->get_option('api_key');
6b2a6c90 75 $this->flickr = new phpFlickr($this->api_key);
b8f46e17 76 $this->flickr_account = $this->get_option('email_address');
539b3d13 77
6b2a6c90 78 $account = optional_param('flickr_account', '', PARAM_RAW);
79 $fulltext = optional_param('flickr_fulltext', '', PARAM_RAW);
9f50fb5f 80 if (empty($fulltext)) {
81 $fulltext = optional_param('s', '', PARAM_RAW);
82 }
6b2a6c90 83 $tag = optional_param('flickr_tag', '', PARAM_RAW);
9f50fb5f 84 $license = optional_param('flickr_license', '', PARAM_RAW);
85
6b2a6c90 86 $this->sess_account = 'flickr_public_'.$this->id.'_account';
87 $this->sess_tag = 'flickr_public_'.$this->id.'_tag';
88 $this->sess_text = 'flickr_public_'.$this->id.'_text';
9f50fb5f 89
90 if (!empty($account) or !empty($fulltext) or !empty($tag) or !empty($license)) {
6b2a6c90 91 $SESSION->{$this->sess_tag} = $tag;
92 $SESSION->{$this->sess_text} = $fulltext;
9f50fb5f 93 $SESSION->{$this->sess_account} = $account;
aa48f05d 94 }
539b3d13 95 }
96297ca2 96
97 /**
6b2a6c90 98 * check flickr account
99 * @return boolean
96297ca2 100 */
dc0976b8 101 public function check_login() {
d68c527f 102 return !empty($this->flickr_account);
103 }
96297ca2 104
105 /**
106 *
6b2a6c90 107 * @param boolean $ajax
108 * @return array
96297ca2 109 */
78ff2983 110 public function print_login() {
111 if ($this->options['ajax']) {
d68c527f 112 $ret = array();
6b2a6c90 113 $fulltext = new stdclass;
114 $fulltext->label = get_string('fulltext', 'repository_flickr_public').': ';
115 $fulltext->id = 'el_fulltext';
116 $fulltext->type = 'text';
117 $fulltext->name = 'flickr_fulltext';
118
119 $tag = new stdclass;
120 $tag->label = get_string('tag', 'repository_flickr_public').': ';
121 $tag->id = 'el_tag';
122 $tag->type = 'text';
123 $tag->name = 'flickr_tag';
124
125 $email_field = new stdclass;
ca0174cc 126 $email_field->label = get_string('username', 'repository_flickr_public').': ';
127 $email_field->id = 'account';
128 $email_field->type = 'text';
129 $email_field->name = 'flickr_account';
45983072 130
16e9dccd 131 $commercial = new stdclass;
132 $commercial->label = get_string('commercialuse', 'repository_flickr_public').': ';
133 $commercial->id = 'flickr_commercial_id';
640bb8c1 134 $commercial->type = 'checkbox';
16e9dccd 135 $commercial->name = 'flickr_commercial';
640bb8c1 136 $commercial->value = 'yes';
9f50fb5f 137
16e9dccd 138 $modification = new stdclass;
139 $modification->label = get_string('modification', 'repository_flickr_public').': ';
140 $modification->id = 'flickr_modification_id';
640bb8c1 141 $modification->type = 'checkbox';
16e9dccd 142 $modification->name = 'flickr_modification';
640bb8c1 143 $modification->value = 'yes';
16e9dccd 144
145 $ret['login'] = array($fulltext, $tag, $email_field, $commercial, $modification);
45983072 146 $ret['login_btn_label'] = get_string('search');
da2946c9 147 $ret['login_btn_action'] = 'search';
d68c527f 148 return $ret;
78ff2983 149 } else {
150 echo '<table>';
151 echo '<tr><td><label>'.get_string('fulltext', 'repository_flickr_public').'</label></td>';
152 echo '<td><input type="text" name="flickr_fulltext" /></td></tr>';
153 echo '<tr><td><label>'.get_string('tag', 'repository_flickr_public').'</label></td>';
154 echo '<td><input type="text" name="flickr_tag" /></td></tr>';
155 echo '<tr><td><label>'.get_string('username', 'repository_flickr_public').'</label></td>';
156 echo '<td><input type="text" name="flickr_account" /></td></tr>';
157
16e9dccd 158 echo '<tr><td><label>'.get_string('commercialuse', 'repository_flickr_public').'</label></td>';
78ff2983 159 echo '<td>';
640bb8c1 160 echo '<input type="checkbox" name="flickr_commercial" value="yes" />';
16e9dccd 161 echo '</td></tr>';
162
163 echo '<tr><td><label>'.get_string('modification', 'repository_flickr_public').'</label></td>';
164 echo '<td>';
640bb8c1 165 echo '<input type="checkbox" name="flickr_modification" value="yes" />';
78ff2983 166 echo '</td></tr>';
167
168 echo '</table>';
169
170 echo '<input type="hidden" name="action" value="search" />';
362a728f 171 echo '<input type="submit" value="'.get_string('search', 'repository').'" />';
539b3d13 172 }
173 }
96297ca2 174
aa48f05d 175 /**
176 *
177 * @return <type>
178 */
179 public function logout() {
6b2a6c90 180 global $SESSION;
181 unset($SESSION->{$this->sess_tag});
182 unset($SESSION->{$this->sess_text});
183 unset($SESSION->{$this->sess_account});
aa48f05d 184 return $this->print_login();
185 }
186
96297ca2 187 /**
188 *
189 * @param <type> $search_text
190 * @return <type>
191 */
353d5cf3 192 public function search($search_text) {
86e68c2d 193 global $SESSION;
d1bfc05e 194 $ret = array();
6b2a6c90 195 if (!empty($this->flickr_account)) {
196 $people = $this->flickr->people_findByEmail($this->flickr_account);
197 $this->nsid = $people['nsid'];
198 }
199 if (!empty($SESSION->{$this->sess_account})) {
200 $people = $this->flickr->people_findByEmail($SESSION->{$this->sess_account});
201 $this->nsid = $people['nsid'];
202 }
203 if (empty($this->nsid)) {
204 $this->nsid = null;
d1bfc05e 205 // user specify a flickr account, but it is not valid
206 if (!empty($this->flickr_account) or !empty($SESSION->{$this->sess_account})) {
207 $ret['e'] = get_string('invalidemail', 'repository_flickr_public');
208 return $ret;
209 }
6b2a6c90 210 }
86e68c2d 211 $is_paging = optional_param('search_paging', '', PARAM_RAW);
86e68c2d 212 if (!empty($is_paging)) {
213 $page = optional_param('p', '', PARAM_INT);
fbd508b4 214 } else {
6b2a6c90 215 $page = 1;
216 }
16e9dccd 217 // including all licenses by default
218 $licenses = array(1=>1, 2, 3, 4, 5, 6, 7);
219
220 $commercial = optional_param('flickr_commercial', '', PARAM_RAW);
221 $modification = optional_param('flickr_modification', '', PARAM_RAW);
222
223 if ($commercial == 'yes') {
224 // including
225 // 4: Attribution License
226 // 5: Attribution ShareAlike
227 // 6: Attribution NoDerives
228 // 7: unknown license
229 unset($licenses[1], $licenses[2], $licenses[3]);
230 }
231 if ($modification == 'yes') {
232 // including
233 // 1: Attribution NonCommercial ShareAlike
234 // 2: Attribution NonCommercial
235 // 4: Attribution License
236 // 5: Attribution ShareAlike
237 // 7: unknown license
238 unset($licenses[3], $licenses[6]);
239 }
640bb8c1 240 //if ($modification == 'sharealike') {
16e9dccd 241 // including
242 // 1: Attribution NonCommercial ShareAlike
243 // 5: Attribution ShareAlike
640bb8c1
MD
244 //unset($licenses[2], $licenses[3], $licenses[4], $licenses[6], $licenses[7]);
245 //}
16e9dccd 246
247 $licenses = implode(',', $licenses);
248
249 if (!empty($SESSION->{$this->sess_tag}) // use tag to search
250 or !empty($SESSION->{$this->sess_text}) // use keyword to search
2f67a9b3 251 or !empty($this->nsid)/*use pre-defined accound*/) {
16e9dccd 252 $photos = $this->flickr->photos_search(array(
253 'tags'=>$SESSION->{$this->sess_tag},
254 'page'=>$page,
255 'per_page'=>24,
256 'user_id'=>$this->nsid,
257 'license'=>$licenses,
258 'text'=>$SESSION->{$this->sess_text}
259 )
260 );
fbd508b4 261 }
009a97ce 262 $ret['total'] = $photos['total'];
263 $ret['perpage'] = $photos['perpage'];
9f50fb5f 264 if (empty($photos)) {
265 $ret['list'] = array();
266 return $ret;
267 }
c438f190 268 $ret = $this->build_list($photos, $page, $ret);
269 $ret['list'] = array_filter($ret['list'], array($this, 'filter'));
270 return $ret;
fbd508b4 271 }
96297ca2 272
273 /**
274 *
5fa358d1 275 * @param string $path
276 * @param int $page
96297ca2 277 * @return <type>
278 */
5fa358d1 279 public function get_listing($path = '', $page = 1) {
539b3d13 280 $people = $this->flickr->people_findByEmail($this->flickr_account);
353d5cf3 281 $this->nsid = $people['nsid'];
5fa358d1 282 $photos = $this->flickr->people_getPublicPhotos($people['nsid'], 'original_format', 24, $page);
86e68c2d 283 $ret = array();
539b3d13 284
5fa358d1 285 return $this->build_list($photos, $page, $ret);
353d5cf3 286 }
96297ca2 287
288 /**
289 *
290 * @param <type> $photos
5fa358d1 291 * @param <type> $page
96297ca2 292 * @return <type>
293 */
5fa358d1 294 private function build_list($photos, $page = 1, &$ret) {
6b2a6c90 295 if (!empty($this->nsid)) {
296 $photos_url = $this->flickr->urls_getUserPhotos($this->nsid);
297 $ret['manage'] = $photos_url;
298 }
539b3d13 299 $ret['list'] = array();
539b3d13 300 $ret['pages'] = $photos['pages'];
5fa358d1 301 if (is_int($page) && $page <= $ret['pages']) {
302 $ret['page'] = $page;
539b3d13 303 } else {
304 $ret['page'] = 1;
305 }
353d5cf3 306 if (!empty($photos['photo'])) {
307 foreach ($photos['photo'] as $p) {
308 if(empty($p['title'])) {
309 $p['title'] = get_string('notitle', 'repository_flickr');
310 }
311 if (isset($p['originalformat'])) {
312 $format = $p['originalformat'];
313 } else {
314 $format = 'jpg';
315 }
0e51661b 316 $format = '.'.$format;
0e51661b 317 if (substr($p['title'], strlen($p['title'])-strlen($format)) != $format) {
d1bfc05e 318 // append author id
5fdc39c7 319 // $p['title'] .= '-'.$p['owner'];
d1bfc05e 320 // append file extension
4317f92f 321 $p['title'] .= $format;
0e51661b 322 }
323 $ret['list'][] = array('title'=>$p['title'], 'source'=>$p['id'],
353d5cf3 324 'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'),
8540f8f9 325 'date'=>'', 'size'=>'unknown', 'url'=>'http://www.flickr.com/photos/'.$p['owner'].'/'.$p['id']);
539b3d13 326 }
539b3d13 327 }
353d5cf3 328 return $ret;
539b3d13 329 }
dc0976b8 330
96297ca2 331 /**
99d52655 332 * Print a search form
96297ca2 333 *
99d52655 334 * @return string
96297ca2 335 */
dc0976b8 336 public function print_search() {
b763c2d9 337 $str = '';
338 $str .= '<input type="hidden" name="repo_id" value="'.$this->id.'" />';
339 $str .= '<input type="hidden" name="ctx_id" value="'.$this->context->id.'" />';
340 $str .= '<input type="hidden" name="seekey" value="'.sesskey().'" />';
341 $str .= '<label>'.get_string('fulltext', 'repository_flickr_public').': </label><br/><input name="s" value="" /><br/>';
342 $str .= '<label>'.get_string('tag', 'repository_flickr_public').'</label><br /><input type="text" name="tag" /><br />';
343 return $str;
539b3d13 344 }
dc0976b8 345
99d52655
DC
346 public function get_link($photo_id) {
347 global $CFG;
348 $result = $this->flickr->photos_getSizes($photo_id);
349 $url = '';
350 if(!empty($result[4])) {
351 $url = $result[4]['source'];
352 } elseif(!empty($result[3])) {
353 $url = $result[3]['source'];
354 } elseif(!empty($result[2])) {
355 $url = $result[2]['source'];
356 }
357 return $url;
358 }
359
96297ca2 360 /**
361 *
009a97ce 362 * @global object $CFG
363 * @param string $photo_id
364 * @param string $file
365 * @return string
96297ca2 366 */
dc0976b8 367 public function get_file($photo_id, $file = '') {
539b3d13 368 global $CFG;
369 $result = $this->flickr->photos_getSizes($photo_id);
370 $url = '';
dc0976b8 371 if (!empty($result[4])) {
539b3d13 372 $url = $result[4]['source'];
353d5cf3 373 } elseif(!empty($result[3])) {
539b3d13 374 $url = $result[3]['source'];
353d5cf3 375 } elseif(!empty($result[2])) {
539b3d13 376 $url = $result[2]['source'];
377 }
a53d4f45 378 $path = $this->prepare_file($file);
379 $fp = fopen($path, 'w');
539b3d13 380 $c = new curl;
353d5cf3 381 $c->download(array(array('url'=>$url, 'file'=>$fp)));
382
d1bfc05e 383 $watermark = get_config('flickr_public', 'watermark');
41076c58 384 if ($watermark === 'on') {
5fdc39c7 385 $img = new moodle_image($path);
41076c58
DC
386 $pathinfo = pathinfo($path);
387 $newpath = $pathinfo['dirname'] . '/wm_' . $pathinfo['basename'];
388 $img->watermark($url, array(10,10), array('ttf'=>true, 'fontsize'=>9))->saveas($newpath);
389 unlink($path);
390 $path = $newpath;
d1bfc05e 391 }
a53d4f45 392 return $path;
539b3d13 393 }
539b3d13 394
96297ca2 395 /**
792ad3aa 396 * Add Instance settings input to Moodle form
96297ca2 397 * @param <type> $
398 */
b8f46e17 399 public function instance_config_form(&$mform) {
400 $mform->addElement('text', 'email_address', get_string('emailaddress', 'repository_flickr_public'));
b7cebc8e 401 //$mform->addRule('email_address', get_string('required'), 'required', null, 'client');
b8f46e17 402 }
403
96297ca2 404 /**
792ad3aa 405 * Names of the instance settings
96297ca2 406 * @return <type>
407 */
b8f46e17 408 public static function get_instance_option_names() {
409 return array('email_address');
539b3d13 410 }
411
96297ca2 412 /**
792ad3aa 413 * Add Plugin settings input to Moodle form
96297ca2 414 * @param <type> $
415 */
daff8f50 416 public function type_config_form(&$mform) {
539b3d13 417 $api_key = get_config('flickr_public', 'api_key');
418 if (empty($api_key)) {
419 $api_key = '';
420 }
421 $strrequired = get_string('required');
d1bfc05e 422
423 $mform->addElement('checkbox', 'watermark', get_string('watermark', 'repository_flickr_public'));
424
539b3d13 425 $mform->addElement('text', 'api_key', get_string('apikey', 'repository_flickr_public'), array('value'=>$api_key,'size' => '40'));
539b3d13 426 $mform->addRule('api_key', $strrequired, 'required', null, 'client');
d1bfc05e 427
428
e49eaacb 429 $mform->addElement('static', null, '', get_string('information','repository_flickr_public'));
539b3d13 430 }
b8f46e17 431
96297ca2 432 /**
792ad3aa 433 * Names of the plugin settings
96297ca2 434 * @return <type>
435 */
1b79955a 436 public static function get_type_option_names() {
d1bfc05e 437 return array('api_key', 'watermark');
b8f46e17 438 }
439
96297ca2 440 /**
83a018ed 441 * is run when moodle administrator add the plugin
96297ca2 442 */
948c2860 443 public static function plugin_init() {
e49eaacb 444 //here we create a default instance for this type
539b3d13 445
60c366e8 446 $id = repository::static_function('flickr_public','create', 'flickr_public', 0, get_system_context(), array('name' => get_string('repositoryname', 'repository_flickr_public'),'email_address' => null), 1);
447 if (empty($id)) {
448 return false;
449 } else {
450 return true;
451 }
dbc01944 452 }
014c1ca0 453 public function supported_filetypes() {
454 return array('web_image');
455 }
41076c58
DC
456 public function supported_returntypes() {
457 return (FILE_INTERNAL | FILE_EXTERNAL);
458 }
539b3d13 459}