MDL-15350: format code (mostly white spaces) + skeleton for documentation
[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
dc0976b8 15/**
16 *
17 */
18class repository_flickr_public extends repository {
539b3d13 19 private $flickr;
20 public $photos;
21
dc0976b8 22 /**
23 *
24 * @param <type> $options
25 * @return <type>
26 */
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
dc0976b8 36 /**
37 *
38 * @param <type> $config
39 * @return <type>
40 */
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
dc0976b8 51 /**
52 *
53 * @return <type>
54 */
55 public function global_search() {
539b3d13 56 if (empty($this->flickr_account)) {
57 return false;
58 } else {
59 return true;
60 }
61 }
62
dc0976b8 63 /**
64 *
65 * @global <type> $CFG
66 * @param <type> $repositoryid
67 * @param <type> $context
68 * @param <type> $options
69 */
70 public function __construct($repositoryid, $context = SITEID, $options = array()) {
d68c527f 71 global $CFG;
dc0976b8 72
539b3d13 73 $options['page'] = optional_param('p', 1, PARAM_INT);
74 parent::__construct($repositoryid, $context, $options);
75 $this->api_key = $this->get_option('api_key');
76 $this->flickr = new phpFlickr($this->api_key);
77
b8f46e17 78 $this->flickr_account = $this->get_option('email_address');
539b3d13 79
d68c527f 80 $account = optional_param('flickr_account', '', PARAM_RAW);
dc0976b8 81 if (!empty($account)) {
d68c527f 82 $people = $this->flickr->people_findByEmail($account);
dc0976b8 83 if (!empty($people)) {
d68c527f 84 $this->flickr_account = $account;
539b3d13 85 } else {
d68c527f 86 throw new repository_exception('invalidemail', 'repository_flickr_public');
539b3d13 87 }
88 }
89 }
dc0976b8 90
91 /**
92 *
93 * @return <type>
94 */
95 public function check_login() {
d68c527f 96 return !empty($this->flickr_account);
97 }
dc0976b8 98
99 /**
100 *
101 * @param <type> $ajax
102 * @return <type>
103 */
104 public function print_login($ajax = true) {
105 if ($ajax) {
d68c527f 106 $ret = array();
ca0174cc 107 $email_field->label = get_string('username', 'repository_flickr_public').': ';
108 $email_field->id = 'account';
109 $email_field->type = 'text';
110 $email_field->name = 'flickr_account';
111 $ret['login'] = array($email_field);
d68c527f 112 return $ret;
539b3d13 113 }
114 }
dc0976b8 115
116 /**
117 *
118 * @return <type>
119 */
120 public function search() {
fbd508b4 121 $people = $this->flickr->people_findByEmail($this->flickr_account);
122 $tag = optional_param('tag', '', PARAM_CLEANHTML);
123 $search = optional_param('s', '', PARAM_CLEANHTML);
dc0976b8 124 if (!empty($tag)) {
fbd508b4 125 $photos = $this->flickr->photos_search(array(
126 'tags'=>$tag
127 ));
128 } else {
129 $photos = $this->flickr->photos_search(array(
130 'user_id'=>$people['nsid'],
131 'text'=>$search));
132 }
133 $ret = array();
134 $ret['list'] = array();
135 $ret['nologin'] = true;
136 $ret['pages'] = $photos['pages'];
137 foreach ($photos['photo'] as $p) {
dc0976b8 138 if (empty($p['title'])) {
fbd508b4 139 $p['title'] = get_string('notitle', 'repository_flickr_public');
140 }
141 if (isset($p['originalformat'])) {
142 $format = $p['originalformat'];
143 } else {
144 $format = 'jpg';
145 }
146 $ret['list'][] =
147 array('title'=>$p['title'].'.'.$format,'source'=>$p['id'],'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'), 'date'=>'', 'size'=>'unknown', 'url'=>'http://www.flickr.com/photos/'.$p['owner'].'/'.$p['id']);
148 }
dc0976b8 149 if (empty($ret)) {
fbd508b4 150 throw new repository_exception('nullphotolist', 'repository_flickr_public');
151 } else {
152 return $ret;
153 }
154 }
dc0976b8 155
156 /**
157 *
158 * @param <type> $path
159 * @param <type> $search
160 * @return <type>
161 */
162 public function get_listing($path = '1', $search = '') {
539b3d13 163 $people = $this->flickr->people_findByEmail($this->flickr_account);
164 $photos_url = $this->flickr->urls_getUserPhotos($people['nsid']);
165
fbd508b4 166 $photos = $this->flickr->people_getPublicPhotos($people['nsid'], 'original_format', 25, $path);
539b3d13 167
168 $ret = array();
169 $ret['manage'] = $photos_url;
170 $ret['list'] = array();
171 $ret['nologin'] = true;
172 $ret['pages'] = $photos['pages'];
dc0976b8 173 if (is_int($path) && $path <= $ret['pages']) {
539b3d13 174 $ret['page'] = $path;
175 } else {
176 $ret['page'] = 1;
177 }
178 foreach ($photos['photo'] as $p) {
dc0976b8 179 if (empty($p['title'])) {
539b3d13 180 $p['title'] = get_string('notitle', 'repository_flickr_public');
181 }
182 if (isset($p['originalformat'])) {
183 $format = $p['originalformat'];
184 } else {
185 $format = 'jpg';
186 }
187 $ret['list'][] =
188 array('title'=>$p['title'].'.'.$format,'source'=>$p['id'],'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'), 'date'=>'', 'size'=>'unknown', 'url'=>$photos_url.$p['id']);
189 }
dc0976b8 190 if (empty($ret)) {
539b3d13 191 throw new repository_exception('nullphotolist', 'repository_flickr_public');
192 } else {
193 return $ret;
194 }
195 }
dc0976b8 196
197 /**
198 *
199 * @return <type>
200 */
201 public function print_listing() {
539b3d13 202 return false;
203 }
dc0976b8 204
205 /**
206 *
207 * @return <type>
208 */
209 public function print_search() {
2b9feb5f 210 parent::print_search();
fbd508b4 211 echo '<label>Keyword: </label><input type="text" name="s" /><br />';
212 echo '<label>Tag: </label><input type="text" name="tag" /><br />';
2b9feb5f 213 return true;
539b3d13 214 }
dc0976b8 215
216 /**
217 *
218 * @global <type> $CFG
219 * @param <type> $photo_id
220 * @param <type> $file
221 * @return <type>
222 */
223 public function get_file($photo_id, $file = '') {
539b3d13 224 global $CFG;
dc0976b8 225
539b3d13 226 $result = $this->flickr->photos_getSizes($photo_id);
227 $url = '';
dc0976b8 228 if (!empty($result[4])) {
539b3d13 229 $url = $result[4]['source'];
dc0976b8 230 } elseif (!empty($result[3])) {
539b3d13 231 $url = $result[3]['source'];
dc0976b8 232 } elseif (!empty($result[2])) {
539b3d13 233 $url = $result[2]['source'];
234 }
235 if (!file_exists($CFG->dataroot.'/repository/download')) {
236 mkdir($CFG->dataroot.'/repository/download/', 0777, true);
237 }
dc0976b8 238 if (is_dir($CFG->dataroot.'/repository/download')) {
539b3d13 239 $dir = $CFG->dataroot.'/repository/download/';
240 }
241
dc0976b8 242 if (empty($file)) {
539b3d13 243 $file = $photo_id.'_'.time().'.jpg';
244 }
dc0976b8 245 if (file_exists($dir.$file)) {
539b3d13 246 $file = uniqid('m').$file;
247 }
248 $fp = fopen($dir.$file, 'w');
249 $c = new curl;
250 $c->download(array(
251 array('url'=>$url, 'file'=>$fp)
252 ));
253 return $dir.$file;
254 }
dc0976b8 255
256 /**
257 *
258 * @return <type>
259 */
539b3d13 260 public static function has_admin_config() {
261 return true;
262 }
263
dc0976b8 264 /**
265 *
266 * @return <type>
267 */
539b3d13 268 public static function has_multiple_instances() {
269 return true;
270 }
271
dc0976b8 272 /**
273 *
274 * @return <type>
275 */
539b3d13 276 public static function has_instance_config() {
b8f46e17 277 return true;
278 }
279
dc0976b8 280 /**
281 *
282 * @param <type> $
283 */
b8f46e17 284 public function instance_config_form(&$mform) {
285 $mform->addElement('text', 'email_address', get_string('emailaddress', 'repository_flickr_public'));
286 //$mform->addRule('email_address', get_string('required'), 'required', null, 'client');
287 }
288
dc0976b8 289 /**
290 *
291 * @return <type>
292 */
b8f46e17 293 public static function get_instance_option_names() {
294 return array('email_address');
539b3d13 295 }
296
dc0976b8 297 /**
298 *
299 * @param <type> $
300 */
539b3d13 301 public function admin_config_form(&$mform) {
302 $api_key = get_config('flickr_public', 'api_key');
303 if (empty($api_key)) {
304 $api_key = '';
305 }
306 $strrequired = get_string('required');
307 $mform->addElement('text', 'api_key', get_string('apikey', 'repository_flickr_public'), array('value'=>$api_key,'size' => '40'));
539b3d13 308 $mform->addRule('api_key', $strrequired, 'required', null, 'client');
309 }
b8f46e17 310
dc0976b8 311 /**
312 *
313 * @return <type>
314 */
b8f46e17 315 public static function get_admin_option_names() {
316 return array('api_key');
317 }
318
dc0976b8 319 /**
320 *
321 */
b8f46e17 322 public static function type_init() {
323 //here we create a default instances for this type
539b3d13 324 }
325
326}
327