MDL-16574: improving configgetremoteaddrconf documentation (merge from 1.9)
[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;
539b3d13 86 } else {
d68c527f 87 throw new repository_exception('invalidemail', 'repository_flickr_public');
539b3d13 88 }
89 }
90 }
96297ca2 91
92 /**
93 *
94 * @return <type>
95 */
dc0976b8 96 public function check_login() {
d68c527f 97 return !empty($this->flickr_account);
98 }
96297ca2 99
100 /**
101 *
102 * @param <type> $ajax
103 * @return <type>
104 */
dc0976b8 105 public function print_login($ajax = true) {
106 if ($ajax) {
d68c527f 107 $ret = array();
ca0174cc 108 $email_field->label = get_string('username', 'repository_flickr_public').': ';
109 $email_field->id = 'account';
110 $email_field->type = 'text';
111 $email_field->name = 'flickr_account';
112 $ret['login'] = array($email_field);
d68c527f 113 return $ret;
539b3d13 114 }
115 }
96297ca2 116
117 /**
118 *
119 * @param <type> $search_text
120 * @return <type>
121 */
353d5cf3 122 public function search($search_text) {
fbd508b4 123 $people = $this->flickr->people_findByEmail($this->flickr_account);
353d5cf3 124 $this->nsid = $people['nsid'];
125 $tag = optional_param('tag', '', PARAM_CLEANHTML);
dc0976b8 126 if (!empty($tag)) {
fbd508b4 127 $photos = $this->flickr->photos_search(array(
128 'tags'=>$tag
129 ));
130 } else {
131 $photos = $this->flickr->photos_search(array(
353d5cf3 132 'user_id'=>$this->nsid,
133 'text'=>$search_text));
fbd508b4 134 }
353d5cf3 135 return $this->build_list($photos);
fbd508b4 136 }
96297ca2 137
138 /**
139 *
140 * @param <type> $path
141 * @return <type>
142 */
353d5cf3 143 public function get_listing($path = '1') {
539b3d13 144 $people = $this->flickr->people_findByEmail($this->flickr_account);
353d5cf3 145 $this->nsid = $people['nsid'];
fbd508b4 146 $photos = $this->flickr->people_getPublicPhotos($people['nsid'], 'original_format', 25, $path);
539b3d13 147
353d5cf3 148 return $this->build_list($photos, $path);
149 }
96297ca2 150
151 /**
152 *
153 * @param <type> $photos
154 * @param <type> $path
155 * @return <type>
156 */
353d5cf3 157 private function build_list($photos, $path = 1) {
158 $photos_url = $this->flickr->urls_getUserPhotos($this->nsid);
539b3d13 159 $ret = array();
160 $ret['manage'] = $photos_url;
161 $ret['list'] = array();
539b3d13 162 $ret['pages'] = $photos['pages'];
dc0976b8 163 if (is_int($path) && $path <= $ret['pages']) {
539b3d13 164 $ret['page'] = $path;
165 } else {
166 $ret['page'] = 1;
167 }
353d5cf3 168 if (!empty($photos['photo'])) {
169 foreach ($photos['photo'] as $p) {
170 if(empty($p['title'])) {
171 $p['title'] = get_string('notitle', 'repository_flickr');
172 }
173 if (isset($p['originalformat'])) {
174 $format = $p['originalformat'];
175 } else {
176 $format = 'jpg';
177 }
178 $ret['list'][] = array('title'=>$p['title'].'.'.$format,'source'=>$p['id'],
179 'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'),
180 'date'=>'', 'size'=>'unknown', 'url'=>$photos_url.$p['id']);
539b3d13 181 }
539b3d13 182 }
353d5cf3 183 return $ret;
539b3d13 184 }
dc0976b8 185
96297ca2 186 /**
187 *
188 * @return <type>
189 */
dc0976b8 190 public function print_listing() {
539b3d13 191 return false;
192 }
dc0976b8 193
96297ca2 194 /**
195 *
196 * @return <type>
197 */
dc0976b8 198 public function print_search() {
2b9feb5f 199 parent::print_search();
8d419e59 200 echo '<label>Tag: </label><br /><input type="text" name="tag" /><br />';
2b9feb5f 201 return true;
539b3d13 202 }
dc0976b8 203
96297ca2 204 /**
205 *
206 * @global <type> $CFG
207 * @param <type> $photo_id
208 * @param <type> $file
209 * @return <type>
210 */
dc0976b8 211 public function get_file($photo_id, $file = '') {
539b3d13 212 global $CFG;
213 $result = $this->flickr->photos_getSizes($photo_id);
214 $url = '';
dc0976b8 215 if (!empty($result[4])) {
539b3d13 216 $url = $result[4]['source'];
353d5cf3 217 } elseif(!empty($result[3])) {
539b3d13 218 $url = $result[3]['source'];
353d5cf3 219 } elseif(!empty($result[2])) {
539b3d13 220 $url = $result[2]['source'];
221 }
222 if (!file_exists($CFG->dataroot.'/repository/download')) {
223 mkdir($CFG->dataroot.'/repository/download/', 0777, true);
224 }
353d5cf3 225 if(is_dir($CFG->dataroot.'/repository/download')) {
539b3d13 226 $dir = $CFG->dataroot.'/repository/download/';
227 }
228
dc0976b8 229 if (empty($file)) {
539b3d13 230 $file = $photo_id.'_'.time().'.jpg';
231 }
dc0976b8 232 if (file_exists($dir.$file)) {
539b3d13 233 $file = uniqid('m').$file;
234 }
235 $fp = fopen($dir.$file, 'w');
236 $c = new curl;
353d5cf3 237 $c->download(array(array('url'=>$url, 'file'=>$fp)));
238
539b3d13 239 return $dir.$file;
240 }
539b3d13 241
96297ca2 242 /**
792ad3aa 243 * Add Instance settings input to Moodle form
96297ca2 244 * @param <type> $
245 */
b8f46e17 246 public function instance_config_form(&$mform) {
247 $mform->addElement('text', 'email_address', get_string('emailaddress', 'repository_flickr_public'));
948c2860 248 $mform->addRule('email_address', get_string('required'), 'required', null, 'client');
b8f46e17 249 }
250
96297ca2 251 /**
792ad3aa 252 * Names of the instance settings
96297ca2 253 * @return <type>
254 */
b8f46e17 255 public static function get_instance_option_names() {
256 return array('email_address');
539b3d13 257 }
258
96297ca2 259 /**
792ad3aa 260 * Add Plugin settings input to Moodle form
96297ca2 261 * @param <type> $
262 */
daff8f50 263 public function type_config_form(&$mform) {
539b3d13 264 $api_key = get_config('flickr_public', 'api_key');
265 if (empty($api_key)) {
266 $api_key = '';
267 }
268 $strrequired = get_string('required');
269 $mform->addElement('text', 'api_key', get_string('apikey', 'repository_flickr_public'), array('value'=>$api_key,'size' => '40'));
539b3d13 270 $mform->addRule('api_key', $strrequired, 'required', null, 'client');
e49eaacb 271 $mform->addElement('static', null, '', get_string('information','repository_flickr_public'));
539b3d13 272 }
b8f46e17 273
96297ca2 274 /**
792ad3aa 275 * Names of the plugin settings
96297ca2 276 * @return <type>
277 */
1b79955a 278 public static function get_type_option_names() {
b8f46e17 279 return array('api_key');
280 }
281
96297ca2 282 /**
83a018ed 283 * is run when moodle administrator add the plugin
96297ca2 284 */
948c2860 285 public static function plugin_init() {
e49eaacb 286 //here we create a default instance for this type
83d94004 287 repository_static_function('flickr_public','create', 'flickr_public', 0, get_system_context(), array('name' => get_string('repositoryname', 'repository_public'),'email_address' => null),1);
539b3d13 288 }
289
290}
291