"MDL-19118, added parameter to comment object"
[moodle.git] / repository / flickr_public / repository.class.php
CommitLineData
539b3d13 1<?php
10d53fd3
DC
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
99eaca9d
DC
18
19require_once($CFG->libdir.'/flickrlib.php');
20require_once(dirname(__FILE__) . '/image.php');
21
539b3d13 22/**
23 * repository_flickr_public class
24 * This one is used to create public repository
25 * You can set up a public account in admin page, so everyone can
26 * access photos in this public account
27 *
10d53fd3
DC
28 * @since 2.0
29 * @package moodlecore
30 * @subpackage repository
31 * @copyright 2009 Dongsheng Cai
539b3d13 32 * @author Dongsheng Cai <dongsheng@moodle.com>
10d53fd3 33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
539b3d13 34 */
dc0976b8 35class repository_flickr_public extends repository {
539b3d13 36 private $flickr;
37 public $photos;
38
96297ca2 39 /**
6b2a6c90 40 * save api_key in config table
41 * @param array $options
42 * @return boolean
96297ca2 43 */
dc0976b8 44 public function set_option($options = array()) {
539b3d13 45 if (!empty($options['api_key'])) {
46 set_config('api_key', trim($options['api_key']), 'flickr_public');
47 }
48 unset($options['api_key']);
99eaca9d 49 return parent::set_option($options);
539b3d13 50 }
51
96297ca2 52 /**
6b2a6c90 53 * get api_key from config table
99eaca9d 54 *
6b2a6c90 55 * @param string $config
56 * @return mixed
96297ca2 57 */
dc0976b8 58 public function get_option($config = '') {
59 if ($config==='api_key') {
539b3d13 60 return trim(get_config('flickr_public', 'api_key'));
61 } else {
62 $options['api_key'] = trim(get_config('flickr_public', 'api_key'));
63 }
99eaca9d 64 return parent::get_option($config);
539b3d13 65 }
66
96297ca2 67 /**
6b2a6c90 68 * is global_search available?
99eaca9d 69 *
6b2a6c90 70 * @return boolean
96297ca2 71 */
dc0976b8 72 public function global_search() {
539b3d13 73 if (empty($this->flickr_account)) {
74 return false;
75 } else {
76 return true;
77 }
78 }
79
96297ca2 80 /**
99eaca9d 81 * constructor method
96297ca2 82 *
6b2a6c90 83 * @global object $CFG
99eaca9d 84 * @global object $SESSION
6b2a6c90 85 * @param int $repositoryid
86 * @param int $context
87 * @param array $options
88 * @param boolean $readonly
96297ca2 89 */
447c7a19 90 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array(), $readonly=0) {
6b2a6c90 91 global $CFG, $SESSION;
948c2860 92 parent::__construct($repositoryid, $context, $options,$readonly);
539b3d13 93 $this->api_key = $this->get_option('api_key');
6b2a6c90 94 $this->flickr = new phpFlickr($this->api_key);
b8f46e17 95 $this->flickr_account = $this->get_option('email_address');
539b3d13 96
6b2a6c90 97 $account = optional_param('flickr_account', '', PARAM_RAW);
98 $fulltext = optional_param('flickr_fulltext', '', PARAM_RAW);
9f50fb5f 99 if (empty($fulltext)) {
100 $fulltext = optional_param('s', '', PARAM_RAW);
101 }
6b2a6c90 102 $tag = optional_param('flickr_tag', '', PARAM_RAW);
9f50fb5f 103 $license = optional_param('flickr_license', '', PARAM_RAW);
104
6b2a6c90 105 $this->sess_account = 'flickr_public_'.$this->id.'_account';
106 $this->sess_tag = 'flickr_public_'.$this->id.'_tag';
107 $this->sess_text = 'flickr_public_'.$this->id.'_text';
9f50fb5f 108
109 if (!empty($account) or !empty($fulltext) or !empty($tag) or !empty($license)) {
6b2a6c90 110 $SESSION->{$this->sess_tag} = $tag;
111 $SESSION->{$this->sess_text} = $fulltext;
9f50fb5f 112 $SESSION->{$this->sess_account} = $account;
aa48f05d 113 }
539b3d13 114 }
96297ca2 115
116 /**
6b2a6c90 117 * check flickr account
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();
6b2a6c90 133 $fulltext = new stdclass;
134 $fulltext->label = get_string('fulltext', 'repository_flickr_public').': ';
135 $fulltext->id = 'el_fulltext';
136 $fulltext->type = 'text';
137 $fulltext->name = 'flickr_fulltext';
138
139 $tag = new stdclass;
140 $tag->label = get_string('tag', 'repository_flickr_public').': ';
141 $tag->id = 'el_tag';
142 $tag->type = 'text';
143 $tag->name = 'flickr_tag';
144
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
16e9dccd 151 $commercial = new stdclass;
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
16e9dccd 158 $modification = new stdclass;
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
96297ca2 208 /**
99eaca9d 209 * search images on flickr
96297ca2 210 *
99eaca9d
DC
211 * @param string $search_text
212 * @return array
96297ca2 213 */
99eaca9d 214 public function search($search_text, $page = 1) {
86e68c2d 215 global $SESSION;
d1bfc05e 216 $ret = array();
99eaca9d
DC
217 if (empty($page)) {
218 $page = 1;
219 }
220
6b2a6c90 221 if (!empty($this->flickr_account)) {
222 $people = $this->flickr->people_findByEmail($this->flickr_account);
223 $this->nsid = $people['nsid'];
224 }
225 if (!empty($SESSION->{$this->sess_account})) {
226 $people = $this->flickr->people_findByEmail($SESSION->{$this->sess_account});
227 $this->nsid = $people['nsid'];
228 }
229 if (empty($this->nsid)) {
230 $this->nsid = null;
d1bfc05e 231 // user specify a flickr account, but it is not valid
232 if (!empty($this->flickr_account) or !empty($SESSION->{$this->sess_account})) {
233 $ret['e'] = get_string('invalidemail', 'repository_flickr_public');
234 return $ret;
235 }
6b2a6c90 236 }
99eaca9d 237
16e9dccd 238 // including all licenses by default
239 $licenses = array(1=>1, 2, 3, 4, 5, 6, 7);
240
241 $commercial = optional_param('flickr_commercial', '', PARAM_RAW);
242 $modification = optional_param('flickr_modification', '', PARAM_RAW);
243
244 if ($commercial == 'yes') {
245 // including
246 // 4: Attribution License
247 // 5: Attribution ShareAlike
248 // 6: Attribution NoDerives
249 // 7: unknown license
250 unset($licenses[1], $licenses[2], $licenses[3]);
251 }
252 if ($modification == 'yes') {
253 // including
254 // 1: Attribution NonCommercial ShareAlike
255 // 2: Attribution NonCommercial
256 // 4: Attribution License
257 // 5: Attribution ShareAlike
258 // 7: unknown license
259 unset($licenses[3], $licenses[6]);
260 }
640bb8c1 261 //if ($modification == 'sharealike') {
16e9dccd 262 // including
263 // 1: Attribution NonCommercial ShareAlike
264 // 5: Attribution ShareAlike
640bb8c1
MD
265 //unset($licenses[2], $licenses[3], $licenses[4], $licenses[6], $licenses[7]);
266 //}
16e9dccd 267
268 $licenses = implode(',', $licenses);
269
270 if (!empty($SESSION->{$this->sess_tag}) // use tag to search
271 or !empty($SESSION->{$this->sess_text}) // use keyword to search
2f67a9b3 272 or !empty($this->nsid)/*use pre-defined accound*/) {
16e9dccd 273 $photos = $this->flickr->photos_search(array(
274 'tags'=>$SESSION->{$this->sess_tag},
275 'page'=>$page,
276 'per_page'=>24,
277 'user_id'=>$this->nsid,
278 'license'=>$licenses,
279 'text'=>$SESSION->{$this->sess_text}
280 )
281 );
fbd508b4 282 }
009a97ce 283 $ret['total'] = $photos['total'];
284 $ret['perpage'] = $photos['perpage'];
9f50fb5f 285 if (empty($photos)) {
286 $ret['list'] = array();
287 return $ret;
288 }
c438f190 289 $ret = $this->build_list($photos, $page, $ret);
290 $ret['list'] = array_filter($ret['list'], array($this, 'filter'));
291 return $ret;
fbd508b4 292 }
96297ca2 293
294 /**
99eaca9d 295 * return an image list
96297ca2 296 *
5fa358d1 297 * @param string $path
298 * @param int $page
99eaca9d 299 * @return array
96297ca2 300 */
5fa358d1 301 public function get_listing($path = '', $page = 1) {
539b3d13 302 $people = $this->flickr->people_findByEmail($this->flickr_account);
353d5cf3 303 $this->nsid = $people['nsid'];
5fa358d1 304 $photos = $this->flickr->people_getPublicPhotos($people['nsid'], 'original_format', 24, $page);
86e68c2d 305 $ret = array();
539b3d13 306
5fa358d1 307 return $this->build_list($photos, $page, $ret);
353d5cf3 308 }
96297ca2 309
310 /**
99eaca9d 311 * build an image list
96297ca2 312 *
99eaca9d
DC
313 * @param array $photos
314 * @param int $page
315 * @return array
96297ca2 316 */
5fa358d1 317 private function build_list($photos, $page = 1, &$ret) {
6b2a6c90 318 if (!empty($this->nsid)) {
319 $photos_url = $this->flickr->urls_getUserPhotos($this->nsid);
320 $ret['manage'] = $photos_url;
321 }
539b3d13 322 $ret['list'] = array();
539b3d13 323 $ret['pages'] = $photos['pages'];
5fa358d1 324 if (is_int($page) && $page <= $ret['pages']) {
325 $ret['page'] = $page;
539b3d13 326 } else {
327 $ret['page'] = 1;
328 }
353d5cf3 329 if (!empty($photos['photo'])) {
330 foreach ($photos['photo'] as $p) {
331 if(empty($p['title'])) {
332 $p['title'] = get_string('notitle', 'repository_flickr');
333 }
334 if (isset($p['originalformat'])) {
335 $format = $p['originalformat'];
336 } else {
337 $format = 'jpg';
338 }
0e51661b 339 $format = '.'.$format;
0e51661b 340 if (substr($p['title'], strlen($p['title'])-strlen($format)) != $format) {
d1bfc05e 341 // append author id
5fdc39c7 342 // $p['title'] .= '-'.$p['owner'];
d1bfc05e 343 // append file extension
4317f92f 344 $p['title'] .= $format;
0e51661b 345 }
346 $ret['list'][] = array('title'=>$p['title'], 'source'=>$p['id'],
353d5cf3 347 'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'),
8540f8f9 348 'date'=>'', 'size'=>'unknown', 'url'=>'http://www.flickr.com/photos/'.$p['owner'].'/'.$p['id']);
539b3d13 349 }
539b3d13 350 }
353d5cf3 351 return $ret;
539b3d13 352 }
dc0976b8 353
96297ca2 354 /**
99d52655 355 * Print a search form
96297ca2 356 *
99d52655 357 * @return string
96297ca2 358 */
dc0976b8 359 public function print_search() {
b763c2d9 360 $str = '';
361 $str .= '<input type="hidden" name="repo_id" value="'.$this->id.'" />';
362 $str .= '<input type="hidden" name="ctx_id" value="'.$this->context->id.'" />';
363 $str .= '<input type="hidden" name="seekey" value="'.sesskey().'" />';
364 $str .= '<label>'.get_string('fulltext', 'repository_flickr_public').': </label><br/><input name="s" value="" /><br/>';
1238246b 365 $str .= '<label>'.get_string('tag', 'repository_flickr_public').'</label><br /><input type="text" name="flickr_tag" /><br />';
b763c2d9 366 return $str;
539b3d13 367 }
dc0976b8 368
99d52655
DC
369 public function get_link($photo_id) {
370 global $CFG;
371 $result = $this->flickr->photos_getSizes($photo_id);
372 $url = '';
373 if(!empty($result[4])) {
374 $url = $result[4]['source'];
375 } elseif(!empty($result[3])) {
376 $url = $result[3]['source'];
377 } elseif(!empty($result[2])) {
378 $url = $result[2]['source'];
379 }
380 return $url;
381 }
382
96297ca2 383 /**
384 *
009a97ce 385 * @global object $CFG
386 * @param string $photo_id
387 * @param string $file
388 * @return string
96297ca2 389 */
dc0976b8 390 public function get_file($photo_id, $file = '') {
539b3d13 391 global $CFG;
392 $result = $this->flickr->photos_getSizes($photo_id);
393 $url = '';
dc0976b8 394 if (!empty($result[4])) {
539b3d13 395 $url = $result[4]['source'];
353d5cf3 396 } elseif(!empty($result[3])) {
539b3d13 397 $url = $result[3]['source'];
353d5cf3 398 } elseif(!empty($result[2])) {
539b3d13 399 $url = $result[2]['source'];
400 }
a53d4f45 401 $path = $this->prepare_file($file);
402 $fp = fopen($path, 'w');
539b3d13 403 $c = new curl;
353d5cf3 404 $c->download(array(array('url'=>$url, 'file'=>$fp)));
405
d1bfc05e 406 $watermark = get_config('flickr_public', 'watermark');
41076c58 407 if ($watermark === 'on') {
5fdc39c7 408 $img = new moodle_image($path);
41076c58
DC
409 $pathinfo = pathinfo($path);
410 $newpath = $pathinfo['dirname'] . '/wm_' . $pathinfo['basename'];
411 $img->watermark($url, array(10,10), array('ttf'=>true, 'fontsize'=>9))->saveas($newpath);
412 unlink($path);
413 $path = $newpath;
d1bfc05e 414 }
14469892 415 return array($path, $url);
539b3d13 416 }
539b3d13 417
96297ca2 418 /**
792ad3aa 419 * Add Instance settings input to Moodle form
99eaca9d 420 * @param object $mform
96297ca2 421 */
aea5595c 422 public function instance_config_form($mform) {
b8f46e17 423 $mform->addElement('text', 'email_address', get_string('emailaddress', 'repository_flickr_public'));
b7cebc8e 424 //$mform->addRule('email_address', get_string('required'), 'required', null, 'client');
b8f46e17 425 }
426
96297ca2 427 /**
792ad3aa 428 * Names of the instance settings
99eaca9d 429 * @return array
96297ca2 430 */
b8f46e17 431 public static function get_instance_option_names() {
432 return array('email_address');
539b3d13 433 }
434
96297ca2 435 /**
792ad3aa 436 * Add Plugin settings input to Moodle form
99eaca9d 437 * @param object $mform
96297ca2 438 */
aea5595c 439 public function type_config_form($mform) {
539b3d13 440 $api_key = get_config('flickr_public', 'api_key');
441 if (empty($api_key)) {
442 $api_key = '';
443 }
444 $strrequired = get_string('required');
d1bfc05e 445
446 $mform->addElement('checkbox', 'watermark', get_string('watermark', 'repository_flickr_public'));
447
539b3d13 448 $mform->addElement('text', 'api_key', get_string('apikey', 'repository_flickr_public'), array('value'=>$api_key,'size' => '40'));
539b3d13 449 $mform->addRule('api_key', $strrequired, 'required', null, 'client');
d1bfc05e 450
451
e49eaacb 452 $mform->addElement('static', null, '', get_string('information','repository_flickr_public'));
539b3d13 453 }
b8f46e17 454
96297ca2 455 /**
792ad3aa 456 * Names of the plugin settings
99eaca9d 457 * @return array
96297ca2 458 */
1b79955a 459 public static function get_type_option_names() {
d1bfc05e 460 return array('api_key', 'watermark');
b8f46e17 461 }
462
96297ca2 463 /**
83a018ed 464 * is run when moodle administrator add the plugin
96297ca2 465 */
948c2860 466 public static function plugin_init() {
e49eaacb 467 //here we create a default instance for this type
539b3d13 468
60c366e8 469 $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);
470 if (empty($id)) {
471 return false;
472 } else {
473 return true;
474 }
dbc01944 475 }
014c1ca0 476 public function supported_filetypes() {
477 return array('web_image');
478 }
41076c58
DC
479 public function supported_returntypes() {
480 return (FILE_INTERNAL | FILE_EXTERNAL);
481 }
539b3d13 482}