add_users_to_usergroup.php 17 KB

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