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   * Contains the import_info class.
  18   *
  19   * @package tool_moodlenet
  20   * @copyright 2020 Jake Dallimore <jrhdallimore@gmail.com>
  21   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   */
  23  namespace tool_moodlenet\local;
  24  
  25  /**
  26   * Class import_info, describing objects which represent a resource being imported by a user.
  27   *
  28   * Objects of this class encapsulate both:
  29   * - information about the resource (remote_resource).
  30   * - config data pertaining to the import process, such as the destination course and section
  31   *   and how the resource should be treated (i.e. the type and the name of the module selected as the import handler)
  32   *
  33   * @copyright 2020 Jake Dallimore <jrhdallimore@gmail.com>
  34   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class import_info {
  37  
  38      /** @var int $userid the user conducting this import. */
  39      protected $userid;
  40  
  41      /** @var remote_resource $resource the resource being imported. */
  42      protected $resource;
  43  
  44      /** @var \stdClass $config config data pertaining to the import process, e.g. course, section, type. */
  45      protected $config;
  46  
  47      /** @var string $id string identifier for this object. */
  48      protected $id;
  49  
  50      /**
  51       * The import_controller constructor.
  52       *
  53       * @param int $userid the id of the user performing the import.
  54       * @param remote_resource $resource the resource being imported.
  55       * @param \stdClass $config import config like 'course', 'section', 'type'.
  56       */
  57      public function __construct(int $userid, remote_resource $resource, \stdClass $config) {
  58          $this->userid = $userid;
  59          $this->resource = $resource;
  60          $this->config = $config;
  61          $this->id = md5($resource->get_url()->get_value());
  62      }
  63  
  64      /**
  65       * Get the id of this object.
  66       */
  67      public function get_id() {
  68          return $this->id;
  69      }
  70  
  71      /**
  72       * Get the remote resource being imported.
  73       *
  74       * @return remote_resource the remote resource being imported.
  75       */
  76      public function get_resource(): remote_resource {
  77          return $this->resource;
  78      }
  79  
  80      /**
  81       * Get the configuration data pertaining to the import.
  82       *
  83       * @return \stdClass the import configuration data.
  84       */
  85      public function get_config(): \stdClass {
  86          return $this->config;
  87      }
  88  
  89      /**
  90       * Set the configuration data pertaining to the import.
  91       *
  92       * @param \stdClass $config the configuration data to set.
  93       */
  94      public function set_config(\stdClass $config): void {
  95          $this->config  = $config;
  96      }
  97  
  98      /**
  99       * Get an import_info object by id.
 100       *
 101       * @param string $id the id of the import_info object to load.
 102       * @return mixed an import_info object if found, otherwise null.
 103       */
 104      public static function load(string $id): ?import_info {
 105          // This currently lives in the session, so we don't need userid.
 106          // It might be useful if we ever move to another storage mechanism however, where we would need it.
 107          global $SESSION;
 108          return isset($SESSION->moodlenetimports[$id]) ? unserialize($SESSION->moodlenetimports[$id]) : null;
 109      }
 110  
 111      /**
 112       * Save this object to a store which is accessible across requests.
 113       */
 114      public function save(): void {
 115          global $SESSION;
 116          $SESSION->moodlenetimports[$this->id] = serialize($this);
 117      }
 118  
 119      /**
 120       * Remove all information about an import from the store.
 121       */
 122      public function purge(): void {
 123          global $SESSION;
 124          unset($SESSION->moodlenetimports[$this->id]);
 125      }
 126  }