access_url_edit_users_to_url.php 11 KB

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