session_course_user.php 9.0 KB

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