class_import.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. // $Id: class_import.php 10215 2006-11-27 13:57:17Z pcool $
  3. /*
  4. ==============================================================================
  5. Dokeos - elearning and course management software
  6. Copyright (c) 2004 Dokeos S.A.
  7. Copyright (c) 2003 Ghent University (UGent)
  8. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  9. Copyright (c) Olivier Brouckaert
  10. Copyright (c) Bart Mollet, Hogeschool Gent
  11. For a full list of contributors, see "credits.txt".
  12. The full license can be read in "license.txt".
  13. This program is free software; you can redistribute it and/or
  14. modify it under the terms of the GNU General Public License
  15. as published by the Free Software Foundation; either version 2
  16. of the License, or (at your option) any later version.
  17. See the GNU General Public License for more details.
  18. Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
  19. ==============================================================================
  20. */
  21. /**
  22. ==============================================================================
  23. * This tool allows platform admins to add classes by uploading a CSV file
  24. * @todo Add some langvars to DLTT
  25. * @package dokeos.admin
  26. ==============================================================================
  27. */
  28. /**
  29. * Validates imported data.
  30. */
  31. function validate_data($classes) {
  32. $errors = array();
  33. foreach ($classes as $index => $class) {
  34. // 1. Check wheter ClassName is available.
  35. if (!isset($class['ClassName']) || strlen(trim($class['ClassName'])) == 0) {
  36. $class['line'] = $index + 2;
  37. $class['error'] = get_lang('MissingClassName');
  38. $errors[] = $class;
  39. }
  40. // 2. Check whether class doesn't exist yet.
  41. else {
  42. if (ClassManager::class_name_exists($class['ClassName'])) {
  43. $class['line'] = $index + 2;
  44. $class['error'] = get_lang('ClassNameExists').' <strong>'.$class['ClassName'].'</strong>';
  45. $errors[] = $class;
  46. }
  47. }
  48. }
  49. return $errors;
  50. }
  51. /**
  52. * Save imported class data to database
  53. */
  54. function save_data($classes) {
  55. $number_of_added_classes = 0;
  56. foreach ($classes as $index => $class) {
  57. if (ClassManager::create_class($class['ClassName'])) {
  58. $number_of_added_classes++;
  59. }
  60. }
  61. return $number_of_added_classes;
  62. }
  63. // Language files that should be included.
  64. $language_file = array ('admin', 'registration');
  65. // Resetting the course id.
  66. $cidReset = true;
  67. // Including some necessary dokeos files.
  68. include '../inc/global.inc.php';
  69. require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  70. require_once api_get_path(LIBRARY_PATH).'classmanager.lib.php';
  71. require_once api_get_path(LIBRARY_PATH).'import.lib.php';
  72. require_once api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php';
  73. // Setting the section (for the tabs).
  74. $this_section = SECTION_PLATFORM_ADMIN;
  75. // Access restrictions.
  76. api_protect_admin_script();
  77. // setting breadcrumbs
  78. $interbreadcrumb[] = array ('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  79. // Database Table Definitions
  80. // Setting the name of the tool.
  81. $tool_name = get_lang('ImportClassListCSV');
  82. // Displaying the header.
  83. Display :: display_header($tool_name);
  84. //api_display_tool_title($tool_name);
  85. set_time_limit(0);
  86. $form = new FormValidator('import_classes');
  87. $form->addElement('file', 'import_file', get_lang('ImportCSVFileLocation'));
  88. $form->addElement('style_submit_button', 'submit', get_lang('Import'), 'class="save"');
  89. if ($form->validate()) {
  90. $classes = Import::csv_to_array($_FILES['import_file']['tmp_name']);
  91. $errors = validate_data($classes);
  92. if (count($errors) == 0) {
  93. $number_of_added_classes = save_data($classes);
  94. Display::display_normal_message($number_of_added_classes.' '.get_lang('ClassesCreated'));
  95. } else {
  96. $error_message = get_lang('ErrorsWhenImportingFile');
  97. $error_message .= '<ul>';
  98. foreach ($errors as $index => $error_class) {
  99. $error_message .= '<li>'.$error_class['error'].' ('.get_lang('Line').' '.$error_class['line'].')';
  100. $error_message .= '</li>';
  101. }
  102. $error_message .= '</ul>';
  103. $error_message .= get_lang('NoClassesHaveBeenCreated');
  104. Display :: display_error_message($error_message);
  105. }
  106. }
  107. $form->display();
  108. ?>
  109. <p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
  110. <blockquote>
  111. <pre>
  112. <b>ClassName</b>
  113. <b>1A</b>
  114. <b>1B</b>
  115. <b>2A group 1</b>
  116. <b>2A group 2</b>
  117. </pre>
  118. </blockquote>
  119. <?php
  120. // Displaying the footer.
  121. Display :: display_footer();