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