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 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * This file is part of the Database module for Moodle
  20   *
  21   * @copyright 2005 Martin Dougiamas  http://dougiamas.com
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   * @package mod_data
  24   */
  25  
  26  require_once('../../config.php');
  27  require_once ('lib.php');
  28  require_once($CFG->libdir.'/csvlib.class.php');
  29  require_once ('import_form.php');
  30  
  31  $id              = optional_param('id', 0, PARAM_INT);  // course module id
  32  $d               = optional_param('d', 0, PARAM_INT);   // database id
  33  $rid             = optional_param('rid', 0, PARAM_INT); // record id
  34  $fielddelimiter  = optional_param('fielddelimiter', ',', PARAM_CLEANHTML); // characters used as field delimiters for csv file import
  35  $fieldenclosure = optional_param('fieldenclosure', '', PARAM_CLEANHTML);   // characters used as record delimiters for csv file import
  36  
  37  $url = new moodle_url('/mod/data/import.php');
  38  if ($rid !== 0) {
  39      $url->param('rid', $rid);
  40  }
  41  if ($fielddelimiter !== '') {
  42      $url->param('fielddelimiter', $fielddelimiter);
  43  }
  44  if ($fieldenclosure !== '') {
  45      $url->param('fieldenclosure', $fieldenclosure);
  46  }
  47  
  48  if ($id) {
  49      $url->param('id', $id);
  50      $PAGE->set_url($url);
  51      $cm     = get_coursemodule_from_id('data', $id, 0, false, MUST_EXIST);
  52      $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
  53      $data   = $DB->get_record('data', array('id'=>$cm->instance), '*', MUST_EXIST);
  54  
  55  } else {
  56      $url->param('d', $d);
  57      $PAGE->set_url($url);
  58      $data   = $DB->get_record('data', array('id'=>$d), '*', MUST_EXIST);
  59      $course = $DB->get_record('course', array('id'=>$data->course), '*', MUST_EXIST);
  60      $cm     = get_coursemodule_from_instance('data', $data->id, $course->id, false, MUST_EXIST);
  61  }
  62  
  63  require_login($course, false, $cm);
  64  
  65  $context = context_module::instance($cm->id);
  66  require_capability('mod/data:manageentries', $context);
  67  
  68  /// Print the page header
  69  $PAGE->navbar->add(get_string('add', 'data'));
  70  $PAGE->set_title($data->name);
  71  $PAGE->set_heading($course->fullname);
  72  navigation_node::override_active_url(new moodle_url('/mod/data/import.php', array('d' => $data->id)));
  73  echo $OUTPUT->header();
  74  echo $OUTPUT->heading_with_help(get_string('uploadrecords', 'mod_data'), 'uploadrecords', 'mod_data');
  75  
  76  /// Groups needed for Add entry tab
  77  $currentgroup = groups_get_activity_group($cm);
  78  $groupmode = groups_get_activity_groupmode($cm);
  79  
  80  $form = new mod_data_import_form(new moodle_url('/mod/data/import.php'), ['dataid' => $data->id]);
  81  if (!$formdata = $form->get_data()) {
  82      /// Upload records section. Only for teachers and the admin.
  83      echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
  84      $form->display();
  85      echo $OUTPUT->box_end();
  86      echo $OUTPUT->footer();
  87      die;
  88  } else {
  89      $filecontent = $form->get_file_content('recordsfile');
  90      $recordsadded = data_import_csv($cm, $data, $filecontent, $formdata->encoding, $formdata->fielddelimiter);
  91  }
  92  
  93  if ($recordsadded > 0) {
  94      echo $OUTPUT->notification($recordsadded. ' '. get_string('recordssaved', 'data'), '');
  95  } else {
  96      echo $OUTPUT->notification(get_string('recordsnotsaved', 'data'), 'notifysuccess');
  97  }
  98  
  99  echo $OUTPUT->continue_button('import.php?d='.$data->id);
 100  
 101  /// Finish the page
 102  echo $OUTPUT->footer();