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 * This plugin is used to access google docs
21 * @package repository_googledocs
22 * @copyright 2009 Dan Poltawski <talktodan@gmail.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once($CFG->dirroot . '/repository/lib.php');
26 require_once($CFG->libdir.'/googleapi.php');
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
36 class repository_googledocs extends repository {
37 private $googleoauth = null;
39 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
40 parent::__construct($repositoryid, $context, $options);
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());
47 $clientid = get_config('googledocs', 'clientid');
48 $secret = get_config('googledocs', 'secret');
49 $this->googleoauth = new google_oauth($clientid, $secret, $returnurl, google_docs::REALM);
54 public function check_login() {
55 return $this->googleoauth->is_logged_in();
58 public function print_login() {
59 $url = $this->googleoauth->get_login_url();
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));
67 echo '<a target="_blank" href="'.$url->out(false).'">'.get_string('login', 'repository').'</a>';
71 public function get_listing($path='', $page = '') {
72 $gdocs = new google_docs($this->googleoauth);
75 $ret['dynload'] = true;
76 $ret['list'] = $gdocs->get_file_list();
80 public function search($search_text, $page = 0) {
81 $gdocs = new google_docs($this->googleoauth);
84 $ret['dynload'] = true;
85 $ret['list'] = $gdocs->get_file_list($search_text);
89 public function logout() {
90 $this->googleoauth->log_out();
91 return parent::logout();
94 public function get_file($url, $file = '') {
95 $gdocs = new google_docs($this->googleoauth);
97 $path = $this->prepare_file($file);
98 return $gdocs->download_file($url, $path);
101 public function supported_filetypes() {
104 public function supported_returntypes() {
105 return FILE_INTERNAL;
108 public static function get_type_option_names() {
109 return array('clientid', 'secret', 'pluginname');
112 public static function type_config_form($mform, $classname = 'repository') {
115 $a->docsurl = get_docs_url('Google_OAuth_2.0_setup');
116 $a->callbackurl = google_oauth::callback_url()->out(false);
118 $mform->addElement('static', null, '', get_string('oauthinfo', 'repository_googledocs', $a));
120 parent::type_config_form($mform);
121 $mform->addElement('text', 'clientid', get_string('clientid', 'repository_googledocs'));
122 $mform->setType('clientid', PARAM_RAW_TRIMMED);
123 $mform->addElement('text', 'secret', get_string('secret', 'repository_googledocs'));
124 $mform->setType('secret', PARAM_RAW_TRIMMED);
126 $strrequired = get_string('required');
127 $mform->addRule('clientid', $strrequired, 'required', null, 'client');
128 $mform->addRule('secret', $strrequired, 'required', null, 'client');
131 // Icon from: http://www.iconspedia.com/icon/google-2706.html.