add_many_session_to_category.php 11 KB

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