access_url_edit_users_to_url.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. *
  6. * @author Julio Montoya <gugli100@gmail.com>
  7. */
  8. // resetting the course id
  9. $cidReset = true;
  10. require_once __DIR__.'/../inc/global.inc.php';
  11. $xajax = new xajax();
  12. $xajax->registerFunction(['search_users', 'AccessUrlEditUsersToUrl', 'search_users']);
  13. // setting the section (for the tabs)
  14. $this_section = SECTION_PLATFORM_ADMIN;
  15. // Access restrictions
  16. api_protect_global_admin_script();
  17. if (!api_get_multiple_access_url()) {
  18. header('Location: index.php');
  19. exit;
  20. }
  21. // Database Table Definitions
  22. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  23. // setting breadcrumbs
  24. $tool_name = get_lang('EditUsersToURL');
  25. $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('PlatformAdmin')];
  26. $interbreadcrumb[] = ['url' => 'access_urls.php', 'name' => get_lang('MultipleAccessURLs')];
  27. $add_type = 'multiple';
  28. if (isset($_REQUEST['add_type']) && $_REQUEST['add_type'] != '') {
  29. $add_type = Security::remove_XSS($_REQUEST['add_type']);
  30. }
  31. $access_url_id = 1;
  32. if (isset($_REQUEST['access_url_id']) && $_REQUEST['access_url_id'] != '') {
  33. $access_url_id = Security::remove_XSS($_REQUEST['access_url_id']);
  34. }
  35. $xajax->processRequests();
  36. $htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/');
  37. $htmlHeadXtra[] = '<script>
  38. function add_user_to_url(code, content) {
  39. document.getElementById("user_to_add").value = "";
  40. document.getElementById("ajax_list_users").innerHTML = "";
  41. destination = document.getElementById("destination_users");
  42. destination.options[destination.length] = new Option(content,code);
  43. destination.selectedIndex = -1;
  44. sortOptions(destination.options);
  45. }
  46. function send() {
  47. if (document.formulaire.access_url_id.value!=0) {
  48. document.formulaire.form_sent.value=0;
  49. document.formulaire.add_type.value=\''.$add_type.'\';
  50. document.formulaire.submit();
  51. }
  52. }
  53. function remove_item(origin) {
  54. for(var i = 0 ; i<origin.options.length ; i++) {
  55. if(origin.options[i].selected) {
  56. origin.options[i]=null;
  57. i = i-1;
  58. }
  59. }
  60. }
  61. </script>';
  62. $errorMsg = '';
  63. $message = '';
  64. if (isset($_POST['form_sent']) && $_POST['form_sent']) {
  65. $form_sent = $_POST['form_sent'];
  66. $UserList = $_POST['sessionUsersList'];
  67. if (!is_array($UserList)) {
  68. $UserList = [];
  69. }
  70. if ($form_sent == 1) {
  71. if ($access_url_id == 0) {
  72. Display::addFlash(Display::return_message(get_lang('SelectURL')));
  73. header('Location: access_url_edit_users_to_url.php');
  74. exit;
  75. } elseif (is_array($UserList)) {
  76. $result = UrlManager::update_urls_rel_user($UserList, $access_url_id);
  77. $url_info = UrlManager::get_url_data_from_id($access_url_id);
  78. if (!empty($result)) {
  79. $message .= 'URL: '.$url_info['url'].'<br />';
  80. }
  81. if (!empty($result['users_added'])) {
  82. $message .= '<h4>'.get_lang('UsersAdded').':</h4>';
  83. $i = 1;
  84. $user_added_list = [];
  85. foreach ($result['users_added'] as $user) {
  86. $user_info = api_get_user_info($user);
  87. if (!empty($user_info)) {
  88. $user_added_list[] = $i.'. '.api_get_person_name($user_info['firstname'], $user_info['lastname'], null, null, null, $user_info['username']);
  89. $i++;
  90. }
  91. }
  92. if (!empty($user_added_list)) {
  93. $message .= implode(', ', $user_added_list);
  94. }
  95. }
  96. if (!empty($result['users_deleted'])) {
  97. $message .= '<br /><h4>'.get_lang('UsersDeleted').': </h4>';
  98. $user_deleted_list = [];
  99. $i = 1;
  100. foreach ($result['users_deleted'] as $user) {
  101. $user_info = api_get_user_info($user);
  102. if (!empty($user_info)) {
  103. $user_deleted_list[] = $i.'. '.api_get_person_name($user_info['firstname'], $user_info['lastname']);
  104. $i++;
  105. }
  106. }
  107. if (!empty($user_deleted_list)) {
  108. $message .= implode(', ', $user_deleted_list);
  109. }
  110. }
  111. }
  112. }
  113. }
  114. Display::display_header($tool_name);
  115. if (!empty($message)) {
  116. echo Display::return_message($message, 'normal', false);
  117. }
  118. echo '<div class="actions">';
  119. echo Display::url(
  120. Display::return_icon('view_more_stats.gif', get_lang('AddUserToURL'), ''),
  121. api_get_path(WEB_CODE_PATH).'admin/access_url_add_users_to_url.php'
  122. );
  123. echo '</div>';
  124. api_display_tool_title($tool_name);
  125. $nosessionUsersList = $sessionUsersList = [];
  126. $ajax_search = $add_type == 'unique' ? true : false;
  127. if ($ajax_search) {
  128. $Users = UrlManager::get_url_rel_user_data($access_url_id);
  129. foreach ($Users as $user) {
  130. $sessionUsersList[$user['user_id']] = $user;
  131. }
  132. } else {
  133. $order_clause = api_sort_by_first_name() ? ' ORDER BY username, firstname, lastname' : ' ORDER BY username, lastname, firstname';
  134. $Users = UrlManager::get_url_rel_user_data(null, $order_clause);
  135. foreach ($Users as $user) {
  136. if ($user['access_url_id'] == $access_url_id) {
  137. $sessionUsersList[$user['user_id']] = $user;
  138. }
  139. }
  140. $sql = "SELECT u.user_id, lastname, firstname, username
  141. FROM $tbl_user u WHERE status <> ".ANONYMOUS." ".
  142. $order_clause;
  143. $result = Database::query($sql);
  144. $Users = Database::store_result($result);
  145. $user_list_leys = array_keys($sessionUsersList);
  146. foreach ($Users as $user) {
  147. if (!in_array($user['user_id'], $user_list_leys)) {
  148. $nosessionUsersList[$user['user_id']] = $user;
  149. }
  150. }
  151. }
  152. if ($add_type == 'multiple') {
  153. $link_add_type_unique = '<a href="'.api_get_self().'?add_type=unique&access_url_id='.$access_url_id.'">'.get_lang('SessionAddTypeUnique').'</a>';
  154. $link_add_type_multiple = get_lang('SessionAddTypeMultiple');
  155. } else {
  156. $link_add_type_unique = get_lang('SessionAddTypeUnique');
  157. $link_add_type_multiple = '<a href="'.api_get_self().'?add_type=multiple&access_url_id='.$access_url_id.'">'.get_lang('SessionAddTypeMultiple').'</a>';
  158. }
  159. $url_list = UrlManager::get_url_data();
  160. ?>
  161. <div style="text-align: left;">
  162. <?php echo $link_add_type_unique; ?>&nbsp;|&nbsp;<?php echo $link_add_type_multiple; ?>
  163. </div>
  164. <br /><br />
  165. <form name="formulaire" method="post" action="<?php echo api_get_self(); ?>" style="margin:0px;" <?php if ($ajax_search) {
  166. echo ' onsubmit="valide();"';
  167. } ?> >
  168. <?php echo get_lang('SelectUrl').' : '; ?>
  169. <select name="access_url_id" onchange="javascript:send();">
  170. <option value="0"> <?php echo get_lang('SelectUrl'); ?></option>
  171. <?php
  172. $url_selected = '';
  173. foreach ($url_list as $url_obj) {
  174. $checked = '';
  175. if (!empty($access_url_id)) {
  176. if ($url_obj['id'] == $access_url_id) {
  177. $checked = 'selected=true';
  178. $url_selected = $url_obj[1];
  179. }
  180. }
  181. if ($url_obj['active'] == 1) {
  182. ?>
  183. <option <?php echo $checked; ?> value="<?php echo $url_obj[0]; ?>"> <?php echo $url_obj[1]; ?></option>
  184. <?php
  185. }
  186. }
  187. ?>
  188. </select>
  189. <br /><br />
  190. <input type="hidden" name="form_sent" value="1" />
  191. <input type="hidden" name="add_type" value = "<?php echo $add_type; ?>" />
  192. <?php
  193. if (!empty($errorMsg)) {
  194. echo Display::return_message($errorMsg, 'normal'); //main API
  195. }
  196. ?>
  197. <table border="0" cellpadding="5" cellspacing="0" width="100%">
  198. <tr>
  199. <td>
  200. <h3>
  201. <?php
  202. $total_users = count($nosessionUsersList) + count($sessionUsersList);
  203. echo get_lang('TotalAvailableUsers').' '.$total_users;
  204. ?>
  205. </h3>
  206. </td>
  207. </tr>
  208. <tr>
  209. <td align="center"><b><?php echo get_lang('UserListInPlatform'); ?> : <?php echo count($nosessionUsersList); ?></b>
  210. </td>
  211. <td></td>
  212. <td align="center"><b><?php echo get_lang('UserListIn').' '.$url_selected; ?> : <?php echo count($sessionUsersList); ?></b></td>
  213. </tr>
  214. <tr>
  215. <td align="center">
  216. <div id="content_source">
  217. <?php if ($ajax_search) {
  218. ?>
  219. <input type="text" id="user_to_add" onkeyup="xajax_search_users(this.value,document.formulaire.access_url_id.options[document.formulaire.access_url_id.selectedIndex].value)" />
  220. <div id="ajax_list_users"></div>
  221. <?php
  222. } else {
  223. ?>
  224. <select id="origin_users" name="nosessionUsersList[]" multiple="multiple" size="15" style="width:380px;">
  225. <?php
  226. foreach ($nosessionUsersList as $enreg) {
  227. ?>
  228. <option value="<?php echo $enreg['user_id']; ?>"><?php echo $enreg['username'].' - '.api_get_person_name($enreg['firstname'], $enreg['lastname']); ?></option>
  229. <?php
  230. }
  231. unset($nosessionUsersList); ?>
  232. </select>
  233. <?php
  234. }
  235. ?>
  236. </div>
  237. </td>
  238. <td width="10%" valign="middle" align="center">
  239. <?php if ($ajax_search) {
  240. ?>
  241. <button class="btn btn-default" type="button" onclick="remove_item(document.getElementById('destination_users'))">
  242. <em class="fa fa-arrow-left"></em>
  243. </button>
  244. <?php
  245. } else {
  246. ?>
  247. <button class="btn btn-default" type="button" onclick="moveItem(document.getElementById('origin_users'), document.getElementById('destination_users'))" >
  248. <em class="fa fa-arrow-right"></em>
  249. </button>
  250. <br /><br />
  251. <button class="btn btn-default" type="button" onclick="moveItem(document.getElementById('destination_users'), document.getElementById('origin_users'))" >
  252. <em class="fa fa-arrow-left"></em>
  253. </button>
  254. <?php
  255. } ?>
  256. <br /><br /><br /><br /><br /><br />
  257. </td>
  258. <td align="center">
  259. <select id="destination_users" name="sessionUsersList[]" multiple="multiple" size="15" style="width:380px;">
  260. <?php
  261. foreach ($sessionUsersList as $enreg) {
  262. ?>
  263. <option value="<?php echo $enreg['user_id']; ?>">
  264. <?php echo $enreg['username'].' - '.api_get_person_name($enreg['firstname'], $enreg['lastname']); ?>
  265. </option>
  266. <?php
  267. }
  268. unset($sessionUsersList);
  269. ?>
  270. </select></td>
  271. </tr>
  272. <tr>
  273. <td colspan="3" align="center">
  274. <br />
  275. <?php
  276. if (isset($_GET['add'])) {
  277. echo '<button class="save" type="button" onclick="valide()" >'.get_lang('AddUsersToURL').'</button>';
  278. } else {
  279. echo '<button class="save" type="button" onclick="valide()" >'.get_lang('EditUsersToURL').'</button>';
  280. }
  281. ?>
  282. </td>
  283. </tr>
  284. </table>
  285. </form>
  286. <script>
  287. function moveItem(origin , destination) {
  288. for(var i = 0 ; i<origin.options.length ; i++) {
  289. if(origin.options[i].selected) {
  290. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  291. origin.options[i]=null;
  292. i = i-1;
  293. }
  294. }
  295. destination.selectedIndex = -1;
  296. sortOptions(destination.options);
  297. }
  298. function sortOptions(options) {
  299. newOptions = new Array();
  300. for (i = 0 ; i<options.length ; i++)
  301. newOptions[i] = options[i];
  302. newOptions = newOptions.sort(mysort);
  303. options.length = 0;
  304. for(i = 0 ; i < newOptions.length ; i++)
  305. options[i] = newOptions[i];
  306. }
  307. function mysort(a, b) {
  308. if(a.text.toLowerCase() > b.text.toLowerCase()){
  309. return 1;
  310. }
  311. if(a.text.toLowerCase() < b.text.toLowerCase()){
  312. return -1;
  313. }
  314. return 0;
  315. }
  316. function valide() {
  317. var options = document.getElementById('destination_users').options;
  318. for (i = 0 ; i<options.length ; i++)
  319. options[i].selected = true;
  320. document.forms.formulaire.submit();
  321. }
  322. function loadUsersInSelect(select) {
  323. var xhr_object = null;
  324. if(window.XMLHttpRequest) // Firefox
  325. xhr_object = new XMLHttpRequest();
  326. else if(window.ActiveXObject) // Internet Explorer
  327. xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
  328. else // XMLHttpRequest non supporté par le navigateur
  329. alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
  330. xhr_object.open("POST", "loadUsersInSelect.ajax.php");
  331. xhr_object.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  332. nosessionUsers = makepost(document.getElementById('origin_users'));
  333. sessionUsers = makepost(document.getElementById('destination_users'));
  334. nosessionClasses = makepost(document.getElementById('origin_classes'));
  335. sessionClasses = makepost(document.getElementById('destination_classes'));
  336. xhr_object.send("nosessionusers="+nosessionUsers+"&sessionusers="+sessionUsers+"&nosessionclasses="+nosessionClasses+"&sessionclasses="+sessionClasses);
  337. xhr_object.onreadystatechange = function() {
  338. if (xhr_object.readyState == 4) {
  339. document.getElementById('content_source').innerHTML = result = xhr_object.responseText;
  340. }
  341. }
  342. }
  343. function makepost(select){
  344. var options = select.options;
  345. var ret = "";
  346. for (i = 0 ; i<options.length ; i++)
  347. ret = ret + options[i].value +'::'+options[i].text+";;";
  348. return ret;
  349. }
  350. </script>
  351. <?php
  352. Display::display_footer();