[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 * CLI Bulk course registration script from a comma separated file. 19 * 20 * @package tool_uploadcourse 21 * @copyright 2012 Piers Harding 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 define('CLI_SCRIPT', true); 26 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'); 31 32 $courseconfig = get_config('moodlecourse'); 33 34 // Now get cli options. 35 list($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 ), 51 array( 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 )); 62 63 if ($unrecognized) { 64 $unrecognized = implode("\n ", $unrecognized); 65 cli_error(get_string('cliunknowoption', 'admin', $unrecognized)); 66 } 67 68 $help = 69 "Execute Course Upload. 70 71 Options: 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 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) 86 87 88 Example: 89 \$sudo -u www-data /usr/bin/php admin/tool/uploadcourse/cli/uploadcourse.php --mode=createnew \\ 90 --updatemode=dataonly --file=./courses.csv --delimiter=comma 91 "; 92 93 if ($options['help']) { 94 echo $help; 95 die(); 96 } 97 echo "Moodle course uploader running ...\n"; 98 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 ); 114 if (!isset($options['mode']) || !isset($modes[$options['mode']])) { 115 echo get_string('invalidmode', 'tool_uploadcourse')."\n"; 116 echo $help; 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 ); 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"; 132 echo $help; 133 die(); 134 } 135 $processoroptions['updatemode'] = $updatemodes[$options['updatemode']]; 136 137 // File. 138 if (!empty($options['file'])) { 139 $options['file'] = realpath($options['file']); 140 } 141 if (!file_exists($options['file'])) { 142 echo get_string('invalidcsvfile', 'tool_uploadcourse')."\n"; 143 echo $help; 144 die(); 145 } 146 147 // Encoding. 148 $encodings = core_text::get_encodings(); 149 if (!isset($encodings[$options['encoding']])) { 150 echo get_string('invalidencoding', 'tool_uploadcourse')."\n"; 151 echo $help; 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. 170 if (isset($options['templatecourse'])) { 171 $processoroptions['templatecourse'] = $options['templatecourse']; 172 } 173 174 // Restore file. 175 if ($options['restorefile']) { 176 $options['restorefile'] = realpath($options['restorefile']); 177 } 178 if ($options['restorefile'] && !file_exists($options['restorefile'])) { 179 echo get_string('invalidrestorefile', 'tool_uploadcourse')."\n"; 180 echo $help; 181 die(); 182 } 183 $processoroptions['restorefile'] = $options['restorefile']; 184 185 // Emulate normal session. 186 cron_setup_user(); 187 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']); 193 unset($content); 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()); 198 } 199 $processor = new tool_uploadcourse_processor($cir, $processoroptions, $defaults); 200 $processor->execute(new tool_uploadcourse_tracker(tool_uploadcourse_tracker::OUTPUT_PLAIN));
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |