bulkcreatenodes.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. global $debuglevel;
  3. global $debugdisplay;
  4. $debuglevel = 4;
  5. $debugdisplay = 4;
  6. define('CLI_SCRIPT', true);
  7. define('CHAMILO_INTERNAL', true);
  8. // this will only run on master chamilo
  9. echo "Starting tool\n";
  10. echo "Chamilo Bulk Nodes Creation v.1.0\n";
  11. echo "=================================\n";
  12. require_once('../../../main/inc/global.inc.php');
  13. require_once('clilib.php'); // cli only functions
  14. // Ensure errors are well explained
  15. ini_set('debug_display', 1);
  16. ini_set('debug_level', E_ALL);
  17. // now get cli options
  18. list($options, $unrecognized) = cli_get_params(
  19. array(
  20. 'interactive' => false,
  21. 'help' => false,
  22. 'config' => false,
  23. 'nodes' => '',
  24. 'lint' => false,
  25. 'verbose' => false
  26. ),
  27. array(
  28. 'h' => 'help',
  29. 'i' => 'interactive',
  30. 'c' => 'config',
  31. 'n' => 'nodes',
  32. 'l' => 'lint',
  33. 'v' => 'verbose'
  34. )
  35. );
  36. $interactive = !empty($options['interactive']);
  37. if ($unrecognized) {
  38. $unrecognized = implode("\n ", $unrecognized);
  39. cli_error("Cli unkown options\n".$unrecognized);
  40. }
  41. if ($options['help']) {
  42. $help = "Command line VChamilo Generator.
  43. Please note you must execute this script with the same uid as apache!
  44. Options:
  45. --interactive Blocks on each step and waits for input to continue
  46. -h, --help Print out this help
  47. -c, --config Define an external config file
  48. -n, --nodes A node descriptor CSV file
  49. -l, --lint Decodes node file and give a report on nodes to be created.
  50. Example:
  51. \$sudo -u www-data /usr/bin/php /var/www/chamilo/plugin/vchamilo/cli/bulkcreatenodes.php --nodes=<nodelist>
  52. "; //TODO: localize - to be translated later when everything is finished
  53. echo $help;
  54. die;
  55. }
  56. // Get all options from config file.
  57. if (!empty($options['config'])) {
  58. echo "Loading config : ".$options['config'];
  59. if (!file_exists($options['config'])){
  60. cli_error('Config file mentioned but not found');
  61. }
  62. $content = file($options['config']);
  63. foreach($content as $l) {
  64. if (preg_match('/^\s+$/', $l)) continue; // Empty lines.
  65. if (preg_match('/^[#\/!;]/', $l)) continue; // Comments (any form).
  66. if (preg_match('/^(.*?)=(.*)$/', $l, $matches)) {
  67. if (in_array($matches[1], $expectedoptions)){
  68. $options[trim($matches[1])] = trim($matches[2]);
  69. }
  70. }
  71. }
  72. }
  73. require_once($_configuration['root_sys'].'local/classes/database.class.php'); // cli only functions
  74. require_once($_configuration['root_sys'].'local/classes/textlib.class.php'); // cli only functions
  75. require_once($_configuration['root_sys'].'local/classes/mootochamlib.php'); // moodle like API
  76. require_once($_configuration['root_sys'] . '/plugin/vchamilo/lib/vchamilo_plugin.class.php');
  77. global $DB;
  78. if ($options['verbose']) echo "building database manager\n";
  79. $DB = new DatabaseManager();
  80. if ($options['verbose']) echo "building plugin vchamilo\n";
  81. $plugininstance = VChamiloPlugin::create();
  82. if (empty($options['nodes'])) {
  83. cli_error('Missing node definition. Halt.');
  84. }
  85. if ($options['verbose']) echo "parsing nodelist\n";
  86. $nodes = vchamilo_parse_csv_nodelist($options['nodes'], $plugininstance);
  87. if ($options['lint']) {
  88. ctrace("Lint mode:\n");
  89. print_object($nodes);
  90. die;
  91. }
  92. if (empty($nodes)) {
  93. cli_error('Node list empty');
  94. }
  95. ctrace('Starting generation');
  96. // Get main admin for further replacement.
  97. $admin = $DB->get_record('user', array('username' => 'admin'));
  98. foreach($nodes as $data) {
  99. ctrace('Making node '.$data->root_web);
  100. if (!empty($data->template)) {
  101. ctrace('Using template '.$data->template);
  102. if (!vchamilo_template_exists($data->template)) {
  103. ctrace('Template not found. Skipping node.');
  104. continue;
  105. }
  106. }
  107. if ($DB->get_record('vchamilo', array('root_web' => $data->root_web))) {
  108. ctrace('Node exists. skipping');
  109. continue;
  110. }
  111. $data->what = 'addinstance';
  112. $data->registeronly = false;
  113. $NDB = null;
  114. $automation = true;
  115. $return = include($_configuration['root_sys'].'plugin/vchamilo/views/editinstance.controller.php');
  116. if ($return == -1) {
  117. cli_error('Node create process error');
  118. }
  119. // This is specific code for presetting any plugin data per instance from the CSV
  120. ctrace('Setting up ent_installer');
  121. if ($NDB) {
  122. // Copy admin account info from master
  123. $NDB->set_field('user', 'password', $admin->password, array('username' => 'admin'), 'user_id');
  124. // Setting ENT_installer values
  125. if (!empty($data->ent_installer)) {
  126. foreach ($data->ent_installer as $setting => $value) {
  127. $settingrec = new StdClass();
  128. $settingrec->variable = 'ent_installer_'.$setting;
  129. $settingrec->subkey = 'ent_installer';
  130. $settingrec->type = 'setting';
  131. $settingrec->category = 'Plugins';
  132. $settingrec->access_url = 1;
  133. $settingrec->selected_value = $value;
  134. ctrace("Setting up {$settingrec->variable}|{$settingrec->subkey} to $value\n");
  135. if ($oldrec = $NDB->get_record('settings_current', array('variable' => $settingrec->variable, 'subkey' => $settingrec->subkey, 'type' => $settingrec->type))){
  136. $settingrec->id = $oldrec->id;
  137. $NDB->update_record('settings_current', $settingrec, 'id');
  138. } else {
  139. $NDB->insert_record('settings_current', $settingrec);
  140. }
  141. }
  142. }
  143. // updating other config values
  144. if (!empty($data->config)) {
  145. ctrace("VChamilo has config overrides\n");
  146. foreach ($data->config as $configkey => $configsetting) {
  147. ctrace("Setting up {$configkey}");
  148. // Note you can just alter existing settings here as we cannot pull enough data from csv headers to get a complete setting descriptor.
  149. $settingrec = new StdClass();
  150. $settingrec->variable = $configkey;
  151. if (!empty($settingrec->subkey)) {
  152. $settingrec->subkey = $configsetting->subkey;
  153. }
  154. // $settingrec->type = 'setting';
  155. // $settingrec->category = 'Plugins';
  156. // $settingrec->access_url = 1;
  157. $settingrec->selected_value = $configsetting->value;
  158. if (!empty($settingrec->subkey)) {
  159. $params = array('variable' => $settingrec->variable, 'subkey' => $settingrec->subkey);
  160. } else {
  161. $params = array('variable' => $settingrec->variable);
  162. }
  163. if ($oldrec = $NDB->get_record('settings_current', $params)) {
  164. ctrace("Updating {$settingrec->variable}|{$settingrec->subkey} to $configsetting->value\n");
  165. $settingrec->id = $oldrec->id;
  166. $NDB->update_record('settings_current', $settingrec, 'id');
  167. }
  168. }
  169. }
  170. $NDB->dismount();
  171. } else {
  172. ctrace('No Side CNX for setup');
  173. }
  174. if ($interactive) {
  175. $input = readline("Continue (y/n|r) ?\n");
  176. if ($input == 'r' || $input == 'R'){
  177. // do nothing, just continue
  178. } elseif ($input == 'n' || $input == 'N'){
  179. echo "finishing\n";
  180. exit;
  181. }
  182. }
  183. }