webservice MDL-17135 add logging to add/edit/delete service action
[moodle.git] / repository / flickr / repository.class.php
CommitLineData
36ea9a6c 1<?php
2/**
6a391ebf 3 * repository_flickr class
539b3d13 4 * This plugin is used to access user's private flickr repository
36ea9a6c 5 *
539b3d13 6 * @author Dongsheng Cai <dongsheng@moodle.com>
36ea9a6c 7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8 */
9
539b3d13 10require_once($CFG->libdir.'/flickrlib.php');
6a391ebf 11
96297ca2 12/**
13 *
14 */
dc0976b8 15class repository_flickr extends repository {
6a391ebf 16 private $flickr;
17 public $photos;
19add4c0 18
96297ca2 19 /**
20 *
21 * @global <type> $SESSION
22 * @global <type> $CFG
23 * @param <type> $repositoryid
24 * @param <type> $context
25 * @param <type> $options
26 */
dc0976b8 27 public function __construct($repositoryid, $context = SITEID, $options = array()) {
d68c527f 28 global $SESSION, $CFG;
29 $options['page'] = optional_param('p', 1, PARAM_INT);
30 parent::__construct($repositoryid, $context, $options);
31
32 $this->setting = 'flickr_';
33
34 $this->api_key = $this->get_option('api_key');
35 $this->secret = $this->get_option('secret');
36
37 $this->token = get_user_preferences($this->setting, '');
38 $this->nsid = get_user_preferences($this->setting.'_nsid', '');
39
40 $this->flickr = new phpFlickr($this->api_key, $this->secret, $this->token);
41
353d5cf3 42 $frob = optional_param('frob', '', PARAM_RAW);
43 if (empty($this->token) && !empty($frob)) {
44 $auth_info = $this->flickr->auth_getToken($frob);
45 $this->token = $auth_info['token'];
46 $this->nsid = $auth_info['user']['nsid'];
47 set_user_preference($this->setting, $auth_info['token']);
48 set_user_preference($this->setting.'_nsid', $auth_info['user']['nsid']);
d68c527f 49 }
50
51 }
96297ca2 52
53 /**
54 *
55 * @return <type>
56 */
dc0976b8 57 public function check_login() {
d68c527f 58 return !empty($this->token);
59 }
96297ca2 60
61 /**
62 *
63 * @return <type>
64 */
dc0976b8 65 public function logout() {
d68c527f 66 set_user_preference($this->setting, '');
67 set_user_preference($this->setting.'_nsid', '');
68 $this->token = '';
69 $this->nsid = '';
70 return $this->print_login();
71 }
96297ca2 72
73 /**
74 *
75 * @param <type> $options
76 * @return <type>
77 */
dc0976b8 78 public function set_option($options = array()) {
27051e43 79 if (!empty($options['api_key'])) {
70d91d90 80 set_config('api_key', trim($options['api_key']), 'flickr');
27051e43 81 }
7fab56b7 82 if (!empty($options['secret'])) {
83 set_config('secret', trim($options['secret']), 'flickr');
84 }
27051e43 85 unset($options['api_key']);
7fab56b7 86 unset($options['secret']);
27051e43 87 $ret = parent::set_option($options);
88 return $ret;
89 }
90
96297ca2 91 /**
92 *
93 * @param <type> $config
94 * @return <type>
95 */
dc0976b8 96 public function get_option($config = '') {
97 if ($config==='api_key') {
70d91d90 98 return trim(get_config('flickr', 'api_key'));
7fab56b7 99 } elseif ($config ==='secret') {
100 return trim(get_config('flickr', 'secret'));
27051e43 101 } else {
70d91d90 102 $options['api_key'] = trim(get_config('flickr', 'api_key'));
7fab56b7 103 $options['secret'] = trim(get_config('flickr', 'secret'));
27051e43 104 }
105 $options = parent::get_option($config);
106 return $options;
107 }
108
96297ca2 109 /**
110 *
111 * @return <type>
112 */
dc0976b8 113 public function global_search() {
8988c36b 114 if (empty($this->token)) {
455860ce 115 return false;
116 } else {
117 return true;
118 }
119 }
96297ca2 120
121 /**
122 *
123 * @param <type> $ajax
124 * @return <type>
125 */
5e98ab96 126 public function print_login() {
127 if ($this->options['ajax']) {
d68c527f 128 $ret = array();
129 $popup_btn = new stdclass;
130 $popup_btn->type = 'popup';
131 $popup_btn->url = $this->flickr->auth();
132 $ret['login'] = array($popup_btn);
133 return $ret;
5e98ab96 134 } else {
362a728f 135 echo '<a target="_blank" href="'.$this->flickr->auth().'">'.get_string('login', 'repository').'</a>';
6a391ebf 136 }
36ea9a6c 137 }
96297ca2 138
139 /**
140 *
141 * @param <type> $photos
5fa358d1 142 * @param <type> $page
96297ca2 143 * @return <type>
144 */
5fa358d1 145 private function build_list($photos, $page = 1) {
353d5cf3 146 $photos_url = $this->flickr->urls_getUserPhotos($this->nsid);
d05ef4a3 147 $ret = array();
148 $ret['manage'] = $photos_url;
149 $ret['list'] = array();
150 $ret['pages'] = $photos['pages'];
009a97ce 151 $ret['total'] = $photos['total'];
152 $ret['perpage'] = $photos['perpage'];
5fa358d1 153 if($page <= $ret['pages']) {
154 $ret['page'] = $page;
6a391ebf 155 } else {
d05ef4a3 156 $ret['page'] = 1;
6a391ebf 157 }
dc0976b8 158 if (!empty($photos['photo'])) {
8988c36b 159 foreach ($photos['photo'] as $p) {
353d5cf3 160 if(empty($p['title'])) {
8988c36b 161 $p['title'] = get_string('notitle', 'repository_flickr');
162 }
163 if (isset($p['originalformat'])) {
164 $format = $p['originalformat'];
165 } else {
166 $format = 'jpg';
167 }
0e51661b 168 $format = '.'.$format;
169 // append extensions to the files
170 if (substr($p['title'], strlen($p['title'])-strlen($format)) != $format) {
4317f92f 171 $p['title'] .= $format;
0e51661b 172 }
173 $ret['list'][] = array('title'=>$p['title'],'source'=>$p['id'],
353d5cf3 174 'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'),
175 'date'=>'', 'size'=>'unknown', 'url'=>$photos_url.$p['id']);
733cfd80 176 }
6a391ebf 177 }
353d5cf3 178 return $ret;
179 }
96297ca2 180
181 /**
182 *
183 * @param <type> $search_text
184 * @return <type>
185 */
353d5cf3 186 public function search($search_text) {
187 $photos = $this->flickr->photos_search(array(
188 'user_id'=>$this->nsid,
8a77c949 189 'per_page'=>24,
353d5cf3 190 'extras'=>'original_format',
191 'text'=>$search_text
192 ));
c438f190 193 $ret = $this->build_list($photos);
194 $ret['list'] = array_filter($ret['list'], array($this, 'filter'));
195 return $ret;
353d5cf3 196 }
96297ca2 197
198 /**
199 *
5fa358d1 200 * @param string $path
201 * @param int $page
96297ca2 202 * @return <type>
203 */
5fa358d1 204 public function get_listing($path = '', $page = '1') {
353d5cf3 205 $photos_url = $this->flickr->urls_getUserPhotos($this->nsid);
206
207 $photos = $this->flickr->photos_search(array(
208 'user_id'=>$this->nsid,
8a77c949 209 'per_page'=>24,
5fa358d1 210 'page'=>$page,
353d5cf3 211 'extras'=>'original_format'
212 ));
5fa358d1 213 return $this->build_list($photos, $page);
36ea9a6c 214 }
96297ca2 215
96297ca2 216 /**
217 *
218 * @global <type> $CFG
219 * @param <type> $photo_id
220 * @param <type> $file
221 * @return <type>
222 */
dc0976b8 223 public function get_file($photo_id, $file = '') {
6a391ebf 224 global $CFG;
225 $result = $this->flickr->photos_getSizes($photo_id);
226 $url = '';
353d5cf3 227 if(!empty($result[4])) {
6a391ebf 228 $url = $result[4]['source'];
353d5cf3 229 } elseif(!empty($result[3])) {
6a391ebf 230 $url = $result[3]['source'];
353d5cf3 231 } elseif(!empty($result[2])) {
6a391ebf 232 $url = $result[2]['source'];
233 }
a53d4f45 234 $path = $this->prepare_file($file);
235 $fp = fopen($path, 'w');
6a391ebf 236 $c = new curl;
237 $c->download(array(
238 array('url'=>$url, 'file'=>$fp)
239 ));
a53d4f45 240 return $path;
6a391ebf 241 }
a6600395 242
96297ca2 243 /**
792ad3aa 244 * Add Plugin settings input to Moodle form
96297ca2 245 * @global <type> $CFG
246 * @param <type> $
247 */
daff8f50 248 public function type_config_form(&$mform) {
7fab56b7 249 global $CFG;
336bb44b 250 $api_key = get_config('flickr', 'api_key');
7892948d 251 $secret = get_config('flickr', 'secret');
252
336bb44b 253 if (empty($api_key)) {
254 $api_key = '';
255 }
0868f619 256 if (empty($secret)) {
257 $secret = '';
258 }
7892948d 259
4a65c39a 260 $strrequired = get_string('required');
0868f619 261 $mform->addElement('text', 'api_key', get_string('apikey', 'repository_flickr'), array('value'=>$api_key,'size' => '40'));
262 $mform->addElement('text', 'secret', get_string('secret', 'repository_flickr'), array('value'=>$secret,'size' => '40'));
e49eaacb 263
264 //retrieve the flickr instances
dbc01944 265 $instances = repository::get_instances(array(),null,false,"flickr");
e49eaacb 266 if (empty($instances)) {
267 $callbackurl = get_string("callbackwarning","repository_flickr");
268 $mform->addElement('static', null, '', $callbackurl);
269 }
270 else {
271 $callbackurl = $CFG->wwwroot.'/repository/ws.php?callback=yes&amp;repo_id='.$instances[0]->id;
272 $mform->addElement('static', 'callbackurl', '', get_string('callbackurltext', 'repository_flickr', $callbackurl));
273 }
dbc01944 274
4a65c39a 275 $mform->addRule('api_key', $strrequired, 'required', null, 'client');
0868f619 276 $mform->addRule('secret', $strrequired, 'required', null, 'client');
4a65c39a 277 }
0868f619 278
96297ca2 279 /**
792ad3aa 280 * Names of the plugin settings
96297ca2 281 * @return <type>
282 */
1b79955a 283 public static function get_type_option_names() {
7fab56b7 284 return array('api_key', 'secret');
4a65c39a 285 }
014c1ca0 286 public function supported_filetypes() {
287 return array('web_image');
288 }
41076c58
DC
289 public function supported_returntypes() {
290 return (FILE_INTERNAL | FILE_EXTERNAL);
291 }
8a77c949 292}