access_url_edit_users_to_url.php 13 KB

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