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   * Bulk course registration script from a comma separated file.
  19   *
  20   * @package    tool_uploadcourse
  21   * @copyright  2011 Piers Harding
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require(__DIR__ . '/../../../config.php');
  26  require_once($CFG->libdir . '/adminlib.php');
  27  require_once($CFG->libdir . '/csvlib.class.php');
  28  
  29  admin_externalpage_setup('tooluploadcourse');
  30  
  31  $importid         = optional_param('importid', '', PARAM_INT);
  32  $previewrows = optional_param('previewrows', 10, PARAM_INT);
  33  
  34  $returnurl = new moodle_url('/admin/tool/uploadcourse/index.php');
  35  
  36  if (empty($importid)) {
  37      $mform1 = new tool_uploadcourse_step1_form();
  38      if ($form1data = $mform1->get_data()) {
  39          $importid = csv_import_reader::get_new_iid('uploadcourse');
  40          $cir = new csv_import_reader($importid, 'uploadcourse');
  41          $content = $mform1->get_file_content('coursefile');
  42          $readcount = $cir->load_csv_content($content, $form1data->encoding, $form1data->delimiter_name);
  43          unset($content);
  44          if ($readcount === false) {
  45              throw new \moodle_exception('csvfileerror', 'tool_uploadcourse', $returnurl, $cir->get_error());
  46          } else if ($readcount == 0) {
  47              throw new \moodle_exception('csvemptyfile', 'error', $returnurl, $cir->get_error());
  48          }
  49      } else {
  50          echo $OUTPUT->header();
  51          echo $OUTPUT->heading_with_help(get_string('uploadcourses', 'tool_uploadcourse'), 'uploadcourses', 'tool_uploadcourse');
  52          $mform1->display();
  53          echo $OUTPUT->footer();
  54          die();
  55      }
  56  } else {
  57      $cir = new csv_import_reader($importid, 'uploadcourse');
  58  }
  59  
  60  // Data to set in the form.
  61  $data = array('importid' => $importid, 'previewrows' => $previewrows);
  62  if (!empty($form1data)) {
  63      // Get options from the first form to pass it onto the second.
  64      foreach ($form1data->options as $key => $value) {
  65          $data["options[$key]"] = $value;
  66      }
  67  }
  68  $context = context_system::instance();
  69  $mform2 = new tool_uploadcourse_step2_form(null, array('contextid' => $context->id, 'columns' => $cir->get_columns(),
  70      'data' => $data));
  71  
  72  // If a file has been uploaded, then process it.
  73  if ($form2data = $mform2->is_cancelled()) {
  74      $cir->cleanup(true);
  75      redirect($returnurl);
  76  } else if ($form2data = $mform2->get_data()) {
  77  
  78      $options = (array) $form2data->options;
  79      $defaults = (array) $form2data->defaults;
  80  
  81      // Custom field defaults.
  82      $customfields = tool_uploadcourse_helper::get_custom_course_field_names();
  83      foreach ($customfields as $customfield) {
  84          $defaults[$customfield] = $form2data->{$customfield};
  85      }
  86  
  87      // Restorefile deserves its own logic because formslib does not really appreciate
  88      // when the name of a filepicker is an array...
  89      $options['restorefile'] = '';
  90      if (!empty($form2data->restorefile)) {
  91          $options['restorefile'] = $mform2->save_temp_file('restorefile');
  92      }
  93      $processor = new tool_uploadcourse_processor($cir, $options, $defaults);
  94  
  95      echo $OUTPUT->header();
  96      if (isset($form2data->showpreview)) {
  97          echo $OUTPUT->heading(get_string('uploadcoursespreview', 'tool_uploadcourse'));
  98          $processor->preview($previewrows, new tool_uploadcourse_tracker(tool_uploadcourse_tracker::OUTPUT_HTML));
  99          $mform2->display();
 100      } else {
 101          echo $OUTPUT->heading(get_string('uploadcoursesresult', 'tool_uploadcourse'));
 102          $processor->execute(new tool_uploadcourse_tracker(tool_uploadcourse_tracker::OUTPUT_HTML));
 103          echo $OUTPUT->continue_button($returnurl);
 104      }
 105  
 106      // Deleting the file after processing or preview.
 107      if (!empty($options['restorefile'])) {
 108          @unlink($options['restorefile']);
 109      }
 110  
 111  } else {
 112      if (!empty($form1data)) {
 113          $options = $form1data->options;
 114      } else if ($submitteddata = $mform2->get_submitted_data()) {
 115          $options = (array)$submitteddata->options;
 116      } else {
 117          // Weird but we still need to provide a value, setting the default step1_form one.
 118          $options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_NEW);
 119      }
 120      $processor = new tool_uploadcourse_processor($cir, $options, array());
 121      echo $OUTPUT->header();
 122      echo $OUTPUT->heading(get_string('uploadcoursespreview', 'tool_uploadcourse'));
 123      $processor->preview($previewrows, new tool_uploadcourse_tracker(tool_uploadcourse_tracker::OUTPUT_HTML));
 124      $mform2->display();
 125  }
 126  
 127  echo $OUTPUT->footer();