usergroup_import.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. /**
  7. * Code
  8. * This tool allows platform admins to add classes by uploading a CSV file
  9. * @todo Add some langvars to DLTT
  10. */
  11. /**
  12. * Validates imported data.
  13. */
  14. function validate_data($classes) {
  15. $errors = array();
  16. $usergroup = new UserGroup();
  17. foreach ($classes as $index => $class) {
  18. // 1. Check wheter ClassName is available.
  19. if (!isset($class['name']) || strlen(trim($class['name'])) == 0) {
  20. $class['line'] = $index + 2;
  21. $class['error'] = get_lang('MissingClassName');
  22. $errors[] = $class;
  23. }
  24. // 2. Check whether class doesn't exist yet.
  25. else {
  26. if ($usergroup->usergroup_exists($class['name'])) {
  27. $class['line'] = $index + 2;
  28. $class['error'] = get_lang('ClassNameExists') . ' <strong>' . $class['ClassName'] . '</strong>';
  29. $errors[] = $class;
  30. }
  31. }
  32. }
  33. return $errors;
  34. }
  35. /**
  36. * Save imported class data to database
  37. */
  38. function save_data($classes) {
  39. $number_of_added_classes = 0;
  40. $usergroup = new UserGroup();
  41. foreach ($classes as $index => $class) {
  42. $id = $usergroup->save($class);
  43. if ($id) {
  44. $number_of_added_classes++;
  45. }
  46. }
  47. return $number_of_added_classes;
  48. }
  49. // Language files that should be included.
  50. $language_file = array('admin', 'registration');
  51. // Resetting the course id.
  52. $cidReset = true;
  53. // Including some necessary dokeos files.
  54. include '../inc/global.inc.php';
  55. require_once api_get_path(LIBRARY_PATH) . 'fileManage.lib.php';
  56. require_once api_get_path(LIBRARY_PATH) . 'import.lib.php';
  57. // Setting the section (for the tabs).
  58. $this_section = SECTION_PLATFORM_ADMIN;
  59. // Access restrictions.
  60. api_protect_admin_script();
  61. // setting breadcrumbs
  62. $interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  63. $interbreadcrumb[] = array('url' => 'usergroups.php', 'name' => get_lang('Classes'));
  64. // Database Table Definitions
  65. // Setting the name of the tool.
  66. $tool_name = get_lang('ImportClassListCSV');
  67. // Displaying the header.
  68. Display :: display_header($tool_name);
  69. set_time_limit(0);
  70. $form = new FormValidator('import_classes');
  71. $form->addElement('file', 'import_file', get_lang('ImportCSVFileLocation'));
  72. $group = array();
  73. $group[] = $form->createElement('radio', 'file_type', '', 'CSV (<a href="example_class.csv" target="_blank">' . get_lang('ExampleCSVFile') . '</a>)', 'csv');
  74. $form->addGroup($group, '', get_lang('FileType'), '<br/>');
  75. $form->addElement('style_submit_button', 'submit', get_lang('Import'), 'class="save"');
  76. if ($form->validate()) {
  77. $classes = Import::csv_to_array($_FILES['import_file']['tmp_name']);
  78. $errors = validate_data($classes);
  79. if (count($errors) == 0) {
  80. $number_of_added_classes = save_data($classes);
  81. Display::display_normal_message($number_of_added_classes . ' ' . get_lang('Added'));
  82. } else {
  83. $error_message = get_lang('ErrorsWhenImportingFile');
  84. $error_message .= '<ul>';
  85. foreach ($errors as $index => $error_class) {
  86. $error_message .= '<li>' . $error_class['error'] . ' (' . get_lang('Line') . ' ' . $error_class['line'] . ')';
  87. $error_message .= '</li>';
  88. }
  89. $error_message .= '</ul>';
  90. $error_message .= get_lang('Error');
  91. Display :: display_error_message($error_message, false);
  92. }
  93. }
  94. $form->display();
  95. ?>
  96. <p><?php echo get_lang('CSVMustLookLike') . ' (' . get_lang('MandatoryFields') . ')'; ?> :</p>
  97. <pre>
  98. <b>name;description</b>
  99. "User group 1";"Description"
  100. </pre>
  101. <?php
  102. // Displaying the footer.
  103. Display :: display_footer();