|
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_picasa extends portfolio_plugin_push_base { 00012 private $sessionkey; 00013 00014 public function supported_formats() { 00015 return array(PORTFOLIO_FORMAT_IMAGE, PORTFOLIO_FORMAT_VIDEO); 00016 } 00017 00018 public static function get_name() { 00019 return get_string('pluginname', 'portfolio_picasa'); 00020 } 00021 00022 public function prepare_package() { 00023 // we send the files as they are, no prep required 00024 return true; 00025 } 00026 00027 public function get_interactive_continue_url(){ 00028 return 'http://picasaweb.google.com/'; 00029 } 00030 00031 public function expected_time($callertime) { 00032 return $callertime; 00033 } 00034 00035 public function send_package() { 00036 if(!$this->sessionkey){ 00037 throw new portfolio_plugin_exception('noauthtoken', 'portfolio_picasa'); 00038 } 00039 00040 $picasa = new google_picasa(new google_authsub($this->sessionkey)); 00041 00042 foreach ($this->exporter->get_tempfiles() as $file) { 00043 00044 if(!$picasa->send_file($file)){ 00045 throw new portfolio_plugin_exception('sendfailed', 'portfolio_picasa', $file->get_filename()); 00046 } 00047 } 00048 } 00049 00050 public function steal_control($stage) { 00051 global $CFG; 00052 if ($stage != PORTFOLIO_STAGE_CONFIG) { 00053 return false; 00054 } 00055 00056 $sesskey = google_picasa::get_sesskey($this->get('user')->id); 00057 00058 if($sesskey){ 00059 try{ 00060 $gauth = new google_authsub($sesskey); 00061 $this->sessionkey = $sesskey; 00062 return false; 00063 }catch(Exception $e){ 00064 // sesskey is not valid, delete store and re-auth 00065 google_picasa::delete_sesskey($this->get('user')->id); 00066 } 00067 } 00068 00069 return google_authsub::login_url($CFG->wwwroot.'/portfolio/add.php?postcontrol=1&id=' . $this->exporter->get('id') . '&sesskey=' . sesskey(), google_picasa::REALM); 00070 } 00071 00072 public function post_control($stage, $params) { 00073 if ($stage != PORTFOLIO_STAGE_CONFIG) { 00074 return; 00075 } 00076 00077 if(!array_key_exists('token', $params)){ 00078 throw new portfolio_plugin_exception('noauthtoken', 'portfolio_picasa'); 00079 } 00080 00081 // we now have our auth token, get a session token.. 00082 $gauth = new google_authsub(false, $params['token']); 00083 00084 $this->sessionkey = $gauth->get_sessiontoken(); 00085 00086 google_picasa::set_sesskey($this->sessionkey, $this->get('user')->id); 00087 } 00088 00089 public static function allows_multiple_instances() { 00090 return false; 00091 } 00092 } 00093 00101 function portfolio_picasa_user_deleted($user){ 00102 // it is only by luck that the user prefstill exists now? 00103 // We probably need a pre-delete event? 00104 if($sesskey = google_picasa::get_sesskey($user->id)){ 00105 try{ 00106 $gauth = new google_authsub($sesskey); 00107 00108 $gauth->revoke_session_token(); 00109 }catch(Exception $e){ 00110 // we don't care that much about success- just being good 00111 // google api citzens 00112 return true; 00113 } 00114 } 00115 00116 return true; 00117 }