add_users_to_usergroup.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. // name of the language file that needs to be included
  7. $language_file = array('admin','registration');
  8. // resetting the course id
  9. $cidReset = true;
  10. // including some necessary files
  11. ////require_once '../inc/global.inc.php';
  12. // setting the section (for the tabs)
  13. $this_section = SECTION_PLATFORM_ADMIN;
  14. // Access restrictions
  15. api_protect_admin_script(true);
  16. // setting breadcrumbs
  17. $interbreadcrumb[] = array('url' => 'index.php','name' => get_lang('PlatformAdmin'));
  18. $interbreadcrumb[] = array('url' => 'usergroups.php','name' => get_lang('Classes'));
  19. // Database Table Definitions
  20. // setting the name of the tool
  21. $tool_name=get_lang('SubscribeUsersToClass');
  22. $add_type = 'multiple';
  23. if(isset($_REQUEST['add_type']) && $_REQUEST['add_type']!=''){
  24. $add_type = Security::remove_XSS($_REQUEST['add_type']);
  25. }
  26. $htmlHeadXtra[] = '
  27. <script>
  28. function checked_in_no_group(checked) {
  29. $("#add_users_to_usergroup").submit();
  30. }
  31. </script>';
  32. $form_sent = 0;
  33. $errorMsg = '';
  34. $extra_field_list= UserManager::get_extra_fields();
  35. $new_field_list = array();
  36. if (is_array($extra_field_list)) {
  37. foreach ($extra_field_list as $extra_field) {
  38. //if is enabled to filter and is a "<select>" field type
  39. if ($extra_field[8]==1 && $extra_field[2]==4 ) {
  40. $new_field_list[] = array('name'=> $extra_field[3], 'variable'=>$extra_field[1], 'data'=> $extra_field[9]);
  41. }
  42. }
  43. }
  44. $usergroup = new UserGroup();
  45. $id = intval($_GET['id']);
  46. $first_letter_user = '';
  47. /*
  48. if (isset($_POST['form_sent']) && $_POST['form_sent']) {
  49. $form_sent = $_POST['form_sent'];
  50. $elements_posted = $_POST['elements_in_name'];
  51. $first_letter_user = $_POST['firstLetterUser'];
  52. if (!is_array($elements_posted)) {
  53. $elements_posted=array();
  54. }
  55. if ($form_sent == 1) {
  56. //added a parameter to send emails when registering a user
  57. //$usergroup->subscribe_users_to_usergroup($id, $elements_posted);
  58. header('Location: usergroups.php');
  59. exit;
  60. }
  61. }*/
  62. //Filter by Extra Fields
  63. $use_extra_fields = false;
  64. if (is_array($extra_field_list)) {
  65. if (is_array($new_field_list) && count($new_field_list)>0 ) {
  66. foreach ($new_field_list as $new_field) {
  67. $varname = 'field_'.$new_field['variable'];
  68. if (UserManager::is_extra_field_available($new_field['variable'])) {
  69. if (isset($_POST[$varname]) && $_POST[$varname]!='0') {
  70. $use_extra_fields = true;
  71. $extra_field_result[]= UserManager::get_extra_user_data_by_value($new_field['variable'], $_POST[$varname]);
  72. }
  73. }
  74. }
  75. }
  76. }
  77. if ($use_extra_fields) {
  78. $final_result = array();
  79. if (count($extra_field_result)>1) {
  80. for($i=0;$i<count($extra_field_result)-1;$i++) {
  81. if (is_array($extra_field_result[$i+1])) {
  82. $final_result = array_intersect($extra_field_result[$i],$extra_field_result[$i+1]);
  83. }
  84. }
  85. } else {
  86. $final_result = $extra_field_result[0];
  87. }
  88. }
  89. $data = $usergroup->get($id);
  90. $list_in = $usergroup->get_users_by_usergroup($id);
  91. $list_all = $usergroup->get_users_by_usergroup();
  92. $order = array('lastname');
  93. if (api_is_western_name_order()) {
  94. $order = array('firstname');
  95. }
  96. $elements_not_in = $elements_in = array();
  97. $complete_user_list = UserManager::get_user_list(array(), $order);
  98. if (!empty($complete_user_list)) {
  99. foreach($complete_user_list as $item) {
  100. if ($use_extra_fields) {
  101. if (!in_array($item['user_id'], $final_result)) {
  102. continue;
  103. }
  104. }
  105. if ($item['status'] == 6 ) continue; //avoid anonymous users
  106. if (in_array($item['user_id'], $list_in)) {
  107. $person_name = api_get_person_name($item['firstname'], $item['lastname']).' ('.$item['username'].')';
  108. $elements_in[$item['user_id']] = $person_name;
  109. }
  110. }
  111. }
  112. $user_with_any_group = isset($_REQUEST['user_with_any_group']) && !empty($_REQUEST['user_with_any_group']) ? true : false;
  113. if ($user_with_any_group) {
  114. $user_list = UserManager::get_user_list_like(array('lastname' => $first_letter_user), $order, true);
  115. $new_user_list = array();
  116. foreach ($user_list as $item) {
  117. if (!in_array($item['user_id'], $list_all)) {
  118. $new_user_list[] = $item;
  119. }
  120. }
  121. $user_list = $new_user_list;
  122. } else {
  123. $user_list = UserManager::get_user_list_like(array('lastname' => $first_letter_user), $order, true);
  124. }
  125. if (!empty($user_list)) {
  126. foreach($user_list as $item) {
  127. if ($use_extra_fields) {
  128. if (!in_array($item['user_id'], $final_result)) {
  129. continue;
  130. }
  131. }
  132. if ($item['status'] == 6 ) continue; //avoid anonymous users
  133. $person_name = api_get_person_name($item['firstname'], $item['lastname']).' ('.$item['username'].')';
  134. if (in_array($item['user_id'], $list_in)) {
  135. //$elements_in[$item['user_id']] = $person_name;
  136. } else {
  137. $elements_not_in[$item['user_id']] = $person_name;
  138. }
  139. }
  140. }
  141. $add_type == 'unique' ? true : false;
  142. Display::display_header($tool_name);
  143. if ($add_type == 'multiple') {
  144. $link_add_type_unique = '<a href="'.api_get_self().'?add_type=unique">'.Display::return_icon('single.gif').get_lang('SessionAddTypeUnique').'</a>';
  145. $link_add_type_multiple = Display::return_icon('multiple.gif').get_lang('SessionAddTypeMultiple');
  146. } else {
  147. $link_add_type_unique = Display::return_icon('single.gif').get_lang('SessionAddTypeUnique');
  148. $link_add_type_multiple = '<a href="'.api_get_self().'?add_type=multiple">'.Display::return_icon('multiple.gif').get_lang('SessionAddTypeMultiple').'</a>';
  149. }
  150. echo '<div class="actions">';
  151. echo '<a href="usergroups.php">'.Display::return_icon('back.png',get_lang('Back'), array(), ICON_SIZE_MEDIUM).'</a>';
  152. echo '<a href="usergroup_user_import.php">'.Display::return_icon('import_csv.png',get_lang('Import'), array(), ICON_SIZE_MEDIUM).'</a>';
  153. echo '</div>';
  154. $form = new FormValidator('add_users_to_usergroup', 'post', api_get_self().'?id='.$id);
  155. $form->addElement('hidden', 'id', $id);
  156. $form->addElement('hidden', 'form_sent', '1');
  157. $form->addElement('hidden', 'add_type', null);
  158. $userList = array();
  159. if (!empty($complete_user_list)) {
  160. foreach ($complete_user_list as $user) {
  161. $userList[$user['user_id']] = api_get_person_name($user['firstname'], $user['lastname']);
  162. }
  163. }
  164. $form->addDoubleMultipleSelect('user_groups', get_lang('GroupTutors'), $userList);
  165. $form->addElement('checkbox', 'user_with_any_group', null, get_lang('UsersRegisteredInAnyGroup'), array('onchange' => 'checked_in_no_group(this.checked);'));
  166. $form->addElement('button', 'submit', get_lang('SubscribeUsersToClass'));
  167. $defaults = array(
  168. 'user_groups' => array_keys($elements_in)
  169. );
  170. $form->setDefaults($defaults);
  171. $form->display();
  172. if ($form->validate()) {
  173. $values = $form->getSubmitValues();
  174. $users = $values['user_groups'];
  175. $usergroup->subscribe_users_to_usergroup($id, $users);
  176. header('Location: usergroups.php');
  177. exit;
  178. }
  179. Display::display_footer();