class_import.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. * Validate the imported data
  30. */
  31. function validate_data($classes)
  32. {
  33. $errors = array();
  34. foreach($classes as $index => $class)
  35. {
  36. //1. Check if ClassName is available
  37. if(!isset($class['ClassName']) || strlen(trim($class['ClassName'])) == 0)
  38. {
  39. $class['line'] = $index+2;
  40. $class['error'] = get_lang('MissingClassName');
  41. $errors[] = $class;
  42. }
  43. //2. Check if class doesn't exist yet
  44. else
  45. {
  46. if(ClassManager::class_name_exists($class['ClassName']))
  47. {
  48. $class['line'] = $index+2;
  49. $class['error'] = get_lang('ClassNameExists');
  50. $errors[] = $class;
  51. }
  52. }
  53. }
  54. return $errors;
  55. }
  56. /**
  57. * Save imported class data to database
  58. */
  59. function save_data($classes)
  60. {
  61. $number_of_added_classes = 0;
  62. foreach($classes as $index => $class)
  63. {
  64. if(ClassManager::create_class($class['ClassName']))
  65. {
  66. $number_of_added_classes++;
  67. }
  68. }
  69. return $number_of_added_classes;
  70. }
  71. // name of the language file that needs to be included
  72. $language_file = array ('admin', 'registration');
  73. // resetting the course id
  74. $cidReset = true;
  75. // including some necessary dokeos files
  76. include ('../inc/global.inc.php');
  77. require_once (api_get_path(LIBRARY_PATH).'fileManage.lib.php');
  78. require_once (api_get_path(LIBRARY_PATH).'classmanager.lib.php');
  79. require_once (api_get_path(LIBRARY_PATH).'import.lib.php');
  80. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  81. // setting the section (for the tabs)
  82. $this_section = SECTION_PLATFORM_ADMIN;
  83. // Access restrictions
  84. api_protect_admin_script();
  85. // setting breadcrumbs
  86. $interbreadcrumb[] = array ("url" => 'index.php', "name" => get_lang('PlatformAdmin'));
  87. // Database Table Definitions
  88. // setting the name of the tool
  89. $tool_name = get_lang('ImportClassListCSV');
  90. // Displaying the header
  91. Display :: display_header($tool_name);
  92. //api_display_tool_title($tool_name);
  93. $form = new FormValidator('import_classes');
  94. $form->addElement('file','import_file',get_lang('ImportCSVFileLocation'));
  95. $form->addElement('submit','submit',get_lang('Ok'));
  96. if( $form->validate())
  97. {
  98. $classes = Import::csv_to_array($_FILES['import_file']['tmp_name']);
  99. $errors = validate_data($classes);
  100. if (count($errors) == 0)
  101. {
  102. $number_of_added_classes = save_data($classes);
  103. Display::display_normal_message($number_of_added_classes.' '.get_lang('ClassesCreated'));
  104. }
  105. else
  106. {
  107. $error_message = get_lang('ErrorsWhenImportingFile');
  108. $error_message .= '<ul>';
  109. foreach ($errors as $index => $error_class)
  110. {
  111. $error_message .= '<li>'.$error_class['error'].' ('.get_lang('Line').' '.$error_class['line'].')';
  112. $error_message .= '</li>';
  113. }
  114. $error_message .= '</ul>';
  115. $error_message .= get_lang('NoClassesHaveBeenCreated');
  116. Display :: display_error_message($error_message);
  117. }
  118. }
  119. $form->display();
  120. ?>
  121. <p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
  122. <blockquote>
  123. <pre>
  124. <b>ClassName</b>
  125. <b>1A</b>
  126. <b>1B</b>
  127. <b>2A group 1</b>
  128. <b>2A group 2</b>
  129. </pre>
  130. </blockquote>
  131. <?php
  132. /*
  133. ==============================================================================
  134. FOOTER
  135. ==============================================================================
  136. */
  137. Display :: display_footer();
  138. ?>