course_import.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. /**
  71. * @param array $teachers
  72. *
  73. * @return array
  74. */
  75. function getTeacherListInArray($teachers)
  76. {
  77. if (!empty($teachers)) {
  78. return explode('|', $teachers);
  79. }
  80. return array();
  81. }
  82. /**
  83. * Saves imported data.
  84. * @param array $courses List of courses
  85. */
  86. function save_data($courses)
  87. {
  88. $msg = '';
  89. foreach ($courses as $course) {
  90. $course_language = api_get_valid_language($course['Language']);
  91. $teachers = getTeacherListInArray($course['Teacher']);
  92. $teacherList = array();
  93. $creatorId = api_get_user_id();
  94. if (!empty($teachers)) {
  95. foreach ($teachers as $teacher) {
  96. $teacherInfo = api_get_user_info_from_username($teacher);
  97. if (!empty($teacherInfo)) {
  98. $teacherList[] = $teacherInfo;
  99. }
  100. }
  101. }
  102. $params = array();
  103. $params['title'] = $course['Title'];
  104. $params['wanted_code'] = $course['Code'];
  105. $params['tutor_name'] = null;
  106. $params['course_category'] = $course['CourseCategory'];
  107. $params['course_language'] = $course_language;
  108. $params['user_id'] = $creatorId;
  109. $addMeAsTeacher = isset($_POST['add_me_as_teacher']) ? $_POST['add_me_as_teacher'] : false;
  110. $params['add_user_as_teacher'] = $addMeAsTeacher;
  111. $courseInfo = CourseManager::create_course($params);
  112. if (!empty($courseInfo)) {
  113. if (!empty($teacherList)) {
  114. foreach ($teacherList as $teacher) {
  115. CourseManager::add_user_to_course(
  116. $teacher['user_id'],
  117. $courseInfo['code'],
  118. COURSEMANAGER
  119. );
  120. }
  121. }
  122. $msg .= '<a href="'.api_get_path(WEB_COURSE_PATH).$courseInfo['directory'].'/">
  123. '.$courseInfo['title'].'</a> '.get_lang('Created').'<br />';
  124. }
  125. }
  126. if (!empty($msg)) {
  127. Display::display_normal_message($msg, false);
  128. }
  129. }
  130. /**
  131. * Read the CSV-file
  132. * @param string $file Path to the CSV-file
  133. * @return array All course-information read from the file
  134. */
  135. function parse_csv_data($file)
  136. {
  137. $courses = Import::csv_to_array($file);
  138. return $courses;
  139. }
  140. $language_file = array('admin', 'registration','create_course', 'document');
  141. $cidReset = true;
  142. require '../inc/global.inc.php';
  143. $this_section = SECTION_PLATFORM_ADMIN;
  144. api_protect_admin_script();
  145. require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  146. require_once api_get_path(LIBRARY_PATH).'import.lib.php';
  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. Display :: display_error_message($error_message, 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. Display :: display_error_message(get_lang('YouMustImportAFileAccordingToSelectedOption'));
  164. } else {
  165. $courses = parse_csv_data($_FILES['import_file']['tmp_name']);
  166. $errors = validate_data($courses);
  167. if (count($errors) == 0) {
  168. save_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. Display :: display_error_message($error_message, false);
  182. }
  183. $form = new FormValidator('import', 'post', api_get_self(), null, array('enctype' => 'multipart/form-data'));
  184. $form->add_header($tool_name);
  185. $form->addElement('file', 'import_file', get_lang('ImportCSVFileLocation'));
  186. $form->addElement('checkbox', 'add_me_as_teacher', null, get_lang('AddMeAsTeacherInCourses'));
  187. $form->addElement('button', 'save', get_lang('Import'));
  188. $form->addElement('hidden', 'formSent', 1);
  189. //$form->setDefaults(array('add_me_as_teacher' => 0));
  190. $form->display();
  191. ?>
  192. <div style="clear: both;"></div>
  193. <p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
  194. <blockquote>
  195. <pre>
  196. <strong>Code</strong>;<strong>Title</strong>;<strong>CourseCategory</strong>;Teacher;Language
  197. BIO0015;Biology;BIO;teacher1;english
  198. BIO0016;Maths;MATH;teacher2|teacher3;english
  199. BIO0017;Language;LANG;;english
  200. </pre>
  201. </blockquote>
  202. <?php
  203. Display :: display_footer();