add_users_to_usergroup.php 17 KB

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