access_url_edit_courses_to_url.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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(
  13. ['search_courses', 'Accessurleditcoursestourl', 'search_courses']
  14. );
  15. // setting the section (for the tabs)
  16. $this_section = SECTION_PLATFORM_ADMIN;
  17. // Access restrictions
  18. api_protect_global_admin_script();
  19. if (!api_get_multiple_access_url()) {
  20. header('Location: index.php');
  21. exit;
  22. }
  23. // setting breadcrumbs
  24. $tool_name = get_lang('EditCoursesToURL');
  25. $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('PlatformAdmin')];
  26. $interbreadcrumb[] = ['url' => 'access_urls.php', 'name' => get_lang('MultipleAccessURLs')];
  27. $add_type = 'multiple';
  28. if (isset($_REQUEST['add_type']) && $_REQUEST['add_type'] != '') {
  29. $add_type = Security::remove_XSS($_REQUEST['add_type']);
  30. }
  31. $access_url_id = 1;
  32. if (isset($_REQUEST['access_url_id']) && $_REQUEST['access_url_id'] != '') {
  33. $access_url_id = Security::remove_XSS($_REQUEST['access_url_id']);
  34. }
  35. $xajax->processRequests();
  36. $htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/');
  37. $htmlHeadXtra[] = '<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. if (isset($_POST['form_sent']) && $_POST['form_sent']) {
  63. $form_sent = $_POST['form_sent'];
  64. $course_list = $_POST['course_list'];
  65. if (!is_array($course_list)) {
  66. $course_list = [];
  67. }
  68. if ($form_sent == 1) {
  69. if ($access_url_id == 0) {
  70. Display::addFlash(Display::return_message(get_lang('SelectURL')));
  71. header('Location: access_url_edit_users_to_url.php?');
  72. } elseif (is_array($course_list)) {
  73. UrlManager::update_urls_rel_course($course_list, $access_url_id);
  74. Display::addFlash(Display::return_message(get_lang('CoursesWereEdited')));
  75. header('Location: access_urls.php?');
  76. }
  77. exit;
  78. }
  79. }
  80. Display::display_header($tool_name);
  81. echo '<div class="actions">';
  82. echo Display::url(
  83. Display::return_icon('view_more_stats.gif', get_lang('AddUserToURL')),
  84. api_get_path(WEB_CODE_PATH).'admin/access_url_add_courses_to_url.php'
  85. );
  86. echo '</div>';
  87. api_display_tool_title($tool_name);
  88. $no_course_list = $course_list = [];
  89. $ajax_search = $add_type == 'unique' ? true : false;
  90. if ($ajax_search) {
  91. $courses = UrlManager::get_url_rel_course_data($access_url_id);
  92. foreach ($courses as $course) {
  93. $course_list[$course['c_id']] = $course;
  94. }
  95. } else {
  96. $courses = UrlManager::get_url_rel_course_data();
  97. foreach ($courses as $course) {
  98. if ($course['access_url_id'] == $access_url_id) {
  99. $course_list[$course['c_id']] = $course;
  100. }
  101. }
  102. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  103. $sql = "SELECT id, code, title
  104. FROM $tbl_course u
  105. ORDER BY title, code";
  106. $result = Database::query($sql);
  107. $courses = Database::store_result($result);
  108. $course_list_leys = array_keys($course_list);
  109. foreach ($courses as $course) {
  110. if (!in_array($course['id'], $course_list_leys)) {
  111. $no_course_list[$course['id']] = $course;
  112. }
  113. }
  114. }
  115. if ($add_type == 'multiple') {
  116. $link_add_type_unique = '<a href="'.api_get_self().'?add_type=unique&access_url_id='.$access_url_id.'">'.
  117. get_lang('SessionAddTypeUnique').'</a>';
  118. $link_add_type_multiple = get_lang('SessionAddTypeMultiple');
  119. } else {
  120. $link_add_type_unique = get_lang('SessionAddTypeUnique');
  121. $link_add_type_multiple = '<a href="'.api_get_self().'?add_type=multiple&access_url_id='.$access_url_id.'">'.
  122. get_lang('SessionAddTypeMultiple').'</a>';
  123. }
  124. $url_list = UrlManager::get_url_data();
  125. ?>
  126. <div style="text-align: left;">
  127. <?php echo $link_add_type_unique; ?>&nbsp;|&nbsp;<?php echo $link_add_type_multiple; ?>
  128. </div>
  129. <br /><br />
  130. <form name="formulaire" method="post" action="<?php echo api_get_self(); ?>" style="margin:0px;" <?php if ($ajax_search) {
  131. echo ' onsubmit="valide();"';
  132. }?> >
  133. <?php echo get_lang('SelectUrl').' : '; ?>
  134. <select name="access_url_id" onchange="javascript:send();">
  135. <option value="0">-- <?php echo get_lang('SelectUrl'); ?> -- </option>
  136. <?php
  137. $url_selected = '';
  138. foreach ($url_list as $url_obj) {
  139. $checked = '';
  140. if (!empty($access_url_id)) {
  141. if ($url_obj[0] == $access_url_id) {
  142. $checked = 'selected=true';
  143. $url_selected = $url_obj[1];
  144. }
  145. }
  146. if ($url_obj['active'] == 1) {
  147. ?>
  148. <option <?php echo $checked; ?> value="<?php echo $url_obj[0]; ?>"> <?php echo $url_obj[1]; ?></option>
  149. <?php
  150. }
  151. }
  152. ?>
  153. </select>
  154. <br /><br />
  155. <input type="hidden" name="form_sent" value="1" />
  156. <input type="hidden" name="add_type" value = "<?php echo $add_type; ?>" />
  157. <table border="0" cellpadding="5" cellspacing="0" width="100%">
  158. <!-- Users -->
  159. <tr>
  160. <td align="center"><b><?php echo get_lang('CourseListInPlatform'); ?> :</b>
  161. </td>
  162. <td></td>
  163. <td align="center"><b><?php echo get_lang('CourseListIn').' '.$url_selected; ?></b></td>
  164. </tr>
  165. <tr>
  166. <td align="center">
  167. <div id="content_source">
  168. <?php
  169. if ($ajax_search) {
  170. ?>
  171. <input type="text" id="course_to_add" onkeyup="xajax_search_courses(this.value,document.formulaire.access_url_id.options[document.formulaire.access_url_id.selectedIndex].value)" />
  172. <div id="ajax_list_courses"></div>
  173. <?php
  174. } else {
  175. ?>
  176. <select id="origin_users" name="no_course_list[]" multiple="multiple" size="15" style="width:380px;">
  177. <?php
  178. foreach ($no_course_list as $no_course) {
  179. ?>
  180. <option value="<?php echo $no_course['id']; ?>"><?php echo $no_course['title'].' ('.$no_course['code'].')'; ?></option>
  181. <?php
  182. }
  183. unset($no_course_list); ?>
  184. </select>
  185. <?php
  186. }
  187. ?>
  188. </div>
  189. </td>
  190. <td width="10%" valign="middle" align="center">
  191. <?php
  192. if ($ajax_search) {
  193. ?>
  194. <button class="btn btn-default" type="button" onclick="remove_item(document.getElementById('destination_users'))" >
  195. <em class="fa fa-arrow-left"></em>
  196. </button>
  197. <?php
  198. } else {
  199. ?>
  200. <button class="btn btn-default" type="button" onclick="moveItem(document.getElementById('origin_users'), document.getElementById('destination_users'))" >
  201. <em class="fa fa-arrow-right"></em>
  202. </button>
  203. <br /><br />
  204. <button class="btn btn-default" type="button" onclick="moveItem(document.getElementById('destination_users'), document.getElementById('origin_users'))" >
  205. <em class="fa fa-arrow-left"></em>
  206. </button>
  207. <?php
  208. }
  209. ?>
  210. <br /><br /><br /><br /><br /><br />
  211. </td>
  212. <td align="center">
  213. <select id="destination_users" name="course_list[]" multiple="multiple" size="15" style="width:380px;">
  214. <?php
  215. foreach ($course_list as $course) {
  216. $courseInfo = api_get_course_info_by_id($course['id']); ?>
  217. <option value="<?php echo $course['id']; ?>">
  218. <?php echo $course['title'].' ('.$courseInfo['code'].')'; ?>
  219. </option>
  220. <?php
  221. }
  222. unset($course_list);
  223. ?>
  224. </select>
  225. </td>
  226. </tr>
  227. <tr>
  228. <td colspan="3" align="center">
  229. <br />
  230. <?php
  231. if (isset($_GET['add'])) {
  232. echo '<button class="btn btn-default" onclick="valide()" >'.get_lang('AddCoursesToURL').'</button>';
  233. } else {
  234. echo '<button class="btn btn-default" onclick="valide()" >'.get_lang('EditCoursesToURL').'</button>';
  235. }
  236. ?>
  237. </td>
  238. </tr>
  239. </table>
  240. </form>
  241. <script>
  242. function moveItem(origin , destination) {
  243. for(var i = 0 ; i<origin.options.length ; i++) {
  244. if(origin.options[i].selected) {
  245. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  246. origin.options[i]=null;
  247. i = i-1;
  248. }
  249. }
  250. destination.selectedIndex = -1;
  251. sortOptions(destination.options);
  252. }
  253. function sortOptions(options) {
  254. newOptions = new Array();
  255. for (i = 0 ; i<options.length ; i++)
  256. newOptions[i] = options[i];
  257. newOptions = newOptions.sort(mysort);
  258. options.length = 0;
  259. for(i = 0 ; i < newOptions.length ; i++)
  260. options[i] = newOptions[i];
  261. }
  262. function mysort(a, b) {
  263. if (a.text.toLowerCase() > b.text.toLowerCase()){
  264. return 1;
  265. }
  266. if (a.text.toLowerCase() < b.text.toLowerCase()){
  267. return -1;
  268. }
  269. return 0;
  270. }
  271. function valide(){
  272. var options = document.getElementById('destination_users').options;
  273. for (i = 0 ; i<options.length ; i++)
  274. options[i].selected = true;
  275. document.forms.formulaire.submit();
  276. }
  277. function loadUsersInSelect(select) {
  278. var xhr_object = null;
  279. if(window.XMLHttpRequest) // Firefox
  280. xhr_object = new XMLHttpRequest();
  281. else if(window.ActiveXObject) // Internet Explorer
  282. xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
  283. else // XMLHttpRequest non supporté par le navigateur
  284. alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
  285. xhr_object.open("POST", "loadUsersInSelect.ajax.php");
  286. xhr_object.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  287. nosessionUsers = makepost(document.getElementById('origin_users'));
  288. sessionUsers = makepost(document.getElementById('destination_users'));
  289. nosessionClasses = makepost(document.getElementById('origin_classes'));
  290. sessionClasses = makepost(document.getElementById('destination_classes'));
  291. xhr_object.send("nosessionusers="+nosessionUsers+"&sessionusers="+sessionUsers+"&nosessionclasses="+nosessionClasses+"&sessionclasses="+sessionClasses);
  292. xhr_object.onreadystatechange = function() {
  293. if(xhr_object.readyState == 4) {
  294. document.getElementById('content_source').innerHTML = result = xhr_object.responseText;
  295. //alert(xhr_object.responseText);
  296. }
  297. }
  298. }
  299. function makepost(select){
  300. var options = select.options;
  301. var ret = "";
  302. for (i = 0 ; i<options.length ; i++)
  303. ret = ret + options[i].value +'::'+options[i].text+";;";
  304. return ret;
  305. }
  306. </script>
  307. <?php
  308. Display::display_footer();