copy_course.php 5.0 KB

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