add_courses_to_session.php 14 KB

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