bulkcreatenodes.php 7.4 KB

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