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