access_url_edit_courses_to_url.php 12 KB

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