access_url_edit_usergroup_to_url.php 13 KB

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