Updated the HEAD build version to 20081121
[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 /**
23 *
24 * @param <type> $options
25 * @return <type>
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 /**
37 *
38 * @param <type> $config
39 * @return <type>
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 /**
52 *
53 * @return <type>
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 *
65 * @global <type> $CFG
66 * @param <type> $repositoryid
67 * @param <type> $context
68 * @param <type> $options
69 * @param <type> $readonly
70 */
948c2860 71 public function __construct($repositoryid, $context = SITEID, $options = array(), $readonly=0) {
d68c527f 72 global $CFG;
353d5cf3 73 $options['page'] = optional_param('p', 1, PARAM_INT);
948c2860 74 parent::__construct($repositoryid, $context, $options,$readonly);
539b3d13 75 $this->api_key = $this->get_option('api_key');
76 $this->flickr = new phpFlickr($this->api_key);
b8f46e17 77 $this->flickr_account = $this->get_option('email_address');
539b3d13 78
353d5cf3 79 // when flickr account hasn't been set by admin, user can
80 // submit a flickr account here.
d68c527f 81 $account = optional_param('flickr_account', '', PARAM_RAW);
dc0976b8 82 if (!empty($account)) {
d68c527f 83 $people = $this->flickr->people_findByEmail($account);
dc0976b8 84 if (!empty($people)) {
d68c527f 85 $this->flickr_account = $account;
aa48f05d 86 set_user_preference('flickr_mail_'.$this->id, $account);
539b3d13 87 } else {
d68c527f 88 throw new repository_exception('invalidemail', 'repository_flickr_public');
539b3d13 89 }
90 }
aa48f05d 91
92 $user_mail = get_user_preferences('flickr_mail_'.$this->id, '');
93 if (empty($this->flickr_account) && !empty($user_mail)) {
94 $this->flickr_account = $user_mail;
95 }
539b3d13 96 }
96297ca2 97
98 /**
99 *
100 * @return <type>
101 */
dc0976b8 102 public function check_login() {
d68c527f 103 return !empty($this->flickr_account);
104 }
96297ca2 105
106 /**
107 *
108 * @param <type> $ajax
109 * @return <type>
110 */
dc0976b8 111 public function print_login($ajax = true) {
112 if ($ajax) {
d68c527f 113 $ret = array();
ca0174cc 114 $email_field->label = get_string('username', 'repository_flickr_public').': ';
115 $email_field->id = 'account';
116 $email_field->type = 'text';
117 $email_field->name = 'flickr_account';
118 $ret['login'] = array($email_field);
d68c527f 119 return $ret;
539b3d13 120 }
121 }
96297ca2 122
aa48f05d 123 /**
124 *
125 * @return <type>
126 */
127 public function logout() {
128 set_user_preference('flickr_mail_'.$this->id, '');
129 return $this->print_login();
130 }
131
96297ca2 132 /**
133 *
134 * @param <type> $search_text
135 * @return <type>
136 */
353d5cf3 137 public function search($search_text) {
86e68c2d 138 global $SESSION;
fbd508b4 139 $people = $this->flickr->people_findByEmail($this->flickr_account);
86e68c2d 140 $sess_tag = 'flickr_public_'.$this->id.'_tag';
141 $sess_text = 'flickr_public_'.$this->id.'_text';
353d5cf3 142 $this->nsid = $people['nsid'];
143 $tag = optional_param('tag', '', PARAM_CLEANHTML);
86e68c2d 144 $is_paging = optional_param('search_paging', '', PARAM_RAW);
145 $page = 1;
146 if (!empty($is_paging)) {
147 $page = optional_param('p', '', PARAM_INT);
148 if (!empty($SESSION->$sess_tag)) {
149 $tag = $SESSION->$sess_tag;
150 }
151 if (!empty($SESSION->$sess_text)) {
152 $search_text = $SESSION->$sess_text;
153 }
154 }
dc0976b8 155 if (!empty($tag)) {
fbd508b4 156 $photos = $this->flickr->photos_search(array(
86e68c2d 157 'tags'=>$tag,
158 'page'=>$page
fbd508b4 159 ));
86e68c2d 160 $SESSION->$sess_tag = $tag;
161
fbd508b4 162 } else {
163 $photos = $this->flickr->photos_search(array(
353d5cf3 164 'user_id'=>$this->nsid,
165 'text'=>$search_text));
86e68c2d 166 $SESSION->$sess_text = $search_text;
fbd508b4 167 }
86e68c2d 168 $ret = array();
7e33b68d 169 return $this->build_list($photos, $page, $ret);
fbd508b4 170 }
96297ca2 171
172 /**
173 *
174 * @param <type> $path
175 * @return <type>
176 */
353d5cf3 177 public function get_listing($path = '1') {
539b3d13 178 $people = $this->flickr->people_findByEmail($this->flickr_account);
353d5cf3 179 $this->nsid = $people['nsid'];
fbd508b4 180 $photos = $this->flickr->people_getPublicPhotos($people['nsid'], 'original_format', 25, $path);
86e68c2d 181 $ret = array();
539b3d13 182
7e33b68d 183 return $this->build_list($photos, $path, $ret);
353d5cf3 184 }
96297ca2 185
186 /**
187 *
188 * @param <type> $photos
189 * @param <type> $path
190 * @return <type>
191 */
35bae111 192 private function build_list($photos, $path = 1, &$ret) {
353d5cf3 193 $photos_url = $this->flickr->urls_getUserPhotos($this->nsid);
539b3d13 194 $ret['manage'] = $photos_url;
195 $ret['list'] = array();
539b3d13 196 $ret['pages'] = $photos['pages'];
dc0976b8 197 if (is_int($path) && $path <= $ret['pages']) {
539b3d13 198 $ret['page'] = $path;
199 } else {
200 $ret['page'] = 1;
201 }
353d5cf3 202 if (!empty($photos['photo'])) {
203 foreach ($photos['photo'] as $p) {
204 if(empty($p['title'])) {
205 $p['title'] = get_string('notitle', 'repository_flickr');
206 }
207 if (isset($p['originalformat'])) {
208 $format = $p['originalformat'];
209 } else {
210 $format = 'jpg';
211 }
212 $ret['list'][] = array('title'=>$p['title'].'.'.$format,'source'=>$p['id'],
213 'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'),
8540f8f9 214 'date'=>'', 'size'=>'unknown', 'url'=>'http://www.flickr.com/photos/'.$p['owner'].'/'.$p['id']);
539b3d13 215 }
539b3d13 216 }
353d5cf3 217 return $ret;
539b3d13 218 }
dc0976b8 219
96297ca2 220 /**
221 *
222 * @return <type>
223 */
dc0976b8 224 public function print_listing() {
539b3d13 225 return false;
226 }
dc0976b8 227
96297ca2 228 /**
229 *
230 * @return <type>
231 */
dc0976b8 232 public function print_search() {
2b9feb5f 233 parent::print_search();
8d419e59 234 echo '<label>Tag: </label><br /><input type="text" name="tag" /><br />';
2b9feb5f 235 return true;
539b3d13 236 }
dc0976b8 237
96297ca2 238 /**
239 *
240 * @global <type> $CFG
241 * @param <type> $photo_id
242 * @param <type> $file
243 * @return <type>
244 */
dc0976b8 245 public function get_file($photo_id, $file = '') {
539b3d13 246 global $CFG;
247 $result = $this->flickr->photos_getSizes($photo_id);
248 $url = '';
dc0976b8 249 if (!empty($result[4])) {
539b3d13 250 $url = $result[4]['source'];
353d5cf3 251 } elseif(!empty($result[3])) {
539b3d13 252 $url = $result[3]['source'];
353d5cf3 253 } elseif(!empty($result[2])) {
539b3d13 254 $url = $result[2]['source'];
255 }
256 if (!file_exists($CFG->dataroot.'/repository/download')) {
257 mkdir($CFG->dataroot.'/repository/download/', 0777, true);
258 }
353d5cf3 259 if(is_dir($CFG->dataroot.'/repository/download')) {
539b3d13 260 $dir = $CFG->dataroot.'/repository/download/';
261 }
262
dc0976b8 263 if (empty($file)) {
539b3d13 264 $file = $photo_id.'_'.time().'.jpg';
265 }
dc0976b8 266 if (file_exists($dir.$file)) {
539b3d13 267 $file = uniqid('m').$file;
268 }
269 $fp = fopen($dir.$file, 'w');
270 $c = new curl;
353d5cf3 271 $c->download(array(array('url'=>$url, 'file'=>$fp)));
272
539b3d13 273 return $dir.$file;
274 }
539b3d13 275
96297ca2 276 /**
792ad3aa 277 * Add Instance settings input to Moodle form
96297ca2 278 * @param <type> $
279 */
b8f46e17 280 public function instance_config_form(&$mform) {
281 $mform->addElement('text', 'email_address', get_string('emailaddress', 'repository_flickr_public'));
b7cebc8e 282 //$mform->addRule('email_address', get_string('required'), 'required', null, 'client');
b8f46e17 283 }
284
96297ca2 285 /**
792ad3aa 286 * Names of the instance settings
96297ca2 287 * @return <type>
288 */
b8f46e17 289 public static function get_instance_option_names() {
290 return array('email_address');
539b3d13 291 }
292
96297ca2 293 /**
792ad3aa 294 * Add Plugin settings input to Moodle form
96297ca2 295 * @param <type> $
296 */
daff8f50 297 public function type_config_form(&$mform) {
539b3d13 298 $api_key = get_config('flickr_public', 'api_key');
299 if (empty($api_key)) {
300 $api_key = '';
301 }
302 $strrequired = get_string('required');
303 $mform->addElement('text', 'api_key', get_string('apikey', 'repository_flickr_public'), array('value'=>$api_key,'size' => '40'));
539b3d13 304 $mform->addRule('api_key', $strrequired, 'required', null, 'client');
e49eaacb 305 $mform->addElement('static', null, '', get_string('information','repository_flickr_public'));
539b3d13 306 }
b8f46e17 307
96297ca2 308 /**
792ad3aa 309 * Names of the plugin settings
96297ca2 310 * @return <type>
311 */
1b79955a 312 public static function get_type_option_names() {
b8f46e17 313 return array('api_key');
314 }
315
96297ca2 316 /**
83a018ed 317 * is run when moodle administrator add the plugin
96297ca2 318 */
948c2860 319 public static function plugin_init() {
e49eaacb 320 //here we create a default instance for this type
b7cebc8e 321 repository_static_function('flickr_public','create', 'flickr_public', 0, get_system_context(), array('name' => get_string('repositoryname', 'repository_flickr_public'),'email_address' => null));
322 }
539b3d13 323
324}
325