add_courses_to_session.php 14 KB

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