MDL-33552 - portfolio api E_STRICT fixes
[moodle.git] / portfolio / googledocs / lib.php
CommitLineData
4317f92f 1<?php
4560fd1b
DP
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
ee91cf95 17/**
18 * Google Documents Portfolio Plugin
19 *
20 * @author Dan Poltawski <talktodan@gmail.com>
ee91cf95 21 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
22 */
f675815e 23require_once($CFG->libdir.'/portfolio/plugin.php');
ee91cf95 24require_once($CFG->libdir.'/googleapi.php');
25
26class portfolio_plugin_googledocs extends portfolio_plugin_push_base {
4560fd1b 27 private $googleoauth = null;
ee91cf95 28
38652d90 29 public function supported_formats() {
4560fd1b 30 return array(PORTFOLIO_FORMAT_FILE);
ee91cf95 31 }
32
33 public static function get_name() {
34 return get_string('pluginname', 'portfolio_googledocs');
35 }
36
37 public function prepare_package() {
4560fd1b 38 // We send the files as they are, no prep required.
4317f92f 39 return true;
ee91cf95 40 }
4454447d 41
4560fd1b 42 public function get_interactive_continue_url() {
ee91cf95 43 return 'http://docs.google.com/';
44 }
45
46 public function expected_time($callertime) {
4560fd1b 47 // We trust what the portfolio says.
ee91cf95 48 return $callertime;
49 }
50
51 public function send_package() {
4560fd1b
DP
52 if (!$this->googleoauth) {
53 throw new portfolio_plugin_exception('noauthtoken', 'portfolio_googledocs');
ee91cf95 54 }
55
4560fd1b 56 $gdocs = new google_docs($this->googleoauth);
ee91cf95 57 foreach ($this->exporter->get_tempfiles() as $file) {
4560fd1b 58 if (!$gdocs->send_file($file)) {
ee91cf95 59 throw new portfolio_plugin_exception('sendfailed', 'portfolio_gdocs', $file->get_filename());
60 }
61 }
62 }
63
64 public function steal_control($stage) {
65 global $CFG;
66 if ($stage != PORTFOLIO_STAGE_CONFIG) {
67 return false;
68 }
69
4560fd1b
DP
70 $this->initialize_oauth();
71 if ($this->googleoauth->is_logged_in()) {
72 return false;
73 } else {
74 return $this->googleoauth->get_login_url();
ee91cf95 75 }
ee91cf95 76 }
77
78 public function post_control($stage, $params) {
79 if ($stage != PORTFOLIO_STAGE_CONFIG) {
80 return;
81 }
82
4560fd1b
DP
83 $this->initialize_oauth();
84 if ($this->googleoauth->is_logged_in()) {
85 return false;
86 } else {
87 return $this->googleoauth->get_login_url();
ee91cf95 88 }
ee91cf95 89 }
90
16f4918a
DP
91 public static function allows_multiple_instances() {
92 return false;
93 }
ee91cf95 94
4560fd1b
DP
95 public static function has_admin_config() {
96 return true;
97 }
98
99 public static function get_allowed_config() {
100 return array('clientid', 'secret');
101 }
102
c17ec774 103 public static function admin_config_form(&$mform) {
4560fd1b
DP
104 $a = new stdClass;
105 $a->docsurl = get_docs_url('Google_OAuth2_Setup');
106 $a->callbackurl = google_oauth::callback_url()->out(false);
107
108 $mform->addElement('static', null, '', get_string('oauthinfo', 'portfolio_googledocs', $a));
109
110 $mform->addElement('text', 'clientid', get_string('clientid', 'portfolio_googledocs'));
111 $mform->addElement('text', 'secret', get_string('secret', 'portfolio_googledocs'));
112
113 $strrequired = get_string('required');
114 $mform->addRule('clientid', $strrequired, 'required', null, 'client');
115 $mform->addRule('secret', $strrequired, 'required', null, 'client');
ee91cf95 116 }
117
4560fd1b
DP
118 private function initialize_oauth() {
119 $returnurl = new moodle_url('/portfolio/add.php');
120 $returnurl->param('postcontrol', 1);
121 $returnurl->param('id', $this->exporter->get('id'));
122 $returnurl->param('sesskey', sesskey());
123
124 $clientid = $this->get_config('clientid');
125 $secret = $this->get_config('secret');
126
db7602af 127 $this->googleoauth = new google_oauth($clientid, $secret, $returnurl, google_docs::REALM);
4560fd1b 128 }
b7acfd64
DP
129
130 public function instance_sanity_check() {
131 $clientid = $this->get_config('clientid');
132 $secret = $this->get_config('secret');
133
134 // If there is no oauth config (e.g. plugins upgraded from < 2.3 then
135 // there will be no config and this plugin should be disabled.
136 if (empty($clientid) or empty($secret)) {
137 return 'nooauthcredentials';
138 }
139 return 0;
140 }
ee91cf95 141}