course_import.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This tool allows platform admins to create courses by uploading a CSV file
  5. * Copyright (c) 2005 Bart Mollet <bart.mollet@hogent.be>
  6. * @package chamilo.admin
  7. */
  8. /**
  9. * Validates imported data.
  10. */
  11. function validate_data($courses)
  12. {
  13. $errors = array ();
  14. $coursecodes = array ();
  15. foreach ($courses as $index => $course) {
  16. $course['line'] = $index +1;
  17. // 1. Check whether mandatory fields are set.
  18. $mandatory_fields = array ('Code', 'Title', 'CourseCategory');
  19. foreach ($mandatory_fields as $field) {
  20. if (!isset($course[$field]) || strlen($course[$field]) == 0) {
  21. $course['error'] = get_lang($field.'Mandatory');
  22. $errors[] = $course;
  23. }
  24. }
  25. // 2. Check current course code.
  26. if (isset ($course['Code']) && strlen($course['Code']) != 0) {
  27. // 2.1 Check whether code has been already used by this CVS-file.
  28. if (isset($coursecodes[$course['Code']])) {
  29. $course['error'] = get_lang('CodeTwiceInFile');
  30. $errors[] = $course;
  31. } else {
  32. // 2.2 Check whether course code has been occupied.
  33. $courseInfo = api_get_course_info($course['Code']);
  34. if (!empty($courseInfo)) {
  35. $course['error'] = get_lang('CodeExists');
  36. $errors[] = $course;
  37. }
  38. }
  39. $coursecodes[$course['Code']] = 1;
  40. }
  41. // 3. Check whether teacher exists.
  42. $teacherList = getTeacherListInArray($course['Teacher']);
  43. if (!empty($teacherList)) {
  44. foreach ($teacherList as $teacher) {
  45. $teacherInfo = api_get_user_info_from_username($teacher);
  46. if (empty($teacherInfo)) {
  47. $course['error'] = get_lang('UnknownTeacher').' ('.$teacher.')';
  48. $errors[] = $course;
  49. } else {
  50. if ($teacherInfo['status'] != COURSEMANAGER) {
  51. $course['error'] = get_lang('UserIsNotATeacher').' ('.$teacher.')';
  52. $errors[] = $course;
  53. }
  54. }
  55. }
  56. }
  57. // 4. Check whether course category exists.
  58. if (isset($course['CourseCategory']) && strlen($course['CourseCategory']) != 0) {
  59. require_once api_get_path(LIBRARY_PATH).'course_category.lib.php';
  60. $categoryInfo = getCategory($course['CourseCategory']);
  61. if (empty($categoryInfo)) {
  62. //@todo this is so bad even all lang variables are wrong ...
  63. $course['error'] = get_lang('UnkownCategoryCourseCode').' ('.$course['CourseCategory'].')';
  64. $errors[] = $course;
  65. }
  66. }
  67. }
  68. return $errors;
  69. }
  70. function getTeacherListInArray($teachers)
  71. {
  72. if (!empty($teachers)) {
  73. return explode('|', $teachers);
  74. }
  75. return array();
  76. }
  77. /**
  78. * Saves imported data.
  79. * @param array List of courses
  80. */
  81. function save_data($courses)
  82. {
  83. $msg = '';
  84. foreach ($courses as $course) {
  85. $course_language = api_get_valid_language($course['Language']);
  86. $teachers = getTeacherListInArray($course['Teacher']);
  87. $teacherList = array();
  88. $creatorId = api_get_user_id();
  89. if (!empty($teachers)) {
  90. foreach ($teachers as $teacher) {
  91. $teacherInfo = api_get_user_info_from_username($teacher);
  92. if (!empty($teacherInfo)) {
  93. $teacherList[] = $teacherInfo;
  94. }
  95. }
  96. }
  97. $params = array();
  98. $params['title'] = $course['Title'];
  99. $params['wanted_code'] = $course['Code'];
  100. $params['tutor_name'] = null;
  101. $params['course_category'] = $course['CourseCategory'];
  102. $params['course_language'] = $course_language;
  103. $params['user_id'] = $creatorId;
  104. $course_info = CourseManager::create_course($params);
  105. if (!empty($course_info)) {
  106. if (!empty($teacherList)) {
  107. foreach ($teacherList as $teacher) {
  108. CourseManager::add_user_to_course($teacher['user_id'], $course_info['code'], COURSEMANAGER);
  109. }
  110. }
  111. $msg .= '<a href="'.api_get_path(WEB_COURSE_PATH).$course_info['directory'].'/">
  112. '.$course_info['title'].'</a> '.get_lang('Created').'<br />';
  113. }
  114. }
  115. if (!empty($msg)) {
  116. Display::display_normal_message($msg, false);
  117. }
  118. }
  119. /**
  120. * Read the CSV-file
  121. * @param string $file Path to the CSV-file
  122. * @return array All course-information read from the file
  123. */
  124. function parse_csv_data($file)
  125. {
  126. $courses = Import::csv_to_array($file);
  127. return $courses;
  128. }
  129. $language_file = array('admin', 'registration','create_course', 'document');
  130. $cidReset = true;
  131. require '../inc/global.inc.php';
  132. $this_section = SECTION_PLATFORM_ADMIN;
  133. api_protect_admin_script();
  134. require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  135. require_once api_get_path(LIBRARY_PATH).'import.lib.php';
  136. $defined_auth_sources[] = PLATFORM_AUTH_SOURCE;
  137. if (is_array($extAuthSource)) {
  138. $defined_auth_sources = array_merge($defined_auth_sources, array_keys($extAuthSource));
  139. }
  140. $tool_name = get_lang('ImportCourses').' CSV';
  141. $interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  142. set_time_limit(0);
  143. Display :: display_header($tool_name);
  144. if ($_POST['formSent']) {
  145. if (empty($_FILES['import_file']['tmp_name'])) {
  146. $error_message = get_lang('UplUploadFailed');
  147. Display :: display_error_message($error_message, false);
  148. } else {
  149. $allowed_file_mimetype = array('csv');
  150. $ext_import_file = substr($_FILES['import_file']['name'], (strrpos($_FILES['import_file']['name'], '.') + 1));
  151. if (!in_array($ext_import_file, $allowed_file_mimetype)) {
  152. Display :: display_error_message(get_lang('YouMustImportAFileAccordingToSelectedOption'));
  153. } else {
  154. $courses = parse_csv_data($_FILES['import_file']['tmp_name']);
  155. $errors = validate_data($courses);
  156. if (count($errors) == 0) {
  157. save_data($courses);
  158. }
  159. }
  160. }
  161. }
  162. if (count($errors) != 0) {
  163. $error_message = '<ul>';
  164. foreach ($errors as $index => $error_course) {
  165. $error_message .= '<li>'.get_lang('Line').' '.$error_course['line'].': <strong>'.$error_course['error'].'</strong>: ';
  166. $error_message .= get_lang('Course').': '.$error_course['Title'].' ('.$error_course['Code'].')';
  167. $error_message .= '</li>';
  168. }
  169. $error_message .= '</ul>';
  170. Display :: display_error_message($error_message, false);
  171. }
  172. ?>
  173. <form method="post" action="<?php echo api_get_self(); ?>" enctype="multipart/form-data" style="margin: 0px;">
  174. <legend><?php echo $tool_name; ?></legend>
  175. <div class="control-group">
  176. <label><?php echo get_lang('ImportCSVFileLocation'); ?></label>
  177. <div class="control">
  178. <input type="file" name="import_file"/>
  179. </div>
  180. </div>
  181. <div class="control-group">
  182. <div class="control">
  183. <button type="submit" class="save" value="<?php echo get_lang('Import'); ?>"><?php echo get_lang('Import'); ?></button>
  184. </div>
  185. </div>
  186. <input type="hidden" name="formSent" value="1"/>
  187. </form>
  188. <div style="clear: both;"></div>
  189. <p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
  190. <blockquote>
  191. <pre>
  192. <strong>Code</strong>;<strong>Title</strong>;<strong>CourseCategory</strong>;Teacher;Language
  193. BIO0015;Biology;BIO;teacher1;english
  194. BIO0016;Maths;MATH;teacher2|teacher3;english
  195. BIO0017;Language;LANG;;english
  196. </pre>
  197. </blockquote>
  198. <?php
  199. Display :: display_footer();