class_import.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. foreach ($classes as $index => $class) {
  17. // 1. Check wheter ClassName is available.
  18. if (!isset($class['ClassName']) || strlen(trim($class['ClassName'])) == 0) {
  19. $class['line'] = $index + 2;
  20. $class['error'] = get_lang('MissingClassName');
  21. $errors[] = $class;
  22. }
  23. // 2. Check whether class doesn't exist yet.
  24. else {
  25. if (ClassManager::class_name_exists($class['ClassName'])) {
  26. $class['line'] = $index + 2;
  27. $class['error'] = get_lang('ClassNameExists').' <strong>'.$class['ClassName'].'</strong>';
  28. $errors[] = $class;
  29. }
  30. }
  31. }
  32. return $errors;
  33. }
  34. /**
  35. * Save imported class data to database
  36. */
  37. function save_data($classes) {
  38. $number_of_added_classes = 0;
  39. foreach ($classes as $index => $class) {
  40. if (ClassManager::create_class($class['ClassName'])) {
  41. $number_of_added_classes++;
  42. }
  43. }
  44. return $number_of_added_classes;
  45. }
  46. // Language files that should be included.
  47. $language_file = array ('admin', 'registration');
  48. // Resetting the course id.
  49. $cidReset = true;
  50. // Including some necessary dokeos files.
  51. include '../inc/global.inc.php';
  52. require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  53. require_once api_get_path(LIBRARY_PATH).'classmanager.lib.php';
  54. require_once api_get_path(LIBRARY_PATH).'import.lib.php';
  55. // Setting the section (for the tabs).
  56. $this_section = SECTION_PLATFORM_ADMIN;
  57. // Access restrictions.
  58. api_protect_admin_script();
  59. // setting breadcrumbs
  60. $interbreadcrumb[] = array ('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  61. $interbreadcrumb[] = array ('url' => 'class_list.php', 'name' => get_lang('Classes'));
  62. // Database Table Definitions
  63. // Setting the name of the tool.
  64. $tool_name = get_lang('ImportClassListCSV');
  65. // Displaying the header.
  66. Display :: display_header($tool_name);
  67. //api_display_tool_title($tool_name);
  68. set_time_limit(0);
  69. $form = new FormValidator('import_classes');
  70. $form->addElement('file', 'import_file', get_lang('ImportCSVFileLocation'));
  71. $form->addElement('style_submit_button', 'submit', get_lang('Import'), 'class="save"');
  72. if ($form->validate()) {
  73. $classes = Import::csv_to_array($_FILES['import_file']['tmp_name']);
  74. $errors = validate_data($classes);
  75. if (count($errors) == 0) {
  76. $number_of_added_classes = save_data($classes);
  77. Display::display_normal_message($number_of_added_classes.' '.get_lang('ClassesCreated'));
  78. } else {
  79. $error_message = get_lang('ErrorsWhenImportingFile');
  80. $error_message .= '<ul>';
  81. foreach ($errors as $index => $error_class) {
  82. $error_message .= '<li>'.$error_class['error'].' ('.get_lang('Line').' '.$error_class['line'].')';
  83. $error_message .= '</li>';
  84. }
  85. $error_message .= '</ul>';
  86. $error_message .= get_lang('NoClassesHaveBeenCreated');
  87. Display :: display_error_message($error_message);
  88. }
  89. }
  90. $form->display();
  91. ?>
  92. <p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
  93. <pre>
  94. <b>ClassName</b>
  95. <b>1A</b>
  96. <b>1B</b>
  97. <b>2A group 1</b>
  98. <b>2A group 2</b>
  99. </pre>
  100. <?php
  101. // Displaying the footer.
  102. Display :: display_footer();