Merge branch 'wip-MDL-61694_Check_Password_Callback' of https://github.com/Spudley...
[moodle.git] / portfolio / picasa / 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 * Picasa 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_picasa extends portfolio_plugin_push_base {
4560fd1b 27 private $googleoauth = null;
ee91cf95 28
38652d90 29 public function supported_formats() {
ee91cf95 30 return array(PORTFOLIO_FORMAT_IMAGE, PORTFOLIO_FORMAT_VIDEO);
31 }
32
33 public static function get_name() {
34 return get_string('pluginname', 'portfolio_picasa');
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://picasaweb.google.com/';
44 }
45
46 public function expected_time($callertime) {
bcf615af
DP
47 // We're forcing this to be run 'interactively' because the plugin
48 // does not support running in cron.
49 return PORTFOLIO_TIME_LOW;
ee91cf95 50 }
51
52 public function send_package() {
4560fd1b 53 if (!$this->googleoauth) {
ee91cf95 54 throw new portfolio_plugin_exception('noauthtoken', 'portfolio_picasa');
55 }
56
4560fd1b 57 $picasa = new google_picasa($this->googleoauth);
ee91cf95 58 foreach ($this->exporter->get_tempfiles() as $file) {
59
4560fd1b 60 if (!$picasa->send_file($file)) {
ee91cf95 61 throw new portfolio_plugin_exception('sendfailed', 'portfolio_picasa', $file->get_filename());
62 }
63 }
64 }
65
66 public function steal_control($stage) {
ee91cf95 67 if ($stage != PORTFOLIO_STAGE_CONFIG) {
68 return false;
69 }
70
4560fd1b 71 $this->initialize_oauth();
ee91cf95 72
4560fd1b
DP
73 if ($this->googleoauth->is_logged_in()) {
74 return false;
75 } else {
76 return $this->googleoauth->get_login_url();
ee91cf95 77 }
ee91cf95 78 }
79
80 public function post_control($stage, $params) {
81 if ($stage != PORTFOLIO_STAGE_CONFIG) {
82 return;
83 }
84
4560fd1b
DP
85 $this->initialize_oauth();
86 if ($this->googleoauth->is_logged_in()) {
87 return false;
88 } else {
89 return $this->googleoauth->get_login_url();
ee91cf95 90 }
4560fd1b 91 }
ee91cf95 92
4560fd1b
DP
93 public static function has_admin_config() {
94 return true;
ee91cf95 95 }
96
16f4918a
DP
97 public static function allows_multiple_instances() {
98 return false;
99 }
ee91cf95 100
4560fd1b
DP
101 public static function get_allowed_config() {
102 return array('clientid', 'secret');
103 }
104
c17ec774 105 public static function admin_config_form(&$mform) {
4560fd1b 106 $a = new stdClass;
8b503936 107 $a->docsurl = get_docs_url('Google_OAuth_2.0_setup');
4560fd1b
DP
108 $a->callbackurl = google_oauth::callback_url()->out(false);
109
110 $mform->addElement('static', null, '', get_string('oauthinfo', 'portfolio_picasa', $a));
111
112 $mform->addElement('text', 'clientid', get_string('clientid', 'portfolio_picasa'));
999427e9 113 $mform->setType('clientid', PARAM_RAW_TRIMMED);
4560fd1b 114 $mform->addElement('text', 'secret', get_string('secret', 'portfolio_picasa'));
999427e9 115 $mform->setType('secret', PARAM_RAW_TRIMMED);
4560fd1b
DP
116
117 $strrequired = get_string('required');
118 $mform->addRule('clientid', $strrequired, 'required', null, 'client');
119 $mform->addRule('secret', $strrequired, 'required', null, 'client');
ee91cf95 120 }
121
4560fd1b
DP
122 private function initialize_oauth() {
123 $returnurl = new moodle_url('/portfolio/add.php');
124 $returnurl->param('postcontrol', 1);
125 $returnurl->param('id', $this->exporter->get('id'));
126 $returnurl->param('sesskey', sesskey());
127
128 $clientid = $this->get_config('clientid');
129 $secret = $this->get_config('secret');
130
db7602af 131 $this->googleoauth = new google_oauth($clientid, $secret, $returnurl, google_picasa::REALM);
4560fd1b 132 }
b7acfd64
DP
133
134 public function instance_sanity_check() {
135 $clientid = $this->get_config('clientid');
136 $secret = $this->get_config('secret');
137
138 // If there is no oauth config (e.g. plugins upgraded from < 2.3 then
139 // there will be no config and this plugin should be disabled.
140 if (empty($clientid) or empty($secret)) {
141 return 'nooauthcredentials';
142 }
143 return 0;
144 }
ee91cf95 145}