add_courses_to_session.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. * @todo use formvalidator
  6. */
  7. // name of the language file that needs to be included.
  8. $language_file = 'admin';
  9. // resetting the course id.
  10. $cidReset = true;
  11. ////require_once '../inc/global.inc.php';
  12. require_once api_get_path(LIBRARY_PATH).'add_courses_to_session_functions.lib.php';
  13. $id_session = isset($_GET['id_session']) ? intval($_GET['id_session']) : null;
  14. $add = isset($_GET['add']) ? Security::remove_XSS($_GET['add']) : null;
  15. SessionManager::protect_session_edit($id_session);
  16. $xajax = new xajax();
  17. $xajax->registerFunction(array('search_courses', 'AddCourseToSession', 'search_courses'));
  18. // Setting the section (for the tabs)
  19. $this_section = SECTION_PLATFORM_ADMIN;
  20. // setting breadcrumbs
  21. $interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  22. $interbreadcrumb[] = array('url' => 'session_list.php','name' => get_lang('SessionList'));
  23. $interbreadcrumb[] = array('url' => "resume_session.php?id_session=".$id_session, "name" => get_lang('SessionOverview'));
  24. // Database Table Definitions
  25. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  26. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  27. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  28. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  29. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  30. // setting the name of the tool
  31. $tool_name= get_lang('SubscribeCoursesToSession');
  32. $add_type = 'multiple';
  33. if(isset($_GET['add_type']) && $_GET['add_type']!=''){
  34. $add_type = Security::remove_XSS($_REQUEST['add_type']);
  35. }
  36. $page = isset($_GET['page']) ? Security::remove_XSS($_GET['page']) : null;
  37. $xajax->processRequests();
  38. $htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/');
  39. $htmlHeadXtra[] = '<script>
  40. function add_course_to_session(code, content) {
  41. document.getElementById("course_to_add").value = "";
  42. document.getElementById("ajax_list_courses_single").innerHTML = "";
  43. destination = document.getElementById("destination");
  44. for (i=0;i<destination.length;i++) {
  45. if (destination.options[i].text == content) {
  46. return false;
  47. }
  48. }
  49. destination.options[destination.length] = new Option(content,code);
  50. destination.selectedIndex = -1;
  51. sortOptions(destination.options);
  52. }
  53. function remove_item(origin)
  54. {
  55. for(var i = 0 ; i<origin.options.length ; i++) {
  56. if(origin.options[i].selected) {
  57. origin.options[i]=null;
  58. i = i-1;
  59. }
  60. }
  61. }
  62. </script>';
  63. $formSent = 0;
  64. $errorMsg = $firstLetterCourse = $firstLetterSession = '';
  65. $CourseList = $SessionList = array();
  66. $courses = $sessions = array();
  67. $noPHP_SELF = true;
  68. if (isset($_POST['formSent']) && $_POST['formSent']) {
  69. $formSent = $_POST['formSent'];
  70. $firstLetterCourse = $_POST['firstLetterCourse'];
  71. $firstLetterSession = $_POST['firstLetterSession'];
  72. $CourseList = $_POST['SessionCoursesList'];
  73. if (!is_array($CourseList)) {
  74. $CourseList = array();
  75. }
  76. $nbr_courses=0;
  77. $id_coach = Database::query("SELECT id_coach FROM $tbl_session WHERE id=$id_session");
  78. $id_coach = Database::fetch_array($id_coach);
  79. $id_coach = $id_coach[0];
  80. $rs = Database::query("SELECT course_code FROM $tbl_session_rel_course WHERE id_session=$id_session");
  81. $existingCourses = Database::store_result($rs);
  82. // Updating only the RRHH users?? why?
  83. $sql = "SELECT id_user FROM $tbl_session_rel_user WHERE id_session = $id_session ";
  84. $result = Database::query($sql);
  85. $UserList = Database::store_result($result);
  86. foreach ($CourseList as $enreg_course) {
  87. $enreg_course = Database::escape_string($enreg_course);
  88. $exists = false;
  89. foreach ($existingCourses as $existingCourse) {
  90. if ($enreg_course == $existingCourse['course_code']) {
  91. $exists = true;
  92. }
  93. }
  94. if (!$exists) {
  95. $sql_insert_rel_course= "INSERT INTO $tbl_session_rel_course(id_session,course_code) VALUES('$id_session','$enreg_course')";
  96. Database::query($sql_insert_rel_course);
  97. $course_info = api_get_course_info($enreg_course);
  98. CourseManager::update_course_ranking($course_info['real_id'], $id_session);
  99. //We add in the existing courses table the current course, to not try to add another time the current course
  100. $existingCourses[]=array('course_code'=>$enreg_course);
  101. $nbr_users=0;
  102. foreach ($UserList as $enreg_user) {
  103. $enreg_user = Database::escape_string($enreg_user['id_user']);
  104. $sql_insert = "INSERT IGNORE INTO $tbl_session_rel_course_rel_user(id_session,course_code,id_user)
  105. VALUES('$id_session','$enreg_course','$enreg_user')";
  106. Database::query($sql_insert);
  107. if(Database::affected_rows()) {
  108. $nbr_users++;
  109. }
  110. }
  111. SessionManager::installCourse($id_session, $course_info['real_id']);
  112. Database::query("UPDATE $tbl_session_rel_course SET nbr_users=$nbr_users WHERE id_session='$id_session' AND course_code='$enreg_course'");
  113. }
  114. }
  115. foreach ($existingCourses as $existingCourse) {
  116. if (!in_array($existingCourse['course_code'], $CourseList)) {
  117. $course_info = api_get_course_info($existingCourse['course_code']);
  118. CourseManager::remove_course_ranking($course_info['real_id'], $id_session);
  119. Database::query("DELETE FROM $tbl_session_rel_course WHERE course_code='".$existingCourse['course_code']."' AND id_session=$id_session");
  120. Database::query("DELETE FROM $tbl_session_rel_course_rel_user WHERE course_code='".$existingCourse['course_code']."' AND id_session=$id_session");
  121. SessionManager::unInstallCourse($id_session, $course_info['real_id']);
  122. }
  123. }
  124. $nbr_courses=count($CourseList);
  125. Database::query("UPDATE $tbl_session SET nbr_courses=$nbr_courses WHERE id='$id_session'");
  126. if (isset($add)) {
  127. header('Location: add_users_to_session.php?id_session='.$id_session.'&add=true');
  128. } else {
  129. header('Location: resume_session.php?id_session='.$id_session);
  130. }
  131. exit;
  132. }
  133. // display the header
  134. Display::display_header($tool_name);
  135. if ($add_type == 'multiple') {
  136. $link_add_type_unique = '<a href="'.api_get_self().'?id_session='.$id_session.'&add='.$add.'&add_type=unique">'.
  137. Display::return_icon('single.gif').get_lang('SessionAddTypeUnique').'</a>';
  138. $link_add_type_multiple = Display::return_icon('multiple.gif').get_lang('SessionAddTypeMultiple').' ';
  139. } else {
  140. $link_add_type_unique = Display::return_icon('single.gif').get_lang('SessionAddTypeUnique').'&nbsp;&nbsp;&nbsp;';
  141. $link_add_type_multiple = '<a href="'.api_get_self().'?id_session='.$id_session.'&add='.$add.'&add_type=multiple">'.
  142. Display::return_icon('multiple.gif').get_lang('SessionAddTypeMultiple').'</a>';
  143. }
  144. // the form header
  145. $session_info = SessionManager::fetch($id_session);
  146. echo '<div class="actions">';
  147. echo $link_add_type_unique.$link_add_type_multiple;
  148. echo '</div>';
  149. $ajax_search = $add_type == 'unique' ? true : false;
  150. $nosessionCourses = $sessionCourses = array();
  151. if ($ajax_search) {
  152. $sql="SELECT code, title, visual_code, id_session
  153. FROM $tbl_course course
  154. INNER JOIN $tbl_session_rel_course session_rel_course
  155. ON course.code = session_rel_course.course_code
  156. AND session_rel_course.id_session = ".intval($id_session)."
  157. ORDER BY ".(sizeof($courses)?"(code IN(".implode(',',$courses).")) DESC,":"")." title";
  158. if (api_is_multiple_url_enabled()) {
  159. $tbl_course_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  160. $access_url_id = api_get_current_access_url_id();
  161. if ($access_url_id != -1){
  162. $sql="SELECT code, title, visual_code, id_session
  163. FROM $tbl_course course
  164. INNER JOIN $tbl_session_rel_course session_rel_course
  165. ON course.code = session_rel_course.course_code
  166. AND session_rel_course.id_session = ".intval($id_session)."
  167. INNER JOIN $tbl_course_rel_access_url url_course ON (url_course.course_code=course.code)
  168. WHERE access_url_id = $access_url_id
  169. ORDER BY ".(sizeof($courses)?"(code IN(".implode(',',$courses).")) DESC,":"")." title";
  170. }
  171. }
  172. $result = Database::query($sql);
  173. $Courses = Database::store_result($result);
  174. foreach ($Courses as $course) {
  175. $sessionCourses[$course['code']] = $course ;
  176. }
  177. } else {
  178. $sql = "SELECT code, title, visual_code, id_session
  179. FROM $tbl_course course
  180. LEFT JOIN $tbl_session_rel_course session_rel_course
  181. ON course.code = session_rel_course.course_code
  182. AND session_rel_course.id_session = ".intval($id_session)."
  183. ORDER BY ".(sizeof($courses)?"(code IN(".implode(',',$courses).")) DESC,":"")." title";
  184. if (api_is_multiple_url_enabled()) {
  185. $tbl_course_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  186. $access_url_id = api_get_current_access_url_id();
  187. if ($access_url_id != -1){
  188. $sql="SELECT code, title, visual_code, id_session
  189. FROM $tbl_course course
  190. LEFT JOIN $tbl_session_rel_course session_rel_course
  191. ON course.code = session_rel_course.course_code
  192. AND session_rel_course.id_session = ".intval($id_session)."
  193. INNER JOIN $tbl_course_rel_access_url url_course ON (url_course.course_code=course.code)
  194. WHERE access_url_id = $access_url_id
  195. ORDER BY ".(sizeof($courses)?"(code IN(".implode(',',$courses).")) DESC,":"")." title";
  196. }
  197. }
  198. $result = Database::query($sql);
  199. $Courses = Database::store_result($result);
  200. foreach ($Courses as $course) {
  201. if ($course['id_session'] == $id_session) {
  202. $sessionCourses[$course['code']] = $course ;
  203. } else {
  204. $nosessionCourses[$course['code']] = $course ;
  205. }
  206. }
  207. }
  208. unset($Courses);
  209. ?>
  210. <form name="formulaire" method="post" action="<?php echo api_get_self(); ?>?page=<?php echo $page; ?>&id_session=<?php echo $id_session; ?><?php if(!empty($_GET['add'])) echo '&add=true' ; ?>" style="margin:0px;" <?php if($ajax_search){echo ' onsubmit="valide();"';}?>>
  211. <legend><?php echo $tool_name.' ('.$session_info['name'].')'; ?></legend>
  212. <input type="hidden" name="formSent" value="1" />
  213. <?php
  214. if (!empty($errorMsg)) {
  215. Display::display_normal_message($errorMsg); //main API
  216. }
  217. ?>
  218. <table border="0" cellpadding="5" cellspacing="0" width="100%" align="center">
  219. <tr>
  220. <td width="45%" align="center"><b><?php echo get_lang('CourseListInPlatform') ?> :</b></td>
  221. <td width="10%">&nbsp;</td>
  222. <td align="center" width="45%"><b><?php echo get_lang('CourseListInSession') ?> :</b></td>
  223. </tr>
  224. <?php if($add_type == 'multiple') { ?>
  225. <tr><td width="45%" align="center">
  226. <?php echo get_lang('FirstLetterCourse'); ?> :
  227. <select name="firstLetterCourse" onchange = "xajax_search_courses(this.value,'multiple')">
  228. <option value="%">--</option>
  229. <?php
  230. echo Display :: get_alphabet_options();
  231. echo Display :: get_numeric_options(0,9,'');
  232. ?>
  233. </select>
  234. </td>
  235. <td>&nbsp;</td></tr>
  236. <?php } ?>
  237. <tr>
  238. <td width="45%" align="center">
  239. <?php
  240. if (!($add_type == 'multiple')) {
  241. ?>
  242. <input type="text" id="course_to_add" onkeyup="xajax_search_courses(this.value,'single')" />
  243. <div id="ajax_list_courses_single"></div>
  244. <?php
  245. } else {
  246. ?>
  247. <div id="ajax_list_courses_multiple">
  248. <select id="origin" name="NoSessionCoursesList[]" multiple="multiple" size="20" style="width:360px;"> <?php
  249. foreach($nosessionCourses as $enreg) {
  250. ?>
  251. <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"'; ?>>
  252. <?php echo $enreg['title'].' ('.$enreg['visual_code'].')'; ?>
  253. </option>
  254. <?php
  255. }
  256. ?></select>
  257. </div>
  258. <?php
  259. }
  260. unset($nosessionCourses);
  261. ?>
  262. </td>
  263. <td width="10%" valign="middle" align="center">
  264. <?php
  265. if ($ajax_search) {
  266. ?>
  267. <button class="arrowl" type="button" onclick="remove_item(document.getElementById('destination'))"></button>
  268. <?php
  269. } else {
  270. ?>
  271. <button class="arrowr" type="button" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))"></button>
  272. <br /><br />
  273. <button class="arrowl" type="button" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))"></button>
  274. <?php
  275. }
  276. ?>
  277. <br /><br /><br /><br /><br /><br />
  278. <?php
  279. if (isset($_GET['add'])) {
  280. echo '<button class="save" type="button" value="" onclick="valide()" >'.get_lang('NextStep').'</button>';
  281. } else {
  282. echo '<button class="save" type="button" value="" onclick="valide()" >'.get_lang('SubscribeCoursesToSession').'</button>';
  283. }
  284. ?>
  285. </td>
  286. <td width="45%" align="center"><select id='destination' name="SessionCoursesList[]" multiple="multiple" size="20" style="width:360px;">
  287. <?php
  288. foreach($sessionCourses as $enreg) {
  289. ?>
  290. <option value="<?php echo $enreg['code']; ?>" title="<?php echo htmlspecialchars($enreg['title'].' ('.$enreg['visual_code'].')',ENT_QUOTES); ?>">
  291. <?php echo $enreg['title'].' ('.$enreg['visual_code'].')'; ?>
  292. </option>
  293. <?php
  294. }
  295. unset($sessionCourses);
  296. ?>
  297. </select></td>
  298. </tr>
  299. </table>
  300. </form>
  301. <script>
  302. function moveItem(origin , destination) {
  303. for(var i = 0 ; i<origin.options.length ; i++) {
  304. if(origin.options[i].selected) {
  305. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  306. origin.options[i]=null;
  307. i = i-1;
  308. }
  309. }
  310. destination.selectedIndex = -1;
  311. sortOptions(destination.options);
  312. }
  313. function sortOptions(options) {
  314. newOptions = new Array();
  315. for (i = 0 ; i<options.length ; i++) {
  316. newOptions[i] = options[i];
  317. }
  318. newOptions = newOptions.sort(mysort);
  319. options.length = 0;
  320. for(i = 0 ; i < newOptions.length ; i++){
  321. options[i] = newOptions[i];
  322. }
  323. }
  324. function mysort(a, b) {
  325. if(a.text.toLowerCase() > b.text.toLowerCase()){
  326. return 1;
  327. }
  328. if(a.text.toLowerCase() < b.text.toLowerCase()){
  329. return -1;
  330. }
  331. return 0;
  332. }
  333. function valide() {
  334. var options = document.getElementById('destination').options;
  335. for (i = 0 ; i<options.length ; i++)
  336. options[i].selected = true;
  337. document.forms.formulaire.submit();
  338. }
  339. </script>
  340. <?php
  341. Display::display_footer();