Revert "MDL-32125 mod_forum: updating subscription mode not reflected"
[moodle.git] / portfolio / googledocs / lib.php
blob2223f9d4831d6673c73e7ff58156214f5d50fa47
1 <?php
2 /**
3 * Google Documents Portfolio Plugin
5 * @author Dan Poltawski <talktodan@gmail.com>
6 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
7 */
8 require_once($CFG->libdir.'/portfolio/plugin.php');
9 require_once($CFG->libdir.'/googleapi.php');
11 class portfolio_plugin_googledocs extends portfolio_plugin_push_base {
12 private $sessiontoken;
14 public function supported_formats() {
15 return array(
16 PORTFOLIO_FORMAT_PLAINHTML,
17 PORTFOLIO_FORMAT_IMAGE,
18 PORTFOLIO_FORMAT_TEXT,
19 PORTFOLIO_FORMAT_PDF,
20 PORTFOLIO_FORMAT_DOCUMENT,
21 PORTFOLIO_FORMAT_PRESENTATION,
22 PORTFOLIO_FORMAT_SPREADSHEET
26 public static function get_name() {
27 return get_string('pluginname', 'portfolio_googledocs');
30 public function prepare_package() {
31 // we send the files as they are, no prep required
32 return true;
35 public function get_interactive_continue_url(){
36 return 'http://docs.google.com/';
39 public function expected_time($callertime) {
40 // we trust what the portfolio says
41 return $callertime;
44 public function send_package() {
46 if(!$this->sessiontoken){
47 throw new portfolio_plugin_exception('nosessiontoken', 'portfolio_googledocs');
50 $gdocs = new google_docs(new google_authsub($this->sessiontoken));
52 foreach ($this->exporter->get_tempfiles() as $file) {
53 if(!$gdocs->send_file($file)){
54 throw new portfolio_plugin_exception('sendfailed', 'portfolio_gdocs', $file->get_filename());
59 public function steal_control($stage) {
60 global $CFG;
61 if ($stage != PORTFOLIO_STAGE_CONFIG) {
62 return false;
65 $sesskey = google_docs::get_sesskey($this->get('user')->id);
67 if($sesskey){
68 try{
69 $gauth = new google_authsub($sesskey);
70 $this->sessiontoken = $sesskey;
71 return false;
72 }catch(Exception $e){
73 // sesskey is not valid, delete store and re-auth
74 google_docs::delete_sesskey($this->get('user')->id);
78 return google_authsub::login_url($CFG->wwwroot.'/portfolio/add.php?postcontrol=1&id=' . $this->exporter->get('id') . '&sesskey=' . sesskey(), google_docs::REALM);
81 public function post_control($stage, $params) {
82 if ($stage != PORTFOLIO_STAGE_CONFIG) {
83 return;
86 if(!array_key_exists('token', $params)){
87 throw new portfolio_plugin_exception('noauthtoken', 'portfolio_googledocs');
90 // we now have our auth token, get a session token..
91 $gauth = new google_authsub(false, $params['token']);
92 $this->sessiontoken = $gauth->get_sessiontoken();
94 google_docs::set_sesskey($this->sessiontoken, $this->get('user')->id);
97 public static function allows_multiple_instances() {
98 return false;
103 * Registers to the user_deleted event to revoke any
104 * subauth tokens we have from them
106 * @param $user user object
107 * @return boolean true in all cases as its only minor cleanup
109 function portfolio_googledocs_user_deleted($user){
110 // it is only by luck that the user prefstill exists now?
111 // We probably need a pre-delete event?
112 if($sesskey = google_docs::get_sesskey($user->id)){
113 try{
114 $gauth = new google_authsub($sesskey);
116 $gauth->revoke_session_token();
117 }catch(Exception $e){
118 // we don't care that much about success- just being good
119 // google api citzens
120 return true;
124 return true;