Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is 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  namespace core\moodlenet;
  18  
  19  use core\oauth2\client;
  20  use moodle_exception;
  21  use stdClass;
  22  use stored_file;
  23  
  24  /**
  25   * API for sharing Moodle LMS resources to MoodleNet instances.
  26   *
  27   * @package   core
  28   * @copyright 2023 Safat Shahin <safat.shahin@moodle.com>
  29   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  abstract class resource_sender {
  32  
  33      /**
  34       * @var int Backup share format - the content is being shared as a Moodle backup file.
  35       */
  36      public const SHARE_FORMAT_BACKUP = 0;
  37  
  38      /**
  39       * @var int Maximum upload file size (1.07 GB).
  40       */
  41      public const MAX_FILESIZE = 1070000000;
  42  
  43      /**
  44       * @var stdClass The course where the activity is located.
  45       */
  46      protected stdClass $course;
  47  
  48      /** @var resource_packager Resource packager. */
  49      protected resource_packager $packager;
  50  
  51      /**
  52       * Class constructor.
  53       *
  54       * @param int $resourceid The resource ID of the resource being shared.
  55       * @param int $userid The user ID who is sharing the activity.
  56       * @param moodlenet_client $moodlenetclient The moodlenet_client object used to perform the share.
  57       * @param client $oauthclient The OAuth 2 client for the MoodleNet instance.
  58       * @param int $shareformat The data format to share in. Defaults to a Moodle backup (SHARE_FORMAT_BACKUP).
  59       * @throws moodle_exception
  60       */
  61      public function __construct(
  62          int $resourceid,
  63          protected int $userid,
  64          protected moodlenet_client $moodlenetclient,
  65          protected client $oauthclient,
  66          protected int $shareformat = self::SHARE_FORMAT_BACKUP,
  67      ) {
  68          if (!in_array($shareformat, self::get_allowed_share_formats(), true)) {
  69              throw new moodle_exception('moodlenet:invalidshareformat');
  70          }
  71      }
  72  
  73      /**
  74       * Return the list of supported share formats.
  75       *
  76       * @return array Array of supported share format values.
  77       */
  78      protected static function get_allowed_share_formats(): array {
  79          return [
  80              self::SHARE_FORMAT_BACKUP,
  81          ];
  82      }
  83  
  84      /**
  85       * Share a resource to MoodleNet.
  86       *
  87       * @return array The HTTP response code from MoodleNet and the MoodleNet draft resource URL (URL empty string on fail).
  88       *               Format: ['responsecode' => 201, 'drafturl' => 'https://draft.mnurl/here']
  89       */
  90      abstract public function share_resource(): array;
  91  
  92      /**
  93       * Prepare the data for sharing, in the format specified.
  94       *
  95       * @return stored_file
  96       */
  97      protected function prepare_share_contents(): stored_file {
  98          return match ($this->shareformat) {
  99              self::SHARE_FORMAT_BACKUP => $this->packager->get_package(),
 100              default => throw new \coding_exception("Unknown share format: {$this->shareformat}'"),
 101          };
 102      }
 103  }