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() {
15 PORTFOLIO_FORMAT_PLAINHTML,
16 PORTFOLIO_FORMAT_IMAGE,
17 PORTFOLIO_FORMAT_TEXT,
19 PORTFOLIO_FORMAT_DOCUMENT,
20 PORTFOLIO_FORMAT_PRESENTATION,
21 PORTFOLIO_FORMAT_SPREADSHEET
25 public static function get_name() {
26 return get_string('pluginname', 'portfolio_googledocs');
29 public function prepare_package() {
30 // we send the files as they are, no prep required
34 public function get_interactive_continue_url(){
35 return 'http://docs.google.com/';
38 public function expected_time($callertime) {
39 // we trust what the portfolio says
43 public function send_package() {
45 if(!$this->sessiontoken){
46 throw new portfolio_plugin_exception('nosessiontoken', 'portfolio_googledocs');
49 $gdocs = new google_docs(new google_authsub($this->sessiontoken));
51 foreach ($this->exporter->get_tempfiles() as $file) {
52 if(!$gdocs->send_file($file)){
53 throw new portfolio_plugin_exception('sendfailed', 'portfolio_gdocs', $file->get_filename());
58 public function steal_control($stage) {
60 if ($stage != PORTFOLIO_STAGE_CONFIG) {
64 $sesskey = google_docs::get_sesskey($this->get('user')->id);
68 $gauth = new google_authsub($sesskey);
69 $this->sessiontoken = $sesskey;
72 // sesskey is not valid, delete store and re-auth
73 google_docs::delete_sesskey($this->get('user')->id);
77 return google_authsub::login_url($CFG->wwwroot.'/portfolio/add.php?postcontrol=1&id=' . $this->exporter->get('id') . '&sesskey=' . sesskey(), google_docs::REALM);
80 public function post_control($stage, $params) {
81 if ($stage != PORTFOLIO_STAGE_CONFIG) {
85 if(!array_key_exists('token', $params)){
86 throw new portfolio_plugin_exception('noauthtoken', 'portfolio_googledocs');
89 // we now have our auth token, get a session token..
90 $gauth = new google_authsub(false, $params['token']);
91 $this->sessiontoken = $gauth->get_sessiontoken();
93 google_docs::set_sesskey($this->sessiontoken, $this->get('user')->id);
96 public static function allows_multiple_instances() {
102 * Registers to the user_deleted event to revoke any
103 * subauth tokens we have from them
105 * @param $user user object
106 * @return boolean true in all cases as its only minor cleanup
108 function portfolio_googledocs_user_deleted($user){
109 // it is only by luck that the user prefstill exists now?
110 // We probably need a pre-delete event?
111 if($sesskey = google_docs::get_sesskey($user->id)){
113 $gauth = new google_authsub($sesskey);
115 $gauth->revoke_session_token();
116 }catch(Exception $e){
117 // we don't care that much about success- just being good
118 // google api citzens