access_url_edit_users_to_url.php 13 KB

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