session_course_user.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. // resetting the course id
  7. $cidReset = true;
  8. require_once __DIR__.'/../inc/global.inc.php';
  9. // setting the section (for the tabs)
  10. $this_section = SECTION_PLATFORM_ADMIN;
  11. $id_session = intval($_GET['id_session']);
  12. SessionManager::protectSession($id_session);
  13. // setting breadcrumbs
  14. $interbreadcrumb[] = array('url' => 'session_list.php', 'name' => get_lang('SessionList'));
  15. $interbreadcrumb[] = array(
  16. 'url' => 'resume_session.php?id_session='.$id_session,
  17. 'name' => get_lang('SessionOverview'),
  18. );
  19. // Database Table Definitions
  20. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  21. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  22. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  23. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  24. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  25. // setting the name of the tool
  26. $tool_name = get_lang('EditSessionCoursesByUser');
  27. $id_user = intval($_GET['id_user']);
  28. if (empty($id_user) || empty($id_session)) {
  29. header('Location: resume_session.php?id_session='.$id_session);
  30. }
  31. if (!api_is_platform_admin()) {
  32. $sql = 'SELECT session_admin_id
  33. FROM '.Database::get_main_table(TABLE_MAIN_SESSION).'
  34. WHERE id='.$id_session;
  35. $rs = Database::query($sql);
  36. if (Database::result($rs, 0, 0) != $_user['user_id']) {
  37. api_not_allowed(true);
  38. }
  39. }
  40. $formSent = 0;
  41. $CourseList = $SessionList = array();
  42. $courses = $sessions = array();
  43. $noPHP_SELF = true;
  44. if (isset($_POST['formSent']) && $_POST['formSent']) {
  45. $formSent = $_POST['formSent'];
  46. $CourseList = isset($_POST['SessionCoursesList']) ? ($_POST['SessionCoursesList']) : null;
  47. if (!is_array($CourseList)) {
  48. $CourseList = array();
  49. }
  50. $sql = "SELECT DISTINCT course.id
  51. FROM $tbl_course course
  52. LEFT JOIN $tbl_session_rel_course session_rel_course
  53. ON course.id = session_rel_course.c_id
  54. INNER JOIN $tbl_session_rel_course_rel_user as srcru
  55. ON (srcru.session_id = session_rel_course.session_id)
  56. WHERE
  57. user_id = $id_user AND
  58. session_rel_course.session_id = $id_session";
  59. $rs = Database::query($sql);
  60. $existingCourses = Database::store_result($rs);
  61. if (empty($CourseList) && empty($existingCourses)) {
  62. Display::addFlash(Display::return_message(get_lang('NoCoursesForThisSession'), 'warning'));
  63. header('Location: session_course_user.php?id_session='.$id_session.'&id_user='.$id_user);
  64. exit;
  65. }
  66. if (count($CourseList) == count($existingCourses)) {
  67. Display::addFlash(Display::return_message(get_lang('MaybeYouWantToDeleteThisUserFromSession')));
  68. header('Location: session_course_user.php?id_session='.$id_session.'&id_user='.$id_user);
  69. exit;
  70. }
  71. foreach ($CourseList as $enreg_course) {
  72. $exists = false;
  73. foreach ($existingCourses as $existingCourse) {
  74. if ($enreg_course == $existingCourse['id']) {
  75. $exists = true;
  76. }
  77. }
  78. if (!$exists) {
  79. $enreg_course = Database::escape_string($enreg_course);
  80. $sql = "DELETE FROM $tbl_session_rel_course_rel_user
  81. WHERE user_id = '".$id_user."' AND c_id='".$enreg_course."' AND session_id=$id_session";
  82. $result = Database::query($sql);
  83. if (Database::affected_rows($result)) {
  84. //update session rel course table
  85. $sql = "UPDATE $tbl_session_rel_course
  86. SET nbr_users= nbr_users - 1
  87. WHERE session_id='$id_session' AND c_id = '$enreg_course'";
  88. Database::query($sql);
  89. }
  90. }
  91. }
  92. foreach ($existingCourses as $existingCourse) {
  93. if (!in_array($existingCourse['id'], $CourseList)) {
  94. $existingCourse = Database::escape_string($existingCourse['id']);
  95. $sql = "INSERT IGNORE INTO $tbl_session_rel_course_rel_user (session_id,c_id,user_id)
  96. VALUES ('$id_session','$existingCourse','$id_user')";
  97. $result = Database::query($sql);
  98. if (Database::affected_rows($result)) {
  99. //update session rel course table
  100. $sql = "UPDATE $tbl_session_rel_course
  101. SET nbr_users= nbr_users + 1
  102. WHERE session_id='$id_session' AND c_id = '$existingCourse'";
  103. Database::query($sql);
  104. }
  105. }
  106. }
  107. Display::addFlash(Display::return_message(get_lang('CoursesUpdated')));
  108. header('Location: session_course_user.php?id_session='.$id_session.'&id_user='.$id_user);
  109. exit;
  110. }
  111. Display::display_header($tool_name);
  112. // the form header
  113. $session_info = SessionManager::fetch($id_session);
  114. $user_info = api_get_user_info($id_user);
  115. echo '<legend>'.$tool_name.': '.$session_info['name'].' - '.$user_info['complete_name'].'</legend>';
  116. $nosessionCourses = $sessionCourses = array();
  117. // actual user
  118. $sql = "SELECT course.id, code, title, visual_code, srcru.session_id
  119. FROM $tbl_course course
  120. INNER JOIN $tbl_session_rel_course_rel_user as srcru
  121. ON course.id = srcru.c_id
  122. WHERE srcru.user_id = $id_user AND session_id = $id_session";
  123. //all
  124. $sql_all = "SELECT course.id, code, title, visual_code, src.session_id
  125. FROM $tbl_course course
  126. INNER JOIN $tbl_session_rel_course as src
  127. ON course.id = src.c_id AND session_id = $id_session";
  128. $result = Database::query($sql);
  129. $Courses = Database::store_result($result);
  130. $result = Database::query($sql_all);
  131. $CoursesAll = Database::store_result($result);
  132. $course_temp = array();
  133. foreach ($Courses as $course) {
  134. $course_temp[] = $course['id'];
  135. }
  136. foreach ($CoursesAll as $course) {
  137. if (in_array($course['id'], $course_temp)) {
  138. $nosessionCourses[$course['id']] = $course;
  139. } else {
  140. $sessionCourses[$course['id']] = $course;
  141. }
  142. }
  143. unset($Courses);
  144. ?>
  145. <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;">
  146. <input type="hidden" name="formSent" value="1" />
  147. <table border="0" cellpadding="5" cellspacing="0" width="100%" align="center">
  148. <tr>
  149. <td width="45%" align="center"><b><?php echo get_lang('CurrentCourses') ?> :</b></td>
  150. <td width="10%">&nbsp;</td>
  151. <td align="center" width="45%"><b><?php echo get_lang('CoursesToAvoid') ?> :</b></td>
  152. </tr>
  153. </td>
  154. <tr>
  155. <td width="45%" align="center">
  156. <div id="ajax_list_courses_multiple">
  157. <select id="origin" name="NoSessionCoursesList[]" multiple="multiple" size="20" style="width:320px;"> <?php
  158. foreach ($nosessionCourses as $enreg) {
  159. ?>
  160. <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>
  161. <?php
  162. }
  163. ?> </select></div> <?php
  164. unset($nosessionCourses);
  165. ?>
  166. </select></td>
  167. <td width="10%" valign="middle" align="center">
  168. <button class="btn btn-default" type="button" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))">
  169. <em class="fa fa-arrow-right"></em>
  170. </button>
  171. <br /><br />
  172. <button class="btn btn-default" type="button" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))">
  173. <em class="fa fa-arrow-left"></em>
  174. </button>
  175. <br /><br /><br /><br /><br /><br />
  176. <?php
  177. echo '<button class="btn btn-primary" type="button" value="" onclick="valide()" >'.
  178. get_lang('EditSessionCourses').'</button>';
  179. ?>
  180. </td>
  181. <td width="45%" align="center">
  182. <select id='destination' name="SessionCoursesList[]" multiple="multiple" size="20" style="width:320px;">
  183. <?php
  184. foreach ($sessionCourses as $enreg) {
  185. ?>
  186. <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>
  187. <?php
  188. }
  189. unset($sessionCourses);
  190. ?>
  191. </select></td>
  192. </tr>
  193. </table>
  194. </form>
  195. <script>
  196. function moveItem(origin , destination) {
  197. for(var i = 0 ; i<origin.options.length ; i++) {
  198. if(origin.options[i].selected) {
  199. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  200. origin.options[i]=null;
  201. i = i-1;
  202. }
  203. }
  204. destination.selectedIndex = -1;
  205. sortOptions(destination.options);
  206. }
  207. function sortOptions(options) {
  208. newOptions = new Array();
  209. for (i = 0 ; i<options.length ; i++) {
  210. newOptions[i] = options[i];
  211. }
  212. newOptions = newOptions.sort(mysort);
  213. options.length = 0;
  214. for(i = 0 ; i < newOptions.length ; i++){
  215. options[i] = newOptions[i];
  216. }
  217. }
  218. function mysort(a, b){
  219. if(a.text.toLowerCase() > b.text.toLowerCase()){
  220. return 1;
  221. }
  222. if(a.text.toLowerCase() < b.text.toLowerCase()){
  223. return -1;
  224. }
  225. return 0;
  226. }
  227. function valide(){
  228. var options = document.getElementById('destination').options;
  229. for (i = 0 ; i<options.length ; i++)
  230. options[i].selected = true;
  231. document.forms.formulaire.submit();
  232. }
  233. </script>
  234. <?php
  235. Display::display_footer();