add_many_session_to_category.php 10 KB

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