add_courses_to_session.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004 Dokeos S.A.
  6. Copyright (c) 2003 University of Ghent (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. Copyright (c) Olivier Brouckaert
  9. For a full list of contributors, see "credits.txt".
  10. The full license can be read in "license.txt".
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15. See the GNU General Public License for more details.
  16. Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
  17. ==============================================================================
  18. */
  19. /**
  20. ==============================================================================
  21. * @package dokeos.admin
  22. ==============================================================================
  23. */
  24. // name of the language file that needs to be included
  25. $language_file='admin';
  26. // resetting the course id
  27. $cidReset=true;
  28. // including some necessary dokeos files
  29. require('../inc/global.inc.php');
  30. // setting the section (for the tabs)
  31. $this_section = SECTION_PLATFORM_ADMIN;
  32. // Access restrictions
  33. api_protect_admin_script();
  34. // setting breadcrumbs
  35. $interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  36. $interbreadcrumb[] = array('url' => 'session_list.php','name' => get_lang('SessionList'));
  37. // Database Table Definitions
  38. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  39. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  40. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  41. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  42. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  43. // setting the name of the tool
  44. $tool_name= get_lang('SubscribeCoursesToSession');
  45. $id_session=intval($_GET['id_session']);
  46. $formSent=0;
  47. $errorMsg=$firstLetterCourse=$firstLetterSession='';
  48. $CourseList=$SessionList=array();
  49. $courses=$sessions=array();
  50. $noPHP_SELF=true;
  51. if($_POST['formSent'])
  52. {
  53. $formSent=$_POST['formSent'];
  54. $firstLetterCourse=$_POST['firstLetterCourse'];
  55. $firstLetterSession=$_POST['firstLetterSession'];
  56. $CourseList=$_POST['SessionCoursesList'];
  57. if(!is_array($CourseList))
  58. {
  59. $CourseList=array();
  60. }
  61. $nbr_courses=0;
  62. $id_coach = api_sql_query("SELECT id_coach FROM $tbl_session WHERE id=$id_session");
  63. $id_coach = mysql_fetch_array($id_coach);
  64. $id_coach = $id_coach[0];
  65. $rs = api_sql_query("SELECT course_code FROM $tbl_session_rel_course WHERE id_session=$id_session");
  66. $existingCourses = api_store_result($rs);
  67. $sql="SELECT id_user
  68. FROM $tbl_session_rel_user
  69. WHERE id_session = $id_session";
  70. $result=api_sql_query($sql,__FILE__,__LINE__);
  71. $UserList=api_store_result($result);
  72. foreach($CourseList as $enreg_course)
  73. {
  74. $exists = false;
  75. foreach($existingCourses as $existingCourse)
  76. {
  77. if($enreg_course == $existingCourse['course_code'])
  78. {
  79. $exists=true;
  80. }
  81. }
  82. if(!$exists)
  83. {
  84. api_sql_query("INSERT INTO $tbl_session_rel_course(id_session,course_code, id_coach) VALUES('$id_session','$enreg_course','$id_coach')",__FILE__,__LINE__);
  85. $nbr_users=0;
  86. foreach($UserList as $enreg_user)
  87. {
  88. $enreg_user = $enreg_user['id_user'];
  89. api_sql_query("INSERT IGNORE INTO $tbl_session_rel_course_rel_user(id_session,course_code,id_user) VALUES('$id_session','$enreg_course','$enreg_user')",__FILE__,__LINE__);
  90. if(mysql_affected_rows())
  91. {
  92. $nbr_users++;
  93. }
  94. }
  95. api_sql_query("UPDATE $tbl_session_rel_course SET nbr_users=$nbr_users WHERE id_session='$id_session' AND course_code='$enreg_course'",__FILE__,__LINE__);
  96. }
  97. }
  98. foreach($existingCourses as $existingCourse) {
  99. if(!in_array($existingCourse['course_code'], $CourseList)){
  100. api_sql_query("DELETE FROM $tbl_session_rel_course WHERE course_code='".$existingCourse['course_code']."' AND id_session=$id_session");
  101. api_sql_query("DELETE FROM $tbl_session_rel_course_rel_user WHERE course_code='".$existingCourse['course_code']."' AND id_session=$id_session");
  102. }
  103. }
  104. $nbr_courses=count($CourseList);
  105. api_sql_query("UPDATE $tbl_session SET nbr_courses=$nbr_courses WHERE id='$id_session'",__FILE__,__LINE__);
  106. if(isset($_GET['add']))
  107. header('Location: add_users_to_session.php?id_session='.$id_session.'&add=true');
  108. else
  109. header('Location: resume_session.php?id_session='.$id_session);
  110. //header('Location: '.$_GET['page'].'?id_session='.$id_session);
  111. }
  112. Display::display_header($tool_name);
  113. api_display_tool_title($tool_name);
  114. $sql="SELECT code, title, visual_code, id_session
  115. FROM $tbl_course
  116. LEFT JOIN $tbl_session_rel_course
  117. ON code = course_code
  118. ORDER BY ".(sizeof($courses)?"(code IN(".implode(',',$courses).")) DESC,":"")." title";
  119. $result=api_sql_query($sql,__FILE__,__LINE__);
  120. $Courses=api_store_result($result);
  121. $nosessionCourses = $sessionCourses = array();
  122. foreach($Courses as $course)
  123. if($course['id_session'] == $id_session)
  124. $sessionCourses[$course['code']] = $course ;
  125. foreach($Courses as $course)
  126. if(empty($sessionCourses[$course['code']]) && empty($nosessionCourses[$course['code']]))
  127. $nosessionCourses[$course['code']] = $course ;
  128. unset($Courses);
  129. ?>
  130. <form name="formulaire" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>?page=<?php echo $_GET['page'] ?>&id_session=<?php echo $id_session; ?><?php if(!empty($_GET['add'])) echo '&add=true' ; ?>" style="margin:0px;">
  131. <input type="hidden" name="formSent" value="1" />
  132. <?php
  133. if(!empty($errorMsg))
  134. {
  135. Display::display_normal_message($errorMsg); //main API
  136. }
  137. ?>
  138. <table border="0" cellpadding="5" cellspacing="0" width="100%" align="center">
  139. <tr>
  140. <td width="45%" align="center"><b><?php echo get_lang('CourseListInPlatform') ?> :</b></td>
  141. <td width="10%">&nbsp;</td>
  142. <td align="center" width="45%"><b><?php echo get_lang('CourseListInSession') ?> :</b></td>
  143. </tr>
  144. <tr>
  145. <td width="45%" align="center"><select id="origin" name="NoSessionCoursesList[]" multiple="multiple" size="20" style="width:300px;">
  146. <?php
  147. foreach($nosessionCourses as $enreg)
  148. {
  149. ?>
  150. <option value="<?php echo $enreg['code']; ?>" <?php if(in_array($enreg['code'],$CourseList)) echo 'selected="selected"'; ?>><?php echo $enreg['title'].' ('.$enreg['visual_code'].')'; ?></option>
  151. <?php
  152. }
  153. unset($nosessionCourses);
  154. ?>
  155. </select></td>
  156. <td width="10%" valign="middle" align="center">
  157. <input type="button" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))" value=">>" />
  158. <br /><br />
  159. <input type="button" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))" value="<<" />
  160. <br /><br /><br /><br /><br /><br />
  161. <?php
  162. if(isset($_GET['add']))
  163. {
  164. echo '<input type="button" value="'.get_lang('NextStep').'" onclick="valide()" />';
  165. }
  166. else
  167. {
  168. echo '<input type="button" value="'.get_lang('Ok').'" onclick="valide()" />';
  169. }
  170. ?>
  171. </td>
  172. <td width="45%" align="center"><select id='destination' name="SessionCoursesList[]" multiple="multiple" size="20" style="width:300px;">
  173. <?php
  174. foreach($sessionCourses as $enreg)
  175. {
  176. ?>
  177. <option value="<?php echo $enreg['code']; ?>"><?php echo $enreg['title'].' ('.$enreg['visual_code'].')'; ?></option>
  178. <?php
  179. }
  180. unset($sessionCourses);
  181. ?>
  182. </select></td>
  183. </tr>
  184. </table>
  185. </form>
  186. <script type="text/javascript">
  187. <!--
  188. function moveItem(origin , destination){
  189. for(var i = 0 ; i<origin.options.length ; i++) {
  190. if(origin.options[i].selected) {
  191. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  192. origin.options[i]=null;
  193. i = i-1;
  194. }
  195. }
  196. destination.selectedIndex = -1;
  197. sortOptions(destination.options);
  198. }
  199. function sortOptions(options) {
  200. newOptions = new Array();
  201. for (i = 0 ; i<options.length ; i++)
  202. newOptions[i] = options[i];
  203. newOptions = newOptions.sort(mysort);
  204. options.length = 0;
  205. for(i = 0 ; i < newOptions.length ; i++)
  206. options[i] = newOptions[i];
  207. }
  208. function mysort(a, b){
  209. if(a.text.toLowerCase() > b.text.toLowerCase()){
  210. return 1;
  211. }
  212. if(a.text.toLowerCase() < b.text.toLowerCase()){
  213. return -1;
  214. }
  215. return 0;
  216. }
  217. function valide(){
  218. var options = document.getElementById('destination').options;
  219. for (i = 0 ; i<options.length ; i++)
  220. options[i].selected = true;
  221. document.forms.formulaire.submit();
  222. }
  223. -->
  224. </script>
  225. <?php
  226. /*
  227. ==============================================================================
  228. FOOTER
  229. ==============================================================================
  230. */
  231. Display::display_footer();
  232. ?>