session_course_user.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. // setting the section (for the tabs)
  7. $this_section = SECTION_PLATFORM_ADMIN;
  8. $id_session = intval($_GET['id_session']);
  9. SessionManager::protect_session_edit($id_session);
  10. // setting breadcrumbs
  11. $interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('Sessions'));
  12. $interbreadcrumb[] = array('url' => 'session_list.php','name' => get_lang('SessionList'));
  13. $interbreadcrumb[] = array('url' => 'resume_session.php?id_session='.Security::remove_XSS($_GET['id_session']),'name' => get_lang('SessionOverview'));
  14. // Database Table Definitions
  15. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  16. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  17. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  18. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  19. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  20. // setting the name of the tool
  21. $tool_name = get_lang('EditSessionCoursesByUser');
  22. $id_user = intval($_GET['id_user']);
  23. if (empty($id_user) || empty($id_session)) {
  24. header('Location: resume_session.php?id_session='.$id_session);
  25. exit;
  26. }
  27. $formSent=0;
  28. $errorMsg=$firstLetterCourse=$firstLetterSession='';
  29. $CourseList=$SessionList=array();
  30. $courses=$sessions=array();
  31. $noPHP_SELF=true;
  32. if (isset($_POST['formSent']) && $_POST['formSent']) {
  33. $formSent = $_POST['formSent'];
  34. $CourseList = $_POST['SessionCoursesList'];
  35. if (!is_array($CourseList)) {
  36. $CourseList=array();
  37. }
  38. $sql="SELECT DISTINCT course.id
  39. FROM $tbl_course course LEFT JOIN $tbl_session_rel_course session_rel_course
  40. ON course.id = session_rel_course.c_id inner join $tbl_session_rel_course_rel_user as srcru
  41. ON (srcru.id_session = session_rel_course.id_session)
  42. WHERE id_user = $id_user and session_rel_course.id_session = $id_session";
  43. $rs = Database::query($sql);
  44. $existingCourses = Database::store_result($rs);
  45. if (count($CourseList) == count($existingCourses)) {
  46. header('Location: session_course_user.php?id_session='.$id_session.'&id_user='.$id_user.'&msg='.get_lang('MaybeYouWantToDeleteThisUserFromSession'));
  47. exit;
  48. }
  49. foreach ($CourseList as $courseId) {
  50. $exists = false;
  51. foreach($existingCourses as $existingCourse) {
  52. if ($enreg_course == $existingCourse['id']) {
  53. $exists=true;
  54. }
  55. }
  56. if (!$exists) {
  57. $courseId = Database::escape_string($courseId);
  58. $sql_delete = "DELETE FROM $tbl_session_rel_course_rel_user
  59. WHERE id_user='".$id_user."' AND c_id ='".$courseId."' AND id_session = $id_session";
  60. $result = Database::query($sql_delete);
  61. if (Database::affected_rows($result)) {
  62. //update session rel course table
  63. $sql_update = "UPDATE $tbl_session_rel_course SET nbr_users= nbr_users - 1 WHERE id_session='$id_session' AND c_id = '$courseId'";
  64. Database::query($sql_update);
  65. }
  66. }
  67. }
  68. foreach ($existingCourses as $existingCourse) {
  69. if(!in_array($existingCourse['id'], $CourseList)) {
  70. $courseId = Database::escape_string($existingCourse['id']);
  71. $sql_insert = "INSERT IGNORE INTO $tbl_session_rel_course_rel_user(id_session,c_id,id_user) VALUES('$id_session','$courseId','$id_user')";
  72. $result = Database::query($sql_insert);
  73. if (Database::affected_rows($result)) {
  74. //update session rel course table
  75. $sql_update = "UPDATE $tbl_session_rel_course SET nbr_users= nbr_users + 1 WHERE id_session='$id_session' AND c_id='$courseId'";
  76. Database::query($sql_update);
  77. }
  78. }
  79. }
  80. header('Location: session_course_user.php?id_session='.$id_session.'&id_user='.$id_user.'&msg='.get_lang('CoursesUpdated'));
  81. exit;
  82. }
  83. // display the dokeos header
  84. Display::display_header($tool_name);
  85. if (!empty($_GET['msg'])) {
  86. Display::display_normal_message(urldecode($_GET['msg']));
  87. }
  88. // the form header
  89. $session_info = SessionManager::fetch($id_session);
  90. $user_info = api_get_user_info($id_user);
  91. echo '<legend>'.$tool_name.': '.$session_info['name'].' - '.$user_info['complete_name'].'</legend>';
  92. $nosessionCourses = $sessionCourses = array();
  93. // actual user
  94. $sql = "SELECT course.id, course.code, title, visual_code, srcru.id_session
  95. FROM $tbl_course course INNER JOIN $tbl_session_rel_course_rel_user as srcru
  96. ON course.id = srcru.c_id
  97. WHERE srcru.id_user = $id_user AND id_session = $id_session";
  98. //all
  99. $sql_all="SELECT course.id, code, title, visual_code, src.id_session " .
  100. "FROM $tbl_course course INNER JOIN $tbl_session_rel_course as src " .
  101. "ON course.id = src.c_id AND id_session = $id_session";
  102. $result=Database::query($sql);
  103. $Courses=Database::store_result($result);
  104. $result=Database::query($sql_all);
  105. $CoursesAll=Database::store_result($result);
  106. $course_temp = array();
  107. foreach($Courses as $course) {
  108. $course_temp[] = $course['id'];
  109. }
  110. foreach($CoursesAll as $course) {
  111. if (in_array($course['id'], $course_temp)) {
  112. $nosessionCourses[$course['id']] = $course ;
  113. } else {
  114. $sessionCourses[$course['id']] = $course ;
  115. }
  116. }
  117. unset($Courses);
  118. ?>
  119. <form name="formulaire" method="post" action="<?php echo api_get_self(); ?>?id_user=<?php echo $id_user; ?>&id_session=<?php echo $id_session; ?>" style="margin:0px;">
  120. <input type="hidden" name="formSent" value="1" />
  121. <?php
  122. if(!empty($errorMsg)) {
  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('CurrentCourses') ?> :</b></td>
  129. <td width="10%">&nbsp;</td>
  130. <td align="center" width="45%"><b><?php echo get_lang('CoursesToAvoid') ?> :</b></td>
  131. </tr>
  132. </td>
  133. <tr>
  134. <td width="45%" align="center">
  135. <div id="ajax_list_courses_multiple">
  136. <select id="origin" name="NoSessionCoursesList[]" multiple="multiple" size="20" style="width:320px;"> <?php
  137. foreach($nosessionCourses as $enreg) {
  138. ?>
  139. <option value="<?php echo $enreg['id']; ?>" <?php echo 'title="'.htmlspecialchars($enreg['title'].' ('.$enreg['visual_code'].')',ENT_QUOTES).'"'; if(in_array($enreg['code'],$CourseList)) echo 'selected="selected"'; ?>><?php echo $enreg['title'].' ('.$enreg['visual_code'].')'; ?></option>
  140. <?php
  141. }
  142. ?> </select></div> <?php
  143. unset($nosessionCourses);
  144. ?>
  145. </select></td>
  146. <td width="10%" valign="middle" align="center">
  147. <button class="arrowr" type="button" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))"></button>
  148. <br /><br />
  149. <button class="arrowl" type="button" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))"></button>
  150. <br /><br /><br /><br /><br /><br />
  151. <?php
  152. echo '<button class="save" type="button" value="" onclick="valide()" >'.get_lang('EditSessionCourses').'</button>';
  153. ?>
  154. </td>
  155. <td width="45%" align="center">
  156. <select id='destination' name="SessionCoursesList[]" multiple="multiple" size="20" style="width:320px;">
  157. <?php
  158. foreach($sessionCourses as $enreg) {
  159. ?>
  160. <option value="<?php echo $enreg['id']; ?>" title="<?php echo htmlspecialchars($enreg['title'].' ('.$enreg['visual_code'].')',ENT_QUOTES); ?>"><?php echo $enreg['title'].' ('.$enreg['visual_code'].')'; ?></option>
  161. <?php
  162. }
  163. unset($sessionCourses);
  164. ?>
  165. </select></td>
  166. </tr>
  167. </table>
  168. </form>
  169. <script>
  170. function valide(){
  171. var options = document.getElementById('destination').options;
  172. for (i = 0 ; i<options.length ; i++)
  173. options[i].selected = true;
  174. document.forms.formulaire.submit();
  175. }
  176. </script>
  177. <?php