session_course_user.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  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. require_once '../inc/global.inc.php';
  11. // setting the section (for the tabs)
  12. $this_section = SECTION_PLATFORM_ADMIN;
  13. $id_session = intval($_GET['id_session']);
  14. SessionManager::protect_session_edit($id_session);
  15. // setting breadcrumbs
  16. $interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  17. $interbreadcrumb[] = array('url' => 'session_list.php','name' => get_lang('SessionList'));
  18. $interbreadcrumb[] = array('url' => 'resume_session.php?id_session='.Security::remove_XSS($_GET['id_session']),'name' => get_lang('SessionOverview'));
  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 FROM '.Database :: get_main_table(TABLE_MAIN_SESSION).' WHERE id='.$id_session;
  33. $rs = Database::query($sql);
  34. if (Database::result($rs,0,0)!=$_user['user_id']) {
  35. api_not_allowed(true);
  36. }
  37. }
  38. $formSent=0;
  39. $errorMsg=$firstLetterCourse=$firstLetterSession='';
  40. $CourseList=$SessionList=array();
  41. $courses=$sessions=array();
  42. $noPHP_SELF=true;
  43. if (isset($_POST['formSent']) && $_POST['formSent']) {
  44. $formSent = $_POST['formSent'];
  45. $CourseList = $_POST['SessionCoursesList'];
  46. if (!is_array($CourseList)) {
  47. $CourseList=array();
  48. }
  49. $sql="SELECT distinct code
  50. FROM $tbl_course course LEFT JOIN $tbl_session_rel_course session_rel_course
  51. ON course.code = session_rel_course.course_code inner join $tbl_session_rel_course_rel_user as srcru
  52. ON (srcru.id_session = session_rel_course.id_session)
  53. WHERE id_user = $id_user and session_rel_course.id_session = $id_session";
  54. $rs = Database::query($sql);
  55. $existingCourses = Database::store_result($rs);
  56. if (count($CourseList) == count($existingCourses)) {
  57. header('Location: session_course_user.php?id_session='.$id_session.'&id_user='.$id_user.'&msg='.get_lang('MaybeYouWantToDeleteThisUserFromSession'));
  58. exit;
  59. }
  60. foreach($CourseList as $enreg_course) {
  61. $exists = false;
  62. foreach($existingCourses as $existingCourse) {
  63. if($enreg_course == $existingCourse['course_code']) {
  64. $exists=true;
  65. }
  66. }
  67. if(!$exists) {
  68. $enreg_course = Database::escape_string($enreg_course);
  69. $sql_delete = "DELETE FROM $tbl_session_rel_course_rel_user
  70. WHERE id_user='".$id_user."' AND course_code='".$enreg_course."' AND id_session=$id_session";
  71. Database::query($sql_delete);
  72. if (Database::affected_rows()) {
  73. //update session rel course table
  74. $sql_update = "UPDATE $tbl_session_rel_course SET nbr_users= nbr_users - 1 WHERE id_session='$id_session' AND course_code='$enreg_course'";
  75. Database::query($sql_update);
  76. }
  77. }
  78. }
  79. foreach($existingCourses as $existingCourse) {
  80. //$sql_insert_rel_course= "INSERT INTO $tbl_session_rel_course(id_session,course_code, id_coach) VALUES('$id_session','$enreg_course','$id_coach')";
  81. if(!in_array($existingCourse['code'], $CourseList)){
  82. $existingCourse = Database::escape_string($existingCourse['code']);
  83. $sql_insert = "INSERT IGNORE INTO $tbl_session_rel_course_rel_user(id_session,course_code,id_user) VALUES('$id_session','$existingCourse','$id_user')";
  84. Database::query($sql_insert);
  85. if(Database::affected_rows()) {
  86. //update session rel course table
  87. $sql_update = "UPDATE $tbl_session_rel_course SET nbr_users= nbr_users + 1 WHERE id_session='$id_session' AND course_code='$existingCourse'";
  88. Database::query($sql_update);
  89. }
  90. }
  91. }
  92. header('Location: session_course_user.php?id_session='.$id_session.'&id_user='.$id_user.'&msg='.get_lang('CoursesUpdated'));
  93. exit;
  94. }
  95. // display the dokeos header
  96. Display::display_header($tool_name);
  97. if (!empty($_GET['msg'])) {
  98. Display::display_normal_message(urldecode($_GET['msg']));
  99. }
  100. // the form header
  101. $session_info = SessionManager::fetch($id_session);
  102. $user_info = api_get_user_info($id_user);
  103. echo '<legend>'.$tool_name.': '.$session_info['name'].' - '.$user_info['complete_name'].'</legend>';
  104. $nosessionCourses = $sessionCourses = array();
  105. // actual user
  106. $sql = "SELECT code, title, visual_code, srcru.id_session
  107. FROM $tbl_course course inner JOIN $tbl_session_rel_course_rel_user as srcru
  108. ON course.code = srcru.course_code WHERE srcru.id_user = $id_user AND id_session = $id_session";
  109. //all
  110. $sql_all="SELECT code, title, visual_code, src.id_session " .
  111. "FROM $tbl_course course inner JOIN $tbl_session_rel_course as src " .
  112. "ON course.code = src.course_code AND id_session = $id_session";
  113. $result=Database::query($sql);
  114. $Courses=Database::store_result($result);
  115. $result=Database::query($sql_all);
  116. $CoursesAll=Database::store_result($result);
  117. $course_temp = array();
  118. foreach($Courses as $course) {
  119. $course_temp[] = $course['code'];
  120. }
  121. foreach($CoursesAll as $course) {
  122. if (in_array($course['code'], $course_temp)) {
  123. $nosessionCourses[$course['code']] = $course ;
  124. } else {
  125. $sessionCourses[$course['code']] = $course ;
  126. }
  127. }
  128. unset($Courses);
  129. ?>
  130. <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;">
  131. <input type="hidden" name="formSent" value="1" />
  132. <?php
  133. if(!empty($errorMsg)) {
  134. Display::display_normal_message($errorMsg); //main API
  135. }
  136. ?>
  137. <table border="0" cellpadding="5" cellspacing="0" width="100%" align="center">
  138. <tr>
  139. <td width="45%" align="center"><b><?php echo get_lang('CurrentCourses') ?> :</b></td>
  140. <td width="10%">&nbsp;</td>
  141. <td align="center" width="45%"><b><?php echo get_lang('CoursesToAvoid') ?> :</b></td>
  142. </tr>
  143. </td>
  144. <tr>
  145. <td width="45%" align="center">
  146. <div id="ajax_list_courses_multiple">
  147. <select id="origin" name="NoSessionCoursesList[]" multiple="multiple" size="20" style="width:320px;"> <?php
  148. foreach($nosessionCourses as $enreg) {
  149. ?>
  150. <option value="<?php echo $enreg['code']; ?>" <?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>
  151. <?php
  152. }
  153. ?> </select></div> <?php
  154. unset($nosessionCourses);
  155. ?>
  156. </select></td>
  157. <td width="10%" valign="middle" align="center">
  158. <button class="arrowr" type="button" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))"></button>
  159. <br /><br />
  160. <button class="arrowl" type="button" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))"></button>
  161. <br /><br /><br /><br /><br /><br />
  162. <?php
  163. echo '<button class="save" type="button" value="" onclick="valide()" >'.get_lang('EditSessionCourses').'</button>';
  164. ?>
  165. </td>
  166. <td width="45%" align="center"><select id='destination' name="SessionCoursesList[]" multiple="multiple" size="20" style="width:320px;">
  167. <?php
  168. foreach($sessionCourses as $enreg) {
  169. ?>
  170. <option value="<?php echo $enreg['code']; ?>" title="<?php echo htmlspecialchars($enreg['title'].' ('.$enreg['visual_code'].')',ENT_QUOTES); ?>"><?php echo $enreg['title'].' ('.$enreg['visual_code'].')'; ?></option>
  171. <?php
  172. }
  173. unset($sessionCourses);
  174. ?>
  175. </select></td>
  176. </tr>
  177. </table>
  178. </form>
  179. <script>
  180. function moveItem(origin , destination) {
  181. for(var i = 0 ; i<origin.options.length ; i++) {
  182. if(origin.options[i].selected) {
  183. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  184. origin.options[i]=null;
  185. i = i-1;
  186. }
  187. }
  188. destination.selectedIndex = -1;
  189. sortOptions(destination.options);
  190. }
  191. function sortOptions(options) {
  192. newOptions = new Array();
  193. for (i = 0 ; i<options.length ; i++) {
  194. newOptions[i] = options[i];
  195. }
  196. newOptions = newOptions.sort(mysort);
  197. options.length = 0;
  198. for(i = 0 ; i < newOptions.length ; i++){
  199. options[i] = newOptions[i];
  200. }
  201. }
  202. function mysort(a, b){
  203. if(a.text.toLowerCase() > b.text.toLowerCase()){
  204. return 1;
  205. }
  206. if(a.text.toLowerCase() < b.text.toLowerCase()){
  207. return -1;
  208. }
  209. return 0;
  210. }
  211. function valide(){
  212. var options = document.getElementById('destination').options;
  213. for (i = 0 ; i<options.length ; i++)
  214. options[i].selected = true;
  215. document.forms.formulaire.submit();
  216. }
  217. </script>
  218. <?php
  219. Display::display_footer();