add_many_session_to_category.php 10 KB

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