add_many_session_to_category.php 10 KB

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