copy_course_session.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CourseBundle\Component\CourseCopy\CourseBuilder;
  4. use Chamilo\CourseBundle\Component\CourseCopy\CourseRestorer;
  5. use Chamilo\CourseBundle\Component\CourseCopy\CourseSelectForm;
  6. use ChamiloSession as Session;
  7. /**
  8. * Copy resources from one course in a session to another one.
  9. *
  10. * @author Christian Fasanando
  11. * @author Julio Montoya <gugli100@gmail.com> Lots of bug fixes/improvements
  12. *
  13. * @package chamilo.backup
  14. */
  15. $cidReset = true;
  16. require_once __DIR__.'/../inc/global.inc.php';
  17. $current_course_tool = TOOL_COURSE_MAINTENANCE;
  18. api_protect_global_admin_script();
  19. api_protect_limit_for_session_admin();
  20. api_set_more_memory_and_time_limits();
  21. $xajax = new xajax();
  22. $xajax->registerFunction('search_courses');
  23. if (!api_is_allowed_to_edit() && !api_is_session_admin()) {
  24. api_not_allowed(true);
  25. }
  26. $action = isset($_POST['action']) ? $_POST['action'] : '';
  27. $this_section = SECTION_PLATFORM_ADMIN;
  28. $nameTools = get_lang('Copy course');
  29. $interbreadcrumb[] = [
  30. 'url' => api_get_path(WEB_CODE_PATH).'admin/index.php',
  31. 'name' => get_lang('Administration'),
  32. ];
  33. // Database Table Definitions
  34. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  35. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  36. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  37. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  38. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  39. /**
  40. * @param string $name
  41. * @param array $sessions
  42. * @param array $attr
  43. *
  44. * @return string
  45. */
  46. function make_select_session_list($name, $sessions, $attr = [])
  47. {
  48. $attributes = '';
  49. if (count($attr) > 0) {
  50. foreach ($attr as $key => $value) {
  51. $attributes .= ' '.$key.'="'.$value.'"';
  52. }
  53. }
  54. $output = '<select id="session" class="form-control" name="'.$name.'" '.$attributes.'>';
  55. if (count($sessions) == 0) {
  56. $output .= '<option value = "0">'.get_lang('There are no sessions available').'</option>';
  57. } else {
  58. $output .= '<option value = "0">'.get_lang('Select a session').'</option>';
  59. }
  60. if (is_array($sessions)) {
  61. foreach ($sessions as $session) {
  62. $categoryName = '';
  63. if (!empty($session['category_name'])) {
  64. $categoryName = ' ('.$session['category_name'].')';
  65. }
  66. $output .= '<option value="'.$session['id'].'">'.
  67. $session['name'].' '.$categoryName.
  68. '</option>';
  69. }
  70. }
  71. $output .= '</select>';
  72. return $output;
  73. }
  74. /**
  75. * @return string
  76. */
  77. function display_form()
  78. {
  79. $html = '';
  80. $sessions = SessionManager::get_sessions_list([], ['name', 'ASC']);
  81. // Link back to the documents overview
  82. $actionsLeft = '<a href="../admin/index.php">'.
  83. Display::return_icon('back.png', get_lang('Back to').' '.get_lang('Administration'), '', ICON_SIZE_MEDIUM).
  84. '</a>';
  85. $html .= Display::toolbarAction('toolbar-copysession', [$actionsLeft]);
  86. $html .= Display::return_message(get_lang('Copy courseFromSessionToSessionExplanation'), 'warning');
  87. $html .= '<form class="form-horizontal" name="formulaire" method="post" action="'.api_get_self().'" >';
  88. $html .= '<div class="form-group">';
  89. // origin
  90. $html .= '<label class="col-sm-2 control-label">'.get_lang('Courses from the original session').': </label>';
  91. $html .= '<div class="col-sm-5">';
  92. $html .= make_select_session_list(
  93. 'sessions_list_origin',
  94. $sessions,
  95. ['onchange' => 'javascript: xajax_search_courses(this.value,\'origin\');']
  96. );
  97. $html .= '</div>';
  98. $html .= '<div class="col-sm-5" id="ajax_list_courses_origin">';
  99. $html .= '<select id="origin" class="form-control" name="SessionCoursesListOrigin[]" ></select>';
  100. $html .= '</div></div>';
  101. //destination
  102. $html .= '<div class="form-group">';
  103. $html .= '<label class="col-sm-2 control-label">'.get_lang('Courses from the destination session').': </label>';
  104. $html .= '<div class="col-sm-5" id="ajax_sessions_list_destination">';
  105. $html .= '<select class="form-control" name="sessions_list_destination" onchange="javascript: xajax_search_courses(this.value,\'destination\');">';
  106. $html .= '<option value = "0">'.get_lang('There are no sessions available').'</option></select ></div>';
  107. $html .= '<div class="col-sm-5" id="ajax_list_courses_destination">';
  108. $html .= '<select id="destination" class="form-control" name="SessionCoursesListDestination[]" ></select>';
  109. $html .= '</div></div>';
  110. $options = '<div class="radio"><label><input type="radio" id="copy_option_1" name="copy_option" value="full_copy" checked="checked"/>';
  111. $options .= get_lang('Full copy').'</label></div>';
  112. $options .= '<div class="radio"><label><input type="radio" id="copy_option_2" name="copy_option" value="select_items" disabled="disabled"/>';
  113. $options .= ' '.get_lang('Let me select learning objects').'</label></div>';
  114. $options .= '<div class="checkbox"><label><input type="checkbox" id="copy_base_content_id" name="copy_only_session_items" />'.get_lang('Copy only session items').'</label></div>';
  115. $html .= Display::panel($options, get_lang('Type of copy'));
  116. $html .= '<div class="form-group"><div class="col-sm-12">';
  117. $html .= '<button class="btn btn-success" type="submit" onclick="javascript:if(!confirm('."'".addslashes(api_htmlentities(get_lang('Please confirm your choice'), ENT_QUOTES))."'".')) return false;"><em class="fa fa-files-o"></em> '.get_lang('Copy course').'</button>';
  118. // Add Security token
  119. $html .= '<input type="hidden" value="'.Security::get_token().'" name="sec_token">';
  120. $html .= '</div></div>';
  121. $html .= '</form>';
  122. echo $html;
  123. }
  124. /**
  125. * @param int $id_session
  126. * @param string $type
  127. *
  128. * @return xajaxResponse
  129. */
  130. function search_courses($id_session, $type)
  131. {
  132. global $tbl_course, $tbl_session_rel_course;
  133. $xajax_response = new xajaxResponse();
  134. $select_destination = '';
  135. $return = null;
  136. if (!empty($type)) {
  137. $id_session = (int) $id_session;
  138. if ($type == 'origin') {
  139. $course_list = SessionManager::get_course_list_by_session_id($id_session);
  140. $temp_course_list = [];
  141. $return .= '<select id="origin" name="SessionCoursesListOrigin[]" class="form-control" onclick="javascript: checkSelected(this.id,\'copy_option_2\',\'title_option2\',\'destination\');">';
  142. foreach ($course_list as $course) {
  143. $temp_course_list[] = "'{$course['code']}'";
  144. $return .= '<option value="'.$course['code'].'" title="'.@htmlspecialchars($course['title'].' ('.$course['visual_code'].')', ENT_QUOTES, api_get_system_encoding()).'">'.$course['title'].' ('.$course['visual_code'].')</option>';
  145. }
  146. $return .= '</select>';
  147. Session::write('course_list', $temp_course_list);
  148. Session::write('session_origin', $id_session);
  149. // Build select for destination sessions where is not included current session from select origin
  150. if (!empty($id_session)) {
  151. $sessions = SessionManager::get_sessions_list([], ['name', 'ASC']);
  152. $select_destination .= '<select name="sessions_list_destination" class="form-control" onchange = "javascript: xajax_search_courses(this.value,\'destination\');">';
  153. $select_destination .= '<option value = "0">-- '.get_lang('Select a session').' --</option>';
  154. foreach ($sessions as $session) {
  155. if ($id_session == $session['id']) {
  156. continue;
  157. }
  158. if (!empty($session['category_name'])) {
  159. $session['category_name'] = ' ('.$session['category_name'].') ';
  160. }
  161. $select_destination .= '<option value="'.$session['id'].'">'.$session['name'].' '.$session['category_name'].'</option>';
  162. }
  163. $select_destination .= '</select>';
  164. $xajax_response->addAssign('ajax_sessions_list_destination', 'innerHTML', api_utf8_encode($select_destination));
  165. } else {
  166. $select_destination .= '<select name="sessions_list_destination" class="form-control" onchange = "javascript: xajax_search_courses(this.value,\'destination\');">';
  167. $select_destination .= '<option value = "0">'.get_lang('There are no sessions available').'</option>';
  168. $select_destination .= '</select>';
  169. $xajax_response->addAssign('ajax_sessions_list_destination', 'innerHTML', api_utf8_encode($select_destination));
  170. }
  171. // Select multiple destination empty
  172. $select_multiple_empty = '<select id="destination" name="SessionCoursesListDestination[]" class="form-control"></select>';
  173. // Send response by ajax
  174. $xajax_response->addAssign('ajax_list_courses_origin', 'innerHTML', api_utf8_encode($return));
  175. $xajax_response->addAssign('ajax_list_courses_destination', 'innerHTML', api_utf8_encode($select_multiple_empty));
  176. } else {
  177. // Search courses by id_session where course codes is include en courses list destination
  178. $sql = "SELECT c.code, c.visual_code, c.title, src.session_id
  179. FROM $tbl_course c, $tbl_session_rel_course src
  180. WHERE src.c_id = c.id
  181. AND src.session_id = '".intval($id_session)."'";
  182. //AND c.code IN ($list_courses_origin)";
  183. $rs = Database::query($sql);
  184. $course_list_destination = [];
  185. $return .= '<select id="destination" name="SessionCoursesListDestination[]" class="form-control">';
  186. while ($course = Database :: fetch_array($rs)) {
  187. $course_list_destination[] = $course['code'];
  188. $return .= '<option value="'.$course['code'].'" title="'.@htmlspecialchars($course['title'].' ('.$course['visual_code'].')', ENT_QUOTES, api_get_system_encoding()).'">'.$course['title'].' ('.$course['visual_code'].')</option>';
  189. }
  190. $return .= '</select>';
  191. Session::write('course_list_destination', $course_list_destination);
  192. // Send response by ajax
  193. $xajax_response->addAssign(
  194. 'ajax_list_courses_destination',
  195. 'innerHTML',
  196. api_utf8_encode($return)
  197. );
  198. }
  199. }
  200. return $xajax_response;
  201. }
  202. $xajax->processRequests();
  203. /* HTML head extra */
  204. $htmlHeadXtra[] = $xajax->getJavascript(api_get_path(WEB_LIBRARY_PATH).'xajax/');
  205. $htmlHeadXtra[] = '<script>
  206. function checkSelected(id_select,id_radio,id_title,id_destination) {
  207. var num=0;
  208. obj_origin = document.getElementById(id_select);
  209. obj_destination = document.getElementById(id_destination);
  210. for (x=0;x<obj_origin.options.length;x++) {
  211. if (obj_origin.options[x].selected) {
  212. if (obj_destination.options.length > 0) {
  213. for (y=0;y<obj_destination.options.length;y++) {
  214. if (obj_origin.options[x].value == obj_destination.options[y].value) {
  215. obj_destination.options[y].selected = true;
  216. }
  217. }
  218. }
  219. num++;
  220. } else {
  221. if (obj_destination.options.length > 0) {
  222. for (y=0;y<obj_destination.options.length;y++) {
  223. if (obj_origin.options[x].value == obj_destination.options[y].value) {
  224. obj_destination.options[y].selected = false;
  225. }
  226. }
  227. }
  228. }
  229. }
  230. }
  231. </script>';
  232. Display::display_header($nameTools);
  233. $with_base_content = true;
  234. if (isset($_POST['copy_only_session_items']) && $_POST['copy_only_session_items']) {
  235. $with_base_content = false;
  236. }
  237. /* MAIN CODE */
  238. if (Security::check_token('post') && (
  239. ($action === 'course_select_form') || (
  240. isset($_POST['copy_option']) &&
  241. $_POST['copy_option'] == 'full_copy'
  242. )
  243. )
  244. ) {
  245. // Clear token
  246. Security::clear_token();
  247. $destination_course = $origin_course = $destination_session = $origin_session = '';
  248. if ($action === 'course_select_form') {
  249. $destination_course = $_POST['destination_course'];
  250. $origin_course = $_POST['origin_course'];
  251. $destination_session = $_POST['destination_session'];
  252. $origin_session = $_POST['origin_session'];
  253. if ($course_code != $origin_course) {
  254. $course = CourseSelectForm::get_posted_course(
  255. 'copy_course',
  256. $origin_session,
  257. $origin_course
  258. );
  259. $cr = new CourseRestorer($course);
  260. //$cr->set_file_option($_POST['same_file_name_option']);
  261. $cr->restore($destination_course, $destination_session);
  262. echo Display::return_message(get_lang('Copying is finished'), 'confirmation');
  263. display_form();
  264. } else {
  265. echo Display::return_message(get_lang('Please select a course'), 'confirm');
  266. display_form();
  267. }
  268. } else {
  269. $arr_course_origin = [];
  270. $arr_course_destination = [];
  271. $destination_session = '';
  272. $origin_session = '';
  273. if (isset($_POST['SessionCoursesListOrigin'])) {
  274. $arr_course_origin = $_POST['SessionCoursesListOrigin'];
  275. }
  276. if (isset($_POST['SessionCoursesListDestination'])) {
  277. $arr_course_destination = $_POST['SessionCoursesListDestination'];
  278. }
  279. if (isset($_POST['sessions_list_destination'])) {
  280. $destination_session = $_POST['sessions_list_destination'];
  281. }
  282. if (isset($_POST['sessions_list_origin'])) {
  283. $origin_session = $_POST['sessions_list_origin'];
  284. }
  285. if ((is_array($arr_course_origin) && count($arr_course_origin) > 0) && !empty($destination_session)) {
  286. //We need only one value
  287. if (count($arr_course_origin) > 1 || count($arr_course_destination) > 1) {
  288. echo Display::return_message(get_lang('You must select a course from original session'), 'error');
  289. } else {
  290. //first element of the array
  291. $course_code = $arr_course_origin[0];
  292. $course_destinatination = $arr_course_destination[0];
  293. if ($course_code != $course_destinatination) {
  294. $course_origin = api_get_course_info($course_code);
  295. $cb = new CourseBuilder('', $course_origin);
  296. $course = $cb->build($origin_session, $course_code, $with_base_content);
  297. $cr = new CourseRestorer($course);
  298. $cr->restore($course_destinatination, $destination_session);
  299. echo Display::return_message(get_lang('Copying is finished'), 'confirm');
  300. display_form();
  301. } else {
  302. echo Display::return_message(get_lang('Please select a course'), 'confirm');
  303. display_form();
  304. }
  305. }
  306. } else {
  307. echo Display::return_message(get_lang('You must select a course from original session'), 'error');
  308. display_form();
  309. }
  310. }
  311. } elseif (Security::check_token('post') && (
  312. isset($_POST['copy_option']) &&
  313. $_POST['copy_option'] == 'select_items'
  314. )
  315. ) {
  316. // Clear token
  317. Security::clear_token();
  318. // Else, if a CourseSelectForm is requested, show it
  319. if (api_get_setting('show_glossary_in_documents') != 'none') {
  320. echo Display::return_message(get_lang('To export a document that has glossary terms with its references to the glossary, you have to make sure you include the glossary tool in the export'), 'normal');
  321. }
  322. $arr_course_origin = [];
  323. $arr_course_destination = [];
  324. $destination_session = '';
  325. $origin_session = '';
  326. if (isset($_POST['SessionCoursesListOrigin'])) {
  327. $arr_course_origin = $_POST['SessionCoursesListOrigin'];
  328. }
  329. if (isset($_POST['SessionCoursesListDestination'])) {
  330. $arr_course_destination = $_POST['SessionCoursesListDestination'];
  331. }
  332. if (isset($_POST['sessions_list_destination'])) {
  333. $destination_session = $_POST['sessions_list_destination'];
  334. }
  335. if (isset($_POST['sessions_list_origin'])) {
  336. $origin_session = $_POST['sessions_list_origin'];
  337. }
  338. if ((is_array($arr_course_origin) && count($arr_course_origin) > 0) && !empty($destination_session)) {
  339. echo Display::return_message(get_lang('If you want to export a course containing a test, you have to make sure the corresponding tests are included in the export, so you have to select them in the list of tests.'), 'normal');
  340. $course_origin = api_get_course_info($arr_course_origin[0]);
  341. $cb = new CourseBuilder('', $course_origin);
  342. $course = $cb->build($origin_session, $arr_course_origin[0], $with_base_content);
  343. $hiddenFields['destination_course'] = $arr_course_destination[0];
  344. $hiddenFields['origin_course'] = $arr_course_origin[0];
  345. $hiddenFields['destination_session'] = $destination_session;
  346. $hiddenFields['origin_session'] = $origin_session;
  347. // Add token to Course select form
  348. $hiddenFields['sec_token'] = Security::get_token();
  349. CourseSelectForm :: display_form($course, $hiddenFields, true);
  350. echo '<div style="float:right"><a href="javascript:window.history.go(-1);">'.
  351. Display::return_icon(
  352. 'back.png',
  353. get_lang('Back').' '.get_lang('To').' '.get_lang('Administration'),
  354. ['style' => 'vertical-align:middle']
  355. ).
  356. get_lang('Back').'</a></div>';
  357. } else {
  358. echo Display::return_message(
  359. get_lang('You must select a course from original session and select a destination session'),
  360. 'error'
  361. );
  362. display_form();
  363. }
  364. } else {
  365. display_form();
  366. }
  367. Display::display_footer();