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