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