|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00008 require_once($CFG->libdir.'/portfolio/plugin.php'); 00009 require_once($CFG->libdir.'/googleapi.php'); 00010 00011 class portfolio_plugin_googledocs extends portfolio_plugin_push_base { 00012 private $sessiontoken; 00013 00014 public function supported_formats() { 00015 return array( 00016 PORTFOLIO_FORMAT_PLAINHTML, 00017 PORTFOLIO_FORMAT_IMAGE, 00018 PORTFOLIO_FORMAT_TEXT, 00019 PORTFOLIO_FORMAT_PDF, 00020 PORTFOLIO_FORMAT_DOCUMENT, 00021 PORTFOLIO_FORMAT_PRESENTATION, 00022 PORTFOLIO_FORMAT_SPREADSHEET 00023 ); 00024 } 00025 00026 public static function get_name() { 00027 return get_string('pluginname', 'portfolio_googledocs'); 00028 } 00029 00030 public function prepare_package() { 00031 // we send the files as they are, no prep required 00032 return true; 00033 } 00034 00035 public function get_interactive_continue_url(){ 00036 return 'http://docs.google.com/'; 00037 } 00038 00039 public function expected_time($callertime) { 00040 // we trust what the portfolio says 00041 return $callertime; 00042 } 00043 00044 public function send_package() { 00045 00046 if(!$this->sessiontoken){ 00047 throw new portfolio_plugin_exception('nosessiontoken', 'portfolio_googledocs'); 00048 } 00049 00050 $gdocs = new google_docs(new google_authsub($this->sessiontoken)); 00051 00052 foreach ($this->exporter->get_tempfiles() as $file) { 00053 if(!$gdocs->send_file($file)){ 00054 throw new portfolio_plugin_exception('sendfailed', 'portfolio_gdocs', $file->get_filename()); 00055 } 00056 } 00057 } 00058 00059 public function steal_control($stage) { 00060 global $CFG; 00061 if ($stage != PORTFOLIO_STAGE_CONFIG) { 00062 return false; 00063 } 00064 00065 $sesskey = google_docs::get_sesskey($this->get('user')->id); 00066 00067 if($sesskey){ 00068 try{ 00069 $gauth = new google_authsub($sesskey); 00070 $this->sessiontoken = $sesskey; 00071 return false; 00072 }catch(Exception $e){ 00073 // sesskey is not valid, delete store and re-auth 00074 google_docs::delete_sesskey($this->get('user')->id); 00075 } 00076 } 00077 00078 return google_authsub::login_url($CFG->wwwroot.'/portfolio/add.php?postcontrol=1&id=' . $this->exporter->get('id') . '&sesskey=' . sesskey(), google_docs::REALM); 00079 } 00080 00081 public function post_control($stage, $params) { 00082 if ($stage != PORTFOLIO_STAGE_CONFIG) { 00083 return; 00084 } 00085 00086 if(!array_key_exists('token', $params)){ 00087 throw new portfolio_plugin_exception('noauthtoken', 'portfolio_googledocs'); 00088 } 00089 00090 // we now have our auth token, get a session token.. 00091 $gauth = new google_authsub(false, $params['token']); 00092 $this->sessiontoken = $gauth->get_sessiontoken(); 00093 00094 google_docs::set_sesskey($this->sessiontoken, $this->get('user')->id); 00095 } 00096 00097 public static function allows_multiple_instances() { 00098 return false; 00099 } 00100 } 00101 00109 function portfolio_googledocs_user_deleted($user){ 00110 // it is only by luck that the user prefstill exists now? 00111 // We probably need a pre-delete event? 00112 if($sesskey = google_docs::get_sesskey($user->id)){ 00113 try{ 00114 $gauth = new google_authsub($sesskey); 00115 00116 $gauth->revoke_session_token(); 00117 }catch(Exception $e){ 00118 // we don't care that much about success- just being good 00119 // google api citzens 00120 return true; 00121 } 00122 } 00123 00124 return true; 00125 }