Portfolio plugins explicitly requires parent library with superclass definition
[moodle.git] / portfolio / picasa / lib.php
CommitLineData
4317f92f 1<?php
ee91cf95 2/**
3 * Picasa Portfolio Plugin
4 *
5 * @author Dan Poltawski <talktodan@gmail.com>
ee91cf95 6 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
7 */
f675815e 8require_once($CFG->libdir.'/portfolio/plugin.php');
ee91cf95 9require_once($CFG->libdir.'/googleapi.php');
10
11class portfolio_plugin_picasa extends portfolio_plugin_push_base {
12 private $sessionkey;
13
38652d90 14 public function supported_formats() {
ee91cf95 15 return array(PORTFOLIO_FORMAT_IMAGE, PORTFOLIO_FORMAT_VIDEO);
16 }
17
18 public static function get_name() {
19 return get_string('pluginname', 'portfolio_picasa');
20 }
21
22 public function prepare_package() {
23 // we send the files as they are, no prep required
4317f92f 24 return true;
ee91cf95 25 }
4454447d 26
5d0dbf13 27 public function get_interactive_continue_url(){
ee91cf95 28 return 'http://picasaweb.google.com/';
29 }
30
31 public function expected_time($callertime) {
32 return $callertime;
33 }
34
35 public function send_package() {
36 if(!$this->sessionkey){
37 throw new portfolio_plugin_exception('noauthtoken', 'portfolio_picasa');
38 }
39
40 $picasa = new google_picasa(new google_authsub($this->sessionkey));
41
42 foreach ($this->exporter->get_tempfiles() as $file) {
43
44 if(!$picasa->send_file($file)){
45 throw new portfolio_plugin_exception('sendfailed', 'portfolio_picasa', $file->get_filename());
46 }
47 }
48 }
49
50 public function steal_control($stage) {
51 global $CFG;
52 if ($stage != PORTFOLIO_STAGE_CONFIG) {
53 return false;
54 }
55
56 $sesskey = google_picasa::get_sesskey($this->get('user')->id);
57
58 if($sesskey){
59 try{
60 $gauth = new google_authsub($sesskey);
61 $this->sessionkey = $sesskey;
62 return false;
63 }catch(Exception $e){
64 // sesskey is not valid, delete store and re-auth
65 google_picasa::delete_sesskey($this->get('user')->id);
66 }
67 }
68
63d09fc5 69 return google_authsub::login_url($CFG->wwwroot.'/portfolio/add.php?postcontrol=1&id=' . $this->exporter->get('id') . '&sesskey=' . sesskey(), google_picasa::REALM);
ee91cf95 70 }
71
72 public function post_control($stage, $params) {
73 if ($stage != PORTFOLIO_STAGE_CONFIG) {
74 return;
75 }
76
77 if(!array_key_exists('token', $params)){
78 throw new portfolio_plugin_exception('noauthtoken', 'portfolio_picasa');
79 }
80
81 // we now have our auth token, get a session token..
82 $gauth = new google_authsub(false, $params['token']);
83
84 $this->sessionkey = $gauth->get_sessiontoken();
85
86 google_picasa::set_sesskey($this->sessionkey, $this->get('user')->id);
87 }
88
16f4918a
DP
89 public static function allows_multiple_instances() {
90 return false;
91 }
ee91cf95 92}
93
94/**
4317f92f 95 * Registers to the user_deleted event to revoke any
ee91cf95 96 * subauth tokens we have from them
97 *
98 * @param $user user object
99 * @return boolean true in all cases as its only minor cleanup
100 */
101function portfolio_picasa_user_deleted($user){
102 // it is only by luck that the user prefstill exists now?
103 // We probably need a pre-delete event?
104 if($sesskey = google_picasa::get_sesskey($user->id)){
105 try{
106 $gauth = new google_authsub($sesskey);
107
108 $gauth->revoke_session_token();
109 }catch(Exception $e){
110 // we don't care that much about success- just being good
111 // google api citzens
112 return true;
113 }
114 }
115
4317f92f 116 return true;
ee91cf95 117}