copy_course.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. /**
  7. * @todo rework file in order to use addFlash
  8. *
  9. * @package chamilo.backup
  10. */
  11. // Setting the global file that gets the general configuration, the databases, the languages, ...
  12. require_once __DIR__.'/../inc/global.inc.php';
  13. $current_course_tool = TOOL_COURSE_MAINTENANCE;
  14. api_protect_course_script(true);
  15. if (!api_is_allowed_to_edit()) {
  16. api_not_allowed(true);
  17. }
  18. api_set_more_memory_and_time_limits();
  19. // Breadcrumbs
  20. $interbreadcrumb[] = [
  21. 'url' => api_get_path(WEB_CODE_PATH).'course_info/maintenance.php?'.api_get_cidreq(),
  22. 'name' => get_lang('Maintenance'),
  23. ];
  24. // The section (for the tabs)
  25. $this_section = SECTION_COURSES;
  26. // Display the header
  27. Display::display_header(get_lang('CopyCourse'));
  28. echo Display::page_header(get_lang('CopyCourse'));
  29. $action = isset($_POST['action']) ? $_POST['action'] : '';
  30. // If a CourseSelectForm is posted or we should copy all resources, then copy them
  31. if (Security::check_token('post') && (
  32. ($action === 'course_select_form') ||
  33. (isset($_POST['copy_option']) && $_POST['copy_option'] === 'full_copy')
  34. )
  35. ) {
  36. // Clear token
  37. Security::clear_token();
  38. if ($action === 'course_select_form') {
  39. $course = CourseSelectForm::get_posted_course('copy_course');
  40. } else {
  41. $cb = new CourseBuilder();
  42. $course = $cb->build();
  43. }
  44. $cr = new CourseRestorer($course);
  45. $cr->set_file_option($_POST['same_file_name_option']);
  46. $cr->restore($_POST['destination_course']);
  47. echo Display::return_message(
  48. get_lang('CopyFinished').': <a href="'.api_get_course_url($_POST['destination_course']).'">'.
  49. Security::remove_XSS($_POST['destination_course']).
  50. '</a>',
  51. 'normal',
  52. false
  53. );
  54. } elseif (Security::check_token('post') && (
  55. isset($_POST['copy_option']) &&
  56. $_POST['copy_option'] === 'select_items'
  57. )
  58. ) {
  59. // Clear token
  60. Security::clear_token();
  61. $cb = new CourseBuilder();
  62. $course = $cb->build();
  63. $hiddenFields = [];
  64. $hiddenFields['same_file_name_option'] = $_POST['same_file_name_option'];
  65. $hiddenFields['destination_course'] = $_POST['destination_course'];
  66. // Add token to Course select form
  67. $hiddenFields['sec_token'] = Security::get_token();
  68. CourseSelectForm::display_form($course, $hiddenFields, true);
  69. } else {
  70. $table_c = Database::get_main_table(TABLE_MAIN_COURSE);
  71. $table_cu = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  72. $user_info = api_get_user_info();
  73. $course_info = api_get_course_info();
  74. $courseList = CourseManager::get_courses_list_by_user_id(
  75. $user_info['user_id'],
  76. false,
  77. false,
  78. false,
  79. [$course_info['real_id']]
  80. );
  81. if (empty($courseList)) {
  82. echo Display::return_message(get_lang('NoDestinationCoursesAvailable'), 'normal');
  83. } else {
  84. $options = [];
  85. foreach ($courseList as $courseItem) {
  86. $courseInfo = api_get_course_info_by_id($courseItem['real_id']);
  87. $options[$courseInfo['code']] = $courseInfo['title'].' ('.$courseInfo['code'].')';
  88. }
  89. $form = new FormValidator(
  90. 'copy_course',
  91. 'post',
  92. api_get_path(WEB_CODE_PATH).'coursecopy/copy_course.php?'.api_get_cidreq()
  93. );
  94. $form->addElement('select', 'destination_course', get_lang('SelectDestinationCourse'), $options);
  95. $group = [];
  96. $group[] = $form->createElement('radio', 'copy_option', null, get_lang('FullCopy'), 'full_copy');
  97. $group[] = $form->createElement('radio', 'copy_option', null, get_lang('LetMeSelectItems'), 'select_items');
  98. $form->addGroup($group, '', get_lang('SelectOptionForBackup'));
  99. $group = [];
  100. $group[] = $form->createElement(
  101. 'radio',
  102. 'same_file_name_option',
  103. null,
  104. get_lang('SameFilenameSkip'),
  105. FILE_SKIP
  106. );
  107. $group[] = $form->createElement(
  108. 'radio',
  109. 'same_file_name_option',
  110. null,
  111. get_lang('SameFilenameRename'),
  112. FILE_RENAME
  113. );
  114. $group[] = $form->createElement(
  115. 'radio',
  116. 'same_file_name_option',
  117. null,
  118. get_lang('SameFilenameOverwrite'),
  119. FILE_OVERWRITE
  120. );
  121. $form->addGroup($group, '', get_lang('SameFilename'));
  122. $form->addProgress();
  123. $form->addButtonSave(get_lang('CopyCourse'));
  124. $form->setDefaults(['copy_option' => 'select_items', 'same_file_name_option' => FILE_OVERWRITE]);
  125. // Add Security token
  126. $token = Security::get_token();
  127. $form->addElement('hidden', 'sec_token');
  128. $form->setConstants(['sec_token' => $token]);
  129. $form->display();
  130. }
  131. }
  132. Display::display_footer();