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 |
* Bulk course registration script from a comma separated file.
|
|
|
19 |
*
|
|
|
20 |
* @package tool_uploadcourse
|
|
|
21 |
* @copyright 2011 Piers Harding
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
require(__DIR__ . '/../../../config.php');
|
|
|
26 |
require_once($CFG->libdir . '/adminlib.php');
|
|
|
27 |
require_once($CFG->libdir . '/csvlib.class.php');
|
|
|
28 |
|
|
|
29 |
$importid = optional_param('importid', '', PARAM_INT);
|
|
|
30 |
$categoryid = optional_param('categoryid', 0, PARAM_INT);
|
|
|
31 |
$previewrows = optional_param('previewrows', 10, PARAM_INT);
|
|
|
32 |
|
|
|
33 |
$returnurl = new moodle_url('/admin/tool/uploadcourse/index.php');
|
|
|
34 |
|
|
|
35 |
if ($categoryid) {
|
|
|
36 |
// When categoryid is specified, setup the page for this category and check capability in its context.
|
|
|
37 |
require_login(null, false);
|
|
|
38 |
$category = core_course_category::get($categoryid);
|
|
|
39 |
$categoryname = isset($category) ? $category->get_formatted_name() : $SITE->fullname;
|
|
|
40 |
$context = context_coursecat::instance($categoryid);
|
|
|
41 |
require_capability('tool/uploadcourse:use', $context);
|
|
|
42 |
$PAGE->set_context($context);
|
|
|
43 |
$PAGE->set_url(new moodle_url('/admin/tool/uploadcourse/index.php', ['categoryid' => $categoryid]));
|
|
|
44 |
$PAGE->set_pagelayout('admin');
|
|
|
45 |
$PAGE->set_title("$categoryname: " . get_string('uploadcourses', 'tool_uploadcourse'));
|
|
|
46 |
$PAGE->set_heading($categoryname);
|
|
|
47 |
} else {
|
|
|
48 |
admin_externalpage_setup('tooluploadcourse');
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
if (empty($importid)) {
|
|
|
52 |
$mform1 = new tool_uploadcourse_step1_form();
|
|
|
53 |
$mform1->set_data(['categoryid' => $categoryid]);
|
|
|
54 |
if ($form1data = $mform1->get_data()) {
|
|
|
55 |
$importid = csv_import_reader::get_new_iid('uploadcourse');
|
|
|
56 |
$cir = new csv_import_reader($importid, 'uploadcourse');
|
|
|
57 |
$content = $mform1->get_file_content('coursefile');
|
|
|
58 |
$readcount = $cir->load_csv_content($content, $form1data->encoding, $form1data->delimiter_name);
|
|
|
59 |
unset($content);
|
|
|
60 |
if ($readcount === false) {
|
|
|
61 |
throw new \moodle_exception('csvfileerror', 'tool_uploadcourse', $returnurl, $cir->get_error());
|
|
|
62 |
} else if ($readcount == 0) {
|
|
|
63 |
throw new \moodle_exception('csvemptyfile', 'error', $returnurl, $cir->get_error());
|
|
|
64 |
}
|
|
|
65 |
} else {
|
|
|
66 |
echo $OUTPUT->header();
|
|
|
67 |
echo $OUTPUT->heading_with_help(get_string('uploadcourses', 'tool_uploadcourse'), 'uploadcourses', 'tool_uploadcourse');
|
|
|
68 |
$mform1->display();
|
|
|
69 |
echo $OUTPUT->footer();
|
|
|
70 |
die();
|
|
|
71 |
}
|
|
|
72 |
} else {
|
|
|
73 |
$cir = new csv_import_reader($importid, 'uploadcourse');
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
// Data to set in the form.
|
|
|
77 |
$categorydefaults = $categoryid ? ['category' => $categoryid] : [];
|
|
|
78 |
$data = ['importid' => $importid, 'previewrows' => $previewrows, 'categoryid' => $categoryid, 'defaults' => $categorydefaults];
|
|
|
79 |
if (!empty($form1data)) {
|
|
|
80 |
// Get options from the first form to pass it onto the second.
|
|
|
81 |
foreach ($form1data->options as $key => $value) {
|
|
|
82 |
$data["options[$key]"] = $value;
|
|
|
83 |
}
|
|
|
84 |
}
|
|
|
85 |
$context = context_system::instance();
|
|
|
86 |
$mform2 = new tool_uploadcourse_step2_form(null, array('contextid' => $context->id, 'columns' => $cir->get_columns(),
|
|
|
87 |
'data' => $data));
|
|
|
88 |
|
|
|
89 |
// If a file has been uploaded, then process it.
|
|
|
90 |
if ($form2data = $mform2->is_cancelled()) {
|
|
|
91 |
$cir->cleanup(true);
|
|
|
92 |
redirect($returnurl);
|
|
|
93 |
} else if ($form2data = $mform2->get_data()) {
|
|
|
94 |
|
|
|
95 |
$options = (array) $form2data->options;
|
|
|
96 |
$defaults = (array) $form2data->defaults;
|
|
|
97 |
|
|
|
98 |
// Custom field defaults.
|
|
|
99 |
$customfields = tool_uploadcourse_helper::get_custom_course_field_names();
|
|
|
100 |
foreach ($customfields as $customfield) {
|
|
|
101 |
$defaults[$customfield] = $form2data->{$customfield};
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
// Restorefile deserves its own logic because formslib does not really appreciate
|
|
|
105 |
// when the name of a filepicker is an array...
|
|
|
106 |
$options['restorefile'] = '';
|
|
|
107 |
if (!empty($form2data->restorefile)) {
|
|
|
108 |
$options['restorefile'] = $mform2->save_temp_file('restorefile');
|
|
|
109 |
}
|
|
|
110 |
$processor = new tool_uploadcourse_processor($cir, $options, $defaults);
|
|
|
111 |
|
|
|
112 |
echo $OUTPUT->header();
|
|
|
113 |
if (isset($form2data->showpreview)) {
|
|
|
114 |
echo $OUTPUT->heading(get_string('uploadcoursespreview', 'tool_uploadcourse'));
|
|
|
115 |
$processor->preview($previewrows, new tool_uploadcourse_tracker(tool_uploadcourse_tracker::OUTPUT_HTML));
|
|
|
116 |
$mform2->display();
|
|
|
117 |
} else {
|
|
|
118 |
echo $OUTPUT->heading(get_string('uploadcoursesresult', 'tool_uploadcourse'));
|
|
|
119 |
$processor->execute(new tool_uploadcourse_tracker(tool_uploadcourse_tracker::OUTPUT_HTML));
|
|
|
120 |
echo $OUTPUT->continue_button($returnurl);
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
// Deleting the file after processing or preview.
|
|
|
124 |
if (!empty($options['restorefile'])) {
|
|
|
125 |
@unlink($options['restorefile']);
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
} else {
|
|
|
129 |
if (!empty($form1data)) {
|
|
|
130 |
$options = $form1data->options;
|
|
|
131 |
} else if ($submitteddata = $mform2->get_submitted_data()) {
|
|
|
132 |
$options = (array)$submitteddata->options;
|
|
|
133 |
} else {
|
|
|
134 |
// Weird but we still need to provide a value, setting the default step1_form one.
|
|
|
135 |
$options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_NEW);
|
|
|
136 |
}
|
|
|
137 |
$processor = new tool_uploadcourse_processor($cir, $options, $categorydefaults);
|
|
|
138 |
echo $OUTPUT->header();
|
|
|
139 |
echo $OUTPUT->heading(get_string('uploadcoursespreview', 'tool_uploadcourse'));
|
|
|
140 |
$processor->preview($previewrows, new tool_uploadcourse_tracker(tool_uploadcourse_tracker::OUTPUT_HTML));
|
|
|
141 |
$mform2->display();
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
echo $OUTPUT->footer();
|