add_many_session_to_category.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. *
  6. * @todo use formvalidator
  7. */
  8. // resetting the course id
  9. $cidReset = true;
  10. require_once __DIR__.'/../inc/global.inc.php';
  11. $xajax = new xajax();
  12. $xajax->registerFunction('search_courses');
  13. // setting the section (for the tabs)
  14. $this_section = SECTION_PLATFORM_ADMIN;
  15. // setting breadcrumbs
  16. $interbreadcrumb[] = ['url' => 'session_list.php', 'name' => get_lang('Session list')];
  17. // Database Table Definitions
  18. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  19. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  20. $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
  21. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  22. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  23. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  24. // setting the name of the tool
  25. $tool_name = get_lang('Subscription sessions in the category');
  26. $add_type = 'multiple';
  27. if (isset($_GET['add_type']) && $_GET['add_type'] != '') {
  28. $add_type = Security::remove_XSS($_REQUEST['add_type']);
  29. }
  30. if (!api_is_platform_admin() && !api_is_session_admin()) {
  31. api_not_allowed(true);
  32. }
  33. $xajax->processRequests();
  34. $htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/');
  35. $htmlHeadXtra[] = '
  36. <script>
  37. function add_course_to_session (code, content) {
  38. document.getElementById("course_to_add").value = "";
  39. document.getElementById("ajax_list_courses_single").innerHTML = "";
  40. destination = document.getElementById("destination");
  41. for (i=0;i<destination.length;i++) {
  42. if(destination.options[i].text == content) {
  43. return false;
  44. }
  45. }
  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.CategorySessionId.value!=0) {
  52. document.formulaire.formSent.value=0;
  53. document.formulaire.submit();
  54. }
  55. }
  56. function remove_item(origin)
  57. {
  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. $formSent = 0;
  67. $errorMsg = $firstLetterCourse = $firstLetterSession = '';
  68. $CourseList = $SessionList = [];
  69. $courses = $sessions = [];
  70. $categoryId = isset($_POST['CategorySessionId']) ? intval($_POST['CategorySessionId']) : null;
  71. if (isset($_POST['formSent']) && $_POST['formSent']) {
  72. $formSent = $_POST['formSent'];
  73. $sessionCategoryList = $_POST['SessionCategoryList'];
  74. if ($categoryId != 0 && count($sessionCategoryList) > 0) {
  75. // Removing all
  76. $sql = "UPDATE $tbl_session SET session_category_id = NULL WHERE session_category_id = $categoryId";
  77. Database::query($sql);
  78. // Adding new
  79. $sessionCategoryList = array_map('intval', $sessionCategoryList);
  80. $session_id = join(',', $sessionCategoryList);
  81. $sql = "UPDATE $tbl_session SET session_category_id = $categoryId WHERE id in ($session_id) ";
  82. Database::query($sql);
  83. header('Location: add_many_session_to_category.php?id_category='.$categoryId.'&msg=ok');
  84. exit;
  85. } else {
  86. header('Location: add_many_session_to_category.php?msg=error');
  87. exit;
  88. }
  89. }
  90. if (isset($_GET['id_category'])) {
  91. $categoryId = intval($_GET['id_category']);
  92. }
  93. if (isset($_GET['msg']) && $_GET['msg'] == 'error') {
  94. $errorMsg = get_lang('Select category and sessions');
  95. }
  96. if (isset($_GET['msg']) && $_GET['msg'] == 'ok') {
  97. $OkMsg = get_lang('Category update');
  98. }
  99. $page = isset($_GET['page']) ? Security::remove_XSS($_GET['page']) : null;
  100. Display::display_header($tool_name);
  101. $where = '';
  102. $rows_category_session = [];
  103. if ((isset($_POST['CategorySessionId']) && $_POST['formSent'] == 0) || isset($_GET['id_category'])) {
  104. $where = 'WHERE session_category_id != '.$categoryId.' OR session_category_id IS NULL';
  105. $sql = 'SELECT id, name FROM '.$tbl_session.' WHERE session_category_id ='.$categoryId.' ORDER BY name';
  106. $result = Database::query($sql);
  107. $rows_category_session = Database::store_result($result);
  108. }
  109. $rows_session_category = SessionManager::get_all_session_category();
  110. if (empty($rows_session_category)) {
  111. echo Display::return_message(get_lang('You need to add a session category first'), 'warning');
  112. Display::display_footer();
  113. exit;
  114. }
  115. if (api_get_multiple_access_url()) {
  116. $table_access_url_rel_session = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  117. $access_url_id = api_get_current_access_url_id();
  118. $sql = "SELECT s.id, s.name FROM $tbl_session s INNER JOIN $table_access_url_rel_session u ON s.id = u.session_id $where AND u.access_url_id = $access_url_id ORDER BY name";
  119. } else {
  120. $sql = "SELECT id, name FROM $tbl_session $where ORDER BY name";
  121. }
  122. $result = Database::query($sql);
  123. $rows_session = Database::store_result($result);
  124. ?>
  125. <form name="formulaire" method="post"
  126. action="<?php echo api_get_self(); ?>?page=<?php echo $page;
  127. if (!empty($_GET['add'])) {
  128. echo '&add=true';
  129. } ?>" style="margin:0px;">
  130. <?php echo '<legend>'.$tool_name.'</legend>'; ?>
  131. <input type="hidden" name="formSent" value="1"/>
  132. <?php
  133. if (!empty($errorMsg)) {
  134. echo Display::return_message($errorMsg, 'error'); //main API
  135. }
  136. if (!empty($OkMsg)) {
  137. echo Display::return_message($OkMsg, 'confirm'); //main API
  138. }
  139. /*
  140. *
  141. * The a/b/c Filter is not a priority
  142. *
  143. * <td width="45%" align="center">
  144. <?php echo get_lang('First letter (code)'); ?> :
  145. <select name="firstLetterCourse" onchange = "xajax_search_courses(this.value,'multiple')">
  146. <option value="%">--</option>
  147. <?php
  148. echo Display :: get_alphabet_options();
  149. echo Display :: get_numeric_options(0,9,'');
  150. ?>
  151. </select>
  152. </td>
  153. */
  154. ?>
  155. <table border="0" cellpadding="5" cellspacing="0" width="100%" align="center">
  156. <tr>
  157. <td align="left"></td>
  158. <td align="left"></td>
  159. <td align="center">
  160. <b><?php echo get_lang('Category name'); ?> :</b><br />
  161. <select name="CategorySessionId" style="width: 320px;" onchange="javascript:send();" >
  162. <option value="0" ></option>
  163. <?php
  164. if (!empty($rows_session_category)) {
  165. foreach ($rows_session_category as $category) {
  166. if ($category['id'] == $categoryId) {
  167. echo '<option value="'.$category['id'].'" selected>'.$category['name'].'</option>';
  168. } else {
  169. echo '<option value="'.$category['id'].'">'.$category['name'].'</option>';
  170. }
  171. }
  172. }
  173. ?>
  174. </select>
  175. </td>
  176. </tr>
  177. <tr>
  178. <td width="45%" align="center"><b><?php echo get_lang('Session listInPlatform'); ?> :</b></td>
  179. <td width="10%">&nbsp;</td>
  180. <td align="center" width="45%"><b><?php echo get_lang('Session listInCategory'); ?> :</b></td>
  181. </tr>
  182. <?php if ($add_type == 'multiple') {
  183. ?>
  184. <tr>
  185. <td>&nbsp;</td></tr>
  186. <?php
  187. } ?>
  188. <tr>
  189. <td width="45%" align="center">
  190. <div id="ajax_list_courses_multiple">
  191. <select id="origin" name="NoSessionCategoryList[]" multiple="multiple" size="20" style="width:320px;">
  192. <?php
  193. foreach ($rows_session as $enreg) {
  194. ?>
  195. <option value="<?php echo $enreg['id']; ?>" <?php echo 'title="'.htmlspecialchars($enreg['name'], ENT_QUOTES).'"';
  196. if (in_array($enreg['id'], $CourseList)) {
  197. echo 'selected="selected"';
  198. } ?>><?php echo $enreg['name']; ?></option>
  199. <?php
  200. } ?>
  201. </select></div>
  202. <?php unset($nosessionCourses); ?>
  203. </td>
  204. <td width="10%" valign="middle" align="center">
  205. <button class="btn btn-default" type="button" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))">
  206. <em class="fa fa-arrow-right"></em>
  207. </button>
  208. <br /><br />
  209. <button class="btn btn-default" type="button" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))">
  210. <em class="fa fa-arrow-left"></em>
  211. </button>
  212. <br /><br /><br /><br /><br /><br />
  213. <?php
  214. echo '<button class="btn btn-primary" type="button" value="" onclick="valide()" >'.get_lang('Subscription sessions in the category').'</button>';
  215. ?>
  216. </td>
  217. <td width="45%" align="center">
  218. <select id='destination' name="SessionCategoryList[]" multiple="multiple" size="20" style="width:320px;">
  219. <?php
  220. foreach ($rows_category_session as $enreg) {
  221. ?>
  222. <option value="<?php echo $enreg['id']; ?>" <?php echo 'title="'.htmlspecialchars($enreg['name'], ENT_QUOTES).'"';
  223. if (in_array($enreg['id'], $CourseList)) {
  224. echo 'selected="selected"';
  225. } ?>><?php echo $enreg['name']; ?></option>
  226. <?php
  227. } ?>
  228. </select></td>
  229. </tr>
  230. </table>
  231. </form>
  232. <script type="text/javascript">
  233. function moveItem(origin , destination) {
  234. for(var i = 0 ; i<origin.options.length ; i++) {
  235. if(origin.options[i].selected) {
  236. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  237. origin.options[i]=null;
  238. i = i-1;
  239. }
  240. }
  241. destination.selectedIndex = -1;
  242. sortOptions(destination.options);
  243. }
  244. function sortOptions(options) {
  245. newOptions = new Array();
  246. for (i = 0 ; i<options.length ; i++) {
  247. newOptions[i] = options[i];
  248. }
  249. newOptions = newOptions.sort(mysort);
  250. options.length = 0;
  251. for(i = 0 ; i < newOptions.length ; i++){
  252. options[i] = newOptions[i];
  253. }
  254. }
  255. function mysort(a, b){
  256. if(a.text.toLowerCase() > b.text.toLowerCase()){
  257. return 1;
  258. }
  259. if(a.text.toLowerCase() < b.text.toLowerCase()){
  260. return -1;
  261. }
  262. return 0;
  263. }
  264. function valide(){
  265. var options = document.getElementById('destination').options;
  266. for (i = 0; i < options.length; i++) {
  267. options[i].selected = true;
  268. }
  269. document.forms.formulaire.submit();
  270. }
  271. </script>
  272. <?php
  273. Display::display_footer();