add_courses_to_session.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. $langFile='admin';
  25. $cidReset=true;
  26. require('../inc/global.inc.php');
  27. api_protect_admin_script();
  28. $id_session=intval($_GET['id_session']);
  29. $formSent=0;
  30. $errorMsg=$firstLetterCourse=$firstLetterSession='';
  31. $CourseList=$SessionList=array();
  32. $courses=$sessions=array();
  33. $tbl_session_rel_course_rel_user=Database::get_main_table(MAIN_SESSION_COURSE_USER_TABLE);
  34. $tbl_session=Database::get_main_table(MAIN_SESSION_TABLE);
  35. $tbl_session_rel_user=Database::get_main_table(MAIN_SESSION_USER_TABLE);
  36. $tbl_session_rel_course=Database::get_main_table(MAIN_SESSION_COURSE_TABLE);
  37. $tbl_course=Database::get_main_table(MAIN_COURSE_TABLE);
  38. $noPHP_SELF=true;
  39. $tool_name= get_lang('SubscribeCoursesToSession');
  40. $interbreadcrumb[]=array("url" => "index.php","name" => get_lang('AdministrationTools'));
  41. $interbreadcrumb[]=array("url" => "session_list.php","name" => "Liste des sessions");
  42. if($_POST['formSent'])
  43. {
  44. $formSent=$_POST['formSent'];
  45. $firstLetterCourse=$_POST['firstLetterCourse'];
  46. $firstLetterSession=$_POST['firstLetterSession'];
  47. $CourseList=$_POST['SessionCoursesList'];
  48. if(!is_array($CourseList))
  49. {
  50. $CourseList=array();
  51. }
  52. $nbr_courses=0;
  53. $id_coach = api_sql_query("SELECT id_coach FROM $tbl_session WHERE id=$id_session");
  54. $id_coach = mysql_fetch_array($id_coach);
  55. $id_coach = $id_coach[0];
  56. $rs = api_sql_query("SELECT course_code FROM $tbl_session_rel_course WHERE id_session=$id_session");
  57. $existingCourses = api_store_result($rs);
  58. $sql="SELECT id_user
  59. FROM $tbl_session_rel_user
  60. WHERE id_session = $id_session";
  61. $result=api_sql_query($sql,__FILE__,__LINE__);
  62. $UserList=api_store_result($result);
  63. foreach($CourseList as $enreg_course)
  64. {
  65. $exists = false;
  66. foreach($existingCourses as $existingCourse){
  67. if($enreg_course == $existingCourse['course_code']){
  68. $exists=true;
  69. }
  70. }
  71. if(!$exists){
  72. api_sql_query("INSERT INTO $tbl_session_rel_course(id_session,course_code, id_coach) VALUES('$id_session','$enreg_course','$id_coach')",__FILE__,__LINE__);
  73. $nbr_users=0;
  74. foreach($UserList as $enreg_user)
  75. {
  76. $enreg_user = $enreg_user['id_user'];
  77. 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__);
  78. if(mysql_affected_rows())
  79. {
  80. $nbr_users++;
  81. }
  82. }
  83. 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__);
  84. }
  85. }
  86. foreach($existingCourses as $existingCourse) {
  87. if(!in_array($existingCourse['course_code'], $CourseList)){
  88. api_sql_query("DELETE FROM $tbl_session_rel_course WHERE course_code='".$existingCourse['course_code']."' AND id_session=$id_session");
  89. api_sql_query("DELETE FROM $tbl_session_rel_course_rel_user WHERE course_code='".$existingCourse['course_code']."' AND id_session=$id_session");
  90. }
  91. }
  92. $nbr_courses=count($CourseList);
  93. api_sql_query("UPDATE $tbl_session SET nbr_courses=$nbr_courses WHERE id='$id_session'",__FILE__,__LINE__);
  94. if(isset($_GET['add']))
  95. header('Location: add_users_to_session.php?id_session='.$id_session.'&add=true');
  96. else
  97. header('Location: resume_session.php?id_session='.$id_session);
  98. //header('Location: '.$_GET['page'].'?id_session='.$id_session);
  99. }
  100. Display::display_header($tool_name);
  101. api_display_tool_title($tool_name);
  102. $sql="SELECT code, title, visual_code, id_session
  103. FROM $tbl_course
  104. LEFT JOIN $tbl_session_rel_course
  105. ON code = course_code
  106. ORDER BY ".(sizeof($courses)?"(code IN(".implode(',',$courses).")) DESC,":"")." title";
  107. $result=api_sql_query($sql,__FILE__,__LINE__);
  108. $Courses=api_store_result($result);
  109. $nosessionCourses = $sessionCourses = array();
  110. foreach($Courses as $course)
  111. if($course['id_session'] == $id_session)
  112. $sessionCourses[$course['code']] = $course ;
  113. foreach($Courses as $course)
  114. if(empty($sessionCourses[$course['code']]) && empty($nosessionCourses[$course['code']]))
  115. $nosessionCourses[$course['code']] = $course ;
  116. unset($Courses);
  117. ?>
  118. <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;">
  119. <input type="hidden" name="formSent" value="1" />
  120. <?php
  121. if(!empty($errorMsg))
  122. {
  123. Display::display_normal_message($errorMsg); //main API
  124. }
  125. ?>
  126. <table border="0" cellpadding="5" cellspacing="0" width="100%" align="center">
  127. <tr>
  128. <td width="45%" align="center"><b><?php echo get_lang('CourseListInPlatform') ?> :</b></td>
  129. <td width="10%">&nbsp;</td>
  130. <td align="center" width="45%"><b><?php echo get_lang('CourseListInSession') ?> :</b></td>
  131. </tr>
  132. <tr>
  133. <td width="45%" align="center"><select id="origin" name="NoSessionCoursesList[]" multiple="multiple" size="20" style="width:300px;">
  134. <?php
  135. foreach($nosessionCourses as $enreg)
  136. {
  137. ?>
  138. <option value="<?php echo $enreg['code']; ?>" <?php if(in_array($enreg['code'],$CourseList)) echo 'selected="selected"'; ?>><?php echo $enreg['title'].' ('.$enreg['visual_code'].')'; ?></option>
  139. <?php
  140. }
  141. unset($nosessionCourses);
  142. ?>
  143. </select></td>
  144. <td width="10%" valign="middle" align="center">
  145. <input type="button" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))" value=">>" />
  146. <br /><br />
  147. <input type="button" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))" value="<<" />
  148. <br /><br /><br /><br /><br /><br />
  149. <?php
  150. if(isset($_GET['add']))
  151. {
  152. echo '<input type="button" value="'.get_lang('NextStep').'" onclick="valide()" />';
  153. }
  154. else
  155. {
  156. echo '<input type="button" value="'.get_lang('Ok').'" onclick="valide()" />';
  157. }
  158. ?>
  159. </td>
  160. <td width="45%" align="center"><select id='destination' name="SessionCoursesList[]" multiple="multiple" size="20" style="width:300px;">
  161. <?php
  162. foreach($sessionCourses as $enreg)
  163. {
  164. ?>
  165. <option value="<?php echo $enreg['code']; ?>"><?php echo $enreg['title'].' ('.$enreg['visual_code'].')'; ?></option>
  166. <?php
  167. }
  168. unset($sessionCourses);
  169. ?>
  170. </select></td>
  171. </tr>
  172. </table>
  173. </form>
  174. <script type="text/javascript">
  175. <!--
  176. function moveItem(origin , destination){
  177. for(var i = 0 ; i<origin.options.length ; i++) {
  178. if(origin.options[i].selected) {
  179. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  180. origin.options[i]=null;
  181. i = i-1;
  182. }
  183. }
  184. destination.selectedIndex = -1;
  185. sortOptions(destination.options);
  186. }
  187. function sortOptions(options) {
  188. newOptions = new Array();
  189. for (i = 0 ; i<options.length ; i++)
  190. newOptions[i] = options[i];
  191. newOptions = newOptions.sort(mysort);
  192. options.length = 0;
  193. for(i = 0 ; i < newOptions.length ; i++)
  194. options[i] = newOptions[i];
  195. }
  196. function mysort(a, b){
  197. if(a.text.toLowerCase() > b.text.toLowerCase()){
  198. return 1;
  199. }
  200. if(a.text.toLowerCase() < b.text.toLowerCase()){
  201. return -1;
  202. }
  203. return 0;
  204. }
  205. function valide(){
  206. var options = document.getElementById('destination').options;
  207. for (i = 0 ; i<options.length ; i++)
  208. options[i].selected = true;
  209. document.forms.formulaire.submit();
  210. }
  211. -->
  212. </script>
  213. <?php
  214. /*
  215. ==============================================================================
  216. FOOTER
  217. ==============================================================================
  218. */
  219. Display::display_footer();
  220. ?>