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.'/googleapi.php');
10 class portfolio_plugin_googledocs extends portfolio_plugin_push_base {
11 private $sessiontoken;
13 public static function supported_formats() {
14 return array(PORTFOLIO_FORMAT_PLAINHTML, PORTFOLIO_FORMAT_IMAGE, PORTFOLIO_FORMAT_TEXT);
17 public static function get_name() {
18 return get_string('pluginname', 'portfolio_googledocs');
21 public function prepare_package() {
22 // we send the files as they are, no prep required
26 public function get_continue_url(){
27 return 'http://docs.google.com/';
30 public function expected_time($callertime) {
31 // we trust what the portfolio says
35 public function send_package() {
37 if(!$this->sessiontoken){
38 throw new portfolio_plugin_exception('nosessiontoken', 'portfolio_googledocs');
41 $gdocs = new google_docs(new google_authsub($this->sessiontoken));
43 foreach ($this->exporter->get_tempfiles() as $file) {
44 if(!$gdocs->send_file($file)){
45 throw new portfolio_plugin_exception('sendfailed', 'portfolio_gdocs', $file->get_filename());
50 public function steal_control($stage) {
52 if ($stage != PORTFOLIO_STAGE_CONFIG) {
56 $sesskey = google_docs::get_sesskey($this->get('user')->id);
60 $gauth = new google_authsub($sesskey);
61 $this->sessiontoken = $sesskey;
64 // sesskey is not valid, delete store and re-auth
65 google_docs::delete_sesskey($this->get('user')->id);
69 return google_authsub::login_url($CFG->wwwroot.'/portfolio/add.php?postcontrol=1&id=' . $this->exporter->get('id') . '&sesskey=' . sesskey(), google_docs::REALM);
72 public function post_control($stage, $params) {
73 if ($stage != PORTFOLIO_STAGE_CONFIG) {
77 if(!array_key_exists('token', $params)){
78 throw new portfolio_plugin_exception('noauthtoken', 'portfolio_googledocs');
81 // we now have our auth token, get a session token..
82 $gauth = new google_authsub(false, $params['token']);
83 $this->sessiontoken = $gauth->get_sessiontoken();
85 google_docs::set_sesskey($this->sessiontoken, $this->get('user')->id);
91 * Registers to the user_deleted event to revoke any
92 * subauth tokens we have from them
94 * @param $user user object
95 * @return boolean true in all cases as its only minor cleanup
97 function portfolio_googledocs_user_deleted($user){
98 // it is only by luck that the user prefstill exists now?
99 // We probably need a pre-delete event?
100 if($sesskey = google_docs::get_sesskey($user->id)){
102 $gauth = new google_authsub($sesskey);
104 $gauth->revoke_session_token();
105 }catch(Exception $e){
106 // we don't care that much about success- just being good
107 // google api citzens