2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Google Documents Portfolio Plugin
20 * @author Dan Poltawski <talktodan@gmail.com>
21 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
23 require_once($CFG->libdir.'/portfolio/plugin.php');
24 require_once($CFG->libdir.'/googleapi.php');
26 class portfolio_plugin_googledocs extends portfolio_plugin_push_base {
27 private $googleoauth = null;
29 public function supported_formats() {
30 return array(PORTFOLIO_FORMAT_FILE);
33 public static function get_name() {
34 return get_string('pluginname', 'portfolio_googledocs');
37 public function prepare_package() {
38 // We send the files as they are, no prep required.
42 public function get_interactive_continue_url() {
43 return 'http://docs.google.com/';
46 public function expected_time($callertime) {
47 // We trust what the portfolio says.
51 public function send_package() {
52 if (!$this->googleoauth) {
53 throw new portfolio_plugin_exception('noauthtoken', 'portfolio_googledocs');
56 $gdocs = new google_docs($this->googleoauth);
57 foreach ($this->exporter->get_tempfiles() as $file) {
58 if (!$gdocs->send_file($file)) {
59 throw new portfolio_plugin_exception('sendfailed', 'portfolio_gdocs', $file->get_filename());
64 public function steal_control($stage) {
66 if ($stage != PORTFOLIO_STAGE_CONFIG) {
70 $this->initialize_oauth();
71 if ($this->googleoauth->is_logged_in()) {
74 return $this->googleoauth->get_login_url();
78 public function post_control($stage, $params) {
79 if ($stage != PORTFOLIO_STAGE_CONFIG) {
83 $this->initialize_oauth();
84 if ($this->googleoauth->is_logged_in()) {
87 return $this->googleoauth->get_login_url();
91 public static function allows_multiple_instances() {
95 public static function has_admin_config() {
99 public static function get_allowed_config() {
100 return array('clientid', 'secret');
103 public static function admin_config_form(&$mform) {
105 $a->docsurl = get_docs_url('Google_OAuth2_Setup');
106 $a->callbackurl = google_oauth::callback_url()->out(false);
108 $mform->addElement('static', null, '', get_string('oauthinfo', 'portfolio_googledocs', $a));
110 $mform->addElement('text', 'clientid', get_string('clientid', 'portfolio_googledocs'));
111 $mform->addElement('text', 'secret', get_string('secret', 'portfolio_googledocs'));
113 $strrequired = get_string('required');
114 $mform->addRule('clientid', $strrequired, 'required', null, 'client');
115 $mform->addRule('secret', $strrequired, 'required', null, 'client');
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());
124 $clientid = $this->get_config('clientid');
125 $secret = $this->get_config('secret');
127 $this->googleoauth = new google_oauth($clientid, $secret, $returnurl, google_docs::REALM);
130 public function instance_sanity_check() {
131 $clientid = $this->get_config('clientid');
132 $secret = $this->get_config('secret');
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';