2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * CLI Bulk course registration script from a comma separated file.
20 * @package tool_uploadcourse
21 * @copyright 2012 Piers Harding
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 define('CLI_SCRIPT', true);
27 require(__DIR__ . '/../../../../config.php');
28 require_once($CFG->libdir . '/clilib.php');
29 require_once($CFG->libdir . '/coursecatlib.php');
30 require_once($CFG->libdir . '/csvlib.class.php');
32 $courseconfig = get_config('moodlecourse');
34 // Now get cli options.
35 list($options, $unrecognized) = cli_get_params(array(
38 'updatemode' => 'nothing',
40 'delimiter' => 'comma',
41 'encoding' => 'UTF-8',
42 'shortnametemplate' => '',
43 'templatecourse' => false,
44 'restorefile' => false,
45 'allowdeletes' => false,
46 'allowrenames' => false,
47 'allowresets' => false,
49 'category' => coursecat::get_default()->id,
58 't' => 'templatecourse',
64 $unrecognized = implode("\n ", $unrecognized);
65 cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
69 "Execute Course Upload.
72 -h, --help Print out this help
73 -m, --mode Import mode: createnew, createall, createorupdate, update
74 -u, --updatemode Update mode: nothing, dataonly, dataordefaults¸ missingonly
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)
89 \$sudo -u www-data /usr/bin/php admin/tool/uploadcourse/cli/uploadcourse.php --mode=createnew \\
90 --updatemode=dataonly --file=./courses.csv --delimiter=comma
93 if ($options['help']) {
97 echo "Moodle course uploader running ...\n";
99 $processoroptions = array(
100 'allowdeletes' => $options['allowdeletes'],
101 'allowrenames' => $options['allowrenames'],
102 'allowresets' => $options['allowresets'],
103 'reset' => $options['reset'],
104 'shortnametemplate' => $options['shortnametemplate']
107 // Confirm that the mode is valid.
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
114 if (!isset($options['mode']) || !isset($modes[$options['mode']])) {
115 echo get_string('invalidmode', 'tool_uploadcourse')."\n";
119 $processoroptions['mode'] = $modes[$options['mode']];
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
128 if (($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";
135 $processoroptions['updatemode'] = $updatemodes[$options['updatemode']];
138 if (!empty($options['file'])) {
139 $options['file'] = realpath($options['file']);
141 if (!file_exists($options['file'])) {
142 echo get_string('invalidcsvfile', 'tool_uploadcourse')."\n";
148 $encodings = textlib::get_encodings();
149 if (!isset($encodings[$options['encoding']])) {
150 echo get_string('invalidencoding', 'tool_uploadcourse')."\n";
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;
170 if (isset($options['templatecourse'])) {
171 $processoroptions['templatecourse'] = $options['templatecourse'];
175 if ($options['restorefile']) {
176 $options['restorefile'] = realpath($options['restorefile']);
178 if ($options['restorefile'] && !file_exists($options['restorefile'])) {
179 echo get_string('invalidrestorefile', 'tool_uploadcourse')."\n";
183 $processoroptions['restorefile'] = $options['restorefile'];
185 // Emulate normal session.
188 // Let's get started!
189 $content = file_get_contents($options['file']);
190 $importid = csv_import_reader::get_new_iid('uploadcourse');
191 $cir = new csv_import_reader($importid, 'uploadcourse');
192 $readcount = $cir->load_csv_content($content, $options['encoding'], $options['delimiter']);
194 if ($readcount === false) {
195 print_error('csvfileerror', 'tool_uploadcourse', '', $cir->get_error());
196 } else if ($readcount == 0) {
197 print_error('csvemptyfile', 'error', '', $cir->get_error());
199 $processor = new tool_uploadcourse_processor($cir, $processoroptions, $defaults);
200 $processor->execute(new tool_uploadcourse_tracker(tool_uploadcourse_tracker::OUTPUT_PLAIN));