course_import.php 7.4 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. * @param array $courses
  12. * @return array $errors
  13. */
  14. function validate_courses_data($courses)
  15. {
  16. $errors = array();
  17. $coursecodes = array();
  18. foreach ($courses as $index => $course) {
  19. $course['line'] = $index + 1;
  20. // 1. Check whether mandatory fields are set.
  21. $mandatory_fields = array('Code', 'Title', 'CourseCategory');
  22. foreach ($mandatory_fields as $field) {
  23. if (empty($course[$field])) {
  24. $course['error'] = get_lang($field.'Mandatory');
  25. $errors[] = $course;
  26. }
  27. }
  28. // 2. Check current course code.
  29. if (!empty($course['Code'])) {
  30. // 2.1 Check whether code has been already used by this CVS-file.
  31. if (isset($coursecodes[$course['Code']])) {
  32. $course['error'] = get_lang('CodeTwiceInFile');
  33. $errors[] = $course;
  34. } else {
  35. // 2.2 Check whether course code has been occupied.
  36. $courseInfo = api_get_course_info($course['Code']);
  37. if (!empty($courseInfo)) {
  38. $course['error'] = get_lang('CodeExists');
  39. $errors[] = $course;
  40. }
  41. }
  42. $coursecodes[$course['Code']] = 1;
  43. }
  44. // 3. Check whether teacher exists.
  45. $teacherList = getTeacherListInArray($course['Teacher']);
  46. if (!empty($teacherList)) {
  47. foreach ($teacherList as $teacher) {
  48. $teacherInfo = api_get_user_info_from_username($teacher);
  49. if (empty($teacherInfo)) {
  50. $course['error'] = get_lang('UnknownTeacher').' ('.$teacher.')';
  51. $errors[] = $course;
  52. }
  53. }
  54. }
  55. if (!empty($course['CourseCategory'])) {
  56. $categoryInfo = CourseCategory::getCategory($course['CourseCategory']);
  57. if (empty($categoryInfo)) {
  58. CourseCategory::addNode($course['CourseCategory'], $course['CourseCategoryName'] ? $course['CourseCategoryName'] : $course['CourseCategory'], 'TRUE', null);
  59. }
  60. } else {
  61. $course['error'] = get_lang('NoCourseCategorySupplied');
  62. $errors[] = $course;
  63. }
  64. }
  65. return $errors;
  66. }
  67. /**
  68. * Get the teacher list
  69. *
  70. * @param array $teachers
  71. * @return array
  72. */
  73. function getTeacherListInArray($teachers)
  74. {
  75. if (!empty($teachers)) {
  76. return explode('|', $teachers);
  77. }
  78. return array();
  79. }
  80. /**
  81. * Saves imported data.
  82. * @param array $courses List of courses
  83. */
  84. function save_courses_data($courses)
  85. {
  86. $msg = '';
  87. foreach ($courses as $course) {
  88. $course_language = $course['Language'];
  89. $teachers = getTeacherListInArray($course['Teacher']);
  90. $teacherList = array();
  91. $creatorId = api_get_user_id();
  92. if (!empty($teachers)) {
  93. foreach ($teachers as $teacher) {
  94. $teacherInfo = api_get_user_info_from_username($teacher);
  95. if (!empty($teacherInfo)) {
  96. $teacherList[] = $teacherInfo;
  97. }
  98. }
  99. }
  100. $params = array();
  101. $params['title'] = $course['Title'];
  102. $params['wanted_code'] = $course['Code'];
  103. $params['tutor_name'] = null;
  104. $params['course_category'] = $course['CourseCategory'];
  105. $params['course_language'] = $course_language;
  106. $params['user_id'] = $creatorId;
  107. $addMeAsTeacher = isset($_POST['add_me_as_teacher']) ? $_POST['add_me_as_teacher'] : false;
  108. $params['add_user_as_teacher'] = $addMeAsTeacher;
  109. $courseInfo = CourseManager::create_course($params);
  110. if (!empty($courseInfo)) {
  111. if (!empty($teacherList)) {
  112. foreach ($teacherList as $teacher) {
  113. CourseManager::add_user_to_course(
  114. $teacher['user_id'],
  115. $courseInfo['code'],
  116. COURSEMANAGER
  117. );
  118. }
  119. }
  120. $msg .= '<a href="'.api_get_path(WEB_COURSE_PATH).$courseInfo['directory'].'/">
  121. '.$courseInfo['title'].'</a> '.get_lang('Created').'<br />';
  122. }
  123. }
  124. if (!empty($msg)) {
  125. Display::addFlash(Display::return_message($msg, 'normal', false));
  126. }
  127. }
  128. /**
  129. * Read the CSV-file
  130. * @param string $file Path to the CSV-file
  131. * @return array All course-information read from the file
  132. */
  133. function parse_csv_courses_data($file)
  134. {
  135. $courses = Import::csv_reader($file);
  136. return $courses;
  137. }
  138. $cidReset = true;
  139. require_once __DIR__.'/../inc/global.inc.php';
  140. $this_section = SECTION_PLATFORM_ADMIN;
  141. api_protect_admin_script();
  142. $defined_auth_sources[] = PLATFORM_AUTH_SOURCE;
  143. if (isset($extAuthSource) && is_array($extAuthSource)) {
  144. $defined_auth_sources = array_merge($defined_auth_sources, array_keys($extAuthSource));
  145. }
  146. $tool_name = get_lang('ImportCourses').' CSV';
  147. $interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  148. set_time_limit(0);
  149. Display::display_header($tool_name);
  150. if (isset($_POST['formSent']) && $_POST['formSent']) {
  151. if (empty($_FILES['import_file']['tmp_name'])) {
  152. $error_message = get_lang('UplUploadFailed');
  153. echo Display::return_message($error_message, 'error', false);
  154. } else {
  155. $allowed_file_mimetype = array('csv');
  156. $ext_import_file = substr($_FILES['import_file']['name'], (strrpos($_FILES['import_file']['name'], '.') + 1));
  157. if (!in_array($ext_import_file, $allowed_file_mimetype)) {
  158. echo Display::return_message(get_lang('YouMustImportAFileAccordingToSelectedOption'), 'error');
  159. } else {
  160. $courses = parse_csv_courses_data($_FILES['import_file']['tmp_name']);
  161. $errors = validate_courses_data($courses);
  162. if (count($errors) == 0) {
  163. save_courses_data($courses);
  164. }
  165. }
  166. }
  167. }
  168. if (isset($errors) && count($errors) != 0) {
  169. $error_message = '<ul>';
  170. foreach ($errors as $index => $error_course) {
  171. $error_message .= '<li>'.get_lang('Line').' '.$error_course['line'].': <strong>'.$error_course['error'].'</strong>: ';
  172. $error_message .= get_lang('Course').': '.$error_course['Title'].' ('.$error_course['Code'].')';
  173. $error_message .= '</li>';
  174. }
  175. $error_message .= '</ul>';
  176. echo Display::return_message($error_message, 'error', false);
  177. }
  178. $form = new FormValidator('import', 'post', api_get_self(), null, array('enctype' => 'multipart/form-data'));
  179. $form->addHeader($tool_name);
  180. $form->addElement('file', 'import_file', get_lang('ImportCSVFileLocation'));
  181. $form->addElement('checkbox', 'add_me_as_teacher', null, get_lang('AddMeAsTeacherInCourses'));
  182. $form->addButtonImport(get_lang('Import'), 'save');
  183. $form->addElement('hidden', 'formSent', 1);
  184. $form->display();
  185. ?>
  186. <div style="clear: both;"></div>
  187. <p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
  188. <blockquote>
  189. <pre>
  190. <strong>Code</strong>;<strong>Title</strong>;<strong>CourseCategory</strong>;<strong>CourseCategoryName</strong>;Teacher;Language
  191. BIO0015;Biology;BIO;Science;teacher1;english
  192. BIO0016;Maths;MATH;Engineerng;teacher2|teacher3;english
  193. BIO0017;Language;LANG;;;english
  194. </pre>
  195. </blockquote>
  196. <?php
  197. Display::display_footer();