copy_course_session.php 17 KB

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