access_url_edit_usergroup_to_url.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. *
  6. * @author Julio Montoya <gugli100@gmail.com>
  7. */
  8. // resetting the course id
  9. $cidReset = true;
  10. require_once __DIR__.'/../inc/global.inc.php';
  11. $xajax = new xajax();
  12. $xajax->registerFunction(['searchUserGroupAjax', 'UserGroup', 'searchUserGroupAjax']);
  13. $userGroup = new UserGroup();
  14. // Setting the section (for the tabs)
  15. $this_section = SECTION_PLATFORM_ADMIN;
  16. // Access restrictions
  17. api_protect_global_admin_script();
  18. if (!api_get_multiple_access_url()) {
  19. header('Location: index.php');
  20. exit;
  21. }
  22. // setting breadcrumbs
  23. $tool_name = get_lang('EditUserGroupToURL');
  24. $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('PlatformAdmin')];
  25. $interbreadcrumb[] = ['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[] = '
  37. <script>
  38. function add_user_to_url(code, content) {
  39. document.getElementById("course_to_add").value = "";
  40. document.getElementById("ajax_list_courses").innerHTML = "";
  41. destination = document.getElementById("destination_users");
  42. destination.options[destination.length] = new Option(content,code);
  43. destination.selectedIndex = -1;
  44. sortOptions(destination.options);
  45. }
  46. function send() {
  47. if (document.formulaire.access_url_id.value!=0) {
  48. document.formulaire.form_sent.value=0;
  49. document.formulaire.add_type.value=\''.$add_type.'\';
  50. document.formulaire.submit();
  51. }
  52. }
  53. function remove_item(origin) {
  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 = $SessionList = [];
  65. $users = $sessions = [];
  66. if (isset($_POST['form_sent']) && $_POST['form_sent']) {
  67. $form_sent = $_POST['form_sent'];
  68. $course_list = $_POST['course_list'];
  69. if (!is_array($course_list)) {
  70. $course_list = [];
  71. }
  72. if ($form_sent == 1) {
  73. if ($access_url_id == 0) {
  74. Display::addFlash(Display::return_message(get_lang('SelectURL')));
  75. header('Location: access_url_edit_users_to_url.php');
  76. } elseif (is_array($course_list)) {
  77. UrlManager::update_urls_rel_usergroup($course_list, $access_url_id);
  78. Display::addFlash(Display::return_message(get_lang('Updated')));
  79. header('Location: access_urls.php');
  80. }
  81. exit;
  82. }
  83. }
  84. Display::display_header($tool_name);
  85. echo '<div class="actions">';
  86. echo Display::url(
  87. Display::return_icon('view_more_stats.gif', get_lang('AddUserGroupToURL'), ''),
  88. api_get_path(WEB_CODE_PATH).'admin/access_url_add_usergroup_to_url.php'
  89. );
  90. echo '</div>';
  91. api_display_tool_title($tool_name);
  92. $noUserGroupList = $userGroupList = [];
  93. $ajax_search = $add_type == 'unique' ? true : false;
  94. if ($ajax_search) {
  95. $userGroups = UrlManager::get_url_rel_usergroup_data($access_url_id);
  96. foreach ($userGroups as $item) {
  97. $userGroupList[$item['id']] = $item;
  98. }
  99. } else {
  100. $userGroups = UrlManager::get_url_rel_usergroup_data();
  101. foreach ($userGroups as $item) {
  102. if ($item['access_url_id'] == $access_url_id) {
  103. $userGroupList[$item['id']] = $item;
  104. }
  105. }
  106. $noUserGroupList = $userGroup->getUserGroupNotInList(array_keys($userGroupList));
  107. }
  108. $link_add_type_unique = ['class' => 'disabled'];
  109. $link_add_type_multiple = [];
  110. if ($add_type == 'multiple') {
  111. $link_add_type_unique = [];
  112. $link_add_type_multiple = ['class' => 'disabled'];
  113. }
  114. ?>
  115. <div class="btn-toolbar">
  116. <div class="btn-group">
  117. <?php
  118. echo Display::toolbarButton(
  119. get_lang('SessionAddTypeUnique'),
  120. api_get_self().'?'.http_build_query([
  121. 'add_type' => 'unique',
  122. 'access_url_id' => $access_url_id,
  123. ]),
  124. 'file-o',
  125. 'default',
  126. $link_add_type_unique
  127. );
  128. echo Display::toolbarButton(
  129. get_lang('SessionAddTypeMultiple'),
  130. api_get_self().'?'.http_build_query([
  131. 'add_type' => 'multiple',
  132. 'access_url_id' => $access_url_id,
  133. ]),
  134. 'files-o',
  135. 'default',
  136. $link_add_type_multiple
  137. );
  138. ?>
  139. </div>
  140. </div>
  141. <?php
  142. $url_list = UrlManager::get_url_data();
  143. ?>
  144. <form
  145. name="formulaire"
  146. method="post"
  147. action="<?php echo api_get_self(); ?>"
  148. style="margin:0px;" <?php if ($ajax_search) {
  149. echo ' onsubmit="valide();"';
  150. } ?>
  151. >
  152. <div class="row">
  153. <div class="col-xs-2">
  154. <label for="access_url_id"><?php echo get_lang('SelectUrl'); ?></label>
  155. </div>
  156. <div class="col-xs-5">
  157. <select name="access_url_id" id="access_url_id" onchange="javascript:send();" class="form-control">
  158. <option value="0">-- <?php echo get_lang('SelectUrl'); ?> --</option>
  159. <?php
  160. $url_selected = '';
  161. foreach ($url_list as $url_obj) {
  162. $checked = '';
  163. if (!empty($access_url_id)) {
  164. if ($url_obj[0] == $access_url_id) {
  165. $checked = 'selected=true';
  166. $url_selected = $url_obj[1];
  167. }
  168. }
  169. if ($url_obj['active'] == 1) {
  170. ?>
  171. <option <?php echo $checked; ?>
  172. value="<?php echo $url_obj[0]; ?>"> <?php echo $url_obj[1]; ?>
  173. </option>
  174. <?php
  175. }
  176. }
  177. ?>
  178. </select>
  179. </div>
  180. </div>
  181. <input type="hidden" name="form_sent" value="1"/>
  182. <input type="hidden" name="add_type" value="<?php echo $add_type; ?>"/>
  183. <?php
  184. if (!empty($errorMsg)) {
  185. echo Display::return_message($errorMsg, 'normal'); //main API
  186. }
  187. ?>
  188. <div class="row">
  189. <div class="col-sm-5">
  190. <label for="<?php echo $ajax_search ? 'course_to_add' : 'origin_users'; ?>"><?php echo get_lang('UserGroupListInPlatform'); ?></label>
  191. <div id="content_source">
  192. <?php if ($ajax_search) {
  193. ?>
  194. <input type="text" id="course_to_add" class="form-control"
  195. onkeyup="xajax_searchUserGroupAjax(this.value,document.formulaire.access_url_id.options[document.formulaire.access_url_id.selectedIndex].value)"/>
  196. <div id="ajax_list_courses"></div>
  197. <?php
  198. } else {
  199. ?>
  200. <select id="origin_users" name="no_course_list[]" multiple="multiple" size="15" class="form-control">
  201. <?php foreach ($noUserGroupList as $noItem) {
  202. ?>
  203. <option value="<?php echo $noItem['id']; ?>"><?php echo $noItem['name']; ?></option>
  204. <?php
  205. } ?>
  206. </select>
  207. <?php
  208. } ?>
  209. </div>
  210. </div>
  211. <div class="col-sm-2 text-center">
  212. <br><br><br><br>
  213. <?php if ($ajax_search) {
  214. ?>
  215. <button class="btn btn-default" type="button"
  216. onclick="remove_item(document.getElementById('destination_users'))">
  217. <em class="fa fa-arrow-left"></em>
  218. </button>
  219. <?php
  220. } else {
  221. ?>
  222. <button class="btn btn-default" type="button"
  223. onclick="moveItem(document.getElementById('origin_users'), document.getElementById('destination_users'))">
  224. <em class="fa fa-arrow-right"></em>
  225. </button>
  226. <br/><br/>
  227. <button class="btn btn-default" type="button"
  228. onclick="moveItem(document.getElementById('destination_users'), document.getElementById('origin_users'))">
  229. <em class="fa fa-arrow-left"></em>
  230. </button>
  231. <?php
  232. } ?>
  233. </div>
  234. <div class="col-sm-5">
  235. <label for="destination_users"><?php printf(get_lang('UserGroupListInX'), $url_selected); ?></label>
  236. <select id="destination_users" name="course_list[]" multiple="multiple" size="15" class="form-control">
  237. <?php foreach ($userGroupList as $item) {
  238. ?>
  239. <option value="<?php echo $item['id']; ?>">
  240. <?php echo $item['name']; ?>
  241. </option>
  242. <?php
  243. } ?>
  244. </select>
  245. </div>
  246. </div>
  247. <div class="row">
  248. <div class="col-xs-12 text-center">
  249. <button class="save btn btn-primary" onclick="valide()">
  250. <span class="fa fa-save fa-fw" aria-hidden="true"></span>
  251. <?php echo isset($_GET['add']) ? get_lang('AddUserGroupToURL') : get_lang('EditUserGroupToURL'); ?>
  252. </button>
  253. </div>
  254. </div>
  255. </form>
  256. <script>
  257. function moveItem(origin, destination) {
  258. for (var i = 0; i < origin.options.length; i++) {
  259. if (origin.options[i].selected) {
  260. destination.options[destination.length] = new Option(origin.options[i].text, origin.options[i].value);
  261. origin.options[i] = null;
  262. i = i - 1;
  263. }
  264. }
  265. destination.selectedIndex = -1;
  266. sortOptions(destination.options);
  267. }
  268. function sortOptions(options) {
  269. newOptions = new Array();
  270. for (i = 0; i < options.length; i++)
  271. newOptions[i] = options[i];
  272. newOptions = newOptions.sort(mysort);
  273. options.length = 0;
  274. for (i = 0; i < newOptions.length; i++)
  275. options[i] = newOptions[i];
  276. }
  277. function mysort(a, b) {
  278. if (a.text.toLowerCase() > b.text.toLowerCase()) {
  279. return 1;
  280. }
  281. if (a.text.toLowerCase() < b.text.toLowerCase()) {
  282. return -1;
  283. }
  284. return 0;
  285. }
  286. function valide() {
  287. var options = document.getElementById('destination_users').options;
  288. for (i = 0; i < options.length; i++)
  289. options[i].selected = true;
  290. document.forms.formulaire.submit();
  291. }
  292. function loadUsersInSelect(select) {
  293. var xhr_object = null;
  294. if (window.XMLHttpRequest) // Firefox
  295. xhr_object = new XMLHttpRequest();
  296. else if (window.ActiveXObject) // Internet Explorer
  297. xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
  298. else // XMLHttpRequest non supporté par le navigateur
  299. alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
  300. xhr_object.open("POST", "loadUsersInSelect.ajax.php");
  301. xhr_object.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  302. nosessionUsers = makepost(document.getElementById('origin_users'));
  303. sessionUsers = makepost(document.getElementById('destination_users'));
  304. nosessionClasses = makepost(document.getElementById('origin_classes'));
  305. sessionClasses = makepost(document.getElementById('destination_classes'));
  306. xhr_object.send("nosessionusers=" + nosessionUsers + "&sessionusers=" + sessionUsers + "&nosessionclasses=" + nosessionClasses + "&sessionclasses=" + sessionClasses);
  307. xhr_object.onreadystatechange = function () {
  308. if (xhr_object.readyState == 4) {
  309. document.getElementById('content_source').innerHTML = result = xhr_object.responseText;
  310. }
  311. }
  312. }
  313. function makepost(select) {
  314. var options = select.options;
  315. var ret = "";
  316. for (i = 0; i < options.length; i++)
  317. ret = ret + options[i].value + '::' + options[i].text + ";;";
  318. return ret;
  319. }
  320. </script>
  321. <?php
  322. Display::display_footer();