-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathupload.php
157 lines (128 loc) · 5.93 KB
/
upload.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Upload form for csv file to handle enrolment of bookings in bulk.
*
* @package mod_facetoface
* @author Kevin Pham <[email protected]>
* @copyright Catalyst IT, 2024
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once('../../config.php');
require_once($CFG->dirroot . '/mod/facetoface/lib.php');
use core\output\notification;
use mod_facetoface\form\upload_bookings_form;
use mod_facetoface\form\confirm_bookings_form;
use mod_facetoface\booking_manager;
$f = optional_param('f', 0, PARAM_INT); // The facetoface module ID.
$fileid = optional_param('fileid', 0, PARAM_INT); // The fileid of the file uploaded.
$validate = optional_param('validate', 0, PARAM_INT); // Whether or not the user wants to process the upload (after verification).
$process = optional_param('process', 0, PARAM_INT); // Whether or not the user wants to process the upload (after verification).
$step = optional_param('step', '', PARAM_ALPHA); // The current step in the process.
$caseinsensitive = optional_param('caseinsensitive', false, PARAM_BOOL); // If emails should match a user case insensitively.
if (!$facetoface = $DB->get_record('facetoface', ['id' => $f])) {
throw new moodle_exception('error:incorrectfacetofaceid', 'facetoface');
}
if (!$course = $DB->get_record('course', ['id' => $facetoface->course])) {
throw new moodle_exception('error:coursemisconfigured', 'facetoface');
}
if (!$cm = get_coursemodule_from_instance('facetoface', $facetoface->id, $course->id)) {
throw new moodle_exception('error:incorrectcoursemoduleid', 'facetoface');
}
require_course_login($course, true, $cm);
$context = context_course::instance($course->id);
$modulecontext = context_module::instance($cm->id);
require_capability('mod/facetoface:editsessions', $context);
require_capability('mod/facetoface:uploadbookings', $context);
// Render form, which should only consist of an upload element.
if ($validate) {
// Form submitted, but not ready for processing -> validate.
$heading = get_string('facetoface:validatebookings', 'facetoface');
$mform = new upload_bookings_form(null);
$data = $mform->get_data();
$fileid = $data->csvfile ?: 0;
$mform = new confirm_bookings_form(null, ['f' => $f, 'fileid' => $fileid, 'caseinsensitive' => $caseinsensitive]);
$bm = new booking_manager($f);
$bm->load_from_file($fileid);
$bm->set_case_insensitive($caseinsensitive);
// Validate entries.
$errors = $bm->validate();
// Set form data to allow user to continue and process the uploaded file on their next form submit.
} else if ($process && $fileid && $f) {
// Form submitted, and ready for processing -> process.
$bm = new booking_manager($f);
$bm->load_from_file($fileid);
$bm->set_case_insensitive($caseinsensitive);
// Get the options selected by the user at confirm time.
$confirmdata = (new confirm_bookings_form(null))->get_data();
if (!empty($confirmdata->suppressemail)) {
$bm->suppress_email();
}
// Validate entries.
$errors = $bm->validate();
if (empty($errors)) {
// Process entries.
$bm->process();
// Logging and events trigger.
$params = [
'context' => $modulecontext,
'objectid' => $f,
];
$event = \mod_facetoface\event\csv_processed::create($params);
$event->add_record_snapshot('facetoface', $facetoface);
$event->trigger();
// Redirect back to start with notification.
redirect(
new moodle_url('/mod/facetoface/upload.php', ['f' => $f]),
get_string('facetoface:csvprocessed', 'mod_facetoface'),
null,
notification::NOTIFY_SUCCESS);
}
$errmsg = get_string('error:bookingsuploadfileerrorsfound', 'mod_facetoface', count($errors));
redirect(
new moodle_url('/mod/facetoface/upload.php', ['f' => $f]),
$errmsg,
null,
notification::NOTIFY_ERROR);
} else {
$mform = new upload_bookings_form(null);
$mform->set_data(['f' => $f, 'validate' => 1]);
// Form not subumitted -> prep the form with current context (f2f module id).
$heading = get_string('facetoface:uploadbookings', 'facetoface');
}
$PAGE->set_url(new moodle_url('/mod/facetoface/upload.php', ['courseid' => $course->id, 'cmid' => $cm->id]));
$PAGE->set_pagelayout('standard');
$PAGE->set_title($heading);
$PAGE->set_heading($heading);
echo $OUTPUT->header();
// List out any issues in a table.
if ($validate && !empty($errors)) {
// Print summary statement.
echo \core\notification::error(get_string('error:bookingsuploadfileerrorsfound', 'mod_facetoface', count($errors)));
$table = new html_table();
$table->attributes['class'] = 'f2fbookingsuploadlist m-auto generaltable mb-2';
$table->head[] = get_string('uucsvline', 'tool_uploaduser');
$table->head[] = get_string('status');
$table->data = $errors;
echo html_writer::tag('div', html_writer::table($table), ['class' => 'flexible-wrap mb-4']);
}
if ($validate && empty($errors)) {
// Bonus: show a preview/summary for good records (e.g. 40 records will be processed).
echo \core\notification::success(get_string('facetoface:uploadreadytoprocess', 'mod_facetoface'));
}
$mform->display();
// Display footer.
echo $OUTPUT->footer();