class_import.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. // $Id: class_import.php 9246 2006-09-25 13:24:53Z bmol $
  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. $langFile = array ('admin', 'registration');
  72. $cidReset = true;
  73. include ('../inc/global.inc.php');
  74. api_protect_admin_script();
  75. require_once (api_get_path(LIBRARY_PATH).'fileManage.lib.php');
  76. require_once (api_get_path(LIBRARY_PATH).'classmanager.lib.php');
  77. require_once (api_get_path(LIBRARY_PATH).'import.lib.php');
  78. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  79. $tool_name = get_lang('ImportClassListCSV');
  80. //$interbreadcrumb[] = array ("url" => "index.php", "name" => get_lang('PlatformAdmin'));
  81. Display :: display_header($tool_name);
  82. //api_display_tool_title($tool_name);
  83. $form = new FormValidator('import_classes');
  84. $form->addElement('file','import_file',get_lang('ImportCSVFileLocation'));
  85. $form->addElement('submit','submit',get_lang('Ok'));
  86. if( $form->validate())
  87. {
  88. $classes = Import::csv_to_array($_FILES['import_file']['tmp_name']);
  89. $errors = validate_data($classes);
  90. if (count($errors) == 0)
  91. {
  92. $number_of_added_classes = save_data($classes);
  93. Display::display_normal_message($number_of_added_classes.' '.get_lang('ClassesCreated'));
  94. }
  95. else
  96. {
  97. $error_message = get_lang('ErrorsWhenImportingFile');
  98. $error_message .= '<ul>';
  99. foreach ($errors as $index => $error_class)
  100. {
  101. $error_message .= '<li>'.$error_class['error'].' ('.get_lang('Line').' '.$error_class['line'].')';
  102. $error_message .= '</li>';
  103. }
  104. $error_message .= '</ul>';
  105. $error_message .= get_lang('NoClassesHaveBeenCreated');
  106. Display :: display_error_message($error_message);
  107. }
  108. }
  109. $form->display();
  110. ?>
  111. <p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
  112. <blockquote>
  113. <pre>
  114. <b>ClassName</b>
  115. <b>1A</b>
  116. <b>1B</b>
  117. <b>2A group 1</b>
  118. <b>2A group 2</b>
  119. </pre>
  120. </blockquote>
  121. <?php
  122. /*
  123. ==============================================================================
  124. FOOTER
  125. ==============================================================================
  126. */
  127. Display :: display_footer();
  128. ?>