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