course_import.php 7.5 KB

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