copy_course.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.backup
  5. */
  6. /**
  7. * Code
  8. */
  9. // Language files that need to be included
  10. $language_file = array('exercice', 'coursebackup', 'admin');
  11. // Setting the global file that gets the general configuration, the databases, the languages, ...
  12. require_once '../inc/global.inc.php';
  13. $current_course_tool = TOOL_COURSE_MAINTENANCE;
  14. api_protect_course_script(true);
  15. // Including additional libraries
  16. require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  17. require_once 'classes/CourseBuilder.class.php';
  18. require_once 'classes/CourseRestorer.class.php';
  19. require_once 'classes/CourseSelectForm.class.php';
  20. // Notice for unauthorized people.
  21. if (!api_is_allowed_to_edit()) {
  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. //ini_set('post_max_size', "512M");
  29. }
  30. // Breadcrumbs
  31. $interbreadcrumb[] = array('url' => '../course_info/maintenance.php', 'name' => get_lang('Maintenance'));
  32. // The section (for the tabs)
  33. $this_section = SECTION_COURSES;
  34. // Display the header
  35. Display::display_header(get_lang('CopyCourse'));
  36. echo Display::page_header(get_lang('CopyCourse'));
  37. /* MAIN CODE */
  38. // If a CourseSelectForm is posted or we should copy all resources, then copy them
  39. if (Security::check_token('post') && (
  40. (
  41. isset($_POST['action']) &&
  42. $_POST['action'] == 'course_select_form') || (
  43. isset($_POST['copy_option']) && $_POST['copy_option'] == 'full_copy'
  44. )
  45. )
  46. ) {
  47. // Clear token
  48. Security::clear_token();
  49. if (isset($_POST['action']) && $_POST['action'] == 'course_select_form') {
  50. $course = CourseSelectForm :: get_posted_course('copy_course');
  51. } else {
  52. $cb = new CourseBuilder();
  53. $course = $cb->build();
  54. }
  55. $cr = new CourseRestorer($course);
  56. $cr->set_file_option($_POST['same_file_name_option']);
  57. $cr->restore($_POST['destination_course']);
  58. Display::display_normal_message(get_lang('CopyFinished').': <a href="'.api_get_course_url($_POST['destination_course']).'">'.$_POST['destination_course'].'</a>', false);
  59. } elseif (Security::check_token('post') && (
  60. isset ($_POST['copy_option']) &&
  61. $_POST['copy_option'] == 'select_items'
  62. )
  63. ) {
  64. // Clear token
  65. Security::clear_token();
  66. $cb = new CourseBuilder();
  67. $course = $cb->build();
  68. $hiddenFields = array();
  69. $hiddenFields['same_file_name_option'] = $_POST['same_file_name_option'];
  70. $hiddenFields['destination_course'] = $_POST['destination_course'];
  71. // Add token to Course select form
  72. $hiddenFields['sec_token'] = Security::get_token();
  73. CourseSelectForm::display_form($course, $hiddenFields, true);
  74. } else {
  75. $table_c = Database :: get_main_table(TABLE_MAIN_COURSE);
  76. $table_cu = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  77. $user_info = api_get_user_info();
  78. $course_info = api_get_course_info();
  79. $sql = 'SELECT *
  80. FROM '.$table_c.' c, '.$table_cu.' cu
  81. WHERE cu.course_code = c.code';
  82. if (!api_is_platform_admin()) {
  83. $sql .= ' AND cu.status=1 ';
  84. }
  85. $sql .= ' AND
  86. target_course_code IS NULL AND
  87. cu.user_id = '.$user_info['user_id'].' AND
  88. c.code != '."'".$course_info['sysCode']."'".'
  89. ORDER BY title ASC';
  90. $res = Database::query($sql);
  91. if (Database::num_rows($res) == 0) {
  92. Display::display_normal_message(get_lang('NoDestinationCoursesAvailable'));
  93. } else {
  94. $options = array();
  95. while ($obj = Database::fetch_object($res)) {
  96. $options[$obj->code] = $obj->title.' ('.$obj->code.')';
  97. }
  98. $form = new FormValidator('copy_course', 'post', 'copy_course.php?'.api_get_cidreq());
  99. $form->addElement('select', 'destination_course', get_lang('SelectDestinationCourse'), $options);
  100. $group = array();
  101. $group[] = $form->createElement('radio', 'copy_option', null, get_lang('FullCopy'), 'full_copy');
  102. $group[] = $form->createElement('radio', 'copy_option', null, get_lang('LetMeSelectItems'), 'select_items');
  103. $form->addGroup($group, '', get_lang('SelectOptionForBackup'));
  104. $group = array();
  105. $group[] = $form->createElement('radio', 'same_file_name_option', null, get_lang('SameFilenameSkip'), FILE_SKIP);
  106. $group[] = $form->createElement('radio', 'same_file_name_option', null, get_lang('SameFilenameRename'), FILE_RENAME);
  107. $group[] = $form->createElement('radio', 'same_file_name_option', null, get_lang('SameFilenameOverwrite'), FILE_OVERWRITE);
  108. $form->addGroup($group, '', get_lang('SameFilename'));
  109. $form->add_progress_bar();
  110. $form->addElement('style_submit_button', 'submit', get_lang('CopyCourse'), 'class="save"');
  111. $form->setDefaults(array('copy_option' =>'select_items','same_file_name_option' => FILE_OVERWRITE));
  112. // Add Security token
  113. $token = Security::get_token();
  114. $form->addElement('hidden', 'sec_token');
  115. $form->setConstants(array('sec_token' => $token));
  116. $form->display();
  117. }
  118. }
  119. Display::display_footer();