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