course_import.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Framework\Container;
  4. /**
  5. * This tool allows platform admins to create courses by uploading a CSV file
  6. * Copyright (c) 2005 Bart Mollet <bart.mollet@hogent.be>
  7. * @package chamilo.admin
  8. */
  9. /**
  10. * Validates imported data.
  11. */
  12. function validate_courses_data($courses)
  13. {
  14. $errors = array ();
  15. $coursecodes = array ();
  16. foreach ($courses as $index => $course) {
  17. $course['line'] = $index +1;
  18. // 1. Check whether mandatory fields are set.
  19. $mandatory_fields = array ('Code', 'Title', 'CourseCategory');
  20. foreach ($mandatory_fields as $field) {
  21. if (empty($course[$field])) {
  22. $course['error'] = get_lang($field.'Mandatory');
  23. $errors[] = $course;
  24. }
  25. }
  26. // 2. Check current course code.
  27. if (!empty($course['Code'])) {
  28. // 2.1 Check whether code has been already used by this CVS-file.
  29. if (isset($coursecodes[$course['Code']])) {
  30. $course['error'] = get_lang('CodeTwiceInFile');
  31. $errors[] = $course;
  32. } else {
  33. // 2.2 Check whether course code has been occupied.
  34. $courseInfo = api_get_course_info($course['Code']);
  35. if (!empty($courseInfo)) {
  36. $course['error'] = get_lang('CodeExists');
  37. $errors[] = $course;
  38. }
  39. }
  40. $coursecodes[$course['Code']] = 1;
  41. }
  42. // 3. Check whether teacher exists.
  43. $teacherList = getTeacherListInArray($course['Teacher']);
  44. if (!empty($teacherList)) {
  45. foreach ($teacherList as $teacher) {
  46. $teacherInfo = api_get_user_info_from_username($teacher);
  47. if (empty($teacherInfo)) {
  48. $course['error'] = get_lang('UnknownTeacher').' ('.$teacher.')';
  49. $errors[] = $course;
  50. }
  51. }
  52. }
  53. // 4. Check whether course category exists.
  54. if (!empty($course['CourseCategory'])) {
  55. $categoryInfo = CourseCategory::getCategory($course['CourseCategory']);
  56. if (empty($categoryInfo)) {
  57. //@todo this is so bad even all lang variables are wrong ...
  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::display_normal_message($msg, 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 '../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' => Container::getRouter()->generate('administration'), '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. Display :: display_error_message($error_message, 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. Display :: display_error_message(get_lang('YouMustImportAFileAccordingToSelectedOption'));
  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. Display :: display_error_message($error_message, 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();