Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/repository/flickr_public/lib.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00031 require_once($CFG->libdir.'/flickrlib.php');
00032 require_once(dirname(__FILE__) . '/image.php');
00033 
00034 class repository_flickr_public extends repository {
00035     private $flickr;
00036     public $photos;
00037 
00048     public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array(), $readonly=0) {
00049         global $CFG, $SESSION;
00050         parent::__construct($repositoryid, $context, $options,$readonly);
00051         $this->api_key = $this->get_option('api_key');
00052         $this->flickr  = new phpFlickr($this->api_key);
00053         $this->flickr_account = $this->get_option('email_address');
00054         $this->usewatermarks = $this->get_option('usewatermarks');
00055 
00056         $account  = optional_param('flickr_account', '', PARAM_RAW);
00057         $fulltext = optional_param('flickr_fulltext', '', PARAM_RAW);
00058         if (empty($fulltext)) {
00059             $fulltext = optional_param('s', '', PARAM_RAW);
00060         }
00061         $tag      = optional_param('flickr_tag', '', PARAM_RAW);
00062         $license  = optional_param('flickr_license', '', PARAM_RAW);
00063 
00064         $this->sess_account = 'flickr_public_'.$this->id.'_account';
00065         $this->sess_tag     = 'flickr_public_'.$this->id.'_tag';
00066         $this->sess_text    = 'flickr_public_'.$this->id.'_text';
00067 
00068         if (!empty($account) or !empty($fulltext) or !empty($tag) or !empty($license)) {
00069             $SESSION->{$this->sess_tag}  = $tag;
00070             $SESSION->{$this->sess_text} = $fulltext;
00071             $SESSION->{$this->sess_account} = $account;
00072         }
00073     }
00074 
00080     public function set_option($options = array()) {
00081         if (!empty($options['api_key'])) {
00082             set_config('api_key', trim($options['api_key']), 'flickr_public');
00083         }
00084         unset($options['api_key']);
00085         return parent::set_option($options);
00086     }
00087 
00094     public function get_option($config = '') {
00095         if ($config==='api_key') {
00096             return trim(get_config('flickr_public', 'api_key'));
00097         } else {
00098             $options['api_key'] = trim(get_config('flickr_public', 'api_key'));
00099         }
00100         return parent::get_option($config);
00101     }
00102 
00108     public function global_search() {
00109         if (empty($this->flickr_account)) {
00110             return false;
00111         } else {
00112             return true;
00113         }
00114     }
00115 
00120     public function check_login() {
00121         return !empty($this->flickr_account);
00122     }
00123 
00130     public function print_login() {
00131         if ($this->options['ajax']) {
00132             $ret = array();
00133             $fulltext = new stdClass();
00134             $fulltext->label = get_string('fulltext', 'repository_flickr_public').': ';
00135             $fulltext->id    = 'el_fulltext';
00136             $fulltext->type = 'text';
00137             $fulltext->name = 'flickr_fulltext';
00138 
00139             $tag = new stdClass();
00140             $tag->label = get_string('tag', 'repository_flickr_public').': ';
00141             $tag->id    = 'el_tag';
00142             $tag->type = 'text';
00143             $tag->name = 'flickr_tag';
00144 
00145             $email_field = new stdClass();
00146             $email_field->label = get_string('username', 'repository_flickr_public').': ';
00147             $email_field->id    = 'account';
00148             $email_field->type = 'text';
00149             $email_field->name = 'flickr_account';
00150 
00151             $commercial = new stdClass();
00152             $commercial->label = get_string('commercialuse', 'repository_flickr_public').': ';
00153             $commercial->id    = 'flickr_commercial_id';
00154             $commercial->type  = 'checkbox';
00155             $commercial->name  = 'flickr_commercial';
00156             $commercial->value = 'yes';
00157 
00158             $modification = new stdClass();
00159             $modification->label = get_string('modification', 'repository_flickr_public').': ';
00160             $modification->id    = 'flickr_modification_id';
00161             $modification->type  = 'checkbox';
00162             $modification->name  = 'flickr_modification';
00163             $modification->value = 'yes';
00164 
00165             $ret['login'] = array($fulltext, $tag, $email_field, $commercial, $modification);
00166             $ret['login_btn_label'] = get_string('search');
00167             $ret['login_btn_action'] = 'search';
00168             return $ret;
00169         } else {
00170             echo '<table>';
00171             echo '<tr><td><label>'.get_string('fulltext', 'repository_flickr_public').'</label></td>';
00172             echo '<td><input type="text" name="flickr_fulltext" /></td></tr>';
00173             echo '<tr><td><label>'.get_string('tag', 'repository_flickr_public').'</label></td>';
00174             echo '<td><input type="text" name="flickr_tag" /></td></tr>';
00175             echo '<tr><td><label>'.get_string('username', 'repository_flickr_public').'</label></td>';
00176             echo '<td><input type="text" name="flickr_account" /></td></tr>';
00177 
00178             echo '<tr><td><label>'.get_string('commercialuse', 'repository_flickr_public').'</label></td>';
00179             echo '<td>';
00180             echo '<input type="checkbox" name="flickr_commercial" value="yes" />';
00181             echo '</td></tr>';
00182 
00183             echo '<tr><td><label>'.get_string('modification', 'repository_flickr_public').'</label></td>';
00184             echo '<td>';
00185             echo '<input type="checkbox" name="flickr_modification" value="yes" />';
00186             echo '</td></tr>';
00187 
00188             echo '</table>';
00189 
00190             echo '<input type="hidden" name="action" value="search" />';
00191             echo '<input type="submit" value="'.get_string('search', 'repository').'" />';
00192         }
00193     }
00194 
00200     public function logout() {
00201         global $SESSION;
00202         unset($SESSION->{$this->sess_tag});
00203         unset($SESSION->{$this->sess_text});
00204         unset($SESSION->{$this->sess_account});
00205         return $this->print_login();
00206     }
00207 
00208     public function license4moodle ($license_id) {
00209         $license = array(
00210             '1' => 'cc-nc-sa',
00211             '2' => 'cc-nc',
00212             '3' => 'cc-nc-nd',
00213             '4' => 'cc',
00214             '5' => 'cc-sa',
00215             '6' => 'cc-nd',
00216             '7' => 'allrightsreserved'
00217             );
00218         return $license[$license_id];
00219     }
00220 
00227     public function search($search_text, $page = 1) {
00228         global $SESSION;
00229         $ret = array();
00230         if (empty($page)) {
00231             $page = 1;
00232         }
00233 
00234         if (!empty($this->flickr_account)) {
00235             $people = $this->flickr->people_findByEmail($this->flickr_account);
00236             $this->nsid = $people['nsid'];
00237         }
00238         if (!empty($SESSION->{$this->sess_account})) {
00239             $people = $this->flickr->people_findByEmail($SESSION->{$this->sess_account});
00240             $this->nsid = $people['nsid'];
00241         }
00242         if (empty($this->nsid)) {
00243             $this->nsid = null;
00244             // user specify a flickr account, but it is not valid
00245             if (!empty($this->flickr_account) or !empty($SESSION->{$this->sess_account})) {
00246                 $ret['e'] = get_string('invalidemail', 'repository_flickr_public');
00247                 return $ret;
00248             }
00249         }
00250 
00251         // including all licenses by default
00252         $licenses = array(1=>1, 2, 3, 4, 5, 6, 7);
00253 
00254         $commercial   = optional_param('flickr_commercial', '', PARAM_RAW);
00255         $modification = optional_param('flickr_modification', '', PARAM_RAW);
00256 
00257         if ($commercial == 'yes') {
00258             // including
00259             // 4: Attribution License
00260             // 5: Attribution ShareAlike
00261             // 6: Attribution NoDerives
00262             // 7: unknown license
00263             unset($licenses[1], $licenses[2], $licenses[3]);
00264         }
00265         if ($modification == 'yes') {
00266             // including
00267             // 1: Attribution NonCommercial ShareAlike
00268             // 2: Attribution NonCommercial
00269             // 4: Attribution License
00270             // 5: Attribution ShareAlike
00271             // 7: unknown license
00272             unset($licenses[3], $licenses[6]);
00273         }
00274         //if ($modification == 'sharealike') {
00275             // including
00276             // 1: Attribution NonCommercial ShareAlike
00277             // 5: Attribution ShareAlike
00278             //unset($licenses[2], $licenses[3], $licenses[4], $licenses[6], $licenses[7]);
00279         //}
00280 
00281         $licenses = implode(',', $licenses);
00282 
00283         $tag  = !empty($SESSION->{$this->sess_tag})  ? $SESSION->{$this->sess_tag}  : null;
00284         $text = !empty($SESSION->{$this->sess_text}) ? $SESSION->{$this->sess_text} : null;
00285         $nsid = !empty($this->nsid) ? $this->nsid : null;
00286 
00287         $photos = $this->flickr->photos_search(array(
00288             'tags'=>$tag,
00289             'page'=>$page,
00290             'per_page'=>24,
00291             'user_id'=>$nsid,
00292             'license'=>$licenses,
00293             'text'=>$text
00294             )
00295         );
00296         $ret['total'] = $photos['total'];
00297         $ret['perpage'] = $photos['perpage'];
00298         if (empty($photos)) {
00299             $ret['list'] = array();
00300             return $ret;
00301         }
00302         $ret = $this->build_list($photos, $page, $ret);
00303         $ret['list'] = array_filter($ret['list'], array($this, 'filter'));
00304         return $ret;
00305     }
00306 
00314     public function get_listing($path = '', $page = 1) {
00315         $people = $this->flickr->people_findByEmail($this->flickr_account);
00316         $this->nsid = $people['nsid'];
00317         $photos = $this->flickr->people_getPublicPhotos($people['nsid'], 'original_format', 24, $page);
00318         $ret = array();
00319 
00320         return $this->build_list($photos, $page, $ret);
00321     }
00322 
00330     private function build_list($photos, $page = 1, &$ret) {
00331         if (!empty($this->nsid)) {
00332             $photos_url = $this->flickr->urls_getUserPhotos($this->nsid);
00333             $ret['manage'] = $photos_url;
00334         }
00335         $ret['list']  = array();
00336         $ret['nosearch'] = true;
00337         $ret['norefresh'] = true;
00338         $ret['logouttext'] = get_string('backtosearch', 'repository_flickr_public');
00339         $ret['pages'] = $photos['pages'];
00340         if (is_int($page) && $page <= $ret['pages']) {
00341             $ret['page'] = $page;
00342         } else {
00343             $ret['page'] = 1;
00344         }
00345         if (!empty($photos['photo'])) {
00346             foreach ($photos['photo'] as $p) {
00347                 if(empty($p['title'])) {
00348                     $p['title'] = get_string('notitle', 'repository_flickr');
00349                 }
00350                 if (isset($p['originalformat'])) {
00351                     $format = $p['originalformat'];
00352                 } else {
00353                     $format = 'jpg';
00354                 }
00355                 $format = '.'.$format;
00356                 if (substr($p['title'], strlen($p['title'])-strlen($format)) != $format) {
00357                     // append author id
00358                     // $p['title'] .= '-'.$p['owner'];
00359                     // append file extension
00360                     $p['title'] .= $format;
00361                 }
00362                 $ret['list'][] = array(
00363                     'title'=>$p['title'],
00364                     'source'=>$p['id'],
00365                     'id'=>$p['id'],
00366                     'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'),
00367                     'date'=>'',
00368                     'size'=>'unknown',
00369                     'url'=>'http://www.flickr.com/photos/'.$p['owner'].'/'.$p['id'],
00370                     'haslicense'=>true,
00371                     'hasauthor'=>true
00372                 );
00373             }
00374         }
00375         return $ret;
00376     }
00377 
00383     public function print_search() {
00384         $str = '';
00385         $str .= '<input type="hidden" name="repo_id" value="'.$this->id.'" />';
00386         $str .= '<input type="hidden" name="ctx_id" value="'.$this->context->id.'" />';
00387         $str .= '<input type="hidden" name="seekey" value="'.sesskey().'" />';
00388         $str .= '<label>'.get_string('fulltext', 'repository_flickr_public').': </label><br/><input name="s" value="" /><br/>';
00389         $str .= '<label>'.get_string('tag', 'repository_flickr_public').'</label><br /><input type="text" name="flickr_tag" /><br />';
00390         return $str;
00391     }
00392 
00393     public function get_link($photo_id) {
00394         global $CFG;
00395         $result = $this->flickr->photos_getSizes($photo_id);
00396         $url = '';
00397         if(!empty($result[4])) {
00398             $url = $result[4]['source'];
00399         } elseif(!empty($result[3])) {
00400             $url = $result[3]['source'];
00401         } elseif(!empty($result[2])) {
00402             $url = $result[2]['source'];
00403         }
00404         return $url;
00405     }
00406 
00414     public function get_file($photo_id, $file = '') {
00415         global $CFG;
00416         $info = $this->flickr->photos_getInfo($photo_id);
00417         if ($info['owner']['realname']) {
00418             $author = $info['owner']['realname'];
00419         } else {
00420             $author = $info['owner']['username'];
00421         }
00422         $copyright = get_string('author', 'repository') . ': ' . $author;
00423         $result = $this->flickr->photos_getSizes($photo_id);
00424         // download link
00425         $source = '';
00426         // flickr photo page
00427         $url = '';
00428         if (!empty($result[4])) {
00429             $source = $result[4]['source'];
00430             $url = $result[4]['url'];
00431         } elseif(!empty($result[3])) {
00432             $source = $result[3]['source'];
00433             $url = $result[3]['url'];
00434         } elseif(!empty($result[2])) {
00435             $source = $result[2]['source'];
00436             $url = $result[2]['url'];
00437         }
00438         $path = $this->prepare_file($file);
00439         $fp = fopen($path, 'w');
00440         $c = new curl;
00441         $c->download(array(array('url'=>$source, 'file'=>$fp)));
00442         // must close file handler, otherwise gd lib will fail to process it
00443         fclose($fp);
00444         if (!empty($this->usewatermarks)) {
00445             $img = new moodle_image($path);
00446             $img->watermark($copyright, array(10,10), array('ttf'=>true, 'fontsize'=>12))->saveas($path);
00447         }
00448 
00449         return array('path'=>$path, 'url'=>$url, 'author'=>$info['owner']['realname'], 'license'=>$this->license4moodle($info['license']));
00450     }
00451 
00456     public function instance_config_form($mform) {
00457         $mform->addElement('text', 'email_address', get_string('emailaddress', 'repository_flickr_public'));
00458         $mform->addElement('checkbox', 'usewatermarks', get_string('watermark', 'repository_flickr_public'));
00459         $mform->setDefault('usewatermarks', 0);
00460     }
00461 
00466     public static function get_instance_option_names() {
00467         return array('email_address', 'usewatermarks');
00468     }
00469 
00474     public function type_config_form($mform) {
00475         $api_key = get_config('flickr_public', 'api_key');
00476         if (empty($api_key)) {
00477             $api_key = '';
00478         }
00479         $strrequired = get_string('required');
00480 
00481         $mform->addElement('text', 'api_key', get_string('apikey', 'repository_flickr_public'), array('value'=>$api_key,'size' => '40'));
00482         $mform->addRule('api_key', $strrequired, 'required', null, 'client');
00483 
00484         $mform->addElement('static', null, '',  get_string('information','repository_flickr_public'));
00485     }
00486 
00491     public static function get_type_option_names() {
00492         return array('api_key', 'pluginname');
00493     }
00494 
00498     public static function plugin_init() {
00499         //here we create a default instance for this type
00500 
00501         $id = repository::static_function('flickr_public','create', 'flickr_public', 0, get_system_context(), array('name'=>'', 'email_address' => null, 'usewatermarks' => false), 0);
00502         if (empty($id)) {
00503             return false;
00504         } else {
00505             return true;
00506         }
00507     }
00508     public function supported_filetypes() {
00509         return array('web_image');
00510     }
00511     public function supported_returntypes() {
00512         return (FILE_INTERNAL | FILE_EXTERNAL);
00513     }
00514 }
 All Data Structures Namespaces Files Functions Variables Enumerations