MDL-13114 tool_uploadcourse: Optimised the import form
[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
FM
25require(__DIR__ . '/../../../config.php');
26require_once(__DIR__ . '/locallib.php');
27require_once($CFG->libdir . '/adminlib.php');
28require_once($CFG->libdir . '/clilib.php');
29require_once($CFG->libdir . '/coursecatlib.php');
30require_once($CFG->libdir . '/csvlib.class.php');
1e3e4efd
FM
31
32admin_externalpage_setup('tooluploadcourse');
3a0fda69
PH
33
34$iid = optional_param('iid', '', PARAM_INT);
35$previewrows = optional_param('previewrows', 10, PARAM_INT);
3a0fda69
PH
36
37$returnurl = new moodle_url('/admin/tool/uploadcourse/index.php');
3a0fda69
PH
38
39if (empty($iid)) {
1e3e4efd
FM
40 $mform1 = new tool_uploadcourse_step1_form();
41 if ($form1data = $mform1->get_data()) {
3a0fda69
PH
42 $iid = csv_import_reader::get_new_iid('uploadcourse');
43 $cir = new csv_import_reader($iid, 'uploadcourse');
3a0fda69 44 $content = $mform1->get_file_content('coursefile');
1e3e4efd 45 $readcount = $cir->load_csv_content($content, $form1data->encoding, $form1data->delimiter_name);
3a0fda69 46 unset($content);
3a0fda69
PH
47 if ($readcount === false) {
48 print_error('csvfileerror', 'tool_uploadcourse', $returnurl, $cir->get_error());
49 } else if ($readcount == 0) {
50 print_error('csvemptyfile', 'error', $returnurl, $cir->get_error());
51 }
3a0fda69
PH
52 } else {
53 echo $OUTPUT->header();
3a0fda69 54 echo $OUTPUT->heading_with_help(get_string('uploadcourses', 'tool_uploadcourse'), 'uploadcourses', 'tool_uploadcourse');
3a0fda69
PH
55 $mform1->display();
56 echo $OUTPUT->footer();
1e3e4efd 57 die();
3a0fda69
PH
58 }
59} else {
60 $cir = new csv_import_reader($iid, 'uploadcourse');
3a0fda69
PH
61}
62
1e3e4efd
FM
63// Data to set in the form.
64$data = array('iid' => $iid, 'previewrows' => $previewrows);
65if (!empty($form1data)) {
66 // Get options from the first form to pass it onto the second.
67 foreach ($form1data->options as $key => $value) {
68 $data["options[$key]"] = $value;
69 }
70}
71$context = context_system::instance();
72$mform2 = new tool_uploadcourse_step2_form(null, array('contextid' => $context->id, 'columns' => $cir->get_columns(),
73 'data' => $data));
3a0fda69
PH
74
75// If a file has been uploaded, then process it.
1e3e4efd 76if ($form2data = $mform2->is_cancelled()) {
3a0fda69
PH
77 $cir->cleanup(true);
78 redirect($returnurl);
1e3e4efd 79} else if ($form2data = $mform2->get_data()) {
3a0fda69 80
1e3e4efd
FM
81 $options = (array) $form2data->options;
82 $defaults = (array) $form2data->defaults;
83 $processor = new tool_uploadcourse_processor($cir, $options, $defaults);
3a0fda69 84
1e3e4efd
FM
85 echo $OUTPUT->header();
86 if (isset($form2data->showpreview)) {
87 echo $OUTPUT->heading(get_string('uploadcoursespreview', 'tool_uploadcourse'));
88 $processor->preview($previewrows, new tool_uploadcourse_tracker(tool_uploadcourse_tracker::OUTPUT_HTML));
89 $mform2->display();
3a0fda69 90 } else {
1e3e4efd
FM
91 echo $OUTPUT->heading(get_string('uploadcoursesresult', 'tool_uploadcourse'));
92 $processor->execute(new tool_uploadcourse_tracker(tool_uploadcourse_tracker::OUTPUT_HTML));
3a0fda69
PH
93 echo $OUTPUT->continue_button($returnurl);
94 }
3a0fda69 95
1e3e4efd
FM
96} else {
97 $processor = new tool_uploadcourse_processor($cir, $form1data->options, array());
98 echo $OUTPUT->header();
7a4ec85e
FM
99 echo $OUTPUT->heading(get_string('uploadcoursespreview', 'tool_uploadcourse'));
100 $processor->preview($previewrows, new tool_uploadcourse_tracker(tool_uploadcourse_tracker::OUTPUT_HTML));
1e3e4efd 101 $mform2->display();
3a0fda69 102}
3a0fda69 103
3a0fda69 104echo $OUTPUT->footer();