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  // 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      print_error('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  print_grade_page_head($course->id, 'import', 'direct', get_string('pluginname', 'gradeimport_direct'), false, false, true,
  51          'userdata', 'gradeimport_direct');
  52  
  53  $renderer = $PAGE->get_renderer('gradeimport_csv');
  54  
  55  // Get the grade items to be matched with the import mapping columns.
  56  $gradeitems = gradeimport_csv_load_data::fetch_grade_items($course->id);
  57  
  58  // If the csv file hasn't been imported yet then look for a form submission or
  59  // show the initial submission form.
  60  if (!$iid) {
  61  
  62      // Set up the import form.
  63      $mform = new gradeimport_direct_import_form(null, array('includeseparator' => true, 'verbosescales' => true, 'acceptedtypes' =>
  64          array('.csv', '.txt')));
  65  
  66      // If the import form has been submitted.
  67      if ($formdata = $mform->get_data()) {
  68          $text = $formdata->userdata;
  69          $csvimport = new gradeimport_csv_load_data();
  70          $csvimport->load_csv_content($text, $formdata->encoding, 'tab', $formdata->previewrows);
  71          $csvimporterror = $csvimport->get_error();
  72          if (!empty($csvimporterror)) {
  73              echo $renderer->errors(array($csvimport->get_error()));
  74              echo $OUTPUT->footer();
  75              die();
  76          }
  77          $iid = $csvimport->get_iid();
  78          echo $renderer->import_preview_page($csvimport->get_headers(), $csvimport->get_previewdata());
  79      } else {
  80          // Display the standard upload file form.
  81          echo $renderer->standard_upload_file_form($course, $mform);
  82          echo $OUTPUT->footer();
  83          die();
  84      }
  85  }
  86  
  87  // Data has already been submitted so we can use the $iid to retrieve it.
  88  $csvimport = new csv_import_reader($iid, 'grade');
  89  $header = $csvimport->get_columns();
  90  // Get a new import code for updating to the grade book.
  91  if (empty($importcode)) {
  92      $importcode = get_new_importcode();
  93  }
  94  
  95  $mappingformdata = array(
  96      'gradeitems' => $gradeitems,
  97      'header' => $header,
  98      'iid' => $iid,
  99      'id' => $id,
 100      'forceimport' => $forceimport,
 101      'importcode' => $importcode,
 102      'verbosescales' => $verbosescales
 103  );
 104  // We create a form to handle mapping data from the file to the database.
 105  $mform2 = new gradeimport_direct_mapping_form(null, $mappingformdata);
 106  
 107  // Here, if we have data, we process the fields and enter the information into the database.
 108  if ($formdata = $mform2->get_data()) {
 109      $gradeimport = new gradeimport_csv_load_data();
 110      $status = $gradeimport->prepare_import_grade_data($header, $formdata, $csvimport, $course->id, $separatemode, $currentgroup,
 111              $verbosescales);
 112  
 113      // At this stage if things are all ok, we commit the changes from temp table.
 114      if ($status) {
 115          grade_import_commit($course->id, $importcode);
 116      } else {
 117          $errors = $gradeimport->get_gradebookerrors();
 118          $errors[] = get_string('importfailed', 'grades');
 119          echo $renderer->errors($errors);
 120      }
 121      echo $OUTPUT->footer();
 122  } else {
 123      // If data hasn't been submitted then display the data mapping form.
 124      $mform2->display();
 125      echo $OUTPUT->footer();
 126  }