diff --git a/admin/tool/uploadcourse/cli/uploadcourse.php b/admin/tool/uploadcourse/cli/uploadcourse.php index fd5eea3a776..bdf0819566c 100644 --- a/admin/tool/uploadcourse/cli/uploadcourse.php +++ b/admin/tool/uploadcourse/cli/uploadcourse.php @@ -15,66 +15,51 @@ // along with Moodle. If not, see . /** - * CLI Bulk course registration script from a comma separated file + * CLI Bulk course registration script from a comma separated file. * * @package tool_uploadcourse - * @subpackage uploadcourse - * @copyright 2004 onwards Martin Dougiamas (http://dougiamas.com) * @copyright 2012 Piers Harding * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * - * Notes: - * - it is required to use the web server account when executing PHP CLI scripts - * - you need to change the "www-data" to match the apache user account - * - use "su" if "sudo" not available - * */ define('CLI_SCRIPT', true); -require(dirname(dirname(dirname(dirname(dirname(__FILE__))))).'/config.php'); - -require_once($CFG->libdir.'/adminlib.php'); -require_once($CFG->libdir.'/csvlib.class.php'); -require_once($CFG->dirroot.'/course/lib.php'); -require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php'); -require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php'); -require_once($CFG->libdir . '/filelib.php'); -require_once($CFG->libdir.'/clilib.php'); -require_once('../locallib.php'); +require(__DIR__ . '/../../../../config.php'); +require_once(__DIR__ . '/../locallib.php'); +require_once($CFG->libdir . '/clilib.php'); +require_once($CFG->libdir . '/coursecatlib.php'); +require_once($CFG->libdir . '/csvlib.class.php'); $courseconfig = get_config('moodlecourse'); // Now get cli options. -list($options, $unrecognized) = cli_get_params( - array('verbose' => false, - 'help' => false, - 'action' => '', - 'mode' => 'nochange', - 'file' => '', - 'delimiter' => 'comma', - 'encoding' => 'UTF-8', - 'category' => false, - 'templateshortname' => false, - 'template' => false, - 'format' => $courseconfig->format, - 'numsections' => $courseconfig->numsections, - 'reset' => false, - ), - array('v' => 'verbose', - 'h' => 'help', - 'a' => 'action', - 'm' => 'mode', - 'f' => 'file', - 'd' => 'delimiter', - 'e' => 'encoding', - 'c' => 'category', - 's' => 'templateshortname', - 't' => 'template', - 'g' => 'format', - 'n' => 'numsections', - 'r' => 'reset', - )); +list($options, $unrecognized) = cli_get_params(array( + 'help' => false, + 'mode' => '', + 'updatemode' => 'nothing', + 'file' => '', + 'delimiter' => 'comma', + 'encoding' => 'UTF-8', + 'shortnametemplate' => '', + 'templatecourse' => false, + 'restorefile' => false, + 'allowdeletes' => false, + 'allowrenames' => false, + 'allowresets' => false, + 'reset' => false, + 'category' => coursecat::get_default()->id, +), +array( + 'h' => 'help', + 'm' => 'mode', + 'u' => 'updatemode', + 'f' => 'file', + 'd' => 'delimiter', + 'e' => 'encoding', + 't' => 'templatecourse', + 'r' => 'restorefile', + 'g' => 'format', +)); if ($unrecognized) { $unrecognized = implode("\n ", $unrecognized); @@ -85,176 +70,132 @@ $help = "Execute Course Upload. Options: --v, --verbose Print verbose progress information -h, --help Print out this help --a, --action Action to perform - addnew, addupdate, update, forceadd --m, --mode Mode of execution - delete, rename, nochange, file, filedefaults, missing --f, --file CSV File --d, --delimiter delimiter - colon,semicolon,tab,cfg,comma --e, --encoding File encoding - utf8 etc --c, --category Course category --s, --templateshortname Template course by shortname --t, --template Template course by backup file --g, --format Course format - weeks,scorm,social,topics --n, --numsections Number of sections --r, --reset Run the course reset by default after each course import +-m, --mode Import mode: createnew, createall, createorupdate, update +-u, --updatemode Update mode: nothing, dataonly, dataordefaults¸ missingonly +-f, --file CSV file +-d, --delimiter CSV delimiter: colon, semicolon, tab, cfg, comma +-e, --encoding CSV file encoding: utf8, ... etc +-t, --templatecourse Shortname of the course to restore after import +-r, --restorefile Backup file to restore after import +--reset Run the course reset after each course import +--allowdeletes Allow courses to be deleted +--allowrenames Allow courses to be renamed +--allowresets Allow courses to be reset +--shortnametemplate Template to generate the shortname from +--category ID of default category (--updatemode dataordefaults will use this value) Example: -\$sudo -u www-data /usr/bin/php admin/tool/uploadcourse/cli/uploadcourse.php --action=addupdate \\ - --mode=delete --file=./courses.csv --delimiter=comma +\$sudo -u www-data /usr/bin/php admin/tool/uploadcourse/cli/uploadcourse.php --action=createnew \\ + --updatemode=dataonly --file=./courses.csv --delimiter=comma "; if ($options['help']) { echo $help; - die; + die(); } echo "Moodle course uploader running ...\n"; -$actions = array('addnew' => CC_COURSE_ADDNEW, - 'addupdate' => CC_COURSE_ADD_UPDATE, - 'update' => CC_COURSE_UPDATE, - 'forceadd' => CC_COURSE_ADDINC); -if (!isset($options['action']) || - !isset($actions[$options['action']]) || - ($options['action'] != 'addnew' && !isset($options['mode']))) { - echo get_string('invalidinput', 'tool_uploadcourse')."\n"; - echo $help; - die; -} +$processoroptions = array( + 'allowdeletes' => $options['allowdeletes'], + 'allowrenames' => $options['allowrenames'], + 'allowresets' => $options['allowresets'], + 'reset' => $options['reset'], + 'shortnametemplate' => $options['shortnametemplate'] +); -if (!empty($options['verbose']) || $CFG->debug) { - define('CC_DEBUG', true); -} - -if (!isset($actions[$options['action']])) { - echo get_string('invalidaction', 'tool_uploadcourse')."\n"; - echo $help; - die; -} -$options['cctype'] = $actions[$options['action']]; - -$updatetype = array('nochange' => CC_UPDATE_NOCHANGES, - 'file' => CC_UPDATE_FILEOVERRIDE, - 'filedefaults' => CC_UPDATE_ALLOVERRIDE, - 'missing' => CC_UPDATE_MISSING); -if ($options['mode'] == 'rename') { - $options['ccallowrenames'] = 1; - unset($options['mode']); -} else if ($options['mode'] == 'delete') { - $options['ccallowdeletes'] = 1; - unset($options['mode']); -} else if (!isset($updatetype[$options['mode']])) { +// Confirm that the mode is valid. +$modes = array( + 'createnew' => tool_uploadcourse_processor::MODE_CREATE_NEW, + 'createall' => tool_uploadcourse_processor::MODE_CREATE_ALL, + 'createorupdate' => tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE, + 'update' => tool_uploadcourse_processor::MODE_UPDATE_ONLY +); +if (!isset($options['mode']) || !isset($modes[$options['mode']])) { echo get_string('invalidmode', 'tool_uploadcourse')."\n"; echo $help; - die; + die(); } -if (isset($options['mode'])) { - $options['ccupdatetype'] = $updatetype[$options['mode']]; -} -$options['ccstandardshortnames'] = 1; -$options['startdate'] = time() + 3600 * 24; -$options['hiddensections'] = $courseconfig->hiddensections; -$options['newsitems'] = $courseconfig->newsitems; -$options['showgrades'] = $courseconfig->showgrades; -$options['showreports'] = $courseconfig->showreports; -$options['maxbytes'] = $courseconfig->maxbytes; -$options['legacyfiles'] = 0; -$options['groupmode'] = $courseconfig->groupmode; -$options['groupmodeforce'] = $courseconfig->groupmodeforce; -$options['visible'] = $courseconfig->visible; -$options['lang'] = $courseconfig->lang; +$processoroptions['mode'] = $modes[$options['mode']]; -if ($options['category']) { - $split = preg_split('|(?get_record('course_categories', array('name'=>trim($cat), 'parent' => $options['category'])); - if (empty($category)) { - echo get_string('invalidcategory', 'tool_uploadcourse')."\n"; - echo $help; - die; - } - $options['category'] = $category->id; - } - $options['cccategory'] = $options['category']; -} else { - $categories = $DB->get_records('course_categories'); - if (empty($categories)) { - echo get_string('invalidcategory', 'tool_uploadcourse')."\n"; - echo $help; - die; - } - $category = array_shift($categories); - $options['cccategory'] = $category->id; +// Check that the update mode is valid. +$updatemodes = array( + 'nothing' => tool_uploadcourse_processor::UPDATE_NOTHING, + 'dataonly' => tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY, + 'dataordefaults' => tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_OR_DEFAUTLS, + 'missingonly' => tool_uploadcourse_processor::UPDATE_MISSING_WITH_DATA_OR_DEFAUTLS +); +if (($processoroptions['mode'] === tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE || + $processoroptions['mode'] === tool_uploadcourse_processor::MODE_UPDATE_ONLY) + && (!isset($options['updatemode']) || !isset($updatemodes[$options['updatemode']]))) { + echo get_string('invalideupdatemode', 'tool_uploadcourse')."\n"; + echo $help; + die(); } +$processoroptions['updatemode'] = $updatemodes[$options['updatemode']]; -if (isset($options['templateshortname'])) { - $options['ccshortname'] = $options['templateshortname']; +// File. +if (!empty($options['file'])) { + $options['file'] = realpath($options['file']); } - -$options['file'] = realpath($options['file']); if (!file_exists($options['file'])) { echo get_string('invalidcsvfile', 'tool_uploadcourse')."\n"; echo $help; - die; + die(); } +// Encoding. $encodings = textlib::get_encodings(); if (!isset($encodings[$options['encoding']])) { echo get_string('invalidencoding', 'tool_uploadcourse')."\n"; echo $help; - die; + die(); } +// Default values. +$defaults = array(); +$defaults['category'] = $options['category']; +$defaults['startdate'] = time() + 3600 * 24; +$defaults['newsitems'] = $courseconfig->newsitems; +$defaults['showgrades'] = $courseconfig->showgrades; +$defaults['showreports'] = $courseconfig->showreports; +$defaults['maxbytes'] = $courseconfig->maxbytes; +$defaults['legacyfiles'] = $CFG->legacyfilesinnewcourses; +$defaults['groupmode'] = $courseconfig->groupmode; +$defaults['groupmodeforce'] = $courseconfig->groupmodeforce; +$defaults['visible'] = $courseconfig->visible; +$defaults['lang'] = $courseconfig->lang; -if ($options['template']) { - $options['template'] = realpath($options['template']); +// Course template. +if (isset($options['templatecourse'])) { + $processoroptions['templatecourse'] = $options['templatecourse']; } -if ($options['template'] && !file_exists($options['template'])) { - echo get_string('invalidtemplatefile', 'tool_uploadcourse')."\n"; + +// Backup file. +if ($options['restorefile']) { + $options['restorefile'] = realpath($options['restorefile']); +} +if ($options['restorefile'] && !file_exists($options['restorefile'])) { + echo get_string('invalidrestorefile', 'tool_uploadcourse')."\n"; echo $help; - die; + die(); } -$tmpdir = $CFG->tempdir . '/backup'; -if (!check_dir_exists($tmpdir, true, true)) { - throw new restore_controller_exception('cannot_create_backup_temp_dir'); -} -$filename = restore_controller::get_tempdir_name(SITEID, $USER->id); -$restorefile = null; -if ($options['template']) { - $restorefile = $options['template']; -} - -$formdata = (object) $options; - - -$returnurl = new moodle_url('/admin/tool/uploadcourse/index.php'); -$bulknurl = new moodle_url('/admin/tool/uploadcourse/index.php'); -$std_fields = tool_uploadcourse_std_fields(); +$processoroptions['restorefile'] = $options['restorefile']; // Emulate normal session. cron_setup_user(); -$content = file_get_contents($formdata->file); +// Let's get started! +$content = file_get_contents($options['file']); $iid = csv_import_reader::get_new_iid('uploadcourse'); $cir = new csv_import_reader($iid, 'uploadcourse'); -$readcount = $cir->load_csv_content($content, $formdata->encoding, $formdata->delimiter); -$filecolumns = tool_uploadcourse_validate_course_upload_columns($cir, $std_fields, $returnurl); +$readcount = $cir->load_csv_content($content, $options['encoding'], $options['delimiter']); unset($content); if ($readcount === false) { print_error('csvfileerror', 'tool_uploadcourse', $returnurl, $cir->get_error()); } else if ($readcount == 0) { print_error('csvemptyfile', 'error', $returnurl, $cir->get_error()); } -echo "CSV read count: ".$readcount."\n"; - -$result = tool_uploadcourse_process_course_upload($formdata, $cir, $filecolumns, $restorefile, true); - -exit($result); +$processor = new tool_uploadcourse_processor($cir, $processoroptions, $defaults); +$processor->execute(new tool_uploadcourse_tracker(tool_uploadcourse_tracker::OUTPUT_PLAIN)); diff --git a/admin/tool/uploadcourse/lang/en/tool_uploadcourse.php b/admin/tool/uploadcourse/lang/en/tool_uploadcourse.php index 27326d59f68..30a68570a8a 100644 --- a/admin/tool/uploadcourse/lang/en/tool_uploadcourse.php +++ b/admin/tool/uploadcourse/lang/en/tool_uploadcourse.php @@ -56,6 +56,7 @@ $string['courseshortnameincremented'] = 'Course shortname incremented {$a->from} $string['courseshortnamegenerated'] = 'Course shortname generated: {$a}'; $string['coursetorestorefromdoesnotexist'] = 'The course to restore from does not exist'; $string['courseupdated'] = 'Course updated'; +$string['csvfileerror'] = 'There is something wrong with the format of the CSV file. Please check the number of headings and columns match, and that the delimiter and file encoding are correct: {$a}'; $string['csvline'] = 'Line'; $string['errorwhilerestoringcourse'] = 'Error while restoring the course'; $string['errorwhiledeletingcourse'] = 'Error while deleting the course'; @@ -65,6 +66,10 @@ $string['id'] = 'ID'; $string['idnumberalreadyinuse'] = 'ID number already used by a course'; $string['invalidbackupfile'] = 'Invalid backup file'; $string['invalidcourseformat'] = 'Invalid course format'; +$string['invalidcsvfile'] = 'Invalid input CSV file'; +$string['invalidencoding'] = 'Invalid encoding'; +$string['invalidmode'] = 'Invalid mode selected'; +$string['invalideupdatemode'] = 'Invalid update mode selected'; $string['invalidroles'] = 'Invalid role names: {$a}'; $string['missingmandatoryfields'] = 'Missing value for mandatory fields: {$a}'; $string['missingshortnamenotemplate'] = 'Missing shortname and shortname template not set'; @@ -73,7 +78,6 @@ $string['updatemodedoessettonothing'] = 'Update mode does not allow anything to $string['uploadcoursesresult'] = 'Upload courses results'; $string['unknownimportmode'] = 'Unknown import mode'; -$string['csvfileerror'] = 'There is something wrong with the format of the CSV file - please check the number of headings and columns match, and that the delimiter and file encoding are correct (don\t use comma-quoted as Moodle does not support it): {$a}'; $string['allowdeletes'] = 'Allow deletes'; $string['allowrenames'] = 'Allow renames'; $string['csvdelimiter'] = 'CSV delimiter'; @@ -161,11 +165,9 @@ $string['missing'] = 'missing'; $string['incorrectformat'] = 'Invalid format specified'; $string['incorrecttemplatefile'] = 'Template file not found'; $string['invalidenrolmethod'] = 'Invalid enrolment method'; -$string['invalidcsvfile'] = 'Invalid input CSV file'; $string['invalidaction'] = 'Invalid action selected'; -$string['invalidmode'] = 'Invalid mode selected'; -$string['invalidtemplatefile'] = 'Invalid template file'; -$string['invalidencoding'] = 'Invalid encoding'; + + $string['invalidcategory'] = 'Invalid category'; $string['coursetemplatename'] = 'Course template shortname'; $string['coursetemplatename_help'] = 'Select an existing course shortname to use as a template for the creation of all courses.';