add_courses_to_session.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 __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(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' => 'session_list.php', 'name' => get_lang('SessionList'));
  19. $interbreadcrumb[] = array(
  20. 'url' => "resume_session.php?id_session=".$sessionId,
  21. 'name' => get_lang('SessionOverview')
  22. );
  23. // Database Table Definitions
  24. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  25. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  26. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  27. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  28. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  29. // setting the name of the tool
  30. $tool_name = get_lang('SubscribeCoursesToSession');
  31. $add_type = 'multiple';
  32. if (isset($_GET['add_type']) && $_GET['add_type'] != '') {
  33. $add_type = Security::remove_XSS($_REQUEST['add_type']);
  34. }
  35. $page = isset($_GET['page']) ? Security::remove_XSS($_GET['page']) : null;
  36. $xajax->processRequests();
  37. $htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/');
  38. $htmlHeadXtra[] = '<script>
  39. function add_course_to_session(code, content) {
  40. document.getElementById("course_to_add").value = "";
  41. document.getElementById("ajax_list_courses_single").innerHTML = "";
  42. destination = document.getElementById("destination");
  43. for (i=0;i<destination.length;i++) {
  44. if (destination.options[i].text == content) {
  45. return false;
  46. }
  47. }
  48. destination.options[destination.length] = new Option(content,code);
  49. destination.selectedIndex = -1;
  50. sortOptions(destination.options);
  51. }
  52. function remove_item(origin)
  53. {
  54. for(var i = 0 ; i<origin.options.length ; i++) {
  55. if(origin.options[i].selected) {
  56. origin.options[i]=null;
  57. i = i-1;
  58. }
  59. }
  60. }
  61. </script>';
  62. $errorMsg = '';
  63. $CourseList = $SessionList = array();
  64. $courses = $sessions = array();
  65. if (isset($_POST['formSent']) && $_POST['formSent']) {
  66. $courseList = $_POST['SessionCoursesList'];
  67. $copyEvaluation = isset($_POST['copy_evaluation']);
  68. SessionManager::add_courses_to_session(
  69. $sessionId,
  70. $courseList,
  71. true,
  72. $copyEvaluation
  73. );
  74. Display::addFlash(Display::return_message(get_lang('Updated')));
  75. if (isset($add)) {
  76. header('Location: add_users_to_session.php?id_session='.$sessionId.'&add=true');
  77. } else {
  78. header('Location: resume_session.php?id_session='.$sessionId);
  79. }
  80. exit;
  81. }
  82. // display the header
  83. Display::display_header($tool_name);
  84. if ($add_type == 'multiple') {
  85. $link_add_type_unique = '<a href="'.api_get_self().'?id_session='.$sessionId.'&add='.$add.'&add_type=unique">'.
  86. Display::return_icon('single.gif').get_lang('SessionAddTypeUnique').'</a>';
  87. $link_add_type_multiple = Display::return_icon('multiple.gif').get_lang('SessionAddTypeMultiple').' ';
  88. } else {
  89. $link_add_type_unique = Display::return_icon('single.gif').get_lang('SessionAddTypeUnique').'&nbsp;&nbsp;&nbsp;';
  90. $link_add_type_multiple = '<a href="'.api_get_self().'?id_session='.$sessionId.'&add='.$add.'&add_type=multiple">'.
  91. Display::return_icon('multiple.gif').get_lang('SessionAddTypeMultiple').'</a>';
  92. }
  93. // the form header
  94. $session_info = SessionManager::fetch($sessionId);
  95. echo '<div class="actions">';
  96. echo $link_add_type_unique.$link_add_type_multiple;
  97. echo '</div>';
  98. $ajax_search = $add_type == 'unique' ? true : false;
  99. $nosessionCourses = $sessionCourses = array();
  100. if ($ajax_search) {
  101. $sql = "SELECT course.id, code, title, visual_code, session_id
  102. FROM $tbl_course course
  103. INNER JOIN $tbl_session_rel_course session_rel_course
  104. ON
  105. course.id = session_rel_course.c_id AND
  106. session_rel_course.session_id = ".$sessionId."
  107. ORDER BY ".(sizeof($courses) ? "(code IN(".implode(',', $courses).")) DESC," : "")." title";
  108. if (api_is_multiple_url_enabled()) {
  109. $tbl_course_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  110. $access_url_id = api_get_current_access_url_id();
  111. if ($access_url_id != -1) {
  112. $sql = "SELECT course.id, code, title, visual_code, session_id
  113. FROM $tbl_course course
  114. INNER JOIN $tbl_session_rel_course session_rel_course
  115. ON course.id = session_rel_course.c_id
  116. AND session_rel_course.session_id = ".$sessionId."
  117. INNER JOIN $tbl_course_rel_access_url url_course
  118. ON (url_course.c_id = course.id)
  119. WHERE access_url_id = $access_url_id
  120. ORDER BY ".(sizeof($courses) ? "(code IN(".implode(',', $courses).")) DESC," : "")." title";
  121. }
  122. }
  123. $result = Database::query($sql);
  124. $Courses = Database::store_result($result);
  125. foreach ($Courses as $course) {
  126. $sessionCourses[$course['id']] = $course;
  127. }
  128. } else {
  129. $sql = "SELECT course.id, code, title, visual_code, session_id
  130. FROM $tbl_course course
  131. LEFT JOIN $tbl_session_rel_course session_rel_course
  132. ON
  133. course.id = session_rel_course.c_id AND
  134. session_rel_course.session_id = ".$sessionId."
  135. ORDER BY ".(sizeof($courses) ? "(code IN(".implode(',', $courses).")) DESC," : "")." title";
  136. if (api_is_multiple_url_enabled()) {
  137. $tbl_course_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  138. $access_url_id = api_get_current_access_url_id();
  139. if ($access_url_id != -1) {
  140. $sql = "SELECT course.id, code, title, visual_code, session_id
  141. FROM $tbl_course course
  142. LEFT JOIN $tbl_session_rel_course session_rel_course
  143. ON
  144. course.id = session_rel_course.c_id AND
  145. session_rel_course.session_id = ".$sessionId."
  146. INNER JOIN $tbl_course_rel_access_url url_course
  147. ON (url_course.c_id = course.id)
  148. WHERE access_url_id = $access_url_id
  149. ORDER BY ".(sizeof($courses) ? "(code IN(".implode(',', $courses).")) DESC," : "")." title";
  150. }
  151. }
  152. $result = Database::query($sql);
  153. $Courses = Database::store_result($result);
  154. foreach ($Courses as $course) {
  155. if ($course['session_id'] == $sessionId) {
  156. $sessionCourses[$course['id']] = $course;
  157. } else {
  158. $nosessionCourses[$course['id']] = $course;
  159. }
  160. }
  161. }
  162. if (!api_is_platform_admin() && api_is_teacher()) {
  163. $coursesFromTeacher = CourseManager::getCoursesFollowedByUser(
  164. api_get_user_id(),
  165. COURSEMANAGER
  166. );
  167. foreach ($nosessionCourses as &$course) {
  168. if (in_array($course['code'], array_keys($coursesFromTeacher))) {
  169. continue;
  170. } else {
  171. unset($nosessionCourses[$course['id']]);
  172. }
  173. }
  174. }
  175. unset($Courses);
  176. ?>
  177. <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();"'; }?>>
  178. <legend><?php echo $tool_name.' ('.$session_info['name'].')'; ?></legend>
  179. <input type="hidden" name="formSent" value="1" />
  180. <?php
  181. if (!empty($errorMsg)) {
  182. echo Display::return_message($errorMsg); //main API
  183. }
  184. ?>
  185. <div id="multiple-add-session" class="row">
  186. <div class="col-md-4">
  187. <label><?php echo get_lang('CourseListInPlatform') ?> :</label>
  188. <?php
  189. if (!($add_type == 'multiple')) {
  190. ?>
  191. <input type="text" id="course_to_add" onkeyup="xajax_search_courses(this.value,'single')" class="form-control"/>
  192. <div id="ajax_list_courses_single"></div>
  193. <?php
  194. } else {
  195. ?>
  196. <div id="ajax_list_courses_multiple">
  197. <select id="origin" name="NoSessionCoursesList[]" multiple="multiple" size="20" class="form-control">
  198. <?php foreach ($nosessionCourses as $enreg) { ?>
  199. <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"'; ?>>
  200. <?php echo $enreg['title'].' ('.$enreg['visual_code'].')'; ?>
  201. </option>
  202. <?php } ?>
  203. </select>
  204. </div>
  205. <?php
  206. }
  207. unset($nosessionCourses);
  208. ?>
  209. </div>
  210. <div class="col-md-4">
  211. <?php if ($add_type == 'multiple') { ?>
  212. <div class="code-course">
  213. <?php echo get_lang('FirstLetterCourse'); ?> :
  214. <select name="firstLetterCourse" onchange = "xajax_search_courses(this.value,'multiple')" class="selectpicker show-tick form-control">
  215. <option value="%">--</option>
  216. <?php
  217. echo Display :: get_alphabet_options();
  218. echo Display :: get_numeric_options(0, 9, '');
  219. ?>
  220. </select>
  221. </div>
  222. <?php } ?>
  223. <div class="control-course">
  224. <?php
  225. if ($ajax_search) {
  226. ?>
  227. <div class="separate-action">
  228. <button class="btn btn-primary" type="button" onclick="remove_item(document.getElementById('destination'))">
  229. <em class="fa fa-chevron-left"></em>
  230. </button>
  231. </div>
  232. <?php
  233. } else {
  234. ?>
  235. <div class="separate-action">
  236. <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'))">
  237. <em class="fa fa-chevron-right"></em>
  238. </button>
  239. </div>
  240. <div class="separate-action">
  241. <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'))">
  242. <em class="fa fa-chevron-left"></em>
  243. </button>
  244. </div>
  245. <?php } ?>
  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 name="next" class="btn btn-success" type="button" value="" onclick="valide()" >'.get_lang('NextStep').'</button>';
  256. } else {
  257. echo '<button name="next" 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();