[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/portfolio/googledocs/ -> lib.php (source)

   1  <?php
   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  
  17  /**
  18   * Google Documents Portfolio Plugin
  19   *
  20   * @author Dan Poltawski <[email protected]>
  21   * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  22   */
  23  require_once($CFG->libdir.'/portfolio/plugin.php');
  24  require_once($CFG->libdir.'/googleapi.php');
  25  
  26  class portfolio_plugin_googledocs extends portfolio_plugin_push_base {
  27      private $googleoauth = null;
  28  
  29      public function supported_formats() {
  30          return array(PORTFOLIO_FORMAT_FILE, PORTFOLIO_FORMAT_RICHHTML);
  31      }
  32  
  33      public static function get_name() {
  34          return get_string('pluginname', 'portfolio_googledocs');
  35      }
  36  
  37      public function prepare_package() {
  38          // We send the files as they are, no prep required.
  39          return true;
  40      }
  41  
  42      public function get_interactive_continue_url() {
  43          return 'http://docs.google.com/';
  44      }
  45  
  46      public function expected_time($callertime) {
  47          // We're forcing this to be run 'interactively' because the plugin
  48          // does not support running in cron.
  49          return PORTFOLIO_TIME_LOW;
  50      }
  51  
  52      public function send_package() {
  53          if (!$this->googleoauth) {
  54              throw new portfolio_plugin_exception('noauthtoken', 'portfolio_googledocs');
  55          }
  56  
  57          $gdocs = new google_docs($this->googleoauth);
  58          foreach ($this->exporter->get_tempfiles() as $file) {
  59              if (!$gdocs->send_file($file)) {
  60                  throw new portfolio_plugin_exception('sendfailed', 'portfolio_gdocs', $file->get_filename());
  61              }
  62          }
  63      }
  64  
  65      public function steal_control($stage) {
  66          global $CFG;
  67          if ($stage != PORTFOLIO_STAGE_CONFIG) {
  68              return false;
  69          }
  70  
  71          $this->initialize_oauth();
  72          if ($this->googleoauth->is_logged_in()) {
  73              return false;
  74          } else {
  75              return $this->googleoauth->get_login_url();
  76          }
  77      }
  78  
  79      public function post_control($stage, $params) {
  80          if ($stage != PORTFOLIO_STAGE_CONFIG) {
  81              return;
  82          }
  83  
  84          $this->initialize_oauth();
  85          if ($this->googleoauth->is_logged_in()) {
  86              return false;
  87          } else {
  88              return $this->googleoauth->get_login_url();
  89          }
  90      }
  91  
  92      public static function allows_multiple_instances() {
  93          return false;
  94      }
  95  
  96      public static function has_admin_config() {
  97          return true;
  98      }
  99  
 100      public static function get_allowed_config() {
 101          return array('clientid', 'secret');
 102      }
 103  
 104      public static function admin_config_form(&$mform) {
 105          $a = new stdClass;
 106          $a->docsurl = get_docs_url('Google_OAuth_2.0_setup');
 107          $a->callbackurl = google_oauth::callback_url()->out(false);
 108  
 109          $mform->addElement('static', null, '', get_string('oauthinfo', 'portfolio_googledocs', $a));
 110  
 111          $mform->addElement('text', 'clientid', get_string('clientid', 'portfolio_googledocs'));
 112          $mform->setType('clientid', PARAM_RAW_TRIMMED);
 113          $mform->addElement('text', 'secret', get_string('secret', 'portfolio_googledocs'));
 114          $mform->setType('secret', PARAM_RAW_TRIMMED);
 115  
 116          $strrequired = get_string('required');
 117          $mform->addRule('clientid', $strrequired, 'required', null, 'client');
 118          $mform->addRule('secret', $strrequired, 'required', null, 'client');
 119      }
 120  
 121      private function initialize_oauth() {
 122          $returnurl = new moodle_url('/portfolio/add.php');
 123          $returnurl->param('postcontrol', 1);
 124          $returnurl->param('id', $this->exporter->get('id'));
 125          $returnurl->param('sesskey', sesskey());
 126  
 127          $clientid = $this->get_config('clientid');
 128          $secret = $this->get_config('secret');
 129  
 130          $this->googleoauth = new google_oauth($clientid, $secret, $returnurl, google_docs::REALM);
 131      }
 132  
 133      public function instance_sanity_check() {
 134          $clientid = $this->get_config('clientid');
 135          $secret = $this->get_config('secret');
 136  
 137          // If there is no oauth config (e.g. plugins upgraded from < 2.3 then
 138          // there will be no config and this plugin should be disabled.
 139          if (empty($clientid) or empty($secret)) {
 140              return 'nooauthcredentials';
 141          }
 142          return 0;
 143      }
 144  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1