course_user_import.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This tool allows platform admins to update course-user relations by uploading
  5. * a CSVfile
  6. * @package chamilo.admin
  7. */
  8. /**
  9. * Validates the imported data.
  10. */
  11. function validate_data($users_courses) {
  12. $errors = array ();
  13. $coursecodes = array ();
  14. foreach ($users_courses as $index => $user_course) {
  15. $user_course['line'] = $index +1;
  16. // 1. Check whether mandatory fields are set.
  17. $mandatory_fields = array ('UserName', 'CourseCode', 'Status');
  18. foreach ($mandatory_fields as $key => $field) {
  19. if (!isset($user_course[$field]) || strlen($user_course[$field]) == 0) {
  20. $user_course['error'] = get_lang($field.'Mandatory');
  21. $errors[] = $user_course;
  22. }
  23. }
  24. // 2. Check whether coursecode exists.
  25. if (isset ($user_course['CourseCode']) && strlen($user_course['CourseCode']) != 0) {
  26. // 2.1 Check whethher code has been allready used by this CVS-file.
  27. if (!isset($coursecodes[$user_course['CourseCode']])) {
  28. // 2.1.1 Check whether course with this code exists in the system.
  29. $course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
  30. $sql = "SELECT * FROM $course_table WHERE code = '".Database::escape_string($user_course['CourseCode'])."'";
  31. $res = Database::query($sql);
  32. if (Database::num_rows($res) == 0) {
  33. $user_course['error'] = get_lang('CodeDoesNotExists');
  34. $errors[] = $user_course;
  35. } else {
  36. $coursecodes[$user_course['CourseCode']] = 1;
  37. }
  38. }
  39. }
  40. // 3. Check whether username exists.
  41. if (isset ($user_course['UserName']) && strlen($user_course['UserName']) != 0)
  42. {
  43. if (UserManager::is_username_available($user_course['UserName'])) {
  44. $user_course['error'] = get_lang('UnknownUser');
  45. $errors[] = $user_course;
  46. }
  47. }
  48. // 4. Check whether status is valid.
  49. if (isset ($user_course['Status']) && strlen($user_course['Status']) != 0) {
  50. if ($user_course['Status'] != COURSEMANAGER && $user_course['Status'] != STUDENT) {
  51. $user_course['error'] = get_lang('UnknownStatus');
  52. $errors[] = $user_course;
  53. }
  54. }
  55. }
  56. return $errors;
  57. }
  58. /**
  59. * Saves imported data.
  60. */
  61. function save_data($users_courses) {
  62. $user_table= Database::get_main_table(TABLE_MAIN_USER);
  63. $course_user_table= Database::get_main_table(TABLE_MAIN_COURSE_USER);
  64. $csv_data = array();
  65. foreach ($users_courses as $index => $user_course) {
  66. $csv_data[$user_course['UserName']][$user_course['CourseCode']] = $user_course['Status'];
  67. }
  68. foreach($csv_data as $username => $csv_subscriptions) {
  69. $user_id = 0;
  70. $sql = "SELECT * FROM $user_table u WHERE u.username = '".Database::escape_string($username)."'";
  71. $res = Database::query($sql);
  72. $obj = Database::fetch_object($res);
  73. $user_id = $obj->user_id;
  74. $sql = "SELECT * FROM $course_user_table cu WHERE cu.user_id = $user_id AND cu.relation_type<>".COURSE_RELATION_TYPE_RRHH." ";
  75. $res = Database::query($sql);
  76. $db_subscriptions = array();
  77. while($obj = Database::fetch_object($res)) {
  78. $db_subscriptions[$obj->course_code] = $obj->status;
  79. }
  80. $to_subscribe = array_diff(array_keys($csv_subscriptions),array_keys($db_subscriptions));
  81. $to_unsubscribe = array_diff(array_keys($db_subscriptions),array_keys($csv_subscriptions));
  82. global $inserted_in_course;
  83. if (!isset($inserted_in_course)) {
  84. $inserted_in_course = array();
  85. }
  86. if($_POST['subscribe']) {
  87. foreach($to_subscribe as $index => $course_code) {
  88. if(CourseManager :: course_exists($course_code)) {
  89. CourseManager::add_user_to_course($user_id,$course_code,$csv_subscriptions[$course_code]);
  90. $course_info = CourseManager::get_course_information($course_code);
  91. $inserted_in_course[$course_code] = $course_info['title'];
  92. }
  93. if (CourseManager :: course_exists($course_code,true)) {
  94. // Also subscribe to virtual courses through check on visual code.
  95. $list = CourseManager :: get_courses_info_from_visual_code($course_code);
  96. foreach ($list as $vcourse) {
  97. if ($vcourse['code'] == $course_code) {
  98. // Ignore, this has already been inserted.
  99. } else {
  100. CourseManager::add_user_to_course($user_id,$vcourse['code'],$csv_subscriptions[$course_code]);
  101. $inserted_in_course[$vcourse['code']] = $vcourse['title'];
  102. }
  103. }
  104. }
  105. }
  106. }
  107. if($_POST['unsubscribe']) {
  108. foreach($to_unsubscribe as $index => $course_code) {
  109. if(CourseManager :: course_exists($course_code)) {
  110. CourseManager::unsubscribe_user($user_id,$course_code);
  111. $course_info = CourseManager::get_course_information($course_code);
  112. $inserted_in_course[$course_code] = $course_info['title'];
  113. }
  114. if (CourseManager :: course_exists($course_code,true)) {
  115. // also subscribe to virtual courses through check on visual code
  116. $list = CourseManager :: get_courses_info_from_visual_code($course_code);
  117. foreach ($list as $vcourse) {
  118. if ($vcourse['code'] == $course_code) {
  119. // Ignore, this has already been inserted.
  120. } else {
  121. CourseManager::unsubscribe_user($user_id,$vcourse['code']);
  122. $inserted_in_course[$vcourse['code']] = $vcourse['title'];
  123. }
  124. }
  125. }
  126. }
  127. }
  128. }
  129. }
  130. /**
  131. * Reads 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. $courses = Import :: csv_to_array($file);
  137. return $courses;
  138. }
  139. // Language files that should be included,
  140. $language_file = array ('admin', 'registration');
  141. $cidReset = true;
  142. // Including the global Dokeos file.
  143. include '../inc/global.inc.php';
  144. // Setting the section (for the tabs).
  145. $this_section = SECTION_PLATFORM_ADMIN;
  146. // Protecting the admin section.
  147. api_protect_admin_script();
  148. $tool_name = get_lang('AddUsersToACourse').' CSV';
  149. $interbreadcrumb[] = array ('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  150. set_time_limit(0);
  151. // Creating the form.
  152. $form = new FormValidator('course_user_import');
  153. $form->addElement('header', '', $tool_name);
  154. $form->addElement('file', 'import_file', get_lang('ImportFileLocation'));
  155. $form->addElement('checkbox', 'subscribe', get_lang('Action'), get_lang('SubscribeUserIfNotAllreadySubscribed'));
  156. $form->addElement('checkbox', 'unsubscribe', '', get_lang('UnsubscribeUserIfSubscriptionIsNotInFile'));
  157. $form->addElement('style_submit_button', 'submit',get_lang('Import'),'class="save"');
  158. if ($form->validate()) {
  159. $users_courses = parse_csv_data($_FILES['import_file']['tmp_name']);
  160. $errors = validate_data($users_courses);
  161. if (count($errors) == 0) {
  162. $inserted_in_course = array();
  163. save_data($users_courses);
  164. // Build the alert message in case there were visual codes subscribed to.
  165. if ($_POST['subscribe']) {
  166. $warn = get_lang('UsersSubscribedToBecauseVisualCode').': ';
  167. } else {
  168. $warn = get_lang('UsersUnsubscribedFromBecauseVisualCode').': ';
  169. }
  170. if (count($inserted_in_course) > 1) {
  171. // The users have been inserted in more than one course.
  172. foreach ($inserted_in_course as $code => $info) {
  173. $warn .= ' '.$info.' ('.$code.'),';
  174. }
  175. $warn = substr($warn,0,-1);
  176. }
  177. Security::clear_token();
  178. $tok = Security::get_token();
  179. header('Location: user_list.php?action=show_message&message='.urlencode(get_lang('FileImported')).'&warn='.urlencode($warn).'&sec_token='.$tok);
  180. exit ();
  181. }
  182. }
  183. // Displaying the header.
  184. Display :: display_header($tool_name);
  185. // Displaying the tool title.
  186. // api_display_tool_title($tool_name);
  187. if (count($errors) != 0) {
  188. $error_message = '<ul>';
  189. foreach ($errors as $index => $error_course) {
  190. $error_message .= '<li>'.get_lang('Line').' '.$error_course['line'].': <strong>'.$error_course['error'].'</strong>: ';
  191. $error_message .= $error_course['Code'].' '.$error_course['Title'];
  192. $error_message .= '</li>';
  193. }
  194. $error_message .= '</ul>';
  195. Display :: display_error_message($error_message);
  196. }
  197. // Displaying the form.
  198. $form->display();
  199. ?>
  200. <p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
  201. <blockquote>
  202. <pre>
  203. <b>UserName</b>;<b>CourseCode</b>;<b>Status</b>
  204. jdoe;course01;<?php echo COURSEMANAGER; ?>
  205. adam;course01;<?php echo STUDENT; ?>
  206. </pre>
  207. <?php
  208. echo COURSEMANAGER.': '.get_lang('Teacher').'<br />';
  209. echo STUDENT.': '.get_lang('Student').'<br />';
  210. ?>
  211. </blockquote>
  212. <?php
  213. // Footer.
  214. Display :: display_footer();