MDL-18167 towareds reimplementing of win installer support
[moodle.git] / portfolio / type / flickr / lib.php
1 <?php
2 require_once($CFG->libdir.'/filelib.php');
3 require_once($CFG->libdir.'/flickrlib.php');
5 class portfolio_plugin_flickr extends portfolio_plugin_push_base {
7     private $flickr;
8     private $token;
9     private $raw_sets;
11     public static function supported_formats() {
12         return array(PORTFOLIO_FORMAT_IMAGE);
13     }
15     public static function get_name() {
16         return get_string('pluginname', 'portfolio_flickr');
17     }
19     public function prepare_package() {
21     }
23     public function send_package() {
24         foreach ($this->exporter->get_tempfiles() as $file) {
25             // @TODO get max size from flickr people_getUploadStatus
26             $filesize = $file->get_filesize();
28             if ($file->is_valid_image()) {
29                 /**
30                 $return = $this->flickr->upload (array('photo' => $file,
31                                                                 'title' => $this->get_export_config('title'),
32                                                                 'description' => $this->get_export_config('description'),
33                                                                 'tags' => $this->get_export_config('tags'),
34                                                                 'is_public' => $this->get_export_config('is_public'),
35                                                                 'is_friend' => $this->get_export_config('is_friend'),
36                                                                 'is_family' => $this->get_export_config('is_family'),
37                                                                 'safety_level' => $this->get_export_config('safety_level'),
38                                                                 'content_type' => $this->get_export_config('content_type'),
39                                                                 'hidden' => $this->get_export_config('hidden')));
40                  */
41                 $return = $this->flickr->upload($file, $this->get_export_config('title'), $this->get_export_config('description'), $this->get_export_config('tags'), $this->get_export_config('is_public'), $this->get_export_config('is_friend'), $this->get_export_config('is_family'));
42                 if ($return) {
43                     // Attach photo to a set if requested
44                     if ($this->get_export_config('set')) {
45                         $this->flickr->photosets_addPhoto($this->get_export_config('set'), $this->flickr->parsed_response['photoid']);
46                     }
47                 } else {
48                     throw new portfolio_plugin_exception('uploadfailed', 'portfolio_flickr', $this->flickr->error_code . ': ' . $this->flickr->error_msg);
49                 }
50             }
51         }
52     }
54     public static function allows_multiple() {
55         return false;
56     }
58     public function get_continue_url() {
59         // return $this->flickr->urls_getUserPhotos();
60         return "http://www.flickr.com/tools/uploader_edit.gne?ids="; // Add ids of uploaded files
61     }
63     public function expected_time($callertime) {
64         return $callertime;
65     }
67     public static function get_allowed_config() {
68         return array('apikey', 'sharedsecret');
69     }
71     public static function has_admin_config() {
72         return true;
73     }
75     public function admin_config_form(&$mform) {
76         $strrequired = get_string('required');
77         $mform->addElement('text', 'apikey', get_string('apikey', 'portfolio_flickr'));
78         $mform->addRule('apikey', $strrequired, 'required', null, 'client');
79         $mform->addElement('text', 'sharedsecret', get_string('sharedsecret', 'portfolio_flickr'));
80         $mform->addRule('sharedsecret', $strrequired, 'required', null, 'client');
81     }
83     public function has_export_config() {
84         return true;
85     }
87     public function get_allowed_user_config() {
88         return array('authtoken', 'nsid');
89     }
91     public function steal_control($stage) {
92         if ($stage != PORTFOLIO_STAGE_CONFIG) {
93             return false;
94         }
95         if ($this->token) {
96             return false;
97         }
99         $token = $this->get_user_config('authtoken', $this->get('user')->id);
100         $nsid = $this->get_user_config('nsid', $this->get('user')->id);
102         $this->flickr = new phpFlickr($this->get_config('apikey'), $this->get_config('sharedsecret'), $token);
104         if (!empty($token)) {
105             $this->token = $token;
106             $this->flickr = new phpFlickr($this->get_config('apikey'), $this->get_config('sharedsecret'), $token);
107             return false;
108         }
109         return $this->flickr->auth('write');
110     }
112     public function post_control($stage, $params) {
113         if ($stage != PORTFOLIO_STAGE_CONFIG) {
114             return;
115         }
116         if (!array_key_exists('frob', $params) || empty($params['frob'])) {
117             throw new portfolio_plugin_exception('noauthtoken', 'portfolio_flickr');
118         }
120         $this->flickr = new phpFlickr($this->get_config('apikey'), $this->get_config('sharedsecret'));
122         $auth_info = $this->flickr->auth_getToken($params['frob']);
124         $this->set_user_config(array('authtoken' => $auth_info['token'], 'nsid' => $auth_info['user']['nsid']), $this->get('user')->id);
125     }
127     public function export_config_form(&$mform) {
128         $mform->addElement('text', 'plugin_title', get_string('title', 'portfolio_flickr'));
129         $mform->addElement('textarea', 'plugin_description', get_string('description'));
130         $mform->addElement('text', 'plugin_tags', get_string('tags'));
131         $mform->addElement('checkbox', 'plugin_is_public', get_string('ispublic', 'portfolio_flickr'));
132         $mform->addElement('checkbox', 'plugin_is_family', get_string('isfamily', 'portfolio_flickr'));
133         $mform->addElement('checkbox', 'plugin_is_friend', get_string('isfriend', 'portfolio_flickr'));
135         $mform->disabledIf('plugin_is_friend', 'plugin_is_public', 'checked');
136         $mform->disabledIf('plugin_is_family', 'plugin_is_public', 'checked');
138         $safety_levels = array(1 => $this->get_export_value_name('safety_level', 1),
139                                2 => $this->get_export_value_name('safety_level', 2),
140                                3 => $this->get_export_value_name('safety_level', 3));
142         $content_types = array(1 => $this->get_export_value_name('content_type', 1),
143                                2 => $this->get_export_value_name('content_type', 2),
144                                3 => $this->get_export_value_name('content_type', 3));
146         $hidden_values = array(1,2);
148         $mform->addElement('select', 'plugin_safety_level', get_string('safetylevel', 'portfolio_flickr'), $safety_levels);
149         $mform->addElement('select', 'plugin_content_type', get_string('contenttype', 'portfolio_flickr'), $content_types);
150         $mform->addElement('advcheckbox', 'plugin_hidden', get_string('hidefrompublicsearches', 'portfolio_flickr'), get_string('yes'), null, $hidden_values);
152         $mform->setDefaults(array('plugin_is_public' => true));
154         $sets = $this->get_sets();
156         if (!empty($sets)) {
157             $sets[0] = '----';
158             $mform->addElement('select', 'plugin_set', get_string('set', 'portfolio_flickr'), $sets);
159         }
160     }
162     private function get_sets() {
163         if (empty($this->raw_sets)) {
164             $this->raw_sets = $this->flickr->photosets_getList();
165         }
167         $sets = array();
168         foreach ($this->raw_sets['photoset'] as $set_data) {
169             $sets[$set_data['id']] = $set_data['title'];
170         }
171         return $sets;
172     }
174     public function get_allowed_export_config() {
175         return array('set', 'title', 'description', 'tags', 'is_public', 'is_family', 'is_friend', 'safety_level', 'content_type', 'hidden');
176     }
178     public function get_export_summary() {
179         return array(get_string('set', 'portfolio_flickr') => $this->get_export_value_name('set', $this->get_export_config('set')),
180                      get_string('title', 'portfolio_flickr') => $this->get_export_config('title'),
181                      get_string('description') => $this->get_export_config('description'),
182                      get_string('tags') => $this->get_export_config('tags'),
183                      get_string('ispublic', 'portfolio_flickr') => $this->get_export_value_name('is_public', $this->get_export_config('is_public')),
184                      get_string('isfamily', 'portfolio_flickr') => $this->get_export_value_name('is_family', $this->get_export_config('is_family')),
185                      get_string('isfriend', 'portfolio_flickr') => $this->get_export_value_name('is_friend', $this->get_export_config('is_friend')),
186                      get_string('safetylevel', 'portfolio_flickr') => $this->get_export_value_name('safety_level', $this->get_export_config('safety_level')),
187                      get_string('contenttype', 'portfolio_flickr') => $this->get_export_value_name('content_type', $this->get_export_config('content_type')),
188                      get_string('hidefrompublicsearches', 'portfolio_flickr') => $this->get_export_value_name('hidden', $this->get_export_config('hidden')));
189     }
191     private function get_export_value_name($param, $value) {
192         $params = array('set' => $this->get_sets(),
193                         'is_public' => array(0 => get_string('no'), 1 => get_string('yes')),
194                         'is_family' => array(0 => get_string('no'), 1 => get_string('yes')),
195                         'is_friend' => array(0 => get_string('no'), 1 => get_string('yes')),
196                         'safety_level' => array(1 => get_string('safe', 'portfolio_flickr'),
197                                                 2 => get_string('moderate', 'portfolio_flickr'),
198                                                 3 => get_string('restricted', 'portfolio_flickr')),
199                         'content_type' => array(1 => get_string('photo', 'portfolio_flickr'),
200                                                 2 => get_string('screenshot', 'portfolio_flickr'),
201                                                 3 => get_string('other', 'portfolio_flickr')),
202                         'hidden' => array(1 => get_string('no'), 2 => get_string('yes')));
204         if (isset($params[$param][$value])) {
205             return $params[$param][$value];
206         } else {
207             return '-';
208         }
209     }