copy_course.php 5.1 KB

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