course_import.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. $categoryInfo = getCategory($course['CourseCategory']);
  60. if (empty($categoryInfo)) {
  61. //@todo this is so bad even all lang variables are wrong ...
  62. $course['error'] = get_lang('UnkownCategoryCourseCode').' ('.$course['CourseCategory'].')';
  63. $errors[] = $course;
  64. }
  65. }
  66. }
  67. return $errors;
  68. }
  69. /**
  70. * @param array $teachers
  71. *
  72. * @return array
  73. */
  74. function getTeacherListInArray($teachers)
  75. {
  76. if (!empty($teachers)) {
  77. return explode('|', $teachers);
  78. }
  79. return array();
  80. }
  81. /**
  82. * Saves imported data.
  83. * @param array $courses List of courses
  84. */
  85. function save_data($courses)
  86. {
  87. $msg = '';
  88. foreach ($courses as $course) {
  89. $course_language = $course['Language'];
  90. $teachers = getTeacherListInArray($course['Teacher']);
  91. $teacherList = array();
  92. $creatorId = api_get_user_id();
  93. if (!empty($teachers)) {
  94. foreach ($teachers as $teacher) {
  95. $teacherInfo = api_get_user_info_from_username($teacher);
  96. if (!empty($teacherInfo)) {
  97. $teacherList[] = $teacherInfo;
  98. }
  99. }
  100. }
  101. $params = array();
  102. $params['title'] = $course['Title'];
  103. $params['wanted_code'] = $course['Code'];
  104. $params['tutor_name'] = null;
  105. $params['course_category'] = $course['CourseCategory'];
  106. $params['course_language'] = $course_language;
  107. $params['user_id'] = $creatorId;
  108. $addMeAsTeacher = isset($_POST['add_me_as_teacher']) ? $_POST['add_me_as_teacher'] : false;
  109. $params['add_user_as_teacher'] = $addMeAsTeacher;
  110. $courseInfo = CourseManager::create_course($params);
  111. if (!empty($courseInfo)) {
  112. if (!empty($teacherList)) {
  113. foreach ($teacherList as $teacher) {
  114. CourseManager::add_user_to_course(
  115. $teacher['user_id'],
  116. $courseInfo['code'],
  117. COURSEMANAGER
  118. );
  119. }
  120. }
  121. $msg .= '<a href="'.api_get_path(WEB_COURSE_PATH).$courseInfo['directory'].'/">
  122. '.$courseInfo['title'].'</a> '.get_lang('Created').'<br />';
  123. }
  124. }
  125. if (!empty($msg)) {
  126. Display::display_normal_message($msg, false);
  127. }
  128. }
  129. /**
  130. * Read the CSV-file
  131. * @param string $file Path to the CSV-file
  132. * @return array All course-information read from the file
  133. */
  134. function parse_csv_data($file)
  135. {
  136. $courses = Import::csvToArray($file);
  137. return $courses;
  138. }
  139. $cidReset = true;
  140. require '../inc/global.inc.php';
  141. $this_section = SECTION_PLATFORM_ADMIN;
  142. api_protect_admin_script();
  143. $defined_auth_sources[] = PLATFORM_AUTH_SOURCE;
  144. if (isset($extAuthSource) && is_array($extAuthSource)) {
  145. $defined_auth_sources = array_merge($defined_auth_sources, array_keys($extAuthSource));
  146. }
  147. $tool_name = get_lang('ImportCourses').' CSV';
  148. $interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  149. set_time_limit(0);
  150. Display :: display_header($tool_name);
  151. if (isset($_POST['formSent']) && $_POST['formSent']) {
  152. if (empty($_FILES['import_file']['tmp_name'])) {
  153. $error_message = get_lang('UplUploadFailed');
  154. Display :: display_error_message($error_message, false);
  155. } else {
  156. $allowed_file_mimetype = array('csv');
  157. $ext_import_file = substr($_FILES['import_file']['name'], (strrpos($_FILES['import_file']['name'], '.') + 1));
  158. if (!in_array($ext_import_file, $allowed_file_mimetype)) {
  159. Display :: display_error_message(get_lang('YouMustImportAFileAccordingToSelectedOption'));
  160. } else {
  161. $courses = parse_csv_data($_FILES['import_file']['tmp_name']);
  162. $errors = validate_data($courses);
  163. if (count($errors) == 0) {
  164. save_data($courses);
  165. }
  166. }
  167. }
  168. }
  169. if (isset($errors) && count($errors) != 0) {
  170. $error_message = '<ul>';
  171. foreach ($errors as $index => $error_course) {
  172. $error_message .= '<li>'.get_lang('Line').' '.$error_course['line'].': <strong>'.$error_course['error'].'</strong>: ';
  173. $error_message .= get_lang('Course').': '.$error_course['Title'].' ('.$error_course['Code'].')';
  174. $error_message .= '</li>';
  175. }
  176. $error_message .= '</ul>';
  177. Display :: display_error_message($error_message, false);
  178. }
  179. $form = new FormValidator('import', 'post', api_get_self(), null, array('enctype' => 'multipart/form-data'));
  180. $form->addHeader($tool_name);
  181. $form->addElement('file', 'import_file', get_lang('ImportCSVFileLocation'));
  182. $form->addElement('checkbox', 'add_me_as_teacher', null, get_lang('AddMeAsTeacherInCourses'));
  183. $form->addButtonImport(get_lang('Import'), 'save');
  184. $form->addElement('hidden', 'formSent', 1);
  185. $form->display();
  186. ?>
  187. <div style="clear: both;"></div>
  188. <p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
  189. <blockquote>
  190. <pre>
  191. <strong>Code</strong>;<strong>Title</strong>;<strong>CourseCategory</strong>;Teacher;Language
  192. BIO0015;Biology;BIO;teacher1;english
  193. BIO0016;Maths;MATH;teacher2|teacher3;english
  194. BIO0017;Language;LANG;;english
  195. </pre>
  196. </blockquote>
  197. <?php
  198. Display :: display_footer();