add_users_to_usergroup.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. // resetting the course id
  7. $cidReset = true;
  8. // including some necessary files
  9. require_once __DIR__.'/../inc/global.inc.php';
  10. // setting the section (for the tabs)
  11. $this_section = SECTION_PLATFORM_ADMIN;
  12. $id = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : 0;
  13. $relation = isset($_REQUEST['relation']) ? (int) $_REQUEST['relation'] : '';
  14. $usergroup = new UserGroup();
  15. $groupInfo = $usergroup->get($id);
  16. $usergroup->protectScript($groupInfo);
  17. // setting breadcrumbs
  18. $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('PlatformAdmin')];
  19. $interbreadcrumb[] = ['url' => 'usergroups.php', 'name' => get_lang('Classes')];
  20. // setting the name of the tool
  21. $tool_name = get_lang('SubscribeUsersToClass');
  22. $htmlHeadXtra[] = '
  23. <script>
  24. $(document).ready( function() {
  25. $("#relation").change(function() {
  26. window.location = "add_users_to_usergroup.php?id='.$id.'" +"&relation=" + $(this).val();
  27. });
  28. });
  29. function add_user_to_session (code, content) {
  30. document.getElementById("user_to_add").value = "";
  31. document.getElementById("ajax_list_users_single").innerHTML = "";
  32. destination = document.getElementById("elements_in");
  33. for (i=0;i<destination.length;i++) {
  34. if(destination.options[i].text == content) {
  35. return false;
  36. }
  37. }
  38. destination.options[destination.length] = new Option(content,code);
  39. destination.selectedIndex = -1;
  40. sortOptions(destination.options);
  41. }
  42. function remove_item(origin) {
  43. for(var i = 0 ; i<origin.options.length ; i++) {
  44. if(origin.options[i].selected) {
  45. origin.options[i]=null;
  46. i = i-1;
  47. }
  48. }
  49. }
  50. function validate_filter() {
  51. document.formulaire.form_sent.value=0;
  52. document.formulaire.submit();
  53. }
  54. function checked_in_no_group(checked)
  55. {
  56. $("#relation")
  57. .find("option")
  58. .attr("selected", false);
  59. $("#first_letter_user")
  60. .find("option")
  61. .attr("selected", false);
  62. document.formulaire.form_sent.value="2";
  63. document.formulaire.submit();
  64. }
  65. function change_select(val) {
  66. $("#user_with_any_group_id").attr("checked", false);
  67. document.formulaire.form_sent.value="2";
  68. document.formulaire.submit();
  69. }
  70. </script>';
  71. $form_sent = 0;
  72. $extra_field_list = UserManager::get_extra_fields();
  73. $new_field_list = [];
  74. if (is_array($extra_field_list)) {
  75. foreach ($extra_field_list as $extra_field) {
  76. //if is enabled to filter and is a "<select>" field type
  77. if ($extra_field[8] == 1 && $extra_field[2] == 4) {
  78. $new_field_list[] = [
  79. 'name' => $extra_field[3],
  80. 'variable' => $extra_field[1], 'data' => $extra_field[9],
  81. ];
  82. }
  83. }
  84. }
  85. if (empty($id)) {
  86. api_not_allowed(true);
  87. }
  88. $first_letter_user = '';
  89. if (isset($_POST['form_sent']) && $_POST['form_sent']) {
  90. $form_sent = $_POST['form_sent'];
  91. $elements_posted = isset($_POST['elements_in_name']) ? $_POST['elements_in_name'] : null;
  92. $first_letter_user = $_POST['firstLetterUser'];
  93. if (!is_array($elements_posted)) {
  94. $elements_posted = [];
  95. }
  96. // If "social group" you need to select a role
  97. if ($groupInfo['group_type'] == 1 && empty($relation)) {
  98. Display::addFlash(Display::return_message(get_lang('SelectRole'), 'warning'));
  99. header('Location: '.api_get_self().'?id='.$id);
  100. exit;
  101. }
  102. if ($form_sent == 1) {
  103. Display::addFlash(Display::return_message(get_lang('Updated')));
  104. // Added a parameter to send emails when registering a user
  105. $usergroup->subscribe_users_to_usergroup(
  106. $id,
  107. $elements_posted,
  108. true,
  109. $relation
  110. );
  111. header('Location: usergroups.php');
  112. exit;
  113. }
  114. }
  115. if (isset($_GET['action']) && $_GET['action'] == 'export') {
  116. $users = $usergroup->getUserListByUserGroup($id);
  117. if (!empty($users)) {
  118. $data = [
  119. ['UserName', 'ClassName'],
  120. ];
  121. foreach ($users as $user) {
  122. $data[] = [$user['username'], $groupInfo['name']];
  123. }
  124. $filename = 'export_user_class_'.api_get_local_time();
  125. Export::arrayToCsv($data, $filename);
  126. exit;
  127. }
  128. }
  129. // Filter by Extra Fields
  130. $use_extra_fields = false;
  131. if (is_array($extra_field_list)) {
  132. if (is_array($new_field_list) && count($new_field_list) > 0) {
  133. foreach ($new_field_list as $new_field) {
  134. $varname = 'field_'.$new_field['variable'];
  135. if (UserManager::is_extra_field_available($new_field['variable'])) {
  136. if (isset($_POST[$varname]) && $_POST[$varname] != '0') {
  137. $use_extra_fields = true;
  138. $extra_field_result[] = UserManager::get_extra_user_data_by_value(
  139. $new_field['variable'],
  140. $_POST[$varname]
  141. );
  142. }
  143. }
  144. }
  145. }
  146. }
  147. if ($use_extra_fields) {
  148. $final_result = [];
  149. if (count($extra_field_result) > 1) {
  150. for ($i = 0; $i < count($extra_field_result) - 1; $i++) {
  151. if (is_array($extra_field_result[$i + 1])) {
  152. $final_result = array_intersect($extra_field_result[$i], $extra_field_result[$i + 1]);
  153. }
  154. }
  155. } else {
  156. $final_result = $extra_field_result[0];
  157. }
  158. }
  159. // Filters
  160. $filters = [
  161. ['type' => 'text', 'name' => 'username', 'label' => get_lang('Username')],
  162. ['type' => 'text', 'name' => 'firstname', 'label' => get_lang('FirstName')],
  163. ['type' => 'text', 'name' => 'lastname', 'label' => get_lang('LastName')],
  164. ['type' => 'text', 'name' => 'official_code', 'label' => get_lang('OfficialCode')],
  165. ['type' => 'text', 'name' => 'email', 'label' => get_lang('Email')],
  166. ];
  167. $searchForm = new FormValidator('search', 'get', api_get_self().'?id='.$id);
  168. $searchForm->addHeader(get_lang('AdvancedSearch'));
  169. $renderer = &$searchForm->defaultRenderer();
  170. $searchForm->addElement('hidden', 'id', $id);
  171. foreach ($filters as $param) {
  172. $searchForm->addElement($param['type'], $param['name'], $param['label']);
  173. }
  174. $searchForm->addButtonSearch();
  175. $filterData = [];
  176. if ($searchForm->validate()) {
  177. $filterData = $searchForm->getSubmitValues();
  178. }
  179. $data = $usergroup->get($id);
  180. $list_in = $usergroup->getUsersByUsergroupAndRelation($id, $relation);
  181. $list_all = $usergroup->get_users_by_usergroup();
  182. $order = ['lastname'];
  183. if (api_is_western_name_order()) {
  184. $order = ['firstname'];
  185. }
  186. $orderListByOfficialCode = api_get_setting('order_user_list_by_official_code');
  187. if ($orderListByOfficialCode === 'true') {
  188. $order = ['official_code', 'lastname'];
  189. }
  190. $conditions = [];
  191. if (!empty($first_letter_user)) {
  192. $conditions['lastname'] = $first_letter_user;
  193. }
  194. if (!empty($filters) && !empty($filterData)) {
  195. foreach ($filters as $filter) {
  196. if (isset($filter['name']) && isset($filterData[$filter['name']])) {
  197. $value = $filterData[$filter['name']];
  198. if (!empty($value)) {
  199. $conditions[$filter['name']] = $value;
  200. }
  201. }
  202. }
  203. }
  204. $elements_not_in = $elements_in = [];
  205. $complete_user_list = UserManager::getUserListLike([], $order, false, 'AND');
  206. if (!empty($complete_user_list)) {
  207. foreach ($complete_user_list as $item) {
  208. if ($use_extra_fields) {
  209. if (!in_array($item['user_id'], $final_result)) {
  210. continue;
  211. }
  212. }
  213. // Avoid anonymous users
  214. if ($item['status'] == 6) {
  215. continue;
  216. }
  217. if (in_array($item['user_id'], $list_in)) {
  218. $officialCode = !empty($item['official_code']) ? ' - '.$item['official_code'] : null;
  219. $person_name = api_get_person_name(
  220. $item['firstname'],
  221. $item['lastname']
  222. ).' ('.$item['username'].') '.$officialCode;
  223. $orderListByOfficialCode = api_get_setting('order_user_list_by_official_code');
  224. if ($orderListByOfficialCode === 'true') {
  225. $officialCode = !empty($item['official_code']) ? $item['official_code'].' - ' : '? - ';
  226. $person_name = $officialCode.api_get_person_name(
  227. $item['firstname'],
  228. $item['lastname']
  229. ).' ('.$item['username'].') ';
  230. }
  231. $elements_in[$item['user_id']] = $person_name;
  232. }
  233. }
  234. }
  235. $user_with_any_group = isset($_REQUEST['user_with_any_group']) && !empty($_REQUEST['user_with_any_group']) ? true : false;
  236. if ($user_with_any_group) {
  237. $user_list = UserManager::getUserListLike($conditions, $order, true, 'AND');
  238. $new_user_list = [];
  239. foreach ($user_list as $item) {
  240. if (!in_array($item['user_id'], $list_all)) {
  241. $new_user_list[] = $item;
  242. }
  243. }
  244. $user_list = $new_user_list;
  245. } else {
  246. $user_list = UserManager::getUserListLike($conditions, $order, true, 'AND');
  247. }
  248. if (!empty($user_list)) {
  249. foreach ($user_list as $item) {
  250. if ($use_extra_fields) {
  251. if (!in_array($item['user_id'], $final_result)) {
  252. continue;
  253. }
  254. }
  255. // Avoid anonymous users
  256. if ($item['status'] == ANONYMOUS) {
  257. continue;
  258. }
  259. $officialCode = !empty($item['official_code']) ? ' - '.$item['official_code'] : null;
  260. $person_name = api_get_person_name(
  261. $item['firstname'],
  262. $item['lastname']
  263. ).' ('.$item['username'].') '.$officialCode;
  264. $orderListByOfficialCode = api_get_setting('order_user_list_by_official_code');
  265. if ($orderListByOfficialCode === 'true') {
  266. $officialCode = !empty($item['official_code']) ? $item['official_code'].' - ' : '? - ';
  267. $person_name = $officialCode.api_get_person_name(
  268. $item['firstname'],
  269. $item['lastname']
  270. ).' ('.$item['username'].') ';
  271. }
  272. if (!in_array($item['user_id'], $list_in)) {
  273. $elements_not_in[$item['user_id']] = $person_name;
  274. }
  275. }
  276. }
  277. Display::display_header($tool_name);
  278. echo '<div class="actions">';
  279. echo '<a href="usergroups.php">'.
  280. Display::return_icon('back.png', get_lang('Back'), [], ICON_SIZE_MEDIUM).'</a>';
  281. echo Display::url(get_lang('AdvancedSearch'), '#', ['class' => 'advanced_options', 'id' => 'advanced_search']);
  282. echo '<a href="usergroup_user_import.php">'.
  283. Display::return_icon('import_csv.png', get_lang('Import'), [], ICON_SIZE_MEDIUM).'</a>';
  284. echo '<a href="'.api_get_self().'?id='.$id.'&action=export">'.
  285. Display::return_icon('export_csv.png', get_lang('Export'), [], ICON_SIZE_MEDIUM).'</a>';
  286. echo '</div>';
  287. echo '<div id="advanced_search_options" style="display:none">';
  288. $searchForm->display();
  289. echo '</div>';
  290. ?>
  291. <form name="formulaire" method="post" action="<?php echo api_get_self(); ?>?id=<?php echo $id; if (!empty($_GET['add'])) {
  292. echo '&add=true';
  293. } ?>" style="margin:0px;">
  294. <?php
  295. echo '<legend>'.$tool_name.': '.$data['name'].'</legend>';
  296. if (is_array($extra_field_list)) {
  297. if (is_array($new_field_list) && count($new_field_list) > 0) {
  298. echo '<h3>'.get_lang('FilterByUser').'</h3>';
  299. foreach ($new_field_list as $new_field) {
  300. echo $new_field['name'];
  301. $varname = 'field_'.$new_field['variable'];
  302. echo '&nbsp;<select name="'.$varname.'">';
  303. echo '<option value="0">--'.get_lang('Select').'--</option>';
  304. foreach ($new_field['data'] as $option) {
  305. $checked = '';
  306. if (isset($_POST[$varname])) {
  307. if ($_POST[$varname] == $option[1]) {
  308. $checked = 'selected="true"';
  309. }
  310. }
  311. echo '<option value="'.$option[1].'" '.$checked.'>'.$option[1].'</option>';
  312. }
  313. echo '</select>';
  314. echo '&nbsp;&nbsp;';
  315. }
  316. echo '<input type="button" value="'.get_lang('Filter').'" onclick="validate_filter()" />';
  317. echo '<br /><br />';
  318. }
  319. }
  320. echo Display::input('hidden', 'id', $id);
  321. echo Display::input('hidden', 'form_sent', '1');
  322. echo Display::input('hidden', 'add_type', null);
  323. ?>
  324. <div class="row">
  325. <div class="col-md-5">
  326. <?php if ($data['group_type'] == UserGroup::SOCIAL_CLASS) {
  327. ?>
  328. <select name="relation" id="relation">
  329. <option value=""><?php echo get_lang('SelectARelationType'); ?></option>
  330. <option value="<?php echo GROUP_USER_PERMISSION_ADMIN; ?>" <?php echo (isset($relation) && $relation == GROUP_USER_PERMISSION_ADMIN) ? 'selected=selected' : ''; ?> >
  331. <?php echo get_lang('Admin'); ?></option>
  332. <option value="<?php echo GROUP_USER_PERMISSION_READER; ?>" <?php echo (isset($relation) && $relation == GROUP_USER_PERMISSION_READER) ? 'selected=selected' : ''; ?> >
  333. <?php echo get_lang('Reader'); ?></option>
  334. <option value="<?php echo GROUP_USER_PERMISSION_PENDING_INVITATION; ?>" <?php echo (isset($relation) && $relation == GROUP_USER_PERMISSION_PENDING_INVITATION) ? 'selected=selected' : ''; ?> >
  335. <?php echo get_lang('PendingInvitation'); ?></option>
  336. <option value="<?php echo GROUP_USER_PERMISSION_MODERATOR; ?>" <?php echo (isset($relation) && $relation == GROUP_USER_PERMISSION_MODERATOR) ? 'selected=selected' : ''; ?> >
  337. <?php echo get_lang('Moderator'); ?></option>
  338. <option value="<?php echo GROUP_USER_PERMISSION_HRM; ?>" <?php echo (isset($relation) && $relation == GROUP_USER_PERMISSION_HRM) ? 'selected=selected' : ''; ?> >
  339. <?php echo get_lang('Drh'); ?></option>
  340. </select>
  341. <?php
  342. } ?>
  343. <div class="multiple_select_header">
  344. <b><?php echo get_lang('UsersInPlatform'); ?> :</b>
  345. <?php echo get_lang('FirstLetterUser'); ?> :
  346. <select id="first_letter_user" name="firstLetterUser" onchange="change_select();">
  347. <option value = "%">--</option>
  348. <?php
  349. echo Display :: get_alphabet_options($first_letter_user);
  350. ?>
  351. </select>
  352. </div>
  353. <?php
  354. echo Display::select(
  355. 'elements_not_in_name',
  356. $elements_not_in,
  357. '',
  358. [
  359. 'class' => 'form-control',
  360. 'multiple' => 'multiple',
  361. 'id' => 'elements_not_in',
  362. 'size' => '15px',
  363. ],
  364. false
  365. );
  366. ?>
  367. <br />
  368. <label class="control-label">
  369. <input type="checkbox" <?php if ($user_with_any_group) {
  370. echo 'checked="checked"';
  371. } ?> onchange="checked_in_no_group(this.checked);" name="user_with_any_group" id="user_with_any_group_id">
  372. <?php echo get_lang('UsersRegisteredInAnyGroup'); ?>
  373. </label>
  374. </div>
  375. <div class="col-md-2">
  376. <div style="padding-top:54px;width:auto;text-align: center;">
  377. <button class="btn btn-default" type="button" onclick="moveItem(document.getElementById('elements_not_in'), document.getElementById('elements_in'))" onclick="moveItem(document.getElementById('elements_not_in'), document.getElementById('elements_in'))">
  378. <em class="fa fa-arrow-right"></em>
  379. </button>
  380. <br /><br />
  381. <button class="btn btn-default" type="button" onclick="moveItem(document.getElementById('elements_in'), document.getElementById('elements_not_in'))" onclick="moveItem(document.getElementById('elements_in'), document.getElementById('elements_not_in'))">
  382. <em class="fa fa-arrow-left"></em>
  383. </button>
  384. </div>
  385. </div>
  386. <div class="col-md-5">
  387. <div class="multiple_select_header">
  388. <b><?php echo get_lang('UsersInGroup'); ?> :</b>
  389. </div>
  390. <?php
  391. echo Display::select(
  392. 'elements_in_name[]',
  393. $elements_in,
  394. '',
  395. [
  396. 'class' => 'form-control',
  397. 'multiple' => 'multiple',
  398. 'id' => 'elements_in',
  399. 'size' => '15px',
  400. ],
  401. false
  402. );
  403. unset($sessionUsersList);
  404. ?>
  405. </div>
  406. </div>
  407. <?php
  408. echo '<button class="btn btn-primary" type="button" value="" onclick="valide()" ><em class="fa fa-check"></em>'.
  409. get_lang('SubscribeUsersToClass').'</button>';
  410. ?>
  411. </form>
  412. <script>
  413. function moveItem(origin , destination) {
  414. for(var i = 0 ; i<origin.options.length ; i++) {
  415. if(origin.options[i].selected) {
  416. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  417. origin.options[i]=null;
  418. i = i-1;
  419. }
  420. }
  421. destination.selectedIndex = -1;
  422. sortOptions(destination.options);
  423. }
  424. function sortOptions(options) {
  425. newOptions = new Array();
  426. for (i = 0 ; i<options.length ; i++)
  427. newOptions[i] = options[i];
  428. newOptions = newOptions.sort(mysort);
  429. options.length = 0;
  430. for (i = 0 ; i < newOptions.length ; i++)
  431. options[i] = newOptions[i];
  432. }
  433. function mysort(a, b) {
  434. if(a.text.toLowerCase() > b.text.toLowerCase()){
  435. return 1;
  436. }
  437. if(a.text.toLowerCase() < b.text.toLowerCase()){
  438. return -1;
  439. }
  440. return 0;
  441. }
  442. function valide() {
  443. var options = document.getElementById('elements_in').options;
  444. for (i = 0 ; i<options.length ; i++)
  445. options[i].selected = true;
  446. document.forms.formulaire.submit();
  447. }
  448. </script>
  449. <?php
  450. Display::display_footer();