source: moodle/trunk/fuentes/admin/tool/uploadcourse/index.php @ 136

Last change on this file since 136 was 136, checked in by mabarracus, 4 years ago

Ported code to xenial

File size: 4.5 KB
Line 
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
25require(__DIR__ . '/../../../config.php');
26require_once($CFG->libdir . '/adminlib.php');
27require_once($CFG->libdir . '/coursecatlib.php');
28require_once($CFG->libdir . '/csvlib.class.php');
29
30admin_externalpage_setup('tooluploadcourse');
31
32$importid         = optional_param('importid', '', PARAM_INT);
33$previewrows = optional_param('previewrows', 10, PARAM_INT);
34
35$returnurl = new moodle_url('/admin/tool/uploadcourse/index.php');
36
37if (empty($importid)) {
38    $mform1 = new tool_uploadcourse_step1_form();
39    if ($form1data = $mform1->get_data()) {
40        $importid = csv_import_reader::get_new_iid('uploadcourse');
41        $cir = new csv_import_reader($importid, 'uploadcourse');
42        $content = $mform1->get_file_content('coursefile');
43        $readcount = $cir->load_csv_content($content, $form1data->encoding, $form1data->delimiter_name);
44        unset($content);
45        if ($readcount === false) {
46            print_error('csvfileerror', 'tool_uploadcourse', $returnurl, $cir->get_error());
47        } else if ($readcount == 0) {
48            print_error('csvemptyfile', 'error', $returnurl, $cir->get_error());
49        }
50    } else {
51        echo $OUTPUT->header();
52        echo $OUTPUT->heading_with_help(get_string('uploadcourses', 'tool_uploadcourse'), 'uploadcourses', 'tool_uploadcourse');
53        $mform1->display();
54        echo $OUTPUT->footer();
55        die();
56    }
57} else {
58    $cir = new csv_import_reader($importid, 'uploadcourse');
59}
60
61// Data to set in the form.
62$data = array('importid' => $importid, 'previewrows' => $previewrows);
63if (!empty($form1data)) {
64    // Get options from the first form to pass it onto the second.
65    foreach ($form1data->options as $key => $value) {
66        $data["options[$key]"] = $value;
67    }
68}
69$context = context_system::instance();
70$mform2 = new tool_uploadcourse_step2_form(null, array('contextid' => $context->id, 'columns' => $cir->get_columns(),
71    'data' => $data));
72
73// If a file has been uploaded, then process it.
74if ($form2data = $mform2->is_cancelled()) {
75    $cir->cleanup(true);
76    redirect($returnurl);
77} else if ($form2data = $mform2->get_data()) {
78
79    $options = (array) $form2data->options;
80    $defaults = (array) $form2data->defaults;
81
82    // Restorefile deserves its own logic because formslib does not really appreciate
83    // when the name of a filepicker is an array...
84    $options['restorefile'] = '';
85    if (!empty($form2data->restorefile)) {
86        $options['restorefile'] = $mform2->save_temp_file('restorefile');
87    }
88    $processor = new tool_uploadcourse_processor($cir, $options, $defaults);
89
90    echo $OUTPUT->header();
91    if (isset($form2data->showpreview)) {
92        echo $OUTPUT->heading(get_string('uploadcoursespreview', 'tool_uploadcourse'));
93        $processor->preview($previewrows, new tool_uploadcourse_tracker(tool_uploadcourse_tracker::OUTPUT_HTML));
94        $mform2->display();
95    } else {
96        echo $OUTPUT->heading(get_string('uploadcoursesresult', 'tool_uploadcourse'));
97        $processor->execute(new tool_uploadcourse_tracker(tool_uploadcourse_tracker::OUTPUT_HTML));
98        echo $OUTPUT->continue_button($returnurl);
99    }
100
101    // Deleting the file after processing or preview.
102    if (!empty($options['restorefile'])) {
103        @unlink($options['restorefile']);
104    }
105
106} else {
107    $processor = new tool_uploadcourse_processor($cir, $form1data->options, array());
108    echo $OUTPUT->header();
109    echo $OUTPUT->heading(get_string('uploadcoursespreview', 'tool_uploadcourse'));
110    $processor->preview($previewrows, new tool_uploadcourse_tracker(tool_uploadcourse_tracker::OUTPUT_HTML));
111    $mform2->display();
112}
113
114echo $OUTPUT->footer();
Note: See TracBrowser for help on using the repository browser.