MDL-35101 Repository: Public Flickr fails when the picture has no license
[moodle.git] / repository / flickr_public / lib.php
CommitLineData
539b3d13 1<?php
10d53fd3
DC
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
67233725
DC
17/**
18 * This plugin is used to access flickr pictures
19 *
20 * @since 2.0
21 * @package repository_flickr_public
22 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25require_once($CFG->dirroot . '/repository/lib.php');
26require_once($CFG->libdir.'/flickrlib.php');
27require_once(dirname(__FILE__) . '/image.php');
28
539b3d13 29/**
30 * repository_flickr_public class
31 * This one is used to create public repository
49d20def
DC
32 * You can set up a public account in admin page, so everyone can access
33 * flickr photos from this plugin
539b3d13 34 *
10d53fd3 35 * @since 2.0
67233725
DC
36 * @package repository_flickr_public
37 * @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
d078f6d3 38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
539b3d13 39 */
dc0976b8 40class repository_flickr_public extends repository {
539b3d13 41 private $flickr;
42 public $photos;
43
49d20def
DC
44 /**
45 * constructor method
46 *
47 * @global object $CFG
48 * @global object $SESSION
49 * @param int $repositoryid
50 * @param int $context
51 * @param array $options
52 * @param boolean $readonly
53 */
54 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array(), $readonly=0) {
55 global $CFG, $SESSION;
56 parent::__construct($repositoryid, $context, $options,$readonly);
57 $this->api_key = $this->get_option('api_key');
58 $this->flickr = new phpFlickr($this->api_key);
59 $this->flickr_account = $this->get_option('email_address');
2c40911a 60 $this->usewatermarks = $this->get_option('usewatermarks');
49d20def
DC
61
62 $account = optional_param('flickr_account', '', PARAM_RAW);
63 $fulltext = optional_param('flickr_fulltext', '', PARAM_RAW);
64 if (empty($fulltext)) {
65 $fulltext = optional_param('s', '', PARAM_RAW);
66 }
67 $tag = optional_param('flickr_tag', '', PARAM_RAW);
68 $license = optional_param('flickr_license', '', PARAM_RAW);
69
70 $this->sess_account = 'flickr_public_'.$this->id.'_account';
71 $this->sess_tag = 'flickr_public_'.$this->id.'_tag';
72 $this->sess_text = 'flickr_public_'.$this->id.'_text';
73
74 if (!empty($account) or !empty($fulltext) or !empty($tag) or !empty($license)) {
75 $SESSION->{$this->sess_tag} = $tag;
76 $SESSION->{$this->sess_text} = $fulltext;
77 $SESSION->{$this->sess_account} = $account;
78 }
79 }
80
96297ca2 81 /**
6b2a6c90 82 * save api_key in config table
83 * @param array $options
84 * @return boolean
96297ca2 85 */
dc0976b8 86 public function set_option($options = array()) {
539b3d13 87 if (!empty($options['api_key'])) {
88 set_config('api_key', trim($options['api_key']), 'flickr_public');
89 }
90 unset($options['api_key']);
99eaca9d 91 return parent::set_option($options);
539b3d13 92 }
93
96297ca2 94 /**
6b2a6c90 95 * get api_key from config table
99eaca9d 96 *
6b2a6c90 97 * @param string $config
98 * @return mixed
96297ca2 99 */
dc0976b8 100 public function get_option($config = '') {
101 if ($config==='api_key') {
539b3d13 102 return trim(get_config('flickr_public', 'api_key'));
103 } else {
104 $options['api_key'] = trim(get_config('flickr_public', 'api_key'));
105 }
99eaca9d 106 return parent::get_option($config);
539b3d13 107 }
108
96297ca2 109 /**
6b2a6c90 110 * is global_search available?
99eaca9d 111 *
6b2a6c90 112 * @return boolean
96297ca2 113 */
dc0976b8 114 public function global_search() {
539b3d13 115 if (empty($this->flickr_account)) {
116 return false;
117 } else {
118 return true;
119 }
120 }
121
96297ca2 122 /**
49d20def 123 * check if flickr account
6b2a6c90 124 * @return boolean
96297ca2 125 */
dc0976b8 126 public function check_login() {
d68c527f 127 return !empty($this->flickr_account);
128 }
96297ca2 129
130 /**
99eaca9d 131 * construct login form
96297ca2 132 *
6b2a6c90 133 * @param boolean $ajax
134 * @return array
96297ca2 135 */
78ff2983 136 public function print_login() {
137 if ($this->options['ajax']) {
d68c527f 138 $ret = array();
6bdfef5d 139 $fulltext = new stdClass();
6b2a6c90 140 $fulltext->label = get_string('fulltext', 'repository_flickr_public').': ';
141 $fulltext->id = 'el_fulltext';
142 $fulltext->type = 'text';
143 $fulltext->name = 'flickr_fulltext';
144
6bdfef5d 145 $tag = new stdClass();
6b2a6c90 146 $tag->label = get_string('tag', 'repository_flickr_public').': ';
147 $tag->id = 'el_tag';
148 $tag->type = 'text';
149 $tag->name = 'flickr_tag';
150
6bdfef5d 151 $email_field = new stdClass();
ca0174cc 152 $email_field->label = get_string('username', 'repository_flickr_public').': ';
153 $email_field->id = 'account';
154 $email_field->type = 'text';
155 $email_field->name = 'flickr_account';
45983072 156
6bdfef5d 157 $commercial = new stdClass();
16e9dccd 158 $commercial->label = get_string('commercialuse', 'repository_flickr_public').': ';
159 $commercial->id = 'flickr_commercial_id';
640bb8c1 160 $commercial->type = 'checkbox';
16e9dccd 161 $commercial->name = 'flickr_commercial';
640bb8c1 162 $commercial->value = 'yes';
9f50fb5f 163
6bdfef5d 164 $modification = new stdClass();
16e9dccd 165 $modification->label = get_string('modification', 'repository_flickr_public').': ';
166 $modification->id = 'flickr_modification_id';
640bb8c1 167 $modification->type = 'checkbox';
16e9dccd 168 $modification->name = 'flickr_modification';
640bb8c1 169 $modification->value = 'yes';
16e9dccd 170
171 $ret['login'] = array($fulltext, $tag, $email_field, $commercial, $modification);
45983072 172 $ret['login_btn_label'] = get_string('search');
da2946c9 173 $ret['login_btn_action'] = 'search';
d68c527f 174 return $ret;
78ff2983 175 } else {
176 echo '<table>';
177 echo '<tr><td><label>'.get_string('fulltext', 'repository_flickr_public').'</label></td>';
178 echo '<td><input type="text" name="flickr_fulltext" /></td></tr>';
179 echo '<tr><td><label>'.get_string('tag', 'repository_flickr_public').'</label></td>';
180 echo '<td><input type="text" name="flickr_tag" /></td></tr>';
181 echo '<tr><td><label>'.get_string('username', 'repository_flickr_public').'</label></td>';
182 echo '<td><input type="text" name="flickr_account" /></td></tr>';
183
16e9dccd 184 echo '<tr><td><label>'.get_string('commercialuse', 'repository_flickr_public').'</label></td>';
78ff2983 185 echo '<td>';
640bb8c1 186 echo '<input type="checkbox" name="flickr_commercial" value="yes" />';
16e9dccd 187 echo '</td></tr>';
188
189 echo '<tr><td><label>'.get_string('modification', 'repository_flickr_public').'</label></td>';
190 echo '<td>';
640bb8c1 191 echo '<input type="checkbox" name="flickr_modification" value="yes" />';
78ff2983 192 echo '</td></tr>';
193
194 echo '</table>';
195
196 echo '<input type="hidden" name="action" value="search" />';
362a728f 197 echo '<input type="submit" value="'.get_string('search', 'repository').'" />';
539b3d13 198 }
199 }
96297ca2 200
aa48f05d 201 /**
99eaca9d 202 * destroy session
aa48f05d 203 *
99eaca9d 204 * @return object
aa48f05d 205 */
206 public function logout() {
6b2a6c90 207 global $SESSION;
208 unset($SESSION->{$this->sess_tag});
209 unset($SESSION->{$this->sess_text});
210 unset($SESSION->{$this->sess_account});
aa48f05d 211 return $this->print_login();
212 }
213
1dce6261
DC
214 public function license4moodle ($license_id) {
215 $license = array(
668ba1e9 216 '0' => 'allrightsreserved',
1dce6261
DC
217 '1' => 'cc-nc-sa',
218 '2' => 'cc-nc',
219 '3' => 'cc-nc-nd',
220 '4' => 'cc',
221 '5' => 'cc-sa',
222 '6' => 'cc-nd',
668ba1e9 223 '7' => 'other'
1dce6261
DC
224 );
225 return $license[$license_id];
226 }
227
96297ca2 228 /**
99eaca9d 229 * search images on flickr
96297ca2 230 *
99eaca9d
DC
231 * @param string $search_text
232 * @return array
96297ca2 233 */
68a7c9a6 234 public function search($search_text, $page = 0) {
86e68c2d 235 global $SESSION;
d1bfc05e 236 $ret = array();
99eaca9d
DC
237 if (empty($page)) {
238 $page = 1;
239 }
240
6b2a6c90 241 if (!empty($this->flickr_account)) {
242 $people = $this->flickr->people_findByEmail($this->flickr_account);
243 $this->nsid = $people['nsid'];
244 }
245 if (!empty($SESSION->{$this->sess_account})) {
246 $people = $this->flickr->people_findByEmail($SESSION->{$this->sess_account});
247 $this->nsid = $people['nsid'];
248 }
249 if (empty($this->nsid)) {
250 $this->nsid = null;
d1bfc05e 251 // user specify a flickr account, but it is not valid
252 if (!empty($this->flickr_account) or !empty($SESSION->{$this->sess_account})) {
253 $ret['e'] = get_string('invalidemail', 'repository_flickr_public');
254 return $ret;
255 }
6b2a6c90 256 }
99eaca9d 257
16e9dccd 258 // including all licenses by default
259 $licenses = array(1=>1, 2, 3, 4, 5, 6, 7);
260
261 $commercial = optional_param('flickr_commercial', '', PARAM_RAW);
262 $modification = optional_param('flickr_modification', '', PARAM_RAW);
263
264 if ($commercial == 'yes') {
265 // including
266 // 4: Attribution License
267 // 5: Attribution ShareAlike
268 // 6: Attribution NoDerives
269 // 7: unknown license
270 unset($licenses[1], $licenses[2], $licenses[3]);
271 }
272 if ($modification == 'yes') {
273 // including
274 // 1: Attribution NonCommercial ShareAlike
275 // 2: Attribution NonCommercial
276 // 4: Attribution License
277 // 5: Attribution ShareAlike
278 // 7: unknown license
279 unset($licenses[3], $licenses[6]);
280 }
640bb8c1 281 //if ($modification == 'sharealike') {
16e9dccd 282 // including
283 // 1: Attribution NonCommercial ShareAlike
284 // 5: Attribution ShareAlike
640bb8c1
MD
285 //unset($licenses[2], $licenses[3], $licenses[4], $licenses[6], $licenses[7]);
286 //}
16e9dccd 287
288 $licenses = implode(',', $licenses);
289
da78b10c
DC
290 $tag = !empty($SESSION->{$this->sess_tag}) ? $SESSION->{$this->sess_tag} : null;
291 $text = !empty($SESSION->{$this->sess_text}) ? $SESSION->{$this->sess_text} : null;
292 $nsid = !empty($this->nsid) ? $this->nsid : null;
293
294 $photos = $this->flickr->photos_search(array(
295 'tags'=>$tag,
296 'page'=>$page,
297 'per_page'=>24,
298 'user_id'=>$nsid,
299 'license'=>$licenses,
300 'text'=>$text
301 )
302 );
009a97ce 303 $ret['total'] = $photos['total'];
304 $ret['perpage'] = $photos['perpage'];
9f50fb5f 305 if (empty($photos)) {
306 $ret['list'] = array();
307 return $ret;
308 }
c438f190 309 $ret = $this->build_list($photos, $page, $ret);
310 $ret['list'] = array_filter($ret['list'], array($this, 'filter'));
311 return $ret;
fbd508b4 312 }
96297ca2 313
314 /**
99eaca9d 315 * return an image list
96297ca2 316 *
5fa358d1 317 * @param string $path
318 * @param int $page
99eaca9d 319 * @return array
96297ca2 320 */
5fa358d1 321 public function get_listing($path = '', $page = 1) {
539b3d13 322 $people = $this->flickr->people_findByEmail($this->flickr_account);
353d5cf3 323 $this->nsid = $people['nsid'];
5fa358d1 324 $photos = $this->flickr->people_getPublicPhotos($people['nsid'], 'original_format', 24, $page);
86e68c2d 325 $ret = array();
539b3d13 326
5fa358d1 327 return $this->build_list($photos, $page, $ret);
353d5cf3 328 }
96297ca2 329
330 /**
99eaca9d 331 * build an image list
96297ca2 332 *
99eaca9d
DC
333 * @param array $photos
334 * @param int $page
335 * @return array
96297ca2 336 */
5fa358d1 337 private function build_list($photos, $page = 1, &$ret) {
6b2a6c90 338 if (!empty($this->nsid)) {
339 $photos_url = $this->flickr->urls_getUserPhotos($this->nsid);
340 $ret['manage'] = $photos_url;
341 }
539b3d13 342 $ret['list'] = array();
fdfb9cbe
DC
343 $ret['nosearch'] = true;
344 $ret['norefresh'] = true;
345 $ret['logouttext'] = get_string('backtosearch', 'repository_flickr_public');
539b3d13 346 $ret['pages'] = $photos['pages'];
5fa358d1 347 if (is_int($page) && $page <= $ret['pages']) {
348 $ret['page'] = $page;
539b3d13 349 } else {
350 $ret['page'] = 1;
351 }
353d5cf3 352 if (!empty($photos['photo'])) {
353 foreach ($photos['photo'] as $p) {
354 if(empty($p['title'])) {
355 $p['title'] = get_string('notitle', 'repository_flickr');
356 }
357 if (isset($p['originalformat'])) {
358 $format = $p['originalformat'];
359 } else {
360 $format = 'jpg';
361 }
0e51661b 362 $format = '.'.$format;
0e51661b 363 if (substr($p['title'], strlen($p['title'])-strlen($format)) != $format) {
d1bfc05e 364 // append author id
5fdc39c7 365 // $p['title'] .= '-'.$p['owner'];
d1bfc05e 366 // append file extension
4317f92f 367 $p['title'] .= $format;
0e51661b 368 }
1dce6261
DC
369 $ret['list'][] = array(
370 'title'=>$p['title'],
371 'source'=>$p['id'],
372 'id'=>$p['id'],
373 'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'),
374 'date'=>'',
375 'size'=>'unknown',
376 'url'=>'http://www.flickr.com/photos/'.$p['owner'].'/'.$p['id'],
377 'haslicense'=>true,
378 'hasauthor'=>true
379 );
539b3d13 380 }
539b3d13 381 }
353d5cf3 382 return $ret;
539b3d13 383 }
dc0976b8 384
96297ca2 385 /**
99d52655 386 * Print a search form
96297ca2 387 *
99d52655 388 * @return string
96297ca2 389 */
dc0976b8 390 public function print_search() {
b763c2d9 391 $str = '';
392 $str .= '<input type="hidden" name="repo_id" value="'.$this->id.'" />';
393 $str .= '<input type="hidden" name="ctx_id" value="'.$this->context->id.'" />';
394 $str .= '<input type="hidden" name="seekey" value="'.sesskey().'" />';
395 $str .= '<label>'.get_string('fulltext', 'repository_flickr_public').': </label><br/><input name="s" value="" /><br/>';
1238246b 396 $str .= '<label>'.get_string('tag', 'repository_flickr_public').'</label><br /><input type="text" name="flickr_tag" /><br />';
b763c2d9 397 return $str;
539b3d13 398 }
dc0976b8 399
d6453211
DC
400 /**
401 * Return photo url by given photo id
402 * @param string $photoid
403 * @return string
404 */
405 private function build_photo_url($photoid) {
406 $result = $this->flickr->photos_getSizes($photoid);
99d52655
DC
407 $url = '';
408 if(!empty($result[4])) {
409 $url = $result[4]['source'];
410 } elseif(!empty($result[3])) {
411 $url = $result[3]['source'];
412 } elseif(!empty($result[2])) {
413 $url = $result[2]['source'];
414 }
415 return $url;
416 }
417
d6453211
DC
418 public function get_link($photoid) {
419 return $this->build_photo_id($photoid);
420 }
421
96297ca2 422 /**
423 *
009a97ce 424 * @global object $CFG
d6453211 425 * @param string $photoid
009a97ce 426 * @param string $file
427 * @return string
96297ca2 428 */
d6453211 429 public function get_file($photoid, $file = '') {
539b3d13 430 global $CFG;
d6453211 431 $info = $this->flickr->photos_getInfo($photoid);
2c40911a
DC
432 if ($info['owner']['realname']) {
433 $author = $info['owner']['realname'];
434 } else {
435 $author = $info['owner']['username'];
436 }
437 $copyright = get_string('author', 'repository') . ': ' . $author;
d6453211 438 $result = $this->flickr->photos_getSizes($photoid);
1dce6261
DC
439 // download link
440 $source = '';
441 // flickr photo page
539b3d13 442 $url = '';
dc0976b8 443 if (!empty($result[4])) {
1dce6261
DC
444 $source = $result[4]['source'];
445 $url = $result[4]['url'];
353d5cf3 446 } elseif(!empty($result[3])) {
1dce6261
DC
447 $source = $result[3]['source'];
448 $url = $result[3]['url'];
353d5cf3 449 } elseif(!empty($result[2])) {
1dce6261
DC
450 $source = $result[2]['source'];
451 $url = $result[2]['url'];
539b3d13 452 }
e53394ba
MG
453 $result = parent::get_file($source, $file);
454 $path = $result['path'];
2c40911a
DC
455 if (!empty($this->usewatermarks)) {
456 $img = new moodle_image($path);
457 $img->watermark($copyright, array(10,10), array('ttf'=>true, 'fontsize'=>12))->saveas($path);
458 }
1dce6261
DC
459
460 return array('path'=>$path, 'url'=>$url, 'author'=>$info['owner']['realname'], 'license'=>$this->license4moodle($info['license']));
539b3d13 461 }
539b3d13 462
96297ca2 463 /**
792ad3aa 464 * Add Instance settings input to Moodle form
99eaca9d 465 * @param object $mform
96297ca2 466 */
4a126f17 467 public static function instance_config_form($mform) {
b8f46e17 468 $mform->addElement('text', 'email_address', get_string('emailaddress', 'repository_flickr_public'));
2c40911a 469 $mform->addElement('checkbox', 'usewatermarks', get_string('watermark', 'repository_flickr_public'));
d55846d8 470 $mform->setDefault('usewatermarks', 0);
b8f46e17 471 }
472
96297ca2 473 /**
792ad3aa 474 * Names of the instance settings
99eaca9d 475 * @return array
96297ca2 476 */
b8f46e17 477 public static function get_instance_option_names() {
2c40911a 478 return array('email_address', 'usewatermarks');
539b3d13 479 }
480
96297ca2 481 /**
792ad3aa 482 * Add Plugin settings input to Moodle form
99eaca9d 483 * @param object $mform
96297ca2 484 */
68a7c9a6 485 public static function type_config_form($mform, $classname = 'repository') {
539b3d13 486 $api_key = get_config('flickr_public', 'api_key');
487 if (empty($api_key)) {
488 $api_key = '';
489 }
490 $strrequired = get_string('required');
d1bfc05e 491
539b3d13 492 $mform->addElement('text', 'api_key', get_string('apikey', 'repository_flickr_public'), array('value'=>$api_key,'size' => '40'));
539b3d13 493 $mform->addRule('api_key', $strrequired, 'required', null, 'client');
d1bfc05e 494
e49eaacb 495 $mform->addElement('static', null, '', get_string('information','repository_flickr_public'));
539b3d13 496 }
b8f46e17 497
96297ca2 498 /**
792ad3aa 499 * Names of the plugin settings
49d20def 500 * @return array
96297ca2 501 */
1b79955a 502 public static function get_type_option_names() {
a5adfa26 503 return array('api_key', 'pluginname');
b8f46e17 504 }
505
96297ca2 506 /**
83a018ed 507 * is run when moodle administrator add the plugin
96297ca2 508 */
948c2860 509 public static function plugin_init() {
e49eaacb 510 //here we create a default instance for this type
539b3d13 511
d55846d8 512 $id = repository::static_function('flickr_public','create', 'flickr_public', 0, get_system_context(), array('name'=>'', 'email_address' => null, 'usewatermarks' => false), 0);
60c366e8 513 if (empty($id)) {
514 return false;
515 } else {
516 return true;
517 }
dbc01944 518 }
014c1ca0 519 public function supported_filetypes() {
520 return array('web_image');
521 }
41076c58
DC
522 public function supported_returntypes() {
523 return (FILE_INTERNAL | FILE_EXTERNAL);
524 }
d6453211
DC
525
526 /**
527 * Return the source information
528 *
529 * @param string $photoid photo id
530 * @return string|null
531 */
532 public function get_file_source_info($photoid) {
533 return $this->build_photo_url($photoid);
534 }
539b3d13 535}