Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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