access_url_edit_users_to_url.php 13 KB

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