usergroup_user_import.php 7.7 KB

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