3 * Google Documents Portfolio Plugin
5 * @author Dan Poltawski <talktodan@gmail.com>
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9 require_once($CFG->libdir.'/googleapi.php');
11 class portfolio_plugin_googledocs extends portfolio_plugin_push_base {
12 private $sessiontoken;
14 public static function supported_formats() {
15 return array(PORTFOLIO_FORMAT_PLAINHTML, PORTFOLIO_FORMAT_IMAGE, PORTFOLIO_FORMAT_TEXT);
18 public static function get_name() {
19 return get_string('pluginname', 'portfolio_googledocs');
22 public function prepare_package() {
23 // we send the files as they are, no prep required
27 public function get_continue_url(){
28 return 'http://docs.google.com/';
31 public function expected_time($callertime) {
32 // we trust what the portfolio says
36 public function send_package() {
38 if(!$this->sessiontoken){
39 throw new portfolio_plugin_exception('nosessiontoken', 'portfolio_googledocs');
42 $gdocs = new google_docs(new google_authsub($this->sessiontoken));
44 foreach ($this->exporter->get_tempfiles() as $file) {
45 if(!$gdocs->send_file($file)){
46 throw new portfolio_plugin_exception('sendfailed', 'portfolio_gdocs', $file->get_filename());
51 public function steal_control($stage) {
53 if ($stage != PORTFOLIO_STAGE_CONFIG) {
57 $sesskey = google_docs::get_sesskey($this->get('user')->id);
61 $gauth = new google_authsub($sesskey);
62 $this->sessiontoken = $sesskey;
65 // sesskey is not valid, delete store and re-auth
66 google_docs::delete_sesskey($this->get('user')->id);
70 return google_authsub::login_url($CFG->wwwroot.'/portfolio/add.php?postcontrol=1', google_docs::REALM);
73 public function post_control($stage, $params) {
74 if ($stage != PORTFOLIO_STAGE_CONFIG) {
78 if(!array_key_exists('token', $params)){
79 throw new portfolio_plugin_exception('noauthtoken', 'portfolio_googledocs');
82 // we now have our auth token, get a session token..
83 $gauth = new google_authsub(false, $params['token']);
84 $this->sessiontoken = $gauth->get_sessiontoken();
86 google_docs::set_sesskey($this->sessiontoken, $this->get('user')->id);
92 * Registers to the user_deleted event to revoke any
93 * subauth tokens we have from them
95 * @param $user user object
96 * @return boolean true in all cases as its only minor cleanup
98 function portfolio_googledocs_user_deleted($user){
99 // it is only by luck that the user prefstill exists now?
100 // We probably need a pre-delete event?
101 if($sesskey = google_docs::get_sesskey($user->id)){
103 $gauth = new google_authsub($sesskey);
105 $gauth->revoke_session_token();
106 }catch(Exception $e){
107 // we don't care that much about success- just being good
108 // google api citzens