MDL-37217 repository: Adding Google API PHP SDK
[moodle.git] / repository / googledocs / lib.php
CommitLineData
6667f9e4 1<?php
10d53fd3
DC
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
6667f9e4 17/**
67233725 18 * This plugin is used to access google docs
6667f9e4 19 *
10d53fd3 20 * @since 2.0
67233725 21 * @package repository_googledocs
61506a0a 22 * @copyright 2009 Dan Poltawski <talktodan@gmail.com>
d078f6d3 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
6667f9e4 24 */
67233725 25require_once($CFG->dirroot . '/repository/lib.php');
6667f9e4 26require_once($CFG->libdir.'/googleapi.php');
27
67233725
DC
28/**
29 * Google Docs Plugin
30 *
31 * @since 2.0
32 * @package repository_googledocs
33 * @copyright 2009 Dan Poltawski <talktodan@gmail.com>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
6667f9e4 36class repository_googledocs extends repository {
4560fd1b 37 private $googleoauth = null;
6667f9e4 38
447c7a19 39 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
6667f9e4 40 parent::__construct($repositoryid, $context, $options);
41
5df1b737
DP
42 $returnurl = new moodle_url('/repository/repository_callback.php');
43 $returnurl->param('callback', 'yes');
44 $returnurl->param('repo_id', $this->id);
45 $returnurl->param('sesskey', sesskey());
4560fd1b
DP
46
47 $clientid = get_config('googledocs', 'clientid');
48 $secret = get_config('googledocs', 'secret');
db7602af 49 $this->googleoauth = new google_oauth($clientid, $secret, $returnurl, google_docs::REALM);
4560fd1b 50
94742bca 51 $this->check_login();
6667f9e4 52 }
53
54 public function check_login() {
4560fd1b 55 return $this->googleoauth->is_logged_in();
6667f9e4 56 }
57
4560fd1b
DP
58 public function print_login() {
59 $url = $this->googleoauth->get_login_url();
60
61 if ($this->options['ajax']) {
62 $popup = new stdClass();
63 $popup->type = 'popup';
64 $popup->url = $url->out(false);
65 return array('login' => array($popup));
66 } else {
67 echo '<a target="_blank" href="'.$url->out(false).'">'.get_string('login', 'repository').'</a>';
6667f9e4 68 }
69 }
70
71 public function get_listing($path='', $page = '') {
4560fd1b 72 $gdocs = new google_docs($this->googleoauth);
6667f9e4 73
74 $ret = array();
75 $ret['dynload'] = true;
76 $ret['list'] = $gdocs->get_file_list();
77 return $ret;
78 }
79
68a7c9a6 80 public function search($search_text, $page = 0) {
4560fd1b 81 $gdocs = new google_docs($this->googleoauth);
6667f9e4 82
83 $ret = array();
84 $ret['dynload'] = true;
68a7c9a6 85 $ret['list'] = $gdocs->get_file_list($search_text);
6667f9e4 86 return $ret;
87 }
88
4560fd1b
DP
89 public function logout() {
90 $this->googleoauth->log_out();
6667f9e4 91 return parent::logout();
92 }
93
68a7c9a6 94 public function get_file($url, $file = '') {
ec7e998f
FM
95 if (empty($url)) {
96 throw new repository_exception('cannotdownload', 'repository');
97 }
4560fd1b 98 $gdocs = new google_docs($this->googleoauth);
4560fd1b 99 $path = $this->prepare_file($file);
7bb7bd2e 100 return $gdocs->download_file($url, $path, self::GETFILE_TIMEOUT);
41076c58 101 }
6667f9e4 102
41076c58 103 public function supported_filetypes() {
4560fd1b 104 return '*';
41076c58
DC
105 }
106 public function supported_returntypes() {
107 return FILE_INTERNAL;
108 }
4560fd1b
DP
109
110 public static function get_type_option_names() {
111 return array('clientid', 'secret', 'pluginname');
112 }
113
114 public static function type_config_form($mform, $classname = 'repository') {
115
116 $a = new stdClass;
8b503936 117 $a->docsurl = get_docs_url('Google_OAuth_2.0_setup');
4560fd1b
DP
118 $a->callbackurl = google_oauth::callback_url()->out(false);
119
120 $mform->addElement('static', null, '', get_string('oauthinfo', 'repository_googledocs', $a));
121
122 parent::type_config_form($mform);
123 $mform->addElement('text', 'clientid', get_string('clientid', 'repository_googledocs'));
999427e9 124 $mform->setType('clientid', PARAM_RAW_TRIMMED);
4560fd1b 125 $mform->addElement('text', 'secret', get_string('secret', 'repository_googledocs'));
999427e9 126 $mform->setType('secret', PARAM_RAW_TRIMMED);
4560fd1b
DP
127
128 $strrequired = get_string('required');
129 $mform->addRule('clientid', $strrequired, 'required', null, 'client');
130 $mform->addRule('secret', $strrequired, 'required', null, 'client');
131 }
6667f9e4 132}
4560fd1b 133// Icon from: http://www.iconspedia.com/icon/google-2706.html.