Merge branch 'MDL-47637-master-fixup' of https://github.com/FMCorz/moodle
[moodle.git] / grade / import / csv / index.php
blobe2426f00bd2332ef49c4af7ba616a389a71d4ba5
1 <?php
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.
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/>.
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');
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);
32 $url = new moodle_url('/grade/import/csv/index.php', array('id'=>$id));
33 if ($separator !== '') {
34 $url->param('separator', $separator);
36 if ($verbosescales !== 1) {
37 $url->param('verbosescales', $verbosescales);
39 $PAGE->set_url($url);
41 if (!$course = $DB->get_record('course', array('id'=>$id))) {
42 print_error('nocourseid');
45 require_login($course);
46 $context = context_course::instance($id);
47 require_capability('moodle/grade:import', $context);
48 require_capability('gradeimport/csv:view', $context);
50 $separatemode = (groups_get_course_groupmode($COURSE) == SEPARATEGROUPS and
51 !has_capability('moodle/site:accessallgroups', $context));
52 $currentgroup = groups_get_course_group($course);
54 print_grade_page_head($course->id, 'import', 'csv', get_string('importcsv', 'grades'));
56 $renderer = $PAGE->get_renderer('gradeimport_csv');
58 // Get the grade items to be matched with the import mapping columns.
59 $gradeitems = gradeimport_csv_load_data::fetch_grade_items($course->id);
61 // If the csv file hasn't been imported yet then look for a form submission or
62 // show the initial submission form.
63 if (!$iid) {
65 // Set up the import form.
66 $mform = new grade_import_form(null, array('includeseparator' => true, 'verbosescales' => $verbosescales, 'acceptedtypes' =>
67 array('.csv', '.txt')));
69 // If the import form has been submitted.
70 if ($formdata = $mform->get_data()) {
71 $text = $mform->get_file_content('userfile');
72 $csvimport = new gradeimport_csv_load_data();
73 $csvimport->load_csv_content($text, $formdata->encoding, $separator, $formdata->previewrows);
74 $csvimporterror = $csvimport->get_error();
75 if (!empty($csvimporterror)) {
76 echo $renderer->errors(array($csvimport->get_error()));
77 echo $OUTPUT->footer();
78 die();
80 $iid = $csvimport->get_iid();
81 echo $renderer->import_preview_page($csvimport->get_headers(), $csvimport->get_previewdata());
82 } else {
83 // Display the standard upload file form.
84 echo $renderer->standard_upload_file_form($course, $mform);
85 echo $OUTPUT->footer();
86 die();
90 // Data has already been submitted so we can use the $iid to retrieve it.
91 $csvimport = new csv_import_reader($iid, 'grade');
92 $header = $csvimport->get_columns();
93 // Get a new import code for updating to the grade book.
94 if (empty($importcode)) {
95 $importcode = get_new_importcode();
98 $mappingformdata = array(
99 'gradeitems' => $gradeitems,
100 'header' => $header,
101 'iid' => $iid,
102 'id' => $id,
103 'importcode' => $importcode,
104 'forceimport' => $forceimport,
105 'verbosescales' => $verbosescales
107 // we create a form to handle mapping data from the file to the database.
108 $mform2 = new grade_import_mapping_form(null, $mappingformdata);
110 // Here, if we have data, we process the fields and enter the information into the database.
111 if ($formdata = $mform2->get_data()) {
112 $gradeimport = new gradeimport_csv_load_data();
113 $status = $gradeimport->prepare_import_grade_data($header, $formdata, $csvimport, $course->id, $separatemode,
114 $currentgroup, $verbosescales);
116 // At this stage if things are all ok, we commit the changes from temp table.
117 if ($status) {
118 grade_import_commit($course->id, $importcode);
119 } else {
120 $errors = $gradeimport->get_gradebookerrors();
121 $errors[] = get_string('importfailed', 'grades');
122 echo $renderer->errors($errors);
124 echo $OUTPUT->footer();
125 } else {
126 // If data hasn't been submitted then display the data mapping form.
127 $mform2->display();
128 echo $OUTPUT->footer();