access_url_edit_users_to_url.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Framework\Container;
  4. /**
  5. * @package chamilo.admin
  6. * @author Julio Montoya <gugli100@gmail.com>
  7. */
  8. // resetting the course id
  9. $cidReset = true;
  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' => Container::getRouter()->generate('administration'), '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. 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 = array();
  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, 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 = array();
  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. Display::display_normal_message($message, 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 = array();
  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. Display::display_normal_message($errorMsg); //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. <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)" />
  223. <div id="ajax_list_users"></div>
  224. <?php } else { ?>
  225. <select id="origin_users" name="nosessionUsersList[]" multiple="multiple" size="15" style="width:380px;">
  226. <?php
  227. foreach ($nosessionUsersList as $enreg) {
  228. ?>
  229. <option value="<?php echo $enreg['user_id']; ?>"><?php echo $enreg['username'].' - '.api_get_person_name($enreg['firstname'], $enreg['lastname']); ?></option>
  230. <?php
  231. }
  232. unset($nosessionUsersList);
  233. ?>
  234. </select>
  235. <?php
  236. }
  237. ?>
  238. </div>
  239. </td>
  240. <td width="10%" valign="middle" align="center">
  241. <?php if ($ajax_search) { ?>
  242. <button class="btn btn-default" type="button" onclick="remove_item(document.getElementById('destination_users'))">
  243. <em class="fa fa-arrow-left"></em>
  244. </button>
  245. <?php } else { ?>
  246. <button class="btn btn-default" type="button" onclick="moveItem(document.getElementById('origin_users'), document.getElementById('destination_users'))" >
  247. <em class="fa fa-arrow-right"></em>
  248. </button>
  249. <br /><br />
  250. <button class="btn btn-default" type="button" onclick="moveItem(document.getElementById('destination_users'), document.getElementById('origin_users'))" >
  251. <em class="fa fa-arrow-left"></em>
  252. </button>
  253. <?php } ?>
  254. <br /><br /><br /><br /><br /><br />
  255. </td>
  256. <td align="center">
  257. <select id="destination_users" name="sessionUsersList[]" multiple="multiple" size="15" style="width:380px;">
  258. <?php
  259. foreach ($sessionUsersList as $enreg) {
  260. ?>
  261. <option value="<?php echo $enreg['user_id']; ?>">
  262. <?php echo $enreg['username'].' - '.api_get_person_name($enreg['firstname'], $enreg['lastname']); ?>
  263. </option>
  264. <?php
  265. }
  266. unset($sessionUsersList);
  267. ?>
  268. </select></td>
  269. </tr>
  270. <tr>
  271. <td colspan="3" align="center">
  272. <br />
  273. <?php
  274. if (isset($_GET['add'])) {
  275. echo '<button class="save" type="button" onclick="valide()" >' . get_lang('AddUsersToURL') . '</button>';
  276. } else {
  277. echo '<button class="save" type="button" onclick="valide()" >' . get_lang('EditUsersToURL') . '</button>';
  278. }
  279. ?>
  280. </td>
  281. </tr>
  282. </table>
  283. </form>
  284. <script>
  285. function moveItem(origin , destination) {
  286. for(var i = 0 ; i<origin.options.length ; i++) {
  287. if(origin.options[i].selected) {
  288. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  289. origin.options[i]=null;
  290. i = i-1;
  291. }
  292. }
  293. destination.selectedIndex = -1;
  294. sortOptions(destination.options);
  295. }
  296. function sortOptions(options) {
  297. newOptions = new Array();
  298. for (i = 0 ; i<options.length ; i++)
  299. newOptions[i] = options[i];
  300. newOptions = newOptions.sort(mysort);
  301. options.length = 0;
  302. for(i = 0 ; i < newOptions.length ; i++)
  303. options[i] = newOptions[i];
  304. }
  305. function mysort(a, b) {
  306. if(a.text.toLowerCase() > b.text.toLowerCase()){
  307. return 1;
  308. }
  309. if(a.text.toLowerCase() < b.text.toLowerCase()){
  310. return -1;
  311. }
  312. return 0;
  313. }
  314. function valide() {
  315. var options = document.getElementById('destination_users').options;
  316. for (i = 0 ; i<options.length ; i++)
  317. options[i].selected = true;
  318. document.forms.formulaire.submit();
  319. }
  320. function loadUsersInSelect(select) {
  321. var xhr_object = null;
  322. if(window.XMLHttpRequest) // Firefox
  323. xhr_object = new XMLHttpRequest();
  324. else if(window.ActiveXObject) // Internet Explorer
  325. xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
  326. else // XMLHttpRequest non supporté par le navigateur
  327. alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
  328. xhr_object.open("POST", "loadUsersInSelect.ajax.php");
  329. xhr_object.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  330. nosessionUsers = makepost(document.getElementById('origin_users'));
  331. sessionUsers = makepost(document.getElementById('destination_users'));
  332. nosessionClasses = makepost(document.getElementById('origin_classes'));
  333. sessionClasses = makepost(document.getElementById('destination_classes'));
  334. xhr_object.send("nosessionusers="+nosessionUsers+"&sessionusers="+sessionUsers+"&nosessionclasses="+nosessionClasses+"&sessionclasses="+sessionClasses);
  335. xhr_object.onreadystatechange = function() {
  336. if (xhr_object.readyState == 4) {
  337. document.getElementById('content_source').innerHTML = result = xhr_object.responseText;
  338. }
  339. }
  340. }
  341. function makepost(select){
  342. var options = select.options;
  343. var ret = "";
  344. for (i = 0 ; i<options.length ; i++)
  345. ret = ret + options[i].value +'::'+options[i].text+";;";
  346. return ret;
  347. }
  348. </script>
  349. <?php
  350. Display::display_footer();