Updated the HEAD build version to 20090619
[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" />';
185 echo '<input type="submit" value="Search" />';
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();
9f50fb5f 250 if (empty($photos)) {
251 $ret['list'] = array();
252 return $ret;
253 }
c438f190 254 $ret = $this->build_list($photos, $page, $ret);
255 $ret['list'] = array_filter($ret['list'], array($this, 'filter'));
256 return $ret;
fbd508b4 257 }
96297ca2 258
259 /**
260 *
5fa358d1 261 * @param string $path
262 * @param int $page
96297ca2 263 * @return <type>
264 */
5fa358d1 265 public function get_listing($path = '', $page = 1) {
539b3d13 266 $people = $this->flickr->people_findByEmail($this->flickr_account);
353d5cf3 267 $this->nsid = $people['nsid'];
5fa358d1 268 $photos = $this->flickr->people_getPublicPhotos($people['nsid'], 'original_format', 24, $page);
86e68c2d 269 $ret = array();
539b3d13 270
5fa358d1 271 return $this->build_list($photos, $page, $ret);
353d5cf3 272 }
96297ca2 273
274 /**
275 *
276 * @param <type> $photos
5fa358d1 277 * @param <type> $page
96297ca2 278 * @return <type>
279 */
5fa358d1 280 private function build_list($photos, $page = 1, &$ret) {
6b2a6c90 281 if (!empty($this->nsid)) {
282 $photos_url = $this->flickr->urls_getUserPhotos($this->nsid);
283 $ret['manage'] = $photos_url;
284 }
539b3d13 285 $ret['list'] = array();
539b3d13 286 $ret['pages'] = $photos['pages'];
5fa358d1 287 if (is_int($page) && $page <= $ret['pages']) {
288 $ret['page'] = $page;
539b3d13 289 } else {
290 $ret['page'] = 1;
291 }
353d5cf3 292 if (!empty($photos['photo'])) {
293 foreach ($photos['photo'] as $p) {
294 if(empty($p['title'])) {
295 $p['title'] = get_string('notitle', 'repository_flickr');
296 }
297 if (isset($p['originalformat'])) {
298 $format = $p['originalformat'];
299 } else {
300 $format = 'jpg';
301 }
0e51661b 302 $format = '.'.$format;
303 // append extensions to the files
304 if (substr($p['title'], strlen($p['title'])-strlen($format)) != $format) {
305 $p['title'] .= $format;
306 }
307 $ret['list'][] = array('title'=>$p['title'], 'source'=>$p['id'],
353d5cf3 308 'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'),
8540f8f9 309 'date'=>'', 'size'=>'unknown', 'url'=>'http://www.flickr.com/photos/'.$p['owner'].'/'.$p['id']);
539b3d13 310 }
539b3d13 311 }
353d5cf3 312 return $ret;
539b3d13 313 }
dc0976b8 314
96297ca2 315 /**
316 *
317 * @return <type>
318 */
dc0976b8 319 public function print_listing() {
539b3d13 320 return false;
321 }
dc0976b8 322
96297ca2 323 /**
324 *
325 * @return <type>
326 */
dc0976b8 327 public function print_search() {
b763c2d9 328 $str = '';
329 $str .= '<input type="hidden" name="repo_id" value="'.$this->id.'" />';
330 $str .= '<input type="hidden" name="ctx_id" value="'.$this->context->id.'" />';
331 $str .= '<input type="hidden" name="seekey" value="'.sesskey().'" />';
332 $str .= '<label>'.get_string('fulltext', 'repository_flickr_public').': </label><br/><input name="s" value="" /><br/>';
333 $str .= '<label>'.get_string('tag', 'repository_flickr_public').'</label><br /><input type="text" name="tag" /><br />';
334 return $str;
539b3d13 335 }
dc0976b8 336
96297ca2 337 /**
338 *
339 * @global <type> $CFG
340 * @param <type> $photo_id
341 * @param <type> $file
342 * @return <type>
343 */
dc0976b8 344 public function get_file($photo_id, $file = '') {
539b3d13 345 global $CFG;
346 $result = $this->flickr->photos_getSizes($photo_id);
347 $url = '';
dc0976b8 348 if (!empty($result[4])) {
539b3d13 349 $url = $result[4]['source'];
353d5cf3 350 } elseif(!empty($result[3])) {
539b3d13 351 $url = $result[3]['source'];
353d5cf3 352 } elseif(!empty($result[2])) {
539b3d13 353 $url = $result[2]['source'];
354 }
a53d4f45 355 $path = $this->prepare_file($file);
356 $fp = fopen($path, 'w');
539b3d13 357 $c = new curl;
353d5cf3 358 $c->download(array(array('url'=>$url, 'file'=>$fp)));
359
a53d4f45 360 return $path;
539b3d13 361 }
539b3d13 362
96297ca2 363 /**
792ad3aa 364 * Add Instance settings input to Moodle form
96297ca2 365 * @param <type> $
366 */
b8f46e17 367 public function instance_config_form(&$mform) {
368 $mform->addElement('text', 'email_address', get_string('emailaddress', 'repository_flickr_public'));
b7cebc8e 369 //$mform->addRule('email_address', get_string('required'), 'required', null, 'client');
b8f46e17 370 }
371
96297ca2 372 /**
792ad3aa 373 * Names of the instance settings
96297ca2 374 * @return <type>
375 */
b8f46e17 376 public static function get_instance_option_names() {
377 return array('email_address');
539b3d13 378 }
379
96297ca2 380 /**
792ad3aa 381 * Add Plugin settings input to Moodle form
96297ca2 382 * @param <type> $
383 */
daff8f50 384 public function type_config_form(&$mform) {
539b3d13 385 $api_key = get_config('flickr_public', 'api_key');
386 if (empty($api_key)) {
387 $api_key = '';
388 }
389 $strrequired = get_string('required');
390 $mform->addElement('text', 'api_key', get_string('apikey', 'repository_flickr_public'), array('value'=>$api_key,'size' => '40'));
539b3d13 391 $mform->addRule('api_key', $strrequired, 'required', null, 'client');
e49eaacb 392 $mform->addElement('static', null, '', get_string('information','repository_flickr_public'));
539b3d13 393 }
b8f46e17 394
96297ca2 395 /**
792ad3aa 396 * Names of the plugin settings
96297ca2 397 * @return <type>
398 */
1b79955a 399 public static function get_type_option_names() {
b8f46e17 400 return array('api_key');
401 }
402
96297ca2 403 /**
83a018ed 404 * is run when moodle administrator add the plugin
96297ca2 405 */
948c2860 406 public static function plugin_init() {
e49eaacb 407 //here we create a default instance for this type
539b3d13 408
60c366e8 409 $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);
410 if (empty($id)) {
411 return false;
412 } else {
413 return true;
414 }
dbc01944 415 }
014c1ca0 416 public function supported_filetypes() {
417 return array('web_image');
418 }
539b3d13 419}