MDL-13114 tool_uploadcourse: CLI uses new classes

This commit is contained in:
Frederic Massart
2013-07-15 10:02:55 +08:00
parent 5dcd7e2867
commit b683185bab
2 changed files with 123 additions and 180 deletions
+116 -175
View File
@@ -15,66 +15,51 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* 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('|(?<!\\\)/|', $options['category']);
$categories = array();
foreach ($split as $cat) {
$cat = preg_replace('/\\\/', '', $cat);
$categories[]= $cat;
}
$options['category'] = 0;
foreach ($categories as $cat) {
// Does the category exist - does the category hierachy make sense.
$category = $DB->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));