web service MDL-12886 fix empty optional description for REST server
[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);
80 $tag = optional_param('flickr_tag', '', PARAM_RAW);
81 $this->sess_account = 'flickr_public_'.$this->id.'_account';
82 $this->sess_tag = 'flickr_public_'.$this->id.'_tag';
83 $this->sess_text = 'flickr_public_'.$this->id.'_text';
84 if (!empty($account) or !empty($fulltext) or !empty($tag)) {
85 $SESSION->{$this->sess_account} = $account;
86 $SESSION->{$this->sess_tag} = $tag;
87 $SESSION->{$this->sess_text} = $fulltext;
4026d724 88 $response = $this->search($fulltext);
89 $response['search_result'] = true;
90 echo json_encode($response);
6b2a6c90 91 exit;
aa48f05d 92 }
539b3d13 93 }
96297ca2 94
95 /**
6b2a6c90 96 * check flickr account
97 * @return boolean
96297ca2 98 */
dc0976b8 99 public function check_login() {
d68c527f 100 return !empty($this->flickr_account);
101 }
96297ca2 102
103 /**
104 *
6b2a6c90 105 * @param boolean $ajax
106 * @return array
96297ca2 107 */
dc0976b8 108 public function print_login($ajax = true) {
109 if ($ajax) {
d68c527f 110 $ret = array();
6b2a6c90 111 $fulltext = new stdclass;
112 $fulltext->label = get_string('fulltext', 'repository_flickr_public').': ';
113 $fulltext->id = 'el_fulltext';
114 $fulltext->type = 'text';
115 $fulltext->name = 'flickr_fulltext';
116
117 $tag = new stdclass;
118 $tag->label = get_string('tag', 'repository_flickr_public').': ';
119 $tag->id = 'el_tag';
120 $tag->type = 'text';
121 $tag->name = 'flickr_tag';
122
123 $email_field = new stdclass;
ca0174cc 124 $email_field->label = get_string('username', 'repository_flickr_public').': ';
125 $email_field->id = 'account';
126 $email_field->type = 'text';
127 $email_field->name = 'flickr_account';
6b2a6c90 128 $ret['login'] = array($fulltext, $tag, $email_field);
d68c527f 129 return $ret;
539b3d13 130 }
131 }
96297ca2 132
aa48f05d 133 /**
134 *
135 * @return <type>
136 */
137 public function logout() {
6b2a6c90 138 global $SESSION;
139 unset($SESSION->{$this->sess_tag});
140 unset($SESSION->{$this->sess_text});
141 unset($SESSION->{$this->sess_account});
aa48f05d 142 return $this->print_login();
143 }
144
96297ca2 145 /**
146 *
147 * @param <type> $search_text
148 * @return <type>
149 */
353d5cf3 150 public function search($search_text) {
86e68c2d 151 global $SESSION;
6b2a6c90 152 if (!empty($this->flickr_account)) {
153 $people = $this->flickr->people_findByEmail($this->flickr_account);
154 $this->nsid = $people['nsid'];
155 }
156 if (!empty($SESSION->{$this->sess_account})) {
157 $people = $this->flickr->people_findByEmail($SESSION->{$this->sess_account});
158 $this->nsid = $people['nsid'];
159 }
160 if (empty($this->nsid)) {
161 $this->nsid = null;
162 }
86e68c2d 163 $is_paging = optional_param('search_paging', '', PARAM_RAW);
86e68c2d 164 if (!empty($is_paging)) {
165 $page = optional_param('p', '', PARAM_INT);
fbd508b4 166 } else {
6b2a6c90 167 $page = 1;
168 }
169 if (!empty($SESSION->{$this->sess_tag}) or !empty($SESSION->{$this->sess_text})
170 or !empty($SESSION->{$this->sess_account})
171 or !empty($this->nsid)) {
4026d724 172
fbd508b4 173 $photos = $this->flickr->photos_search(array(
6b2a6c90 174 'tags'=>$SESSION->{$this->sess_tag},
175 'page'=>$page,
8a77c949 176 'per_page'=>24,
353d5cf3 177 'user_id'=>$this->nsid,
4026d724 178 'text'=>$SESSION->{$this->sess_text}));
fbd508b4 179 }
86e68c2d 180 $ret = array();
7e33b68d 181 return $this->build_list($photos, $page, $ret);
fbd508b4 182 }
96297ca2 183
184 /**
185 *
5fa358d1 186 * @param string $path
187 * @param int $page
96297ca2 188 * @return <type>
189 */
5fa358d1 190 public function get_listing($path = '', $page = 1) {
539b3d13 191 $people = $this->flickr->people_findByEmail($this->flickr_account);
353d5cf3 192 $this->nsid = $people['nsid'];
5fa358d1 193 $photos = $this->flickr->people_getPublicPhotos($people['nsid'], 'original_format', 24, $page);
86e68c2d 194 $ret = array();
539b3d13 195
5fa358d1 196 return $this->build_list($photos, $page, $ret);
353d5cf3 197 }
96297ca2 198
199 /**
200 *
201 * @param <type> $photos
5fa358d1 202 * @param <type> $page
96297ca2 203 * @return <type>
204 */
5fa358d1 205 private function build_list($photos, $page = 1, &$ret) {
6b2a6c90 206 if (!empty($this->nsid)) {
207 $photos_url = $this->flickr->urls_getUserPhotos($this->nsid);
208 $ret['manage'] = $photos_url;
209 }
539b3d13 210 $ret['list'] = array();
539b3d13 211 $ret['pages'] = $photos['pages'];
5fa358d1 212 if (is_int($page) && $page <= $ret['pages']) {
213 $ret['page'] = $page;
539b3d13 214 } else {
215 $ret['page'] = 1;
216 }
353d5cf3 217 if (!empty($photos['photo'])) {
218 foreach ($photos['photo'] as $p) {
219 if(empty($p['title'])) {
220 $p['title'] = get_string('notitle', 'repository_flickr');
221 }
222 if (isset($p['originalformat'])) {
223 $format = $p['originalformat'];
224 } else {
225 $format = 'jpg';
226 }
227 $ret['list'][] = array('title'=>$p['title'].'.'.$format,'source'=>$p['id'],
228 'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'),
8540f8f9 229 'date'=>'', 'size'=>'unknown', 'url'=>'http://www.flickr.com/photos/'.$p['owner'].'/'.$p['id']);
539b3d13 230 }
539b3d13 231 }
353d5cf3 232 return $ret;
539b3d13 233 }
dc0976b8 234
96297ca2 235 /**
236 *
237 * @return <type>
238 */
dc0976b8 239 public function print_listing() {
539b3d13 240 return false;
241 }
dc0976b8 242
96297ca2 243 /**
244 *
245 * @return <type>
246 */
dc0976b8 247 public function print_search() {
6b2a6c90 248 echo '<input type="hidden" name="repo_id" value="'.$this->id.'" />';
249 echo '<input type="hidden" name="ctx_id" value="'.$this->context->id.'" />';
250 echo '<input type="hidden" name="seekey" value="'.sesskey().'" />';
251 echo '<label>'.get_string('fulltext', 'repository_flickr_public').': </label><br/><input name="s" value="" /><br/>';
252 echo '<label>'.get_string('tag', 'repository_flickr_public').'</label><br /><input type="text" name="tag" /><br />';
2b9feb5f 253 return true;
539b3d13 254 }
dc0976b8 255
96297ca2 256 /**
257 *
258 * @global <type> $CFG
259 * @param <type> $photo_id
260 * @param <type> $file
261 * @return <type>
262 */
dc0976b8 263 public function get_file($photo_id, $file = '') {
539b3d13 264 global $CFG;
265 $result = $this->flickr->photos_getSizes($photo_id);
266 $url = '';
dc0976b8 267 if (!empty($result[4])) {
539b3d13 268 $url = $result[4]['source'];
353d5cf3 269 } elseif(!empty($result[3])) {
539b3d13 270 $url = $result[3]['source'];
353d5cf3 271 } elseif(!empty($result[2])) {
539b3d13 272 $url = $result[2]['source'];
273 }
274 if (!file_exists($CFG->dataroot.'/repository/download')) {
275 mkdir($CFG->dataroot.'/repository/download/', 0777, true);
276 }
353d5cf3 277 if(is_dir($CFG->dataroot.'/repository/download')) {
539b3d13 278 $dir = $CFG->dataroot.'/repository/download/';
279 }
280
dc0976b8 281 if (empty($file)) {
539b3d13 282 $file = $photo_id.'_'.time().'.jpg';
283 }
dc0976b8 284 if (file_exists($dir.$file)) {
539b3d13 285 $file = uniqid('m').$file;
286 }
287 $fp = fopen($dir.$file, 'w');
288 $c = new curl;
353d5cf3 289 $c->download(array(array('url'=>$url, 'file'=>$fp)));
290
539b3d13 291 return $dir.$file;
292 }
539b3d13 293
96297ca2 294 /**
792ad3aa 295 * Add Instance settings input to Moodle form
96297ca2 296 * @param <type> $
297 */
b8f46e17 298 public function instance_config_form(&$mform) {
299 $mform->addElement('text', 'email_address', get_string('emailaddress', 'repository_flickr_public'));
b7cebc8e 300 //$mform->addRule('email_address', get_string('required'), 'required', null, 'client');
b8f46e17 301 }
302
96297ca2 303 /**
792ad3aa 304 * Names of the instance settings
96297ca2 305 * @return <type>
306 */
b8f46e17 307 public static function get_instance_option_names() {
308 return array('email_address');
539b3d13 309 }
310
96297ca2 311 /**
792ad3aa 312 * Add Plugin settings input to Moodle form
96297ca2 313 * @param <type> $
314 */
daff8f50 315 public function type_config_form(&$mform) {
539b3d13 316 $api_key = get_config('flickr_public', 'api_key');
317 if (empty($api_key)) {
318 $api_key = '';
319 }
320 $strrequired = get_string('required');
321 $mform->addElement('text', 'api_key', get_string('apikey', 'repository_flickr_public'), array('value'=>$api_key,'size' => '40'));
539b3d13 322 $mform->addRule('api_key', $strrequired, 'required', null, 'client');
e49eaacb 323 $mform->addElement('static', null, '', get_string('information','repository_flickr_public'));
539b3d13 324 }
b8f46e17 325
96297ca2 326 /**
792ad3aa 327 * Names of the plugin settings
96297ca2 328 * @return <type>
329 */
1b79955a 330 public static function get_type_option_names() {
b8f46e17 331 return array('api_key');
332 }
333
96297ca2 334 /**
83a018ed 335 * is run when moodle administrator add the plugin
96297ca2 336 */
948c2860 337 public static function plugin_init() {
e49eaacb 338 //here we create a default instance for this type
539b3d13 339
dbc01944 340 repository::static_function('flickr_public','create', 'flickr_public', 0, get_system_context(), array('name' => get_string('repositoryname', 'repository_flickr_public'),'email_address' => null),1);
341 }
014c1ca0 342 public function supported_filetypes() {
343 return array('web_image');
344 }
539b3d13 345}