session_course_user.php 7.8 KB

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