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