Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   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   * Page to select WHAT to do with a given resource stored on MoodleNet.
  19   *
  20   * This collates and presents the same options as a user would see for a drag and drop upload.
  21   * That is, it leverages the dndupload_register() hooks and delegates the resource handling to the dndupload_handle hooks.
  22   *
  23   * This page requires a course, section an resourceurl to be provided via import_info.
  24   *
  25   * @package     tool_moodlenet
  26   * @copyright   2020 Jake Dallimore <jrhdallimore@gmail.com>
  27   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   */
  29  use tool_moodlenet\local\import_handler_registry;
  30  use tool_moodlenet\local\import_processor;
  31  use tool_moodlenet\local\import_info;
  32  use tool_moodlenet\local\import_strategy_file;
  33  use tool_moodlenet\local\import_strategy_link;
  34  
  35  require_once(__DIR__ . '/../../../config.php');
  36  require_once($CFG->dirroot . '/course/lib.php');
  37  
  38  $module = optional_param('module', null, PARAM_PLUGIN);
  39  $import = optional_param('import', null, PARAM_ALPHA);
  40  $cancel = optional_param('cancel', null, PARAM_ALPHA);
  41  $id = required_param('id', PARAM_ALPHANUM);
  42  
  43  if (is_null($importinfo = import_info::load($id))) {
  44      throw new moodle_exception('missinginvalidpostdata', 'tool_moodlenet');
  45  }
  46  
  47  // Resolve course and section params.
  48  // If course is not already set in the importinfo, we require it in the URL params.
  49  $config = $importinfo->get_config();
  50  if (!isset($config->course)) {
  51      $course = required_param('course', PARAM_INT);
  52      $config->course = $course;
  53      $config->section = 0;
  54      $importinfo->set_config($config);
  55      $importinfo->save();
  56  }
  57  
  58  // Access control.
  59  require_login($config->course, false);
  60  require_capability('moodle/course:manageactivities', context_course::instance($config->course));
  61  if (!get_config('tool_moodlenet', 'enablemoodlenet')) {
  62      print_error('moodlenetnotenabled', 'tool_moodlenet');
  63  }
  64  
  65  // If the user cancelled, break early.
  66  if ($cancel) {
  67      redirect(new moodle_url('/course/view.php', ['id' => $config->course]));
  68  }
  69  
  70  // Set up required objects.
  71  $course = get_course($config->course);
  72  $handlerregistry = new import_handler_registry($course, $USER);
  73  switch ($config->type) {
  74      case 'file':
  75          $strategy = new import_strategy_file();
  76          break;
  77      case 'link':
  78      default:
  79          $strategy = new import_strategy_link();
  80          break;
  81  }
  82  
  83  if ($import && $module) {
  84      confirm_sesskey();
  85  
  86      $handlerinfo = $handlerregistry->get_resource_handler_for_mod_and_strategy($importinfo->get_resource(), $module, $strategy);
  87      if (is_null($handlerinfo)) {
  88          throw new coding_exception("Invalid handler '$module'. The import handler could not be found.");
  89      }
  90      $importproc = new import_processor($course, $config->section, $importinfo->get_resource(), $handlerinfo, $handlerregistry);
  91      $importproc->process();
  92  
  93      $importinfo->purge(); // We don't need information about the import any more.
  94  
  95      redirect(new moodle_url('/course/view.php', ['id' => $course->id]));
  96  }
  97  
  98  // Setup the page and display the form.
  99  $PAGE->set_context(context_course::instance($course->id));
 100  $PAGE->set_pagelayout('base');
 101  $PAGE->set_title(get_string('coursetitle', 'moodle', array('course' => $course->fullname)));
 102  $PAGE->set_heading($course->fullname);
 103  $PAGE->set_url(new moodle_url('/admin/tool/moodlenet/options.php'));
 104  
 105  // Fetch the handlers supporting this resource. We'll display each of these as an option in the form.
 106  $handlercontext = [];
 107  foreach ($handlerregistry->get_resource_handlers_for_strategy($importinfo->get_resource(), $strategy) as $handler) {
 108      $handlercontext[] = [
 109          'module' => $handler->get_module_name(),
 110          'message' => $handler->get_description(),
 111      ];
 112  }
 113  
 114  // Template context.
 115  $context = [
 116      'resourcename' => $importinfo->get_resource()->get_name(),
 117      'resourcetype' => $importinfo->get_config()->type,
 118      'resourceurl' => urlencode($importinfo->get_resource()->get_url()->get_value()),
 119      'course' => $course->id,
 120      'section' => $config->section,
 121      'sesskey' => sesskey(),
 122      'handlers' => $handlercontext,
 123      'oneoption' => sizeof($handlercontext) === 1
 124  ];
 125  
 126  echo $OUTPUT->header();
 127  echo $PAGE->get_renderer('core')->render_from_template('tool_moodlenet/import_options_select', $context);
 128  echo $OUTPUT->footer();