Portfolio plugins explicitly requires parent library with superclass definition
[moodle.git] / portfolio / flickr / lib.php
1 <?php
2 require_once($CFG->libdir.'/portfolio/plugin.php');
3 require_once($CFG->libdir.'/filelib.php');
4 require_once($CFG->libdir.'/flickrlib.php');
6 class portfolio_plugin_flickr extends portfolio_plugin_push_base {
8     private $flickr;
9     private $token;
10     private $raw_sets;
12     public function supported_formats() {
13         return array(PORTFOLIO_FORMAT_IMAGE);
14     }
16     public static function get_name() {
17         return get_string('pluginname', 'portfolio_flickr');
18     }
20     public function prepare_package() {
22     }
24     public function send_package() {
25         foreach ($this->exporter->get_tempfiles() as $file) {
26             // @TODO get max size from flickr people_getUploadStatus
27             $filesize = $file->get_filesize();
29             if ($file->is_valid_image()) {
30                 /**
31                 $return = $this->flickr->upload (array('photo' => $file,
32                                                                 'title' => $this->get_export_config('title'),
33                                                                 'description' => $this->get_export_config('description'),
34                                                                 'tags' => $this->get_export_config('tags'),
35                                                                 'is_public' => $this->get_export_config('is_public'),
36                                                                 'is_friend' => $this->get_export_config('is_friend'),
37                                                                 'is_family' => $this->get_export_config('is_family'),
38                                                                 'safety_level' => $this->get_export_config('safety_level'),
39                                                                 'content_type' => $this->get_export_config('content_type'),
40                                                                 'hidden' => $this->get_export_config('hidden')));
41                  */
42                 $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'));
43                 if ($return) {
44                     // Attach photo to a set if requested
45                     if ($this->get_export_config('set')) {
46                         $this->flickr->photosets_addPhoto($this->get_export_config('set'), $this->flickr->parsed_response['photoid']);
47                     }
48                 } else {
49                     throw new portfolio_plugin_exception('uploadfailed', 'portfolio_flickr', $this->flickr->error_code . ': ' . $this->flickr->error_msg);
50                 }
51             }
52         }
53     }
55     public static function allows_multiple_instances() {
56         return false;
57     }
59     public function get_interactive_continue_url() {
60         // return $this->flickr->urls_getUserPhotos();
61         return "http://www.flickr.com/tools/uploader_edit.gne?ids="; // Add ids of uploaded files
62     }
64     public function expected_time($callertime) {
65         return $callertime;
66     }
68     public static function get_allowed_config() {
69         return array('apikey', 'sharedsecret');
70     }
72     public static function has_admin_config() {
73         return true;
74     }
76     public function admin_config_form(&$mform) {
77         $strrequired = get_string('required');
78         $mform->addElement('text', 'apikey', get_string('apikey', 'portfolio_flickr'));
79         $mform->addRule('apikey', $strrequired, 'required', null, 'client');
80         $mform->addElement('text', 'sharedsecret', get_string('sharedsecret', 'portfolio_flickr'));
81         $mform->addRule('sharedsecret', $strrequired, 'required', null, 'client');
82     }
84     public function has_export_config() {
85         return true;
86     }
88     public function get_allowed_user_config() {
89         return array('authtoken', 'nsid');
90     }
92     public function steal_control($stage) {
93         if ($stage != PORTFOLIO_STAGE_CONFIG) {
94             return false;
95         }
96         if ($this->token) {
97             return false;
98         }
100         $token = $this->get_user_config('authtoken', $this->get('user')->id);
101         $nsid = $this->get_user_config('nsid', $this->get('user')->id);
103         $this->flickr = new phpFlickr($this->get_config('apikey'), $this->get_config('sharedsecret'), $token);
105         if (!empty($token)) {
106             $this->token = $token;
107             $this->flickr = new phpFlickr($this->get_config('apikey'), $this->get_config('sharedsecret'), $token);
108             return false;
109         }
110         return $this->flickr->auth('write');
111     }
113     public function post_control($stage, $params) {
114         if ($stage != PORTFOLIO_STAGE_CONFIG) {
115             return;
116         }
117         if (!array_key_exists('frob', $params) || empty($params['frob'])) {
118             throw new portfolio_plugin_exception('noauthtoken', 'portfolio_flickr');
119         }
121         $this->flickr = new phpFlickr($this->get_config('apikey'), $this->get_config('sharedsecret'));
123         $auth_info = $this->flickr->auth_getToken($params['frob']);
125         $this->set_user_config(array('authtoken' => $auth_info['token'], 'nsid' => $auth_info['user']['nsid']), $this->get('user')->id);
126     }
128     public function export_config_form(&$mform) {
129         $mform->addElement('text', 'plugin_title', get_string('title', 'portfolio_flickr'));
130         $mform->addElement('textarea', 'plugin_description', get_string('description'));
131         $mform->addElement('text', 'plugin_tags', get_string('tags'));
132         $mform->addElement('checkbox', 'plugin_is_public', get_string('ispublic', 'portfolio_flickr'));
133         $mform->addElement('checkbox', 'plugin_is_family', get_string('isfamily', 'portfolio_flickr'));
134         $mform->addElement('checkbox', 'plugin_is_friend', get_string('isfriend', 'portfolio_flickr'));
136         $mform->disabledIf('plugin_is_friend', 'plugin_is_public', 'checked');
137         $mform->disabledIf('plugin_is_family', 'plugin_is_public', 'checked');
139         $safety_levels = array(1 => $this->get_export_value_name('safety_level', 1),
140                                2 => $this->get_export_value_name('safety_level', 2),
141                                3 => $this->get_export_value_name('safety_level', 3));
143         $content_types = array(1 => $this->get_export_value_name('content_type', 1),
144                                2 => $this->get_export_value_name('content_type', 2),
145                                3 => $this->get_export_value_name('content_type', 3));
147         $hidden_values = array(1,2);
149         $mform->addElement('select', 'plugin_safety_level', get_string('safetylevel', 'portfolio_flickr'), $safety_levels);
150         $mform->addElement('select', 'plugin_content_type', get_string('contenttype', 'portfolio_flickr'), $content_types);
151         $mform->addElement('advcheckbox', 'plugin_hidden', get_string('hidefrompublicsearches', 'portfolio_flickr'), get_string('yes'), null, $hidden_values);
153         $mform->setDefaults(array('plugin_is_public' => true));
155         $sets = $this->get_sets();
157         if (!empty($sets)) {
158             $sets[0] = '----';
159             $mform->addElement('select', 'plugin_set', get_string('set', 'portfolio_flickr'), $sets);
160         }
161     }
163     private function get_sets() {
164         if (empty($this->raw_sets)) {
165             $this->raw_sets = $this->flickr->photosets_getList();
166         }
168         $sets = array();
169         foreach ($this->raw_sets['photoset'] as $set_data) {
170             $sets[$set_data['id']] = $set_data['title'];
171         }
172         return $sets;
173     }
175     public function get_allowed_export_config() {
176         return array('set', 'title', 'description', 'tags', 'is_public', 'is_family', 'is_friend', 'safety_level', 'content_type', 'hidden');
177     }
179     public function get_export_summary() {
180         return array(get_string('set', 'portfolio_flickr') => $this->get_export_value_name('set', $this->get_export_config('set')),
181                      get_string('title', 'portfolio_flickr') => $this->get_export_config('title'),
182                      get_string('description') => $this->get_export_config('description'),
183                      get_string('tags') => $this->get_export_config('tags'),
184                      get_string('ispublic', 'portfolio_flickr') => $this->get_export_value_name('is_public', $this->get_export_config('is_public')),
185                      get_string('isfamily', 'portfolio_flickr') => $this->get_export_value_name('is_family', $this->get_export_config('is_family')),
186                      get_string('isfriend', 'portfolio_flickr') => $this->get_export_value_name('is_friend', $this->get_export_config('is_friend')),
187                      get_string('safetylevel', 'portfolio_flickr') => $this->get_export_value_name('safety_level', $this->get_export_config('safety_level')),
188                      get_string('contenttype', 'portfolio_flickr') => $this->get_export_value_name('content_type', $this->get_export_config('content_type')),
189                      get_string('hidefrompublicsearches', 'portfolio_flickr') => $this->get_export_value_name('hidden', $this->get_export_config('hidden')));
190     }
192     private function get_export_value_name($param, $value) {
193         $params = array('set' => $this->get_sets(),
194                         'is_public' => array(0 => get_string('no'), 1 => get_string('yes')),
195                         'is_family' => array(0 => get_string('no'), 1 => get_string('yes')),
196                         'is_friend' => array(0 => get_string('no'), 1 => get_string('yes')),
197                         'safety_level' => array(1 => get_string('safe', 'portfolio_flickr'),
198                                                 2 => get_string('moderate', 'portfolio_flickr'),
199                                                 3 => get_string('restricted', 'portfolio_flickr')),
200                         'content_type' => array(1 => get_string('photo', 'portfolio_flickr'),
201                                                 2 => get_string('screenshot', 'portfolio_flickr'),
202                                                 3 => get_string('other', 'portfolio_flickr')),
203                         'hidden' => array(1 => get_string('no'), 2 => get_string('yes')));
205         if (isset($params[$param][$value])) {
206             return $params[$param][$value];
207         } else {
208             return '-';
209         }
210     }