|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00030 require_once($CFG->libdir.'/flickrlib.php'); 00031 00035 class repository_flickr extends repository { 00036 private $flickr; 00037 public $photos; 00038 00045 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) { 00046 global $SESSION, $CFG; 00047 $options['page'] = optional_param('p', 1, PARAM_INT); 00048 parent::__construct($repositoryid, $context, $options); 00049 00050 $this->setting = 'flickr_'; 00051 00052 $this->api_key = $this->get_option('api_key'); 00053 $this->secret = $this->get_option('secret'); 00054 00055 $this->token = get_user_preferences($this->setting, ''); 00056 $this->nsid = get_user_preferences($this->setting.'_nsid', ''); 00057 00058 $this->flickr = new phpFlickr($this->api_key, $this->secret, $this->token); 00059 00060 $frob = optional_param('frob', '', PARAM_RAW); 00061 if (empty($this->token) && !empty($frob)) { 00062 $auth_info = $this->flickr->auth_getToken($frob); 00063 $this->token = $auth_info['token']; 00064 $this->nsid = $auth_info['user']['nsid']; 00065 set_user_preference($this->setting, $auth_info['token']); 00066 set_user_preference($this->setting.'_nsid', $auth_info['user']['nsid']); 00067 } 00068 00069 } 00070 00075 public function check_login() { 00076 return !empty($this->token); 00077 } 00078 00083 public function logout() { 00084 set_user_preference($this->setting, ''); 00085 set_user_preference($this->setting.'_nsid', ''); 00086 $this->token = ''; 00087 $this->nsid = ''; 00088 return $this->print_login(); 00089 } 00090 00096 public function set_option($options = array()) { 00097 if (!empty($options['api_key'])) { 00098 set_config('api_key', trim($options['api_key']), 'flickr'); 00099 } 00100 if (!empty($options['secret'])) { 00101 set_config('secret', trim($options['secret']), 'flickr'); 00102 } 00103 unset($options['api_key']); 00104 unset($options['secret']); 00105 $ret = parent::set_option($options); 00106 return $ret; 00107 } 00108 00114 public function get_option($config = '') { 00115 if ($config==='api_key') { 00116 return trim(get_config('flickr', 'api_key')); 00117 } elseif ($config ==='secret') { 00118 return trim(get_config('flickr', 'secret')); 00119 } else { 00120 $options['api_key'] = trim(get_config('flickr', 'api_key')); 00121 $options['secret'] = trim(get_config('flickr', 'secret')); 00122 } 00123 $options = parent::get_option($config); 00124 return $options; 00125 } 00126 00131 public function global_search() { 00132 if (empty($this->token)) { 00133 return false; 00134 } else { 00135 return true; 00136 } 00137 } 00138 00143 public function print_login() { 00144 if ($this->options['ajax']) { 00145 $ret = array(); 00146 $popup_btn = new stdClass(); 00147 $popup_btn->type = 'popup'; 00148 $popup_btn->url = $this->flickr->auth(); 00149 $ret['login'] = array($popup_btn); 00150 return $ret; 00151 } else { 00152 echo '<a target="_blank" href="'.$this->flickr->auth().'">'.get_string('login', 'repository').'</a>'; 00153 } 00154 } 00155 00162 private function build_list($photos, $page = 1) { 00163 $photos_url = $this->flickr->urls_getUserPhotos($this->nsid); 00164 $ret = array(); 00165 $ret['manage'] = $photos_url; 00166 $ret['list'] = array(); 00167 $ret['pages'] = $photos['pages']; 00168 $ret['total'] = $photos['total']; 00169 $ret['perpage'] = $photos['perpage']; 00170 if($page <= $ret['pages']) { 00171 $ret['page'] = $page; 00172 } else { 00173 $ret['page'] = 1; 00174 } 00175 if (!empty($photos['photo'])) { 00176 foreach ($photos['photo'] as $p) { 00177 if(empty($p['title'])) { 00178 $p['title'] = get_string('notitle', 'repository_flickr'); 00179 } 00180 if (isset($p['originalformat'])) { 00181 $format = $p['originalformat']; 00182 } else { 00183 $format = 'jpg'; 00184 } 00185 $format = '.'.$format; 00186 // append extensions to the files 00187 if (substr($p['title'], strlen($p['title'])-strlen($format)) != $format) { 00188 $p['title'] .= $format; 00189 } 00190 $ret['list'][] = array('title'=>$p['title'],'source'=>$p['id'], 00191 'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'), 00192 'date'=>'', 'size'=>'unknown', 'url'=>$photos_url.$p['id']); 00193 } 00194 } 00195 return $ret; 00196 } 00197 00203 public function search($search_text) { 00204 $photos = $this->flickr->photos_search(array( 00205 'user_id'=>$this->nsid, 00206 'per_page'=>24, 00207 'extras'=>'original_format', 00208 'text'=>$search_text 00209 )); 00210 $ret = $this->build_list($photos); 00211 $ret['list'] = array_filter($ret['list'], array($this, 'filter')); 00212 return $ret; 00213 } 00214 00221 public function get_listing($path = '', $page = '1') { 00222 $photos_url = $this->flickr->urls_getUserPhotos($this->nsid); 00223 00224 $photos = $this->flickr->photos_search(array( 00225 'user_id'=>$this->nsid, 00226 'per_page'=>24, 00227 'page'=>$page, 00228 'extras'=>'original_format' 00229 )); 00230 return $this->build_list($photos, $page); 00231 } 00232 00233 public function get_link($photo_id) { 00234 global $CFG; 00235 $result = $this->flickr->photos_getSizes($photo_id); 00236 $url = ''; 00237 if(!empty($result[4])) { 00238 $url = $result[4]['source']; 00239 } elseif(!empty($result[3])) { 00240 $url = $result[3]['source']; 00241 } elseif(!empty($result[2])) { 00242 $url = $result[2]['source']; 00243 } 00244 return $url; 00245 } 00246 00253 public function get_file($photo_id, $file = '') { 00254 global $CFG; 00255 $result = $this->flickr->photos_getSizes($photo_id); 00256 $url = ''; 00257 if(!empty($result[4])) { 00258 $url = $result[4]['source']; 00259 } elseif(!empty($result[3])) { 00260 $url = $result[3]['source']; 00261 } elseif(!empty($result[2])) { 00262 $url = $result[2]['source']; 00263 } 00264 $path = $this->prepare_file($file); 00265 $fp = fopen($path, 'w'); 00266 $c = new curl; 00267 $c->download(array( 00268 array('url'=>$url, 'file'=>$fp) 00269 )); 00270 return array('path'=>$path, 'url'=>$url); 00271 } 00272 00277 public function type_config_form($mform) { 00278 global $CFG; 00279 $api_key = get_config('flickr', 'api_key'); 00280 $secret = get_config('flickr', 'secret'); 00281 00282 if (empty($api_key)) { 00283 $api_key = ''; 00284 } 00285 if (empty($secret)) { 00286 $secret = ''; 00287 } 00288 00289 $strrequired = get_string('required'); 00290 $mform->addElement('text', 'api_key', get_string('apikey', 'repository_flickr'), array('value'=>$api_key,'size' => '40')); 00291 $mform->addElement('text', 'secret', get_string('secret', 'repository_flickr'), array('value'=>$secret,'size' => '40')); 00292 00293 //retrieve the flickr instances 00294 $params = array(); 00295 $params['context'] = array(); 00296 //$params['currentcontext'] = $this->context; 00297 $params['onlyvisible'] = false; 00298 $params['type'] = 'flickr'; 00299 $instances = repository::get_instances($params); 00300 if (empty($instances)) { 00301 $callbackurl = get_string('callbackwarning', 'repository_flickr'); 00302 $mform->addElement('static', null, '', $callbackurl); 00303 } else { 00304 $instance = array_shift($instances); 00305 $callbackurl = $CFG->wwwroot.'/repository/repository_callback.php?repo_id='.$instance->id; 00306 $mform->addElement('static', 'callbackurl', '', get_string('callbackurltext', 'repository_flickr', $callbackurl)); 00307 } 00308 00309 $mform->addRule('api_key', $strrequired, 'required', null, 'client'); 00310 $mform->addRule('secret', $strrequired, 'required', null, 'client'); 00311 } 00312 00317 public static function get_type_option_names() { 00318 return array('api_key', 'secret', 'pluginname'); 00319 } 00320 public function supported_filetypes() { 00321 return array('web_image'); 00322 } 00323 public function supported_returntypes() { 00324 return (FILE_INTERNAL | FILE_EXTERNAL); 00325 } 00326 }