"MDL-16698, use non-js filepicker to pick up files"
[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 */
dc0976b8 112 public function print_login($ajax = true) {
113 if ($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;
539b3d13 156 }
157 }
96297ca2 158
aa48f05d 159 /**
160 *
161 * @return <type>
162 */
163 public function logout() {
6b2a6c90 164 global $SESSION;
165 unset($SESSION->{$this->sess_tag});
166 unset($SESSION->{$this->sess_text});
167 unset($SESSION->{$this->sess_account});
aa48f05d 168 return $this->print_login();
169 }
170
96297ca2 171 /**
172 *
173 * @param <type> $search_text
174 * @return <type>
175 */
353d5cf3 176 public function search($search_text) {
86e68c2d 177 global $SESSION;
6b2a6c90 178 if (!empty($this->flickr_account)) {
179 $people = $this->flickr->people_findByEmail($this->flickr_account);
180 $this->nsid = $people['nsid'];
181 }
182 if (!empty($SESSION->{$this->sess_account})) {
183 $people = $this->flickr->people_findByEmail($SESSION->{$this->sess_account});
184 $this->nsid = $people['nsid'];
185 }
186 if (empty($this->nsid)) {
187 $this->nsid = null;
188 }
86e68c2d 189 $is_paging = optional_param('search_paging', '', PARAM_RAW);
86e68c2d 190 if (!empty($is_paging)) {
191 $page = optional_param('p', '', PARAM_INT);
fbd508b4 192 } else {
6b2a6c90 193 $page = 1;
194 }
195 if (!empty($SESSION->{$this->sess_tag}) or !empty($SESSION->{$this->sess_text})
9f50fb5f 196 or !empty($SESSION->{$this->sess_account}) or !empty($this->nsid))
197 {
198 if ( empty($SESSION->{$this->sess_license}) or $SESSION->{$this->sess_license}=='all') {
199 $photos = $this->flickr->photos_search(array(
200 'tags'=>$SESSION->{$this->sess_tag},
201 'page'=>$page,
202 'per_page'=>24,
203 'user_id'=>$this->nsid,
204 'text'=>$SESSION->{$this->sess_text}
205 )
206 );
207 } else {
208 $photos = $this->flickr->photos_search(array(
209 'tags'=>$SESSION->{$this->sess_tag},
210 'page'=>$page,
211 'per_page'=>24,
212 'user_id'=>$this->nsid,
213 'license'=>$SESSION->{$this->sess_license},
214 'text'=>$SESSION->{$this->sess_text}
215 )
216 );
217 }
fbd508b4 218 }
86e68c2d 219 $ret = array();
9f50fb5f 220 if (empty($photos)) {
221 $ret['list'] = array();
222 return $ret;
223 }
c438f190 224 $ret = $this->build_list($photos, $page, $ret);
225 $ret['list'] = array_filter($ret['list'], array($this, 'filter'));
226 return $ret;
fbd508b4 227 }
96297ca2 228
229 /**
230 *
5fa358d1 231 * @param string $path
232 * @param int $page
96297ca2 233 * @return <type>
234 */
5fa358d1 235 public function get_listing($path = '', $page = 1) {
539b3d13 236 $people = $this->flickr->people_findByEmail($this->flickr_account);
353d5cf3 237 $this->nsid = $people['nsid'];
5fa358d1 238 $photos = $this->flickr->people_getPublicPhotos($people['nsid'], 'original_format', 24, $page);
86e68c2d 239 $ret = array();
539b3d13 240
5fa358d1 241 return $this->build_list($photos, $page, $ret);
353d5cf3 242 }
96297ca2 243
244 /**
245 *
246 * @param <type> $photos
5fa358d1 247 * @param <type> $page
96297ca2 248 * @return <type>
249 */
5fa358d1 250 private function build_list($photos, $page = 1, &$ret) {
6b2a6c90 251 if (!empty($this->nsid)) {
252 $photos_url = $this->flickr->urls_getUserPhotos($this->nsid);
253 $ret['manage'] = $photos_url;
254 }
539b3d13 255 $ret['list'] = array();
539b3d13 256 $ret['pages'] = $photos['pages'];
5fa358d1 257 if (is_int($page) && $page <= $ret['pages']) {
258 $ret['page'] = $page;
539b3d13 259 } else {
260 $ret['page'] = 1;
261 }
353d5cf3 262 if (!empty($photos['photo'])) {
263 foreach ($photos['photo'] as $p) {
264 if(empty($p['title'])) {
265 $p['title'] = get_string('notitle', 'repository_flickr');
266 }
267 if (isset($p['originalformat'])) {
268 $format = $p['originalformat'];
269 } else {
270 $format = 'jpg';
271 }
0e51661b 272 $format = '.'.$format;
273 // append extensions to the files
274 if (substr($p['title'], strlen($p['title'])-strlen($format)) != $format) {
275 $p['title'] .= $format;
276 }
277 $ret['list'][] = array('title'=>$p['title'], 'source'=>$p['id'],
353d5cf3 278 'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'),
8540f8f9 279 'date'=>'', 'size'=>'unknown', 'url'=>'http://www.flickr.com/photos/'.$p['owner'].'/'.$p['id']);
539b3d13 280 }
539b3d13 281 }
353d5cf3 282 return $ret;
539b3d13 283 }
dc0976b8 284
96297ca2 285 /**
286 *
287 * @return <type>
288 */
dc0976b8 289 public function print_listing() {
539b3d13 290 return false;
291 }
dc0976b8 292
96297ca2 293 /**
294 *
295 * @return <type>
296 */
dc0976b8 297 public function print_search() {
b763c2d9 298 $str = '';
299 $str .= '<input type="hidden" name="repo_id" value="'.$this->id.'" />';
300 $str .= '<input type="hidden" name="ctx_id" value="'.$this->context->id.'" />';
301 $str .= '<input type="hidden" name="seekey" value="'.sesskey().'" />';
302 $str .= '<label>'.get_string('fulltext', 'repository_flickr_public').': </label><br/><input name="s" value="" /><br/>';
303 $str .= '<label>'.get_string('tag', 'repository_flickr_public').'</label><br /><input type="text" name="tag" /><br />';
304 return $str;
539b3d13 305 }
dc0976b8 306
96297ca2 307 /**
308 *
309 * @global <type> $CFG
310 * @param <type> $photo_id
311 * @param <type> $file
312 * @return <type>
313 */
dc0976b8 314 public function get_file($photo_id, $file = '') {
539b3d13 315 global $CFG;
316 $result = $this->flickr->photos_getSizes($photo_id);
317 $url = '';
dc0976b8 318 if (!empty($result[4])) {
539b3d13 319 $url = $result[4]['source'];
353d5cf3 320 } elseif(!empty($result[3])) {
539b3d13 321 $url = $result[3]['source'];
353d5cf3 322 } elseif(!empty($result[2])) {
539b3d13 323 $url = $result[2]['source'];
324 }
a53d4f45 325 $path = $this->prepare_file($file);
326 $fp = fopen($path, 'w');
539b3d13 327 $c = new curl;
353d5cf3 328 $c->download(array(array('url'=>$url, 'file'=>$fp)));
329
a53d4f45 330 return $path;
539b3d13 331 }
539b3d13 332
96297ca2 333 /**
792ad3aa 334 * Add Instance settings input to Moodle form
96297ca2 335 * @param <type> $
336 */
b8f46e17 337 public function instance_config_form(&$mform) {
338 $mform->addElement('text', 'email_address', get_string('emailaddress', 'repository_flickr_public'));
b7cebc8e 339 //$mform->addRule('email_address', get_string('required'), 'required', null, 'client');
b8f46e17 340 }
341
96297ca2 342 /**
792ad3aa 343 * Names of the instance settings
96297ca2 344 * @return <type>
345 */
b8f46e17 346 public static function get_instance_option_names() {
347 return array('email_address');
539b3d13 348 }
349
96297ca2 350 /**
792ad3aa 351 * Add Plugin settings input to Moodle form
96297ca2 352 * @param <type> $
353 */
daff8f50 354 public function type_config_form(&$mform) {
539b3d13 355 $api_key = get_config('flickr_public', 'api_key');
356 if (empty($api_key)) {
357 $api_key = '';
358 }
359 $strrequired = get_string('required');
360 $mform->addElement('text', 'api_key', get_string('apikey', 'repository_flickr_public'), array('value'=>$api_key,'size' => '40'));
539b3d13 361 $mform->addRule('api_key', $strrequired, 'required', null, 'client');
e49eaacb 362 $mform->addElement('static', null, '', get_string('information','repository_flickr_public'));
539b3d13 363 }
b8f46e17 364
96297ca2 365 /**
792ad3aa 366 * Names of the plugin settings
96297ca2 367 * @return <type>
368 */
1b79955a 369 public static function get_type_option_names() {
b8f46e17 370 return array('api_key');
371 }
372
96297ca2 373 /**
83a018ed 374 * is run when moodle administrator add the plugin
96297ca2 375 */
948c2860 376 public static function plugin_init() {
e49eaacb 377 //here we create a default instance for this type
539b3d13 378
60c366e8 379 $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);
380 if (empty($id)) {
381 return false;
382 } else {
383 return true;
384 }
dbc01944 385 }
014c1ca0 386 public function supported_filetypes() {
387 return array('web_image');
388 }
539b3d13 389}