Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   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   * This is the main endpoint which MoodleNet instances POST to.
  19   *
  20   * MoodleNet instances send the user agent to this endpoint via a form POST.
  21   * Then:
  22   * 1. The POSTed resource information is put in a session store for cross-request access.
  23   * 2. This page makes a GET request for admin/tool/moodlenet/index.php (the import confirmation page).
  24   * 3. Then, depending on whether the user is authenticated, the user will either:
  25   * - If not authenticated, they will be asked to login, after which they will see the confirmation page (leveraging $wantsurl).
  26   * - If authenticated, they will see the confirmation page immediately.
  27   *
  28   * @package     tool_moodlenet
  29   * @copyright   2020 Jake Dallimore <jrhdallimore@gmail.com>
  30   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  
  33  use tool_moodlenet\local\import_info;
  34  use tool_moodlenet\local\remote_resource;
  35  use tool_moodlenet\local\url;
  36  
  37  require_once(__DIR__ . '/../../../config.php');
  38  
  39  // The integration must be enabled for this import endpoint to be active.
  40  if (!get_config('tool_moodlenet', 'enablemoodlenet')) {
  41      throw new \moodle_exception('moodlenetnotenabled', 'tool_moodlenet');
  42  }
  43  
  44  $resourceurl = required_param('resourceurl', PARAM_URL);
  45  $resourceinfo = required_param('resource_info', PARAM_RAW);
  46  $resourceinfo = json_decode($resourceinfo);
  47  $type = optional_param('type', 'link', PARAM_TEXT);
  48  $course = optional_param('course', 0, PARAM_INT);
  49  $section = optional_param('section', 0, PARAM_INT);
  50  // If course isn't provided, course and section are null.
  51  if (empty($course)) {
  52      $course = null;
  53      $section = null;
  54  }
  55  $name = validate_param($resourceinfo->name, PARAM_TEXT);
  56  $description = validate_param($resourceinfo->summary, PARAM_TEXT);
  57  
  58  // Only accept POSTs.
  59  if (!empty($_POST)) {
  60      // Store information about the import of the resource for the current user.
  61      $importconfig = (object) [
  62          'course' => $course,
  63          'section' => $section,
  64          'type' => $type,
  65      ];
  66      $metadata = (object) [
  67          'name' => $name,
  68          'description' => $description ?? ''
  69      ];
  70  
  71      require_once($CFG->libdir . '/filelib.php');
  72      $importinfo = new import_info(
  73          $USER->id,
  74          new remote_resource(new \curl(), new url($resourceurl), $metadata),
  75          $importconfig
  76      );
  77      $importinfo->save();
  78  
  79      // Redirect to the import confirmation page, detouring via the log in page if required.
  80      redirect(new moodle_url('/admin/tool/moodlenet/index.php', ['id' => $importinfo->get_id()]));
  81  
  82  }
  83  
  84  // Invalid or missing POST data. Show an error to the user.
  85  throw new \moodle_exception('missinginvalidpostdata', 'tool_moodlenet');