copy_course_session.php 17 KB

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