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   * Bulk user registration script from a comma separated file
  19   *
  20   * @package    tool
  21   * @subpackage uploaduser
  22   * @copyright  2004 onwards Martin Dougiamas (http://dougiamas.com)
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  require('../../../config.php');
  27  require_once($CFG->libdir.'/adminlib.php');
  28  require_once($CFG->libdir.'/csvlib.class.php');
  29  require_once($CFG->dirroot.'/'.$CFG->admin.'/tool/uploaduser/locallib.php');
  30  require_once($CFG->dirroot.'/'.$CFG->admin.'/tool/uploaduser/user_form.php');
  31  
  32  $iid         = optional_param('iid', '', PARAM_INT);
  33  $previewrows = optional_param('previewrows', 10, PARAM_INT);
  34  
  35  core_php_time_limit::raise(60 * 60); // 1 hour should be enough.
  36  raise_memory_limit(MEMORY_HUGE);
  37  
  38  admin_externalpage_setup('tooluploaduser');
  39  
  40  $returnurl = new moodle_url('/admin/tool/uploaduser/index.php');
  41  $bulknurl  = new moodle_url('/admin/user/user_bulk.php');
  42  
  43  if (empty($iid)) {
  44      $mform1 = new admin_uploaduser_form1();
  45  
  46      if ($formdata = $mform1->get_data()) {
  47          $iid = csv_import_reader::get_new_iid('uploaduser');
  48          $cir = new csv_import_reader($iid, 'uploaduser');
  49  
  50          $content = $mform1->get_file_content('userfile');
  51  
  52          $readcount = $cir->load_csv_content($content, $formdata->encoding, $formdata->delimiter_name);
  53          $csvloaderror = $cir->get_error();
  54          unset($content);
  55  
  56          if (!is_null($csvloaderror)) {
  57              throw new \moodle_exception('csvloaderror', '', $returnurl, $csvloaderror);
  58          }
  59          // Continue to form2.
  60  
  61      } else {
  62          echo $OUTPUT->header();
  63  
  64          echo $OUTPUT->heading_with_help(get_string('uploadusers', 'tool_uploaduser'), 'uploadusers', 'tool_uploaduser');
  65  
  66          $mform1->display();
  67          echo $OUTPUT->footer();
  68          die;
  69      }
  70  } else {
  71      $cir = new csv_import_reader($iid, 'uploaduser');
  72  }
  73  
  74  // Test if columns ok.
  75  $process = new \tool_uploaduser\process($cir);
  76  $filecolumns = $process->get_file_columns();
  77  
  78  $mform2 = new admin_uploaduser_form2(null,
  79      ['columns' => $filecolumns, 'data' => ['iid' => $iid, 'previewrows' => $previewrows]]);
  80  
  81  // If a file has been uploaded, then process it.
  82  if ($formdata = $mform2->is_cancelled()) {
  83      $cir->cleanup(true);
  84      redirect($returnurl);
  85  
  86  } else if ($formdata = $mform2->get_data()) {
  87      // Print the header.
  88      echo $OUTPUT->header();
  89      echo $OUTPUT->heading(get_string('uploadusersresult', 'tool_uploaduser'));
  90  
  91      $process->set_form_data($formdata);
  92      $process->process();
  93  
  94      echo $OUTPUT->box_start('boxwidthnarrow boxaligncenter generalbox', 'uploadresults');
  95      echo html_writer::tag('p', join('<br />', $process->get_stats()));
  96      echo $OUTPUT->box_end();
  97  
  98      if ($process->get_bulk()) {
  99          echo $OUTPUT->continue_button($bulknurl);
 100      } else {
 101          echo $OUTPUT->continue_button($returnurl);
 102      }
 103      echo $OUTPUT->footer();
 104      die;
 105  }
 106  
 107  // Print the header.
 108  echo $OUTPUT->header();
 109  
 110  echo $OUTPUT->heading(get_string('uploaduserspreview', 'tool_uploaduser'));
 111  
 112  // NOTE: this is JUST csv processing preview, we must not prevent import from here if there is something in the file!!
 113  // this was intended for validation of csv formatting and encoding, not filtering the data!!!!
 114  // we definitely must not process the whole file!
 115  
 116  // Preview table data.
 117  $table = new \tool_uploaduser\preview($cir, $filecolumns, $previewrows);
 118  
 119  echo html_writer::tag('div', html_writer::table($table), ['class' => 'flexible-wrap']);
 120  
 121  // Print the form if valid values are available.
 122  if ($table->get_no_error()) {
 123      $mform2->display();
 124  }
 125  echo $OUTPUT->footer();
 126  die;