MDL-20821 MDL-20889 reverting previous cleanup, we need to import new versions instead
[moodle.git] / portfolio / type / googledocs / lib.php
CommitLineData
4317f92f 1<?php
ee91cf95 2/**
3 * Google Documents Portfolio Plugin
4 *
5 * @author Dan Poltawski <talktodan@gmail.com>
ee91cf95 6 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
7 */
8require_once($CFG->libdir.'/googleapi.php');
9
10class portfolio_plugin_googledocs extends portfolio_plugin_push_base {
11 private $sessiontoken;
12
13 public static function supported_formats() {
14 return array(PORTFOLIO_FORMAT_PLAINHTML, PORTFOLIO_FORMAT_IMAGE, PORTFOLIO_FORMAT_TEXT);
15 }
16
17 public static function get_name() {
18 return get_string('pluginname', 'portfolio_googledocs');
19 }
20
21 public function prepare_package() {
22 // we send the files as they are, no prep required
4317f92f 23 return true;
ee91cf95 24 }
25
26 public function get_continue_url(){
27 return 'http://docs.google.com/';
28 }
29
30 public function expected_time($callertime) {
31 // we trust what the portfolio says
32 return $callertime;
33 }
34
35 public function send_package() {
36
37 if(!$this->sessiontoken){
38 throw new portfolio_plugin_exception('nosessiontoken', 'portfolio_googledocs');
39 }
40
41 $gdocs = new google_docs(new google_authsub($this->sessiontoken));
42
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());
46 }
47 }
48 }
49
50 public function steal_control($stage) {
51 global $CFG;
52 if ($stage != PORTFOLIO_STAGE_CONFIG) {
53 return false;
54 }
55
56 $sesskey = google_docs::get_sesskey($this->get('user')->id);
57
58 if($sesskey){
59 try{
60 $gauth = new google_authsub($sesskey);
61 $this->sessiontoken = $sesskey;
62 return false;
63 }catch(Exception $e){
64 // sesskey is not valid, delete store and re-auth
65 google_docs::delete_sesskey($this->get('user')->id);
66 }
67 }
68
63d09fc5 69 return google_authsub::login_url($CFG->wwwroot.'/portfolio/add.php?postcontrol=1&id=' . $this->exporter->get('id') . '&sesskey=' . sesskey(), google_docs::REALM);
ee91cf95 70 }
71
72 public function post_control($stage, $params) {
73 if ($stage != PORTFOLIO_STAGE_CONFIG) {
74 return;
75 }
76
77 if(!array_key_exists('token', $params)){
78 throw new portfolio_plugin_exception('noauthtoken', 'portfolio_googledocs');
79 }
80
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();
84
85 google_docs::set_sesskey($this->sessiontoken, $this->get('user')->id);
86 }
87
88}
89
90/**
4317f92f 91 * Registers to the user_deleted event to revoke any
ee91cf95 92 * subauth tokens we have from them
93 *
94 * @param $user user object
95 * @return boolean true in all cases as its only minor cleanup
96 */
97function 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)){
101 try{
102 $gauth = new google_authsub($sesskey);
103
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
108 return true;
109 }
110 }
111
4317f92f 112 return true;
ee91cf95 113}