course settings: MDL-17501 check for moodle/course:visibility in the course settings...
[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 *
186 * @param <type> $path
187 * @return <type>
188 */
353d5cf3 189 public function get_listing($path = '1') {
539b3d13 190 $people = $this->flickr->people_findByEmail($this->flickr_account);
353d5cf3 191 $this->nsid = $people['nsid'];
8a77c949 192 $photos = $this->flickr->people_getPublicPhotos($people['nsid'], 'original_format', 24, $path);
86e68c2d 193 $ret = array();
539b3d13 194
7e33b68d 195 return $this->build_list($photos, $path, $ret);
353d5cf3 196 }
96297ca2 197
198 /**
199 *
200 * @param <type> $photos
201 * @param <type> $path
202 * @return <type>
203 */
35bae111 204 private function build_list($photos, $path = 1, &$ret) {
6b2a6c90 205 if (!empty($this->nsid)) {
206 $photos_url = $this->flickr->urls_getUserPhotos($this->nsid);
207 $ret['manage'] = $photos_url;
208 }
539b3d13 209 $ret['list'] = array();
539b3d13 210 $ret['pages'] = $photos['pages'];
dc0976b8 211 if (is_int($path) && $path <= $ret['pages']) {
539b3d13 212 $ret['page'] = $path;
213 } else {
214 $ret['page'] = 1;
215 }
353d5cf3 216 if (!empty($photos['photo'])) {
217 foreach ($photos['photo'] as $p) {
218 if(empty($p['title'])) {
219 $p['title'] = get_string('notitle', 'repository_flickr');
220 }
221 if (isset($p['originalformat'])) {
222 $format = $p['originalformat'];
223 } else {
224 $format = 'jpg';
225 }
226 $ret['list'][] = array('title'=>$p['title'].'.'.$format,'source'=>$p['id'],
227 'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'),
8540f8f9 228 'date'=>'', 'size'=>'unknown', 'url'=>'http://www.flickr.com/photos/'.$p['owner'].'/'.$p['id']);
539b3d13 229 }
539b3d13 230 }
353d5cf3 231 return $ret;
539b3d13 232 }
dc0976b8 233
96297ca2 234 /**
235 *
236 * @return <type>
237 */
dc0976b8 238 public function print_listing() {
539b3d13 239 return false;
240 }
dc0976b8 241
96297ca2 242 /**
243 *
244 * @return <type>
245 */
dc0976b8 246 public function print_search() {
6b2a6c90 247 echo '<input type="hidden" name="repo_id" value="'.$this->id.'" />';
248 echo '<input type="hidden" name="ctx_id" value="'.$this->context->id.'" />';
249 echo '<input type="hidden" name="seekey" value="'.sesskey().'" />';
250 echo '<label>'.get_string('fulltext', 'repository_flickr_public').': </label><br/><input name="s" value="" /><br/>';
251 echo '<label>'.get_string('tag', 'repository_flickr_public').'</label><br /><input type="text" name="tag" /><br />';
2b9feb5f 252 return true;
539b3d13 253 }
dc0976b8 254
96297ca2 255 /**
256 *
257 * @global <type> $CFG
258 * @param <type> $photo_id
259 * @param <type> $file
260 * @return <type>
261 */
dc0976b8 262 public function get_file($photo_id, $file = '') {
539b3d13 263 global $CFG;
264 $result = $this->flickr->photos_getSizes($photo_id);
265 $url = '';
dc0976b8 266 if (!empty($result[4])) {
539b3d13 267 $url = $result[4]['source'];
353d5cf3 268 } elseif(!empty($result[3])) {
539b3d13 269 $url = $result[3]['source'];
353d5cf3 270 } elseif(!empty($result[2])) {
539b3d13 271 $url = $result[2]['source'];
272 }
273 if (!file_exists($CFG->dataroot.'/repository/download')) {
274 mkdir($CFG->dataroot.'/repository/download/', 0777, true);
275 }
353d5cf3 276 if(is_dir($CFG->dataroot.'/repository/download')) {
539b3d13 277 $dir = $CFG->dataroot.'/repository/download/';
278 }
279
dc0976b8 280 if (empty($file)) {
539b3d13 281 $file = $photo_id.'_'.time().'.jpg';
282 }
dc0976b8 283 if (file_exists($dir.$file)) {
539b3d13 284 $file = uniqid('m').$file;
285 }
286 $fp = fopen($dir.$file, 'w');
287 $c = new curl;
353d5cf3 288 $c->download(array(array('url'=>$url, 'file'=>$fp)));
289
539b3d13 290 return $dir.$file;
291 }
539b3d13 292
96297ca2 293 /**
792ad3aa 294 * Add Instance settings input to Moodle form
96297ca2 295 * @param <type> $
296 */
b8f46e17 297 public function instance_config_form(&$mform) {
298 $mform->addElement('text', 'email_address', get_string('emailaddress', 'repository_flickr_public'));
b7cebc8e 299 //$mform->addRule('email_address', get_string('required'), 'required', null, 'client');
b8f46e17 300 }
301
96297ca2 302 /**
792ad3aa 303 * Names of the instance settings
96297ca2 304 * @return <type>
305 */
b8f46e17 306 public static function get_instance_option_names() {
307 return array('email_address');
539b3d13 308 }
309
96297ca2 310 /**
792ad3aa 311 * Add Plugin settings input to Moodle form
96297ca2 312 * @param <type> $
313 */
daff8f50 314 public function type_config_form(&$mform) {
539b3d13 315 $api_key = get_config('flickr_public', 'api_key');
316 if (empty($api_key)) {
317 $api_key = '';
318 }
319 $strrequired = get_string('required');
320 $mform->addElement('text', 'api_key', get_string('apikey', 'repository_flickr_public'), array('value'=>$api_key,'size' => '40'));
539b3d13 321 $mform->addRule('api_key', $strrequired, 'required', null, 'client');
e49eaacb 322 $mform->addElement('static', null, '', get_string('information','repository_flickr_public'));
539b3d13 323 }
b8f46e17 324
96297ca2 325 /**
792ad3aa 326 * Names of the plugin settings
96297ca2 327 * @return <type>
328 */
1b79955a 329 public static function get_type_option_names() {
b8f46e17 330 return array('api_key');
331 }
332
96297ca2 333 /**
83a018ed 334 * is run when moodle administrator add the plugin
96297ca2 335 */
948c2860 336 public static function plugin_init() {
e49eaacb 337 //here we create a default instance for this type
539b3d13 338
dbc01944 339 repository::static_function('flickr_public','create', 'flickr_public', 0, get_system_context(), array('name' => get_string('repositoryname', 'repository_flickr_public'),'email_address' => null),1);
340 }
539b3d13 341}
342