class_user_import.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. /**
  7. * Code
  8. * This tool allows platform admins to update class-user relations by uploading
  9. * a CSVfile
  10. */
  11. /**
  12. * Validates imported data.
  13. */
  14. function validate_data($user_classes) {
  15. global $purification_option_for_usernames;
  16. $errors = array ();
  17. $classcodes = array ();
  18. if (!isset($_POST['subscribe']) && !isset($_POST['subscribe'])) {
  19. $user_class['error'] = get_lang('SelectAnAction');
  20. $errors[] = $user_class;
  21. return $errors;
  22. }
  23. foreach ($user_classes as $index => $user_class) {
  24. $user_class['line'] = $index + 1;
  25. // 1. Check whether mandatory fields are set.
  26. $mandatory_fields = array ('UserName', 'ClassName');
  27. foreach ($mandatory_fields as $key => $field) {
  28. if (!isset ($user_class[$field]) || strlen($user_class[$field]) == 0) {
  29. $user_class['error'] = get_lang($field.'Mandatory');
  30. $errors[] = $user_class;
  31. }
  32. }
  33. // 2. Check whether classcode exists.
  34. if (isset ($user_class['ClassName']) && strlen($user_class['ClassName']) != 0) {
  35. // 2.1 Check whether code has been allready used in this CVS-file.
  36. if (!isset ($classcodes[$user_class['ClassName']])) {
  37. // 2.1.1 Check whether code exists in DB.
  38. $class_table = Database :: get_main_table(TABLE_MAIN_CLASS);
  39. $sql = "SELECT * FROM $class_table WHERE name = '".Database::escape_string($user_class['ClassName'])."'";
  40. $res = Database::query($sql);
  41. if (Database::num_rows($res) == 0) {
  42. $user_class['error'] = get_lang('CodeDoesNotExists').': '.$user_class['ClassName'];
  43. $errors[] = $user_class;
  44. } else {
  45. $classcodes[$user_class['CourseCode']] = 1;
  46. }
  47. }
  48. }
  49. // 3. Check username, first, check whether it is empty.
  50. if (!UserManager::is_username_empty($user_class['UserName'])) {
  51. // 3.1. Check whether username is too long.
  52. if (UserManager::is_username_too_long($user_class['UserName'])) {
  53. $user_class['error'] = get_lang('UserNameTooLong').': '.$user_class['UserName'];
  54. $errors[] = $user_class;
  55. }
  56. $username = UserManager::purify_username($user_class['UserName'], $purification_option_for_usernames);
  57. // 3.2. Check whether username exists.
  58. if (UserManager::is_username_available($username)) {
  59. $user_class['error'] = get_lang('UnknownUser').': '.$username;
  60. $errors[] = $user_class;
  61. }
  62. }
  63. }
  64. return $errors;
  65. }
  66. /**
  67. * Saves imported data.
  68. */
  69. function save_data($users_classes) {
  70. global $purification_option_for_usernames;
  71. // Table definitions.
  72. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  73. $class_user_table = Database :: get_main_table(TABLE_MAIN_CLASS_USER);
  74. $class_table = Database :: get_main_table(TABLE_MAIN_CLASS);
  75. // Data parsing: purification + conversion (UserName, ClassName) --> (user_is, class_id)
  76. $csv_data = array ();
  77. foreach ($users_classes as $index => $user_class) {
  78. $sql1 = "SELECT user_id FROM $user_table WHERE username = '".Database::escape_string(UserManager::purify_username($user_class['UserName'], $purification_option_for_usernames))."'";
  79. $res1 = Database::query($sql1);
  80. $obj1 = Database::fetch_object($res1);
  81. $sql2 = "SELECT id FROM $class_table WHERE name = '".Database::escape_string(trim($user_class['ClassName']))."'";
  82. $res2 = Database::query($sql2);
  83. $obj2 = Database::fetch_object($res2);
  84. if ($obj1 && $obj2) {
  85. $csv_data[$obj1->user_id][$obj2->id] = 1;
  86. }
  87. }
  88. // Logic for processing the request (data + UI options).
  89. $db_subscriptions = array();
  90. foreach ($csv_data as $user_id => $csv_subscriptions) {
  91. $sql = "SELECT class_id FROM $class_user_table cu WHERE cu.user_id = $user_id";
  92. $res = Database::query($sql);
  93. while ($obj = Database::fetch_object($res)) {
  94. $db_subscriptions[$obj->class_id] = 1;
  95. }
  96. $to_subscribe = array_diff(array_keys($csv_subscriptions), array_keys($db_subscriptions));
  97. $to_unsubscribe = array_diff(array_keys($db_subscriptions), array_keys($csv_subscriptions));
  98. // Subscriptions for new classes.
  99. if ($_POST['subscribe']) {
  100. foreach ($to_subscribe as $class_id) {
  101. ClassManager::add_user($user_id, $class_id);
  102. }
  103. }
  104. // Unsubscription from previous classes.
  105. if ($_POST['unsubscribe']) {
  106. foreach ($to_unsubscribe as $class_id) {
  107. ClassManager::unsubscribe_user($user_id, $class_id);
  108. }
  109. }
  110. }
  111. }
  112. /**
  113. * Reads a CSV-file.
  114. * @param string $file Path to the CSV-file
  115. * @return array All course-information read from the file
  116. */
  117. function parse_csv_data($file) {
  118. $courses = Import::csv_to_array($file);
  119. return $courses;
  120. }
  121. $language_file = array('admin', 'registration');
  122. $cidReset = true;
  123. require_once '../inc/global.inc.php';
  124. $this_section = SECTION_PLATFORM_ADMIN;
  125. api_protect_admin_script(true);
  126. require_once api_get_path(LIBRARY_PATH).'import.lib.php';
  127. $tool_name = get_lang('AddUsersToAClass').' CSV';
  128. $interbreadcrumb[] = array ('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  129. $interbreadcrumb[] = array ('url' => 'class_list.php', 'name' => get_lang('Classes'));
  130. // Set this option to true to enforce strict purification for usenames.
  131. $purification_option_for_usernames = false;
  132. set_time_limit(0);
  133. $form = new FormValidator('class_user_import');
  134. $form->addElement('file', 'import_file', get_lang('ImportFileLocation'));
  135. $form->addElement('checkbox', 'subscribe', get_lang('Action'), get_lang('SubscribeUserIfNotAllreadySubscribed'));
  136. $form->addElement('checkbox', 'unsubscribe', '', get_lang('UnsubscribeUserIfSubscriptionIsNotInFile'));
  137. $form->addElement('style_submit_button', 'submit', get_lang('Import'), 'class="save"');
  138. if ($form->validate()) {
  139. $users_classes = parse_csv_data($_FILES['import_file']['tmp_name']);
  140. $errors = validate_data($users_classes);
  141. if (count($errors) == 0) {
  142. save_data($users_classes);
  143. header('Location: class_list.php?action=show_message&message='.urlencode(get_lang('FileImported')));
  144. exit();
  145. }
  146. }
  147. Display :: display_header($tool_name);
  148. api_display_tool_title($tool_name);
  149. if (count($errors) != 0) {
  150. $error_message = "\n";
  151. foreach ($errors as $index => $error_class_user) {
  152. $error_message .= get_lang('Line').' '.$error_class_user['line'].': '.$error_class_user['error'].'</b>';
  153. $error_message .= "<br />";
  154. }
  155. $error_message .= "\n";
  156. Display :: display_error_message($error_message, false);
  157. }
  158. $form->display();
  159. ?>
  160. <p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
  161. <pre>
  162. <b>UserName</b>;<b>ClassName</b>
  163. jdoe;class01
  164. adam;class01
  165. </pre>
  166. <?php
  167. Display :: display_footer();