Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   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   * Microsoft Live Skydrive Repository Plugin
  19   *
  20   * @package    repository_skydrive
  21   * @copyright  2012 Lancaster University Network Services Ltd
  22   * @author     Dan Poltawski <dan.poltawski@luns.net.uk>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  require_once ('microsoftliveapi.php');
  29  
  30  /**
  31   * Microsoft skydrive repository plugin.
  32   *
  33   * @package    repository_skydrive
  34   * @copyright  2012 Lancaster University Network Services Ltd
  35   * @author     Dan Poltawski <dan.poltawski@luns.net.uk>
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class repository_skydrive extends repository {
  39      /** @var microsoft_skydrive skydrive oauth2 api helper object */
  40      private $skydrive = null;
  41  
  42      /**
  43       * Constructor
  44       *
  45       * @param int $repositoryid repository instance id.
  46       * @param int|stdClass $context a context id or context object.
  47       * @param array $options repository options.
  48       */
  49      public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
  50          parent::__construct($repositoryid, $context, $options);
  51  
  52          $clientid = get_config('skydrive', 'clientid');
  53          $secret = get_config('skydrive', 'secret');
  54          $returnurl = new moodle_url('/repository/repository_callback.php');
  55          $returnurl->param('callback', 'yes');
  56          $returnurl->param('repo_id', $this->id);
  57          $returnurl->param('sesskey', sesskey());
  58  
  59          $this->skydrive = new microsoft_skydrive($clientid, $secret, $returnurl);
  60          $this->check_login();
  61      }
  62  
  63      /**
  64       * Checks whether the user is logged in or not.
  65       *
  66       * @return bool true when logged in
  67       */
  68      public function check_login() {
  69          return $this->skydrive->is_logged_in();
  70      }
  71  
  72      /**
  73       * Print the login form, if required
  74       *
  75       * @return array of login options
  76       */
  77      public function print_login() {
  78          $url = $this->skydrive->get_login_url();
  79  
  80          if ($this->options['ajax']) {
  81              $popup = new stdClass();
  82              $popup->type = 'popup';
  83              $popup->url = $url->out(false);
  84              return array('login' => array($popup));
  85          } else {
  86              echo '<a target="_blank" href="'.$url->out(false).'">'.get_string('login', 'repository').'</a>';
  87          }
  88      }
  89  
  90      /**
  91       * Given a path, and perhaps a search, get a list of files.
  92       *
  93       * See details on {@link http://docs.moodle.org/dev/Repository_plugins}
  94       *
  95       * @param string $path identifier for current path
  96       * @param string $page the page number of file list
  97       * @return array list of files including meta information as specified by parent.
  98       */
  99      public function get_listing($path='', $page = '') {
 100          $ret = array();
 101          $ret['dynload'] = true;
 102          $ret['nosearch'] = true;
 103          $ret['manage'] = 'https://skydrive.live.com/';
 104  
 105          $fileslist = $this->skydrive->get_file_list($path);
 106          // Filter list for accepted types. Hopefully this will be done by core some day.
 107          $fileslist = array_filter($fileslist, array($this, 'filter'));
 108          $ret['list'] = $fileslist;
 109  
 110          // Generate path bar, always start with the plugin name.
 111          $ret['path']   = array();
 112          $ret['path'][] = array('name'=> $this->name, 'path'=>'');
 113  
 114          // Now add each level folder.
 115          $trail = '';
 116          if (!empty($path)) {
 117              $parts = explode('/', $path);
 118              foreach ($parts as $folderid) {
 119                  if (!empty($folderid)) {
 120                      $trail .= ('/'.$folderid);
 121                      $ret['path'][] = array('name' => $this->skydrive->get_folder_name($folderid),
 122                                             'path' => $trail);
 123                  }
 124              }
 125          }
 126  
 127          return $ret;
 128      }
 129  
 130      /**
 131       * Downloads a repository file and saves to a path.
 132       *
 133       * @param string $id identifier of file
 134       * @param string $filename to save file as
 135       * @return array with keys:
 136       *          path: internal location of the file
 137       *          url: URL to the source
 138       */
 139      public function get_file($id, $filename = '') {
 140          $path = $this->prepare_file($filename);
 141          return $this->skydrive->download_file($id, $path);
 142      }
 143  
 144      /**
 145       * Return names of the options to display in the repository form
 146       *
 147       * @return array of option names
 148       */
 149      public static function get_type_option_names() {
 150          return array('clientid', 'secret', 'pluginname');
 151      }
 152  
 153      /**
 154       * Setup repistory form.
 155       *
 156       * @param moodleform $mform Moodle form (passed by reference)
 157       * @param string $classname repository class name
 158       */
 159      public static function type_config_form($mform, $classname = 'repository') {
 160          global $OUTPUT;
 161  
 162          $a = new stdClass;
 163          $a->callbackurl = microsoft_skydrive::callback_url()->out(false);
 164          $mform->addElement('static', null, '', get_string('oauthinfo', 'repository_skydrive', $a));
 165  
 166          $mform->addElement('static', null, '', $OUTPUT->notification(get_string('deprecatedwarning', 'repository_skydrive', $a)));
 167  
 168          parent::type_config_form($mform);
 169          $strrequired = get_string('required');
 170          $mform->addElement('text', 'clientid', get_string('clientid', 'repository_skydrive'));
 171          $mform->addElement('text', 'secret', get_string('secret', 'repository_skydrive'));
 172          $mform->addRule('clientid', $strrequired, 'required', null, 'client');
 173          $mform->addRule('secret', $strrequired, 'required', null, 'client');
 174          $mform->setType('clientid', PARAM_RAW_TRIMMED);
 175          $mform->setType('secret', PARAM_RAW_TRIMMED);
 176      }
 177  
 178      /**
 179       * Logout from repository instance and return
 180       * login form.
 181       *
 182       * @return page to display
 183       */
 184      public function logout() {
 185          $this->skydrive->log_out();
 186          return $this->print_login();
 187      }
 188  
 189      /**
 190       * This repository doesn't support global search.
 191       *
 192       * @return bool if supports global search
 193       */
 194      public function global_search() {
 195          return false;
 196      }
 197  
 198      /**
 199       * This repoistory supports any filetype.
 200       *
 201       * @return string '*' means this repository support any files
 202       */
 203      public function supported_filetypes() {
 204          return '*';
 205      }
 206  
 207      /**
 208       * This repostiory only supports internal files
 209       *
 210       * @return int return type bitmask supported
 211       */
 212      public function supported_returntypes() {
 213          return FILE_INTERNAL;
 214      }
 215  }