MDL-13114 tool_uploadcourse: Fixed typo in CLI help
[moodle.git] / admin / tool / uploadcourse / cli / uploadcourse.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/**
b683185b 18 * CLI Bulk course registration script from a comma separated file.
3a0fda69
PH
19 *
20 * @package tool_uploadcourse
3a0fda69
PH
21 * @copyright 2012 Piers Harding
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3a0fda69
PH
23 */
24
25define('CLI_SCRIPT', true);
26
b683185b 27require(__DIR__ . '/../../../../config.php');
b683185b
FM
28require_once($CFG->libdir . '/clilib.php');
29require_once($CFG->libdir . '/coursecatlib.php');
30require_once($CFG->libdir . '/csvlib.class.php');
3a0fda69
PH
31
32$courseconfig = get_config('moodlecourse');
33
34// Now get cli options.
b683185b
FM
35list($options, $unrecognized) = cli_get_params(array(
36 'help' => false,
37 'mode' => '',
38 'updatemode' => 'nothing',
39 'file' => '',
40 'delimiter' => 'comma',
41 'encoding' => 'UTF-8',
42 'shortnametemplate' => '',
43 'templatecourse' => false,
44 'restorefile' => false,
45 'allowdeletes' => false,
46 'allowrenames' => false,
47 'allowresets' => false,
48 'reset' => false,
49 'category' => coursecat::get_default()->id,
50),
51array(
52 'h' => 'help',
53 'm' => 'mode',
54 'u' => 'updatemode',
55 'f' => 'file',
56 'd' => 'delimiter',
57 'e' => 'encoding',
58 't' => 'templatecourse',
59 'r' => 'restorefile',
60 'g' => 'format',
61));
3a0fda69
PH
62
63if ($unrecognized) {
64 $unrecognized = implode("\n ", $unrecognized);
65 cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
66}
67
68$help =
69"Execute Course Upload.
70
71Options:
3a0fda69 72-h, --help Print out this help
b683185b
FM
73-m, --mode Import mode: createnew, createall, createorupdate, update
74-u, --updatemode Update mode: nothing, dataonly, dataordefaults¸ missingonly
75-f, --file CSV file
76-d, --delimiter CSV delimiter: colon, semicolon, tab, cfg, comma
77-e, --encoding CSV file encoding: utf8, ... etc
78-t, --templatecourse Shortname of the course to restore after import
79-r, --restorefile Backup file to restore after import
80--reset Run the course reset after each course import
81--allowdeletes Allow courses to be deleted
82--allowrenames Allow courses to be renamed
83--allowresets Allow courses to be reset
84--shortnametemplate Template to generate the shortname from
85--category ID of default category (--updatemode dataordefaults will use this value)
3a0fda69
PH
86
87
88Example:
f45d7088 89\$sudo -u www-data /usr/bin/php admin/tool/uploadcourse/cli/uploadcourse.php --mode=createnew \\
b683185b 90 --updatemode=dataonly --file=./courses.csv --delimiter=comma
3a0fda69
PH
91";
92
93if ($options['help']) {
94 echo $help;
b683185b 95 die();
3a0fda69
PH
96}
97echo "Moodle course uploader running ...\n";
98
b683185b
FM
99$processoroptions = array(
100 'allowdeletes' => $options['allowdeletes'],
101 'allowrenames' => $options['allowrenames'],
102 'allowresets' => $options['allowresets'],
103 'reset' => $options['reset'],
104 'shortnametemplate' => $options['shortnametemplate']
105);
106
107// Confirm that the mode is valid.
108$modes = array(
109 'createnew' => tool_uploadcourse_processor::MODE_CREATE_NEW,
110 'createall' => tool_uploadcourse_processor::MODE_CREATE_ALL,
111 'createorupdate' => tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE,
112 'update' => tool_uploadcourse_processor::MODE_UPDATE_ONLY
113);
114if (!isset($options['mode']) || !isset($modes[$options['mode']])) {
3a0fda69
PH
115 echo get_string('invalidmode', 'tool_uploadcourse')."\n";
116 echo $help;
b683185b
FM
117 die();
118}
119$processoroptions['mode'] = $modes[$options['mode']];
120
121// Check that the update mode is valid.
122$updatemodes = array(
123 'nothing' => tool_uploadcourse_processor::UPDATE_NOTHING,
124 'dataonly' => tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY,
125 'dataordefaults' => tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_OR_DEFAUTLS,
126 'missingonly' => tool_uploadcourse_processor::UPDATE_MISSING_WITH_DATA_OR_DEFAUTLS
127);
128if (($processoroptions['mode'] === tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE ||
129 $processoroptions['mode'] === tool_uploadcourse_processor::MODE_UPDATE_ONLY)
130 && (!isset($options['updatemode']) || !isset($updatemodes[$options['updatemode']]))) {
131 echo get_string('invalideupdatemode', 'tool_uploadcourse')."\n";
132 echo $help;
133 die();
3a0fda69 134}
b683185b 135$processoroptions['updatemode'] = $updatemodes[$options['updatemode']];
3a0fda69 136
b683185b
FM
137// File.
138if (!empty($options['file'])) {
139 $options['file'] = realpath($options['file']);
3a0fda69 140}
3a0fda69
PH
141if (!file_exists($options['file'])) {
142 echo get_string('invalidcsvfile', 'tool_uploadcourse')."\n";
143 echo $help;
b683185b 144 die();
3a0fda69
PH
145}
146
b683185b 147// Encoding.
3a0fda69
PH
148$encodings = textlib::get_encodings();
149if (!isset($encodings[$options['encoding']])) {
150 echo get_string('invalidencoding', 'tool_uploadcourse')."\n";
151 echo $help;
b683185b
FM
152 die();
153}
154
155// Default values.
156$defaults = array();
157$defaults['category'] = $options['category'];
158$defaults['startdate'] = time() + 3600 * 24;
159$defaults['newsitems'] = $courseconfig->newsitems;
160$defaults['showgrades'] = $courseconfig->showgrades;
161$defaults['showreports'] = $courseconfig->showreports;
162$defaults['maxbytes'] = $courseconfig->maxbytes;
163$defaults['legacyfiles'] = $CFG->legacyfilesinnewcourses;
164$defaults['groupmode'] = $courseconfig->groupmode;
165$defaults['groupmodeforce'] = $courseconfig->groupmodeforce;
166$defaults['visible'] = $courseconfig->visible;
167$defaults['lang'] = $courseconfig->lang;
168
169// Course template.
170if (isset($options['templatecourse'])) {
171 $processoroptions['templatecourse'] = $options['templatecourse'];
172}
173
e0164eb3 174// Restore file.
b683185b
FM
175if ($options['restorefile']) {
176 $options['restorefile'] = realpath($options['restorefile']);
177}
178if ($options['restorefile'] && !file_exists($options['restorefile'])) {
179 echo get_string('invalidrestorefile', 'tool_uploadcourse')."\n";
3a0fda69 180 echo $help;
b683185b 181 die();
3a0fda69 182}
b683185b 183$processoroptions['restorefile'] = $options['restorefile'];
3a0fda69
PH
184
185// Emulate normal session.
186cron_setup_user();
187
b683185b
FM
188// Let's get started!
189$content = file_get_contents($options['file']);
3f57c87e
FM
190$importid = csv_import_reader::get_new_iid('uploadcourse');
191$cir = new csv_import_reader($importid, 'uploadcourse');
b683185b 192$readcount = $cir->load_csv_content($content, $options['encoding'], $options['delimiter']);
3a0fda69
PH
193unset($content);
194if ($readcount === false) {
22b3be36 195 print_error('csvfileerror', 'tool_uploadcourse', '', $cir->get_error());
3a0fda69 196} else if ($readcount == 0) {
22b3be36 197 print_error('csvemptyfile', 'error', '', $cir->get_error());
3a0fda69 198}
b683185b
FM
199$processor = new tool_uploadcourse_processor($cir, $processoroptions, $defaults);
200$processor->execute(new tool_uploadcourse_tracker(tool_uploadcourse_tracker::OUTPUT_PLAIN));