3 * Google Documents Portfolio Plugin
5 * @author Dan Poltawski <talktodan@gmail.com>
6 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8 require_once($CFG->libdir
.'/portfolio/plugin.php');
9 require_once($CFG->libdir
.'/googleapi.php');
11 class portfolio_plugin_googledocs
extends portfolio_plugin_push_base
{
12 private $sessiontoken;
14 public function supported_formats() {
16 PORTFOLIO_FORMAT_PLAINHTML
,
17 PORTFOLIO_FORMAT_IMAGE
,
18 PORTFOLIO_FORMAT_TEXT
,
20 PORTFOLIO_FORMAT_DOCUMENT
,
21 PORTFOLIO_FORMAT_PRESENTATION
,
22 PORTFOLIO_FORMAT_SPREADSHEET
26 public static function get_name() {
27 return get_string('pluginname', 'portfolio_googledocs');
30 public function prepare_package() {
31 // we send the files as they are, no prep required
35 public function get_interactive_continue_url(){
36 return 'http://docs.google.com/';
39 public function expected_time($callertime) {
40 // we trust what the portfolio says
44 public function send_package() {
46 if(!$this->sessiontoken
){
47 throw new portfolio_plugin_exception('nosessiontoken', 'portfolio_googledocs');
50 $gdocs = new google_docs(new google_authsub($this->sessiontoken
));
52 foreach ($this->exporter
->get_tempfiles() as $file) {
53 if(!$gdocs->send_file($file)){
54 throw new portfolio_plugin_exception('sendfailed', 'portfolio_gdocs', $file->get_filename());
59 public function steal_control($stage) {
61 if ($stage != PORTFOLIO_STAGE_CONFIG
) {
65 $sesskey = google_docs
::get_sesskey($this->get('user')->id
);
69 $gauth = new google_authsub($sesskey);
70 $this->sessiontoken
= $sesskey;
73 // sesskey is not valid, delete store and re-auth
74 google_docs
::delete_sesskey($this->get('user')->id
);
78 return google_authsub
::login_url($CFG->wwwroot
.'/portfolio/add.php?postcontrol=1&id=' . $this->exporter
->get('id') . '&sesskey=' . sesskey(), google_docs
::REALM
);
81 public function post_control($stage, $params) {
82 if ($stage != PORTFOLIO_STAGE_CONFIG
) {
86 if(!array_key_exists('token', $params)){
87 throw new portfolio_plugin_exception('noauthtoken', 'portfolio_googledocs');
90 // we now have our auth token, get a session token..
91 $gauth = new google_authsub(false, $params['token']);
92 $this->sessiontoken
= $gauth->get_sessiontoken();
94 google_docs
::set_sesskey($this->sessiontoken
, $this->get('user')->id
);
97 public static function allows_multiple_instances() {
103 * Registers to the user_deleted event to revoke any
104 * subauth tokens we have from them
106 * @param $user user object
107 * @return boolean true in all cases as its only minor cleanup
109 function portfolio_googledocs_user_deleted($user){
110 // it is only by luck that the user prefstill exists now?
111 // We probably need a pre-delete event?
112 if($sesskey = google_docs
::get_sesskey($user->id
)){
114 $gauth = new google_authsub($sesskey);
116 $gauth->revoke_session_token();
117 }catch(Exception
$e){
118 // we don't care that much about success- just being good
119 // google api citzens