Chamilo is a learning management system focused on ease of use and accessibility
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
chamilo-lms/public/plugin/vchamilo/cli/bulkcreatenodes.php

230 lines
7.3 KiB

<?php
exit;
global $debuglevel;
global $debugdisplay;
$debuglevel = 4;
$debugdisplay = 4;
define('CLI_SCRIPT', true);
define('CHAMILO_INTERNAL', true);
// this will only run on master chamilo
echo "Starting tool\n";
echo "Chamilo Bulk Nodes Creation v.1.0\n";
echo "=================================\n";
require_once '../../../main/inc/global.inc.php';
require_once 'clilib.php'; // cli only functions
// Ensure errors are well explained
ini_set('debug_display', 1);
ini_set('debug_level', E_ALL);
// now get cli options
list($options, $unrecognized) = cli_get_params(
[
'interactive' => false,
'help' => false,
'config' => false,
'nodes' => '',
'lint' => false,
'verbose' => false,
],
[
'h' => 'help',
'i' => 'interactive',
'c' => 'config',
'n' => 'nodes',
'l' => 'lint',
'v' => 'verbose',
]
);
$interactive = !empty($options['interactive']);
if ($unrecognized) {
$unrecognized = implode("\n ", $unrecognized);
cli_error("Cli unkown options\n".$unrecognized);
}
if ($options['help']) {
$help = 'Command line VChamilo Generator.
Please note you must execute this script with the same uid as apache!
Options:
--interactive Blocks on each step and waits for input to continue
-h, --help Print out this help
-c, --config Define an external config file
-n, --nodes A node descriptor CSV file
-l, --lint Decodes node file and give a report on nodes to be created.
Example:
$sudo -u www-data /usr/bin/php /var/www/chamilo/plugin/vchamilo/cli/bulkcreatenodes.php --nodes=<nodelist>
'; //TODO: localize - to be translated later when everything is finished
echo $help;
die;
}
// Get all options from config file.
if (!empty($options['config'])) {
echo 'Loading config : '.$options['config'];
if (!file_exists($options['config'])) {
cli_error('Config file mentioned but not found');
}
$content = file($options['config']);
foreach ($content as $l) {
if (preg_match('/^\s+$/', $l)) {
continue;
} // Empty lines.
if (preg_match('/^[#\/!;]/', $l)) {
continue;
} // Comments (any form).
if (preg_match('/^(.*?)=(.*)$/', $l, $matches)) {
if (in_array($matches[1], $expectedoptions)) {
$options[trim($matches[1])] = trim($matches[2]);
}
}
}
}
require_once $_configuration['root_sys'].'local/classes/database.class.php'; // cli only functions
require_once $_configuration['root_sys'].'local/classes/textlib.class.php'; // cli only functions
require_once $_configuration['root_sys'].'local/classes/mootochamlib.php'; // moodle like API
require_once $_configuration['root_sys'].'/plugin/vchamilo/lib/vchamilo_plugin.class.php';
global $DB;
if ($options['verbose']) {
echo "building database manager\n";
}
$DB = new DatabaseManager();
if ($options['verbose']) {
echo "building plugin vchamilo\n";
}
$plugin = VChamiloPlugin::create();
if (empty($options['nodes'])) {
cli_error('Missing node definition. Halt.');
}
if ($options['verbose']) {
echo "parsing nodelist\n";
}
$nodes = vchamilo_parse_csv_nodelist($options['nodes'], $plugin);
if ($options['lint']) {
ctrace("Lint mode:\n");
print_object($nodes);
die;
}
if (empty($nodes)) {
cli_error('Node list empty');
}
ctrace('Starting generation');
// Get main admin for further replacement.
$admin = $DB->get_record('user', ['username' => 'admin']);
foreach ($nodes as $data) {
ctrace('Making node '.$data->root_web);
if (!empty($data->template)) {
ctrace('Using template '.$data->template);
if (!vchamilo_template_exists($data->template)) {
ctrace('Template not found. Skipping node.');
continue;
}
}
if ($DB->get_record('vchamilo', ['root_web' => $data->root_web])) {
ctrace('Node exists. skipping');
continue;
}
$data->what = 'addinstance';
$data->registeronly = false;
$NDB = null;
$automation = true;
$return = include $_configuration['root_sys'].'plugin/vchamilo/views/editinstance.controller.php';
if (-1 == $return) {
cli_error('Node create process error');
}
// This is specific code for presetting any plugin data per instance from the CSV
ctrace('Setting up ent_installer');
if ($NDB) {
// Copy admin account info from master
$NDB->set_field('user', 'password', $admin->password, ['username' => 'admin'], 'user_id');
// Setting ENT_installer values
if (!empty($data->ent_installer)) {
foreach ($data->ent_installer as $setting => $value) {
$settingrec = new StdClass();
$settingrec->variable = 'ent_installer_'.$setting;
$settingrec->subkey = 'ent_installer';
$settingrec->type = 'setting';
$settingrec->category = 'Plugins';
$settingrec->access_url = 1;
$settingrec->selected_value = $value;
ctrace("Setting up {$settingrec->variable}|{$settingrec->subkey} to $value\n");
if ($oldrec = $NDB->get_record('settings_current', ['variable' => $settingrec->variable, 'subkey' => $settingrec->subkey, 'type' => $settingrec->type])) {
$settingrec->id = $oldrec->id;
$NDB->update_record('settings_current', $settingrec, 'id');
} else {
$NDB->insert_record('settings_current', $settingrec);
}
}
}
// updating other config values
if (!empty($data->config)) {
ctrace("VChamilo has config overrides\n");
foreach ($data->config as $configkey => $configsetting) {
ctrace("Setting up {$configkey}");
// Note you can just alter existing settings here as we cannot pull enough data from csv headers to get a complete setting descriptor.
$settingrec = new StdClass();
$settingrec->variable = $configkey;
if (!empty($settingrec->subkey)) {
$settingrec->subkey = $configsetting->subkey;
}
// $settingrec->type = 'setting';
// $settingrec->category = 'Plugins';
// $settingrec->access_url = 1;
$settingrec->selected_value = $configsetting->value;
if (!empty($settingrec->subkey)) {
$params = ['variable' => $settingrec->variable, 'subkey' => $settingrec->subkey];
} else {
$params = ['variable' => $settingrec->variable];
}
if ($oldrec = $NDB->get_record('settings_current', $params)) {
ctrace("Updating {$settingrec->variable}|{$settingrec->subkey} to $configsetting->value\n");
$settingrec->id = $oldrec->id;
$NDB->update_record('settings_current', $settingrec, 'id');
}
}
}
$NDB->dismount();
} else {
ctrace('No Side CNX for setup');
}
if ($interactive) {
$input = readline("Continue (y/n|r) ?\n");
if ('r' == $input || 'R' == $input) {
// do nothing, just continue
} elseif ('n' == $input || 'N' == $input) {
echo "finishing\n";
exit;
}
}
}