Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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  require_once("../../../config.php");
  19  require_once($CFG->libdir.'/gradelib.php');
  20  require_once($CFG->dirroot.'/grade/lib.php');
  21  require_once($CFG->dirroot. '/grade/import/grade_import_form.php');
  22  require_once($CFG->dirroot.'/grade/import/lib.php');
  23  require_once($CFG->libdir . '/csvlib.class.php');
  24  
  25  $id            = required_param('id', PARAM_INT); // Course id.
  26  $separator     = optional_param('separator', '', PARAM_ALPHA);
  27  $verbosescales = optional_param('verbosescales', 1, PARAM_BOOL);
  28  $iid           = optional_param('iid', null, PARAM_INT);
  29  $importcode    = optional_param('importcode', '', PARAM_FILE);
  30  $forceimport   = optional_param('forceimport', false, PARAM_BOOL);
  31  
  32  $url = new moodle_url('/grade/import/csv/index.php', array('id'=>$id));
  33  if ($separator !== '') {
  34      $url->param('separator', $separator);
  35  }
  36  if ($verbosescales !== 1) {
  37      $url->param('verbosescales', $verbosescales);
  38  }
  39  $PAGE->set_url($url);
  40  
  41  if (!$course = $DB->get_record('course', array('id'=>$id))) {
  42      print_error('invalidcourseid');
  43  }
  44  
  45  require_login($course);
  46  $context = context_course::instance($id);
  47  require_capability('moodle/grade:import', $context);
  48  require_capability('gradeimport/csv:view', $context);
  49  
  50  $separatemode = (groups_get_course_groupmode($COURSE) == SEPARATEGROUPS and
  51          !has_capability('moodle/site:accessallgroups', $context));
  52  $currentgroup = groups_get_course_group($course);
  53  
  54  $actionbar = new \core_grades\output\import_action_bar($context, $PAGE->url, 'csv');
  55  print_grade_page_head($course->id, 'import', 'csv', get_string('importcsv', 'grades'), false, false, true,
  56      'importcsv', 'grades', null, $actionbar);
  57  
  58  $renderer = $PAGE->get_renderer('gradeimport_csv');
  59  
  60  // Get the grade items to be matched with the import mapping columns.
  61  $gradeitems = gradeimport_csv_load_data::fetch_grade_items($course->id);
  62  
  63  // If the csv file hasn't been imported yet then look for a form submission or
  64  // show the initial submission form.
  65  if (!$iid) {
  66  
  67      // Set up the import form.
  68      $mform = new grade_import_form(null, array('includeseparator' => true, 'verbosescales' => $verbosescales, 'acceptedtypes' =>
  69              array('.csv', '.txt')));
  70  
  71      // If the import form has been submitted.
  72      if ($formdata = $mform->get_data()) {
  73          $text = $mform->get_file_content('userfile');
  74          $csvimport = new gradeimport_csv_load_data();
  75          $csvimport->load_csv_content($text, $formdata->encoding, $separator, $formdata->previewrows);
  76          $csvimporterror = $csvimport->get_error();
  77          if (!empty($csvimporterror)) {
  78              echo $renderer->errors(array($csvimport->get_error()));
  79              echo $OUTPUT->footer();
  80              die();
  81          }
  82          $iid = $csvimport->get_iid();
  83          echo $renderer->import_preview_page($csvimport->get_headers(), $csvimport->get_previewdata());
  84      } else {
  85          // Display the standard upload file form.
  86          echo $renderer->standard_upload_file_form($course, $mform);
  87          echo $OUTPUT->footer();
  88          die();
  89      }
  90  }
  91  
  92  // Data has already been submitted so we can use the $iid to retrieve it.
  93  $csvimport = new csv_import_reader($iid, 'grade');
  94  $header = $csvimport->get_columns();
  95  // Get a new import code for updating to the grade book.
  96  if (empty($importcode)) {
  97      $importcode = get_new_importcode();
  98  }
  99  
 100  $mappingformdata = array(
 101      'gradeitems' => $gradeitems,
 102      'header' => $header,
 103      'iid' => $iid,
 104      'id' => $id,
 105      'importcode' => $importcode,
 106      'forceimport' => $forceimport,
 107      'verbosescales' => $verbosescales
 108  );
 109  // we create a form to handle mapping data from the file to the database.
 110  $mform2 = new grade_import_mapping_form(null, $mappingformdata);
 111  
 112  // Here, if we have data, we process the fields and enter the information into the database.
 113  if ($formdata = $mform2->get_data()) {
 114      $gradeimport = new gradeimport_csv_load_data();
 115      $status = $gradeimport->prepare_import_grade_data($header, $formdata, $csvimport, $course->id, $separatemode,
 116              $currentgroup, $verbosescales);
 117  
 118      // At this stage if things are all ok, we commit the changes from temp table.
 119      if ($status) {
 120          grade_import_commit($course->id, $importcode);
 121      } else {
 122          $errors = $gradeimport->get_gradebookerrors();
 123          $errors[] = get_string('importfailed', 'grades');
 124          echo $renderer->errors($errors);
 125      }
 126      echo $OUTPUT->footer();
 127  } else {
 128      // If data hasn't been submitted then display the data mapping form.
 129      $mform2->display();
 130      echo $OUTPUT->footer();
 131  }