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.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 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   * Landing page for all imports from MoodleNet.
  19   *
  20   * This page asks the user to confirm the import process, and takes them to the relevant next step.
  21   *
  22   * @package     tool_moodlenet
  23   * @copyright   2020 Jake Dallimore <jrhdallimore@gmail.com>
  24   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  use tool_moodlenet\local\import_info;
  28  use tool_moodlenet\local\import_backup_helper;
  29  
  30  require_once(__DIR__ . '/../../../config.php');
  31  require_once($CFG->dirroot .'/course/lib.php');
  32  
  33  $cancel = optional_param('cancel', null, PARAM_TEXT);
  34  $continue = optional_param('continue', null, PARAM_TEXT);
  35  $id = required_param('id', PARAM_ALPHANUM);
  36  
  37  if (is_null($importinfo = import_info::load($id))) {
  38      throw new moodle_exception('missinginvalidpostdata', 'tool_moodlenet');
  39  }
  40  
  41  // Access control.
  42  require_login($importinfo->get_config()->course, false); // Course may be null here - that's ok.
  43  if ($importinfo->get_config()->course) {
  44      require_capability('moodle/course:manageactivities', context_course::instance($importinfo->get_config()->course));
  45  }
  46  if (!get_config('tool_moodlenet', 'enablemoodlenet')) {
  47      throw new \moodle_exception('moodlenetnotenabled', 'tool_moodlenet');
  48  }
  49  
  50  // Handle the form submits.
  51  // This page POSTs to self to verify the sesskey for the confirm action.
  52  // The next page will either be:
  53  // - 1. The restore process for a course or module, if the file is an mbz file.
  54  // - 2. The 'select a course' tool page, if course and section are not provided.
  55  // - 3. The 'select what to do with the content' tool page, provided course and section are present.
  56  // - 4. The dashboard, if the user decides to cancel and course or section is not found.
  57  // - 5. The course home, if the user decides to cancel but the course and section are found.
  58  if ($cancel) {
  59      if (!empty($importinfo->get_config()->course)) {
  60          $url = new \moodle_url('/course/view.php', ['id' => $importinfo->get_config()->course]);
  61      } else {
  62          $url = new \moodle_url('/');
  63      }
  64      redirect($url);
  65  } else if ($continue) {
  66      confirm_sesskey();
  67  
  68      // Handle backups.
  69      if (strtolower($importinfo->get_resource()->get_extension()) == 'mbz') {
  70          if (empty($importinfo->get_config()->course)) {
  71              // Find a course that the user has permission to upload a backup file.
  72              // This is likely to be very slow on larger sites.
  73              $context = import_backup_helper::get_context_for_user($USER->id);
  74  
  75              if (is_null($context)) {
  76                  throw new \moodle_exception('nopermissions', 'error', '', get_string('restore:uploadfile', 'core_role'));
  77              }
  78          } else {
  79              $context = context_course::instance($importinfo->get_config()->course);
  80          }
  81  
  82          $importbackuphelper = new import_backup_helper($importinfo->get_resource(), $USER, $context);
  83          $storedfile = $importbackuphelper->get_stored_file();
  84  
  85          $url = new \moodle_url('/backup/restorefile.php', [
  86              'component' => $storedfile->get_component(),
  87              'filearea' => $storedfile->get_filearea(),
  88              'itemid' => $storedfile->get_itemid(),
  89              'filepath' => $storedfile->get_filepath(),
  90              'filename' => $storedfile->get_filename(),
  91              'filecontextid' => $storedfile->get_contextid(),
  92              'contextid' => $context->id,
  93              'action' => 'choosebackupfile'
  94          ]);
  95          redirect($url);
  96      }
  97  
  98      // Handle adding files to a course.
  99      // Course and section data present and confirmed. Redirect to the option select view.
 100      if (!is_null($importinfo->get_config()->course) && !is_null($importinfo->get_config()->section)) {
 101          redirect(new \moodle_url('/admin/tool/moodlenet/options.php', ['id' => $id]));
 102      }
 103  
 104      if (is_null($importinfo->get_config()->course)) {
 105          redirect(new \moodle_url('/admin/tool/moodlenet/select.php', ['id' => $id]));
 106      }
 107  }
 108  
 109  // Display the page.
 110  $PAGE->set_context(context_system::instance());
 111  $PAGE->set_pagelayout('base');
 112  $PAGE->set_title(get_string('addingaresource', 'tool_moodlenet'));
 113  $PAGE->set_heading(get_string('addingaresource', 'tool_moodlenet'));
 114  $url = new moodle_url('/admin/tool/moodlenet/index.php');
 115  $PAGE->set_url($url);
 116  $renderer = $PAGE->get_renderer('core');
 117  
 118  // Relevant confirmation form.
 119  $context = $context = [
 120      'resourceurl' => $importinfo->get_resource()->get_url()->get_value(),
 121      'resourcename' => $importinfo->get_resource()->get_name(),
 122      'resourcetype' => $importinfo->get_config()->type,
 123      'sesskey' => sesskey()
 124  ];
 125  if (!is_null($importinfo->get_config()->course) && !is_null($importinfo->get_config()->section)) {
 126      $course = get_course($importinfo->get_config()->course);
 127      $context = array_merge($context, [
 128          'course' => $course->id,
 129          'coursename' => $course->shortname,
 130          'section' => $importinfo->get_config()->section
 131      ]);
 132  }
 133  
 134  echo $OUTPUT->header();
 135  echo $renderer->render_from_template('tool_moodlenet/import_confirmation', $context);
 136  echo $OUTPUT->footer();