access_url_edit_users_to_url.php 13 KB

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