Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/portfolio/flickr/lib.php
Go to the documentation of this file.
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 
00025 defined('MOODLE_INTERNAL') || die();
00026 
00027 require_once($CFG->libdir.'/portfolio/plugin.php');
00028 require_once($CFG->libdir.'/filelib.php');
00029 require_once($CFG->libdir.'/flickrlib.php');
00030 
00031 class portfolio_plugin_flickr extends portfolio_plugin_push_base {
00032 
00033     private $flickr;
00034     private $token;
00035     private $raw_sets;
00036 
00037     public function supported_formats() {
00038         return array(PORTFOLIO_FORMAT_IMAGE);
00039     }
00040 
00041     public static function get_name() {
00042         return get_string('pluginname', 'portfolio_flickr');
00043     }
00044 
00045     public function prepare_package() {
00046 
00047     }
00048 
00049     public function send_package() {
00050         foreach ($this->exporter->get_tempfiles() as $file) {
00051             // @TODO get max size from flickr people_getUploadStatus
00052             $filesize = $file->get_filesize();
00053 
00054             if ($file->is_valid_image()) {
00055                 $return = $this->flickr->upload($file, array(
00056                         'title'         => $this->get_export_config('title'),
00057                         'description'   => $this->get_export_config('description'),
00058                         'tags'          => $this->get_export_config('tags'),
00059                         'is_public'     => $this->get_export_config('is_public'),
00060                         'is_friend'     => $this->get_export_config('is_friend'),
00061                         'is_family'     => $this->get_export_config('is_family'),
00062                         'safety_level'  => $this->get_export_config('safety_level'),
00063                         'content_type'  => $this->get_export_config('content_type'),
00064                         'hidden'        => $this->get_export_config('hidden')));
00065                 if ($return) {
00066                     // Attach photo to a set if requested
00067                     if ($this->get_export_config('set')) {
00068                         $this->flickr->photosets_addPhoto($this->get_export_config('set'),
00069                             $this->flickr->parsed_response['photoid']);
00070                     }
00071                 } else {
00072                     throw new portfolio_plugin_exception('uploadfailed', 'portfolio_flickr',
00073                         $this->flickr->error_code . ': ' . $this->flickr->error_msg);
00074                 }
00075             }
00076         }
00077     }
00078 
00079     public static function allows_multiple_instances() {
00080         return false;
00081     }
00082 
00083     public function get_interactive_continue_url() {
00084         return $this->flickr->urls_getUserPhotos();
00085     }
00086 
00087     public function expected_time($callertime) {
00088         return $callertime;
00089     }
00090 
00091     public static function get_allowed_config() {
00092         return array('apikey', 'sharedsecret');
00093     }
00094 
00095     public static function has_admin_config() {
00096         return true;
00097     }
00098 
00099     public function admin_config_form(&$mform) {
00100         global $CFG;
00101 
00102         $strrequired = get_string('required');
00103         $mform->addElement('text', 'apikey', get_string('apikey', 'portfolio_flickr'), array('size' => 30));
00104         $mform->addRule('apikey', $strrequired, 'required', null, 'client');
00105         $mform->addElement('text', 'sharedsecret', get_string('sharedsecret', 'portfolio_flickr'));
00106         $mform->addRule('sharedsecret', $strrequired, 'required', null, 'client');
00107         $a = new stdClass();
00108         $a->applyurl = 'http://www.flickr.com/services/api/keys/apply/';
00109         $a->keysurl = 'http://www.flickr.com/services/api/keys/';
00110         $a->callbackurl = $CFG->wwwroot . '/portfolio/add.php?postcontrol=1&type=flickr';
00111         $mform->addElement('static', 'setupinfo', get_string('setupinfo', 'portfolio_flickr'),
00112             get_string('setupinfodetails', 'portfolio_flickr', $a));
00113     }
00114 
00115     public function has_export_config() {
00116         return true;
00117     }
00118 
00119     public function get_allowed_user_config() {
00120         return array('authtoken', 'nsid');
00121     }
00122 
00123     public function steal_control($stage) {
00124         if ($stage != PORTFOLIO_STAGE_CONFIG) {
00125             return false;
00126         }
00127         if ($this->token) {
00128             return false;
00129         }
00130 
00131         $token = $this->get_user_config('authtoken', $this->get('user')->id);
00132         $nsid = $this->get_user_config('nsid', $this->get('user')->id);
00133 
00134         $this->flickr = new phpFlickr($this->get_config('apikey'), $this->get_config('sharedsecret'), $token);
00135 
00136         if (!empty($token)) {
00137             $this->token = $token;
00138             $this->flickr = new phpFlickr($this->get_config('apikey'), $this->get_config('sharedsecret'), $token);
00139             return false;
00140         }
00141         return $this->flickr->auth('write');
00142     }
00143 
00144     public function post_control($stage, $params) {
00145         if ($stage != PORTFOLIO_STAGE_CONFIG) {
00146             return;
00147         }
00148         if (!array_key_exists('frob', $params) || empty($params['frob'])) {
00149             throw new portfolio_plugin_exception('noauthtoken', 'portfolio_flickr');
00150         }
00151 
00152         $this->flickr = new phpFlickr($this->get_config('apikey'), $this->get_config('sharedsecret'));
00153 
00154         $auth_info = $this->flickr->auth_getToken($params['frob']);
00155 
00156         $this->set_user_config(array('authtoken' => $auth_info['token'], 'nsid' => $auth_info['user']['nsid']), $this->get('user')->id);
00157     }
00158 
00159     public function export_config_form(&$mform) {
00160         $mform->addElement('text', 'plugin_title', get_string('title', 'portfolio_flickr'));
00161         $mform->addElement('textarea', 'plugin_description', get_string('description'));
00162         $mform->addElement('text', 'plugin_tags', get_string('tags'));
00163         $mform->addElement('checkbox', 'plugin_is_public', get_string('ispublic', 'portfolio_flickr'));
00164         $mform->addElement('checkbox', 'plugin_is_family', get_string('isfamily', 'portfolio_flickr'));
00165         $mform->addElement('checkbox', 'plugin_is_friend', get_string('isfriend', 'portfolio_flickr'));
00166 
00167         $mform->disabledIf('plugin_is_friend', 'plugin_is_public', 'checked');
00168         $mform->disabledIf('plugin_is_family', 'plugin_is_public', 'checked');
00169 
00170         $safety_levels = array(1 => $this->get_export_value_name('safety_level', 1),
00171                                2 => $this->get_export_value_name('safety_level', 2),
00172                                3 => $this->get_export_value_name('safety_level', 3));
00173 
00174         $content_types = array(1 => $this->get_export_value_name('content_type', 1),
00175                                2 => $this->get_export_value_name('content_type', 2),
00176                                3 => $this->get_export_value_name('content_type', 3));
00177 
00178         $hidden_values = array(1,2);
00179 
00180         $mform->addElement('select', 'plugin_safety_level', get_string('safetylevel', 'portfolio_flickr'), $safety_levels);
00181         $mform->addElement('select', 'plugin_content_type', get_string('contenttype', 'portfolio_flickr'), $content_types);
00182         $mform->addElement('advcheckbox', 'plugin_hidden', get_string('hidefrompublicsearches', 'portfolio_flickr'), get_string('yes'), null, $hidden_values);
00183 
00184         $mform->setDefaults(array('plugin_is_public' => true));
00185 
00186         $sets = $this->get_sets();
00187 
00188         if (!empty($sets)) {
00189             $sets[0] = '----';
00190             $mform->addElement('select', 'plugin_set', get_string('set', 'portfolio_flickr'), $sets);
00191         }
00192     }
00193 
00194     private function get_sets() {
00195         if (empty($this->raw_sets)) {
00196             $this->raw_sets = $this->flickr->photosets_getList();
00197         }
00198 
00199         $sets = array();
00200         foreach ($this->raw_sets['photoset'] as $set_data) {
00201             $sets[$set_data['id']] = $set_data['title'];
00202         }
00203         return $sets;
00204     }
00205 
00206     public function get_allowed_export_config() {
00207         return array('set', 'title', 'description', 'tags', 'is_public', 'is_family', 'is_friend', 'safety_level', 'content_type', 'hidden');
00208     }
00209 
00210     public function get_export_summary() {
00211         return array(get_string('set', 'portfolio_flickr') => $this->get_export_value_name('set', $this->get_export_config('set')),
00212                      get_string('title', 'portfolio_flickr') => $this->get_export_config('title'),
00213                      get_string('description') => $this->get_export_config('description'),
00214                      get_string('tags') => $this->get_export_config('tags'),
00215                      get_string('ispublic', 'portfolio_flickr') => $this->get_export_value_name('is_public', $this->get_export_config('is_public')),
00216                      get_string('isfamily', 'portfolio_flickr') => $this->get_export_value_name('is_family', $this->get_export_config('is_family')),
00217                      get_string('isfriend', 'portfolio_flickr') => $this->get_export_value_name('is_friend', $this->get_export_config('is_friend')),
00218                      get_string('safetylevel', 'portfolio_flickr') => $this->get_export_value_name('safety_level', $this->get_export_config('safety_level')),
00219                      get_string('contenttype', 'portfolio_flickr') => $this->get_export_value_name('content_type', $this->get_export_config('content_type')),
00220                      get_string('hidefrompublicsearches', 'portfolio_flickr') => $this->get_export_value_name('hidden', $this->get_export_config('hidden')));
00221     }
00222 
00223     private function get_export_value_name($param, $value) {
00224         $params = array('set' => $this->get_sets(),
00225                         'is_public' => array(0 => get_string('no'), 1 => get_string('yes')),
00226                         'is_family' => array(0 => get_string('no'), 1 => get_string('yes')),
00227                         'is_friend' => array(0 => get_string('no'), 1 => get_string('yes')),
00228                         'safety_level' => array(1 => get_string('safe', 'portfolio_flickr'),
00229                                                 2 => get_string('moderate', 'portfolio_flickr'),
00230                                                 3 => get_string('restricted', 'portfolio_flickr')),
00231                         'content_type' => array(1 => get_string('photo', 'portfolio_flickr'),
00232                                                 2 => get_string('screenshot', 'portfolio_flickr'),
00233                                                 3 => get_string('other', 'portfolio_flickr')),
00234                         'hidden' => array(1 => get_string('no'), 2 => get_string('yes')));
00235 
00236         if (isset($params[$param][$value])) {
00237             return $params[$param][$value];
00238         } else {
00239             return '-';
00240         }
00241     }
00242 
00246     public static function allows_multiple_exports() {
00247         return false;
00248     }
00249 }
 All Data Structures Namespaces Files Functions Variables Enumerations