add_courses_to_session.php 15 KB

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