MDL-13766
[moodle.git] / repository / flickr / repository.class.php
CommitLineData
36ea9a6c 1<?php
2/**
6a391ebf 3 * repository_flickr class
36ea9a6c 4 * This is a subclass of repository class
5 *
6a391ebf 6 * @author Dongsheng Cai
7 * @version 0.1 dev
36ea9a6c 8 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9 */
10
6a391ebf 11require_once($CFG->dirroot.'/repository/flickr/'.'phpFlickr.php');
12
13class repository_flickr extends repository{
14 private $flickr;
15 public $photos;
19add4c0 16
27051e43 17 public function set_option($options = array()){
18 if (!empty($options['api_key'])) {
70d91d90 19 set_config('api_key', trim($options['api_key']), 'flickr');
27051e43 20 }
21 unset($options['api_key']);
22 $ret = parent::set_option($options);
23 return $ret;
24 }
25
26 public function get_option($config = ''){
27 if($config==='api_key'){
70d91d90 28 return trim(get_config('flickr', 'api_key'));
27051e43 29 } else {
70d91d90 30 $options['api_key'] = trim(get_config('flickr', 'api_key'));
27051e43 31 }
32 $options = parent::get_option($config);
33 return $options;
34 }
35
455860ce 36 public function global_search(){
37 global $SESSION;
38 $sess_name = 'flickrmail'.$this->id;
39 if (empty($SESSION->$sess_name)) {
455860ce 40 return false;
41 } else {
42 return true;
43 }
44 }
45
36ea9a6c 46 public function __construct($repositoryid, $context = SITEID, $options = array()){
d72ff7a2 47 global $SESSION, $action, $CFG;
6a391ebf 48 $options['page'] = optional_param('p', 1, PARAM_INT);
36ea9a6c 49 parent::__construct($repositoryid, $context, $options);
eb9aa683 50 $this->api_key = $this->get_option('api_key');
621dca51 51 if (empty($this->api_key)) {
621dca51 52 }
53 $this->flickr = new phpFlickr($this->api_key);
6a391ebf 54
55 $reset = optional_param('reset', 0, PARAM_INT);
eb9aa683 56 $sess_name = 'flickrmail'.$this->id;
6a391ebf 57 if(!empty($reset)) {
58 // logout from flickr
eb9aa683 59 unset($SESSION->$sess_name);
60 set_user_preference('flickrmail'.$this->id, '');
6a391ebf 61 }
62
eb9aa683 63 if(!empty($SESSION->$sess_name)) {
6a391ebf 64 if(empty($action)) {
65 $action = 'list';
66 }
67 } else {
68 // get flickr account
eb9aa683 69 $account = optional_param('flickrmail', '', PARAM_RAW);
70 if(!empty($account)) {
71 $people = $this->flickr->people_findByEmail($account);
6a391ebf 72 if(!empty($people)) {
73 $remember = optional_param('remember', '', PARAM_RAW);
74 if(!empty($remember)) {
eb9aa683 75 set_user_preference('flickrmail'.$this->id, $account);
6a391ebf 76 }
eb9aa683 77 $SESSION->$sess_name = $account;
5fd3e8f7 78 if (empty($account)) {
6a391ebf 79 $action = 'list';
5fd3e8f7 80 } else {
81 $action = 'login';
6a391ebf 82 }
83 } else {
84 throw new repository_exception('invalidemail', 'repository_flickr');
85 }
86 } else {
eb9aa683 87 if($account = get_user_preferences('flickrmail'.$this->id, '')){
88 $SESSION->$sess_name = $account;
6a391ebf 89 if(empty($action)) {
90 $action = 'list';
91 }
5fd3e8f7 92 } else {
93 $action = 'login';
6a391ebf 94 }
95 }
96 }
36ea9a6c 97 }
0eb58cf4 98 public function print_login($ajax = true){
36ea9a6c 99 global $SESSION;
eb9aa683 100 $sess_name = 'flickrmail'.$this->id;
101 if(empty($SESSION->$sess_name)) {
6a391ebf 102 $str =<<<EOD
103<form id="moodle-repo-login">
104<label for="account">Account (Email)</label><br/>
105<input type='text' name='flickrmail' id='account' />
4a65c39a 106<input type='hidden' name='id' value='$this->id' /><br/>
6a391ebf 107<input type='checkbox' name='remember' id="keepid" value='true' /> <label for="keepid">Remember? </label>
108<p><input type='button' onclick="repository_client.login()" value="Go" /></p>
109</form>
110EOD;
111 if($ajax){
112 $ret = array();
0eaa8c5c 113 $e1->label = get_string('username', 'repository_flickr').': ';
6a391ebf 114 $e1->id = 'account';
115 $e1->type = 'text';
116 $e1->name = 'flickrmail';
117
118 $e2->id = 'keepid';
0eaa8c5c 119 $e2->label = get_string('remember', 'repository_flickr').' ';
6a391ebf 120 $e2->type = 'checkbox';
121 $e2->name = 'remember';
122
123 $e3->type = 'hidden';
124 $e3->name = 'repo_id';
4a65c39a 125 $e3->value = $this->id;
d373f63f 126 $ret['login'] = array($e1, $e2, $e3);
6a391ebf 127 return $ret;
128 }else{
129 echo $str;
130 }
131 } else {
132 return $this->get_listing();
133 }
36ea9a6c 134 }
6a391ebf 135 public function get_listing($path = '1', $search = ''){
36ea9a6c 136 global $SESSION;
eb9aa683 137 $sess_name = 'flickrmail'.$this->id;
138 $people = $this->flickr->people_findByEmail($SESSION->$sess_name);
6a391ebf 139 $photos_url = $this->flickr->urls_getUserPhotos($people['nsid']);
140
141 if(!empty($search)) {
142 // do searching, if $path is not empty, ignore it.
143 $photos = $this->flickr->photos_search(array('user_id'=>$people['nsid'], 'text'=>$search));
144 } elseif(!empty($path) && empty($search)) {
7dfb3c1c 145 $photos = $this->flickr->people_getPublicPhotos($people['nsid'], 'original_format', 25, $path);
6a391ebf 146 }
147
d05ef4a3 148 $ret = array();
149 $ret['manage'] = $photos_url;
150 $ret['list'] = array();
151 $ret['pages'] = $photos['pages'];
152 if(is_int($path) && $path <= $ret['pages']) {
153 $ret['page'] = $path;
6a391ebf 154 } else {
d05ef4a3 155 $ret['page'] = 1;
6a391ebf 156 }
157 foreach ($photos['photo'] as $p) {
158 if(empty($p['title'])) {
159 $p['title'] = get_string('notitle', 'repository_flickr');
160 }
733cfd80 161 if (isset($p['originalformat'])) {
162 $format = $p['originalformat'];
163 } else {
164 $format = 'jpg';
165 }
d05ef4a3 166 $ret['list'][] =
336bb44b 167 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']);
6a391ebf 168 }
bf1fccf0 169 if(empty($ret)) {
6a391ebf 170 throw new repository_exception('nullphotolist', 'repository_flickr');
bf1fccf0 171 } else {
172 return $ret;
173 }
36ea9a6c 174 }
175 public function print_listing(){
6a391ebf 176 if(empty($this->photos)){
177 $this->get_listing();
178 }
179 $str = '';
180 $str .= '<h2>Account: <span>'.$this->photos['a'].'</span></h2>';
181 foreach ((array)$this->photos['photo'] as $photo) {
182 $str .= "<a href='".$this->photos['url'].$photo[id]."'>";
183 $str .= "<img border='0' alt='$photo[title]' ".
184 "src=" . $photo['thumbnail'] . ">";
185 $str .= "</a>";
186 $i++;
187
188 if ($i % 4 == 0) {
189 $str .= "<br/>";
190 }
191 }
192 $str .= <<<EOD
193<style type='text/css'>
194#paging{margin-top: 10px; clear:both}
195#paging a{padding: 4px; border: 1px solid gray}
196</style>
197EOD;
198 $str .= '<div id="paging">';
199 for($i=1; $i <= $this->photos['pages']; $i++) {
4a65c39a 200 $str .= '<a href="###" onclick="cr('.$this->id.', '.$i.', 0)">';
6a391ebf 201 $str .= $i;
202 $str .= '</a> ';
203 }
204 $str .= '</div>';
205 echo $str;
36ea9a6c 206 }
207 public function print_search(){
6a391ebf 208 echo '<input type="text" name="Search" value="search terms..." size="40" class="right"/>';
36ea9a6c 209 return true;
210 }
6a391ebf 211 public function get_file($photo_id, $file = ''){
212 global $CFG;
213 $result = $this->flickr->photos_getSizes($photo_id);
214 $url = '';
215 if(!empty($result[4])) {
216 $url = $result[4]['source'];
217 } elseif(!empty($result[3])) {
218 $url = $result[3]['source'];
219 } elseif(!empty($result[2])) {
220 $url = $result[2]['source'];
221 }
222 if (!file_exists($CFG->dataroot.'/repository/download')) {
223 mkdir($CFG->dataroot.'/repository/download/', 0777, true);
224 }
225 if(is_dir($CFG->dataroot.'/repository/download')) {
226 $dir = $CFG->dataroot.'/repository/download/';
227 }
228
229 if(empty($file)) {
230 $file = $photo_id.'_'.time().'.jpg';
231 }
232 if(file_exists($dir.$file)) {
233 $file = uniqid('m').$file;
234 }
235 $fp = fopen($dir.$file, 'w');
236 $c = new curl;
237 $c->download(array(
238 array('url'=>$url, 'file'=>$fp)
239 ));
240 return $dir.$file;
241 }
4a65c39a 242 public static function has_admin_config() {
243 return true;
244 }
a6600395 245
faaa613d 246 public static function has_multiple_instances() {
247 return true;
248 }
249
250 public static function has_instance_config() {
251 return false;
252 }
253
4a65c39a 254 public function admin_config_form(&$mform) {
336bb44b 255 $api_key = get_config('flickr', 'api_key');
256 if (empty($api_key)) {
257 $api_key = '';
258 }
4a65c39a 259 $strrequired = get_string('required');
a6600395 260 $mform->addElement('text', 'api_key', get_string('apikey', 'repository_boxnet'), array('value'=>$api_key,'size' => '40'));
4a65c39a 261 $mform->addRule('api_key', $strrequired, 'required', null, 'client');
262 }
a6600395 263 public static function get_admin_option_names(){
4a65c39a 264 return array('api_key');
265 }
a6600395 266
36ea9a6c 267}