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