"MDL-19180, clean up repository url download plugin, and make it support non-js file...
[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>
9 * @version $Id$
10 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
11 */
12
13require_once($CFG->libdir.'/flickrlib.php');
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 $this->sess_license = 'flickr_public_'.$this->id.'_license';
90
91 if (!empty($account) or !empty($fulltext) or !empty($tag) or !empty($license)) {
6b2a6c90 92 $SESSION->{$this->sess_tag} = $tag;
93 $SESSION->{$this->sess_text} = $fulltext;
9f50fb5f 94 $SESSION->{$this->sess_account} = $account;
95 $SESSION->{$this->sess_license} = $license;
aa48f05d 96 }
539b3d13 97 }
96297ca2 98
99 /**
6b2a6c90 100 * check flickr account
101 * @return boolean
96297ca2 102 */
dc0976b8 103 public function check_login() {
d68c527f 104 return !empty($this->flickr_account);
105 }
96297ca2 106
107 /**
108 *
6b2a6c90 109 * @param boolean $ajax
110 * @return array
96297ca2 111 */
78ff2983 112 public function print_login() {
113 if ($this->options['ajax']) {
d68c527f 114 $ret = array();
6b2a6c90 115 $fulltext = new stdclass;
116 $fulltext->label = get_string('fulltext', 'repository_flickr_public').': ';
117 $fulltext->id = 'el_fulltext';
118 $fulltext->type = 'text';
119 $fulltext->name = 'flickr_fulltext';
120
121 $tag = new stdclass;
122 $tag->label = get_string('tag', 'repository_flickr_public').': ';
123 $tag->id = 'el_tag';
124 $tag->type = 'text';
125 $tag->name = 'flickr_tag';
126
127 $email_field = new stdclass;
ca0174cc 128 $email_field->label = get_string('username', 'repository_flickr_public').': ';
129 $email_field->id = 'account';
130 $email_field->type = 'text';
131 $email_field->name = 'flickr_account';
45983072 132
9f50fb5f 133 $license = new stdclass;
134 $license->label = get_string('license', 'repository_flickr_public').': ';
135 $license->id = 'flickr_license_type';
136 $license->type = 'radio';
137 $license->name = 'flickr_license';
138 // all -> all licenses
139 // cc -> creative commons
140 // ccc -> reeative commons commercial
141 $license->value = implode('|', array('all', 1, 2, 3, 4, 5, 6));
142 $license->value_label = implode('|', array(
143 get_string('all', 'repository_flickr_public'),
144 get_string('by-nc-sa', 'repository_flickr_public'),
145 get_string('by-nc', 'repository_flickr_public'),
146 get_string('by-nc-nd', 'repository_flickr_public'),
147 get_string('by', 'repository_flickr_public'),
148 get_string('by-sa', 'repository_flickr_public'),
149 get_string('by-nd', 'repository_flickr_public')
150 ));
151
152 $ret['login'] = array($fulltext, $tag, $email_field, $license);
45983072 153 $ret['login_btn_label'] = get_string('search');
da2946c9 154 $ret['login_btn_action'] = 'search';
d68c527f 155 return $ret;
78ff2983 156 } else {
157 echo '<table>';
158 echo '<tr><td><label>'.get_string('fulltext', 'repository_flickr_public').'</label></td>';
159 echo '<td><input type="text" name="flickr_fulltext" /></td></tr>';
160 echo '<tr><td><label>'.get_string('tag', 'repository_flickr_public').'</label></td>';
161 echo '<td><input type="text" name="flickr_tag" /></td></tr>';
162 echo '<tr><td><label>'.get_string('username', 'repository_flickr_public').'</label></td>';
163 echo '<td><input type="text" name="flickr_account" /></td></tr>';
164
165 echo '<tr><td><label>'.get_string('license', 'repository_flickr_public').'</label></td>';
166 echo '<td>';
167 echo '<input type="radio" name="flickr_license" value="all" /> '.get_string('all', 'repository_flickr_public');
168 echo '<br />';
169 echo '<input type="radio" name="flickr_license" value="1" /> '.get_string('by-nc-sa', 'repository_flickr_public');
170 echo '<br />';
171 echo '<input type="radio" name="flickr_license" value="2" /> '.get_string('by-nc', 'repository_flickr_public');
172 echo '<br />';
173 echo '<input type="radio" name="flickr_license" value="3" /> '.get_string('by-nc-nd', 'repository_flickr_public');
174 echo '<br />';
175 echo '<input type="radio" name="flickr_license" value="4" /> '.get_string('by', 'repository_flickr_public');
176 echo '<br />';
177 echo '<input type="radio" name="flickr_license" value="5" /> '.get_string('by-sa', 'repository_flickr_public');
178 echo '<br />';
179 echo '<input type="radio" name="flickr_license" value="6" /> '.get_string('by-nd', 'repository_flickr_public');
180 echo '</td></tr>';
181
182 echo '</table>';
183
184 echo '<input type="hidden" name="action" value="search" />';
362a728f 185 echo '<input type="submit" value="'.get_string('search', 'repository').'" />';
539b3d13 186 }
187 }
96297ca2 188
aa48f05d 189 /**
190 *
191 * @return <type>
192 */
193 public function logout() {
6b2a6c90 194 global $SESSION;
195 unset($SESSION->{$this->sess_tag});
196 unset($SESSION->{$this->sess_text});
197 unset($SESSION->{$this->sess_account});
aa48f05d 198 return $this->print_login();
199 }
200
96297ca2 201 /**
202 *
203 * @param <type> $search_text
204 * @return <type>
205 */
353d5cf3 206 public function search($search_text) {
86e68c2d 207 global $SESSION;
6b2a6c90 208 if (!empty($this->flickr_account)) {
209 $people = $this->flickr->people_findByEmail($this->flickr_account);
210 $this->nsid = $people['nsid'];
211 }
212 if (!empty($SESSION->{$this->sess_account})) {
213 $people = $this->flickr->people_findByEmail($SESSION->{$this->sess_account});
214 $this->nsid = $people['nsid'];
215 }
216 if (empty($this->nsid)) {
217 $this->nsid = null;
218 }
86e68c2d 219 $is_paging = optional_param('search_paging', '', PARAM_RAW);
86e68c2d 220 if (!empty($is_paging)) {
221 $page = optional_param('p', '', PARAM_INT);
fbd508b4 222 } else {
6b2a6c90 223 $page = 1;
224 }
225 if (!empty($SESSION->{$this->sess_tag}) or !empty($SESSION->{$this->sess_text})
9f50fb5f 226 or !empty($SESSION->{$this->sess_account}) or !empty($this->nsid))
227 {
228 if ( empty($SESSION->{$this->sess_license}) or $SESSION->{$this->sess_license}=='all') {
229 $photos = $this->flickr->photos_search(array(
230 'tags'=>$SESSION->{$this->sess_tag},
231 'page'=>$page,
232 'per_page'=>24,
233 'user_id'=>$this->nsid,
234 'text'=>$SESSION->{$this->sess_text}
235 )
236 );
237 } else {
238 $photos = $this->flickr->photos_search(array(
239 'tags'=>$SESSION->{$this->sess_tag},
240 'page'=>$page,
241 'per_page'=>24,
242 'user_id'=>$this->nsid,
243 'license'=>$SESSION->{$this->sess_license},
244 'text'=>$SESSION->{$this->sess_text}
245 )
246 );
247 }
fbd508b4 248 }
86e68c2d 249 $ret = array();
009a97ce 250 $ret['total'] = $photos['total'];
251 $ret['perpage'] = $photos['perpage'];
9f50fb5f 252 if (empty($photos)) {
253 $ret['list'] = array();
254 return $ret;
255 }
c438f190 256 $ret = $this->build_list($photos, $page, $ret);
257 $ret['list'] = array_filter($ret['list'], array($this, 'filter'));
258 return $ret;
fbd508b4 259 }
96297ca2 260
261 /**
262 *
5fa358d1 263 * @param string $path
264 * @param int $page
96297ca2 265 * @return <type>
266 */
5fa358d1 267 public function get_listing($path = '', $page = 1) {
539b3d13 268 $people = $this->flickr->people_findByEmail($this->flickr_account);
353d5cf3 269 $this->nsid = $people['nsid'];
5fa358d1 270 $photos = $this->flickr->people_getPublicPhotos($people['nsid'], 'original_format', 24, $page);
86e68c2d 271 $ret = array();
539b3d13 272
5fa358d1 273 return $this->build_list($photos, $page, $ret);
353d5cf3 274 }
96297ca2 275
276 /**
277 *
278 * @param <type> $photos
5fa358d1 279 * @param <type> $page
96297ca2 280 * @return <type>
281 */
5fa358d1 282 private function build_list($photos, $page = 1, &$ret) {
6b2a6c90 283 if (!empty($this->nsid)) {
284 $photos_url = $this->flickr->urls_getUserPhotos($this->nsid);
285 $ret['manage'] = $photos_url;
286 }
539b3d13 287 $ret['list'] = array();
539b3d13 288 $ret['pages'] = $photos['pages'];
5fa358d1 289 if (is_int($page) && $page <= $ret['pages']) {
290 $ret['page'] = $page;
539b3d13 291 } else {
292 $ret['page'] = 1;
293 }
353d5cf3 294 if (!empty($photos['photo'])) {
295 foreach ($photos['photo'] as $p) {
296 if(empty($p['title'])) {
297 $p['title'] = get_string('notitle', 'repository_flickr');
298 }
299 if (isset($p['originalformat'])) {
300 $format = $p['originalformat'];
301 } else {
302 $format = 'jpg';
303 }
0e51661b 304 $format = '.'.$format;
305 // append extensions to the files
306 if (substr($p['title'], strlen($p['title'])-strlen($format)) != $format) {
307 $p['title'] .= $format;
308 }
309 $ret['list'][] = array('title'=>$p['title'], 'source'=>$p['id'],
353d5cf3 310 'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'),
8540f8f9 311 'date'=>'', 'size'=>'unknown', 'url'=>'http://www.flickr.com/photos/'.$p['owner'].'/'.$p['id']);
539b3d13 312 }
539b3d13 313 }
353d5cf3 314 return $ret;
539b3d13 315 }
dc0976b8 316
96297ca2 317 /**
318 *
319 * @return <type>
320 */
dc0976b8 321 public function print_search() {
b763c2d9 322 $str = '';
323 $str .= '<input type="hidden" name="repo_id" value="'.$this->id.'" />';
324 $str .= '<input type="hidden" name="ctx_id" value="'.$this->context->id.'" />';
325 $str .= '<input type="hidden" name="seekey" value="'.sesskey().'" />';
326 $str .= '<label>'.get_string('fulltext', 'repository_flickr_public').': </label><br/><input name="s" value="" /><br/>';
327 $str .= '<label>'.get_string('tag', 'repository_flickr_public').'</label><br /><input type="text" name="tag" /><br />';
328 return $str;
539b3d13 329 }
dc0976b8 330
96297ca2 331 /**
332 *
009a97ce 333 * @global object $CFG
334 * @param string $photo_id
335 * @param string $file
336 * @return string
96297ca2 337 */
dc0976b8 338 public function get_file($photo_id, $file = '') {
539b3d13 339 global $CFG;
340 $result = $this->flickr->photos_getSizes($photo_id);
341 $url = '';
dc0976b8 342 if (!empty($result[4])) {
539b3d13 343 $url = $result[4]['source'];
353d5cf3 344 } elseif(!empty($result[3])) {
539b3d13 345 $url = $result[3]['source'];
353d5cf3 346 } elseif(!empty($result[2])) {
539b3d13 347 $url = $result[2]['source'];
348 }
a53d4f45 349 $path = $this->prepare_file($file);
350 $fp = fopen($path, 'w');
539b3d13 351 $c = new curl;
353d5cf3 352 $c->download(array(array('url'=>$url, 'file'=>$fp)));
353
a53d4f45 354 return $path;
539b3d13 355 }
539b3d13 356
96297ca2 357 /**
792ad3aa 358 * Add Instance settings input to Moodle form
96297ca2 359 * @param <type> $
360 */
b8f46e17 361 public function instance_config_form(&$mform) {
362 $mform->addElement('text', 'email_address', get_string('emailaddress', 'repository_flickr_public'));
b7cebc8e 363 //$mform->addRule('email_address', get_string('required'), 'required', null, 'client');
b8f46e17 364 }
365
96297ca2 366 /**
792ad3aa 367 * Names of the instance settings
96297ca2 368 * @return <type>
369 */
b8f46e17 370 public static function get_instance_option_names() {
371 return array('email_address');
539b3d13 372 }
373
96297ca2 374 /**
792ad3aa 375 * Add Plugin settings input to Moodle form
96297ca2 376 * @param <type> $
377 */
daff8f50 378 public function type_config_form(&$mform) {
539b3d13 379 $api_key = get_config('flickr_public', 'api_key');
380 if (empty($api_key)) {
381 $api_key = '';
382 }
383 $strrequired = get_string('required');
384 $mform->addElement('text', 'api_key', get_string('apikey', 'repository_flickr_public'), array('value'=>$api_key,'size' => '40'));
539b3d13 385 $mform->addRule('api_key', $strrequired, 'required', null, 'client');
e49eaacb 386 $mform->addElement('static', null, '', get_string('information','repository_flickr_public'));
539b3d13 387 }
b8f46e17 388
96297ca2 389 /**
792ad3aa 390 * Names of the plugin settings
96297ca2 391 * @return <type>
392 */
1b79955a 393 public static function get_type_option_names() {
b8f46e17 394 return array('api_key');
395 }
396
96297ca2 397 /**
83a018ed 398 * is run when moodle administrator add the plugin
96297ca2 399 */
948c2860 400 public static function plugin_init() {
e49eaacb 401 //here we create a default instance for this type
539b3d13 402
60c366e8 403 $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);
404 if (empty($id)) {
405 return false;
406 } else {
407 return true;
408 }
dbc01944 409 }
014c1ca0 410 public function supported_filetypes() {
411 return array('web_image');
412 }
539b3d13 413}