add_users_to_usergroup.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 add_user_to_session (code, content) {
  29. document.getElementById("user_to_add").value = "";
  30. document.getElementById("ajax_list_users_single").innerHTML = "";
  31. destination = document.getElementById("elements_in");
  32. for (i=0;i<destination.length;i++) {
  33. if(destination.options[i].text == content) {
  34. return false;
  35. }
  36. }
  37. destination.options[destination.length] = new Option(content,code);
  38. destination.selectedIndex = -1;
  39. sortOptions(destination.options);
  40. }
  41. function remove_item(origin) {
  42. for(var i = 0 ; i<origin.options.length ; i++) {
  43. if(origin.options[i].selected) {
  44. origin.options[i]=null;
  45. i = i-1;
  46. }
  47. }
  48. }
  49. function validate_filter() {
  50. document.formulaire.add_type.value = \''.$add_type.'\';
  51. document.formulaire.form_sent.value=0;
  52. document.formulaire.submit();
  53. }
  54. function checked_in_no_group(checked) {
  55. $("#first_letter_user")
  56. .find("option")
  57. .attr("selected", false);
  58. document.formulaire.form_sent.value="2";
  59. document.formulaire.submit();
  60. }
  61. function change_select(val) {
  62. $("#user_with_any_group_id").attr("checked", false);
  63. document.formulaire.form_sent.value="2";
  64. document.formulaire.submit();
  65. }
  66. </script>';
  67. $form_sent = 0;
  68. $errorMsg = '';
  69. $extra_field_list= UserManager::get_extra_fields();
  70. $new_field_list = array();
  71. if (is_array($extra_field_list)) {
  72. foreach ($extra_field_list as $extra_field) {
  73. //if is enabled to filter and is a "<select>" field type
  74. if ($extra_field[8]==1 && $extra_field[2]==4 ) {
  75. $new_field_list[] = array('name'=> $extra_field[3], 'variable'=>$extra_field[1], 'data'=> $extra_field[9]);
  76. }
  77. }
  78. }
  79. $usergroup = new UserGroup();
  80. $id = intval($_GET['id']);
  81. $first_letter_user = '';
  82. if (isset($_POST['form_sent']) && $_POST['form_sent']) {
  83. $form_sent = $_POST['form_sent'];
  84. $elements_posted = $_POST['elements_in_name'];
  85. $first_letter_user = $_POST['firstLetterUser'];
  86. if (!is_array($elements_posted)) {
  87. $elements_posted=array();
  88. }
  89. if ($form_sent == 1) {
  90. //added a parameter to send emails when registering a user
  91. $usergroup->subscribe_users_to_usergroup($id, $elements_posted);
  92. header('Location: usergroups.php');
  93. exit;
  94. }
  95. }
  96. //Filter by Extra Fields
  97. $use_extra_fields = false;
  98. if (is_array($extra_field_list)) {
  99. if (is_array($new_field_list) && count($new_field_list)>0 ) {
  100. foreach ($new_field_list as $new_field) {
  101. $varname = 'field_'.$new_field['variable'];
  102. if (UserManager::is_extra_field_available($new_field['variable'])) {
  103. if (isset($_POST[$varname]) && $_POST[$varname]!='0') {
  104. $use_extra_fields = true;
  105. $extra_field_result[]= UserManager::get_extra_user_data_by_value($new_field['variable'], $_POST[$varname]);
  106. }
  107. }
  108. }
  109. }
  110. }
  111. if ($use_extra_fields) {
  112. $final_result = array();
  113. if (count($extra_field_result)>1) {
  114. for($i=0;$i<count($extra_field_result)-1;$i++) {
  115. if (is_array($extra_field_result[$i+1])) {
  116. $final_result = array_intersect($extra_field_result[$i],$extra_field_result[$i+1]);
  117. }
  118. }
  119. } else {
  120. $final_result = $extra_field_result[0];
  121. }
  122. }
  123. $data = $usergroup->get($id);
  124. $list_in = $usergroup->get_users_by_usergroup($id);
  125. $list_all = $usergroup->get_users_by_usergroup();
  126. $order = array('lastname');
  127. if (api_is_western_name_order()) {
  128. $order = array('firstname');
  129. }
  130. $elements_not_in = $elements_in = array();
  131. $complete_user_list = UserManager::get_user_list(array(), $order);
  132. if (!empty($complete_user_list)) {
  133. foreach($complete_user_list as $item) {
  134. if ($use_extra_fields) {
  135. if (!in_array($item['user_id'], $final_result)) {
  136. continue;
  137. }
  138. }
  139. if ($item['status'] == 6 ) continue; //avoid anonymous users
  140. if (in_array($item['user_id'], $list_in)) {
  141. $person_name = api_get_person_name($item['firstname'], $item['lastname']).' ('.$item['username'].')';
  142. $elements_in[$item['user_id']] = $person_name;
  143. }
  144. }
  145. }
  146. $user_with_any_group = isset($_REQUEST['user_with_any_group']) && !empty($_REQUEST['user_with_any_group']) ? true : false;
  147. if ($user_with_any_group) {
  148. $user_list = UserManager::get_user_list_like(array('lastname' => $first_letter_user), $order, true);
  149. $new_user_list = array();
  150. foreach ($user_list as $item) {
  151. if (!in_array($item['user_id'], $list_all)) {
  152. $new_user_list[] = $item;
  153. }
  154. }
  155. $user_list = $new_user_list;
  156. } else {
  157. $user_list = UserManager::get_user_list_like(array('lastname' => $first_letter_user), $order, true);
  158. }
  159. if (!empty($user_list)) {
  160. foreach($user_list as $item) {
  161. if ($use_extra_fields) {
  162. if (!in_array($item['user_id'], $final_result)) {
  163. continue;
  164. }
  165. }
  166. if ($item['status'] == 6 ) continue; //avoid anonymous users
  167. $person_name = api_get_person_name($item['firstname'], $item['lastname']).' ('.$item['username'].')';
  168. if (in_array($item['user_id'], $list_in)) {
  169. //$elements_in[$item['user_id']] = $person_name;
  170. } else {
  171. $elements_not_in[$item['user_id']] = $person_name;
  172. }
  173. }
  174. }
  175. $add_type == 'unique' ? true : false;
  176. Display::display_header($tool_name);
  177. if ($add_type == 'multiple') {
  178. $link_add_type_unique = '<a href="'.api_get_self().'?add_type=unique">'.Display::return_icon('single.gif').get_lang('SessionAddTypeUnique').'</a>';
  179. $link_add_type_multiple = Display::return_icon('multiple.gif').get_lang('SessionAddTypeMultiple');
  180. } else {
  181. $link_add_type_unique = Display::return_icon('single.gif').get_lang('SessionAddTypeUnique');
  182. $link_add_type_multiple = '<a href="'.api_get_self().'?add_type=multiple">'.Display::return_icon('multiple.gif').get_lang('SessionAddTypeMultiple').'</a>';
  183. }
  184. echo '<div class="actions">';
  185. echo '<a href="usergroups.php">'.Display::return_icon('back.png',get_lang('Back'), array(), ICON_SIZE_MEDIUM).'</a>';
  186. echo '<a href="usergroup_user_import.php">'.Display::return_icon('import_csv.png',get_lang('Import'), array(), ICON_SIZE_MEDIUM).'</a>';
  187. echo '</div>';
  188. ?>
  189. <form name="formulaire" method="post" action="<?php echo api_get_self(); ?>?id=<?php echo $id; if(!empty($_GET['add'])) echo '&add=true' ; ?>" style="margin:0px;">
  190. <?php
  191. echo '<legend>'.$tool_name.': '.$data['name'].'</legend>';
  192. if ($add_type=='multiple') {
  193. if (is_array($extra_field_list)) {
  194. if (is_array($new_field_list) && count($new_field_list)>0 ) {
  195. echo '<h3>'.get_lang('FilterUsers').'</h3>';
  196. foreach ($new_field_list as $new_field) {
  197. echo $new_field['name'];
  198. $varname = 'field_'.$new_field['variable'];
  199. echo '&nbsp;<select name="'.$varname.'">';
  200. echo '<option value="0">--'.get_lang('Select').'--</option>';
  201. foreach ($new_field['data'] as $option) {
  202. $checked='';
  203. if (isset($_POST[$varname])) {
  204. if ($_POST[$varname]==$option[1]) {
  205. $checked = 'selected="true"';
  206. }
  207. }
  208. echo '<option value="'.$option[1].'" '.$checked.'>'.$option[1].'</option>';
  209. }
  210. echo '</select>';
  211. echo '&nbsp;&nbsp;';
  212. }
  213. echo '<input type="button" value="'.get_lang('Filter').'" onclick="validate_filter()" />';
  214. echo '<br /><br />';
  215. }
  216. }
  217. }
  218. echo Display::input('hidden','id',$id);
  219. echo Display::input('hidden','form_sent','1');
  220. echo Display::input('hidden','add_type',null);
  221. if(!empty($errorMsg)) {
  222. Display::display_normal_message($errorMsg);
  223. }
  224. ?>
  225. <div class="row">
  226. <div class="span5">
  227. <div class="multiple_select_header">
  228. <b><?php echo get_lang('UsersInPlatform') ?> :</b>
  229. <?php echo get_lang('FirstLetterUser'); ?> :
  230. <select id="first_letter_user" name="firstLetterUser" onchange="change_select();">
  231. <option value = "%">--</option>
  232. <?php
  233. echo Display :: get_alphabet_options($first_letter_user);
  234. ?>
  235. </select>
  236. </div>
  237. <?php echo Display::select('elements_not_in_name', $elements_not_in, '',array('class'=>'span5', 'multiple'=>'multiple','id'=>'elements_not_in','size'=>'15px'),false); ?>
  238. <br />
  239. <label class="control-label">
  240. <input type="checkbox" <?php if ($user_with_any_group) echo 'checked="checked"';?> onchange="checked_in_no_group(this.checked);" name="user_with_any_group" id="user_with_any_group_id">
  241. <?php echo get_lang('UsersRegisteredInAnyGroup'); ?>
  242. </label>
  243. </div>
  244. <div class="span2">
  245. <div style="padding-top:54px;width:auto;text-align: center;">
  246. <button class="arrowr" type="button" onclick="moveItem(document.getElementById('elements_not_in'), document.getElementById('elements_in'))" onclick="moveItem(document.getElementById('elements_not_in'), document.getElementById('elements_in'))"></button>
  247. <br /><br />
  248. <button class="arrowl" type="button" onclick="moveItem(document.getElementById('elements_in'), document.getElementById('elements_not_in'))" onclick="moveItem(document.getElementById('elements_in'), document.getElementById('elements_not_in'))"></button>
  249. </div>
  250. </div>
  251. <div class="span5">
  252. <div class="multiple_select_header">
  253. <b><?php echo get_lang('UsersInGroup') ?> :</b>
  254. </div>
  255. <?php
  256. echo Display::select('elements_in_name[]', $elements_in, '', array('class'=>'span5', 'multiple'=>'multiple','id'=>'elements_in','size'=>'15px'),false );
  257. unset($sessionUsersList);
  258. ?>
  259. </div>
  260. </div>
  261. <?php
  262. echo '<button class="save" type="button" value="" onclick="valide()" >'.get_lang('SubscribeUsersToClass').'</button>';
  263. ?>
  264. </form>
  265. <script>
  266. function valide(){
  267. var options = document.getElementById('elements_in').options;
  268. for (i = 0 ; i<options.length ; i++)
  269. options[i].selected = true;
  270. document.forms.formulaire.submit();
  271. }
  272. </script>
  273. <?php
  274. Display::display_footer();