add_courses_to_session.php 14 KB

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