copy_survey.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.backup
  5. */
  6. require_once __DIR__.'/../inc/global.inc.php';
  7. $current_course_tool = TOOL_COURSE_MAINTENANCE;
  8. api_protect_course_script(true);
  9. // Notice for unauthorized people.
  10. if (!api_is_allowed_to_edit()) {
  11. api_not_allowed(true);
  12. }
  13. // Breadcrumbs
  14. $interbreadcrumb[] = [
  15. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey_list.php?'.api_get_cidreq(),
  16. 'name' => get_lang('Survey list'),
  17. ];
  18. // The section (for the tabs)
  19. $this_section = SECTION_COURSES;
  20. $surveyId = isset($_GET['survey_id']) ? (int) $_GET['survey_id'] : 0;
  21. if (empty($surveyId)) {
  22. api_not_allowed(true);
  23. }
  24. $survey = SurveyManager::get_survey($surveyId);
  25. if (empty($survey)) {
  26. api_not_allowed(true);
  27. }
  28. $surveyTitle = str_replace('&nbsp;', '', strip_tags($survey['title'].' ('.$survey['code'].') '));
  29. $form = new FormValidator('copy_survey', 'post', api_get_self().'?survey_id='.$surveyId.'&'.api_get_cidreq());
  30. $form->addElement(
  31. 'text',
  32. 'survey_title',
  33. get_lang('Survey'),
  34. ['value' => $surveyTitle, 'disabled' => 'disabled']
  35. );
  36. $form->addSelectAjax(
  37. 'destination_course',
  38. get_lang('Select target course'),
  39. null,
  40. [
  41. 'url' => api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=get_my_courses_and_sessions&'.api_get_cidreq(),
  42. ]
  43. );
  44. $form->addButtonCopy(get_lang('Copy survey'));
  45. // Add Security token
  46. $token = Security::get_existing_token();
  47. $form->addElement('hidden', 'sec_token');
  48. $form->setConstants(['sec_token' => $token]);
  49. // If a CourseSelectForm is posted or we should copy all resources, then copy them
  50. if ($form->validate() && Security::check_token('post')) {
  51. // Clear token
  52. Security::clear_token();
  53. $values = $form->getSubmitValues();
  54. $courseKey = $values['destination_course'];
  55. $courseParts = explode('_', $courseKey);
  56. $courseId = $courseParts[0];
  57. $sessionId = $courseParts[1];
  58. // Copy the survey to the target course
  59. $surveyCopyId = SurveyManager::copySurveySession($surveyId, $courseId, $sessionId);
  60. if ($surveyCopyId) {
  61. // Empty the copied survey
  62. SurveyManager::emptySurveyFromId($surveyCopyId);
  63. Display::addFlash(Display::return_message(get_lang('Survey copied')));
  64. } else {
  65. Display::addFlash(Display::return_message(get_lang('There was an error.'), 'warning'));
  66. }
  67. header('Location: '.api_get_self().'?'.api_get_cidreq().'&survey_id='.$surveyId);
  68. exit;
  69. }
  70. Display::display_header(get_lang('Copy survey'));
  71. echo Display::page_header(get_lang('Copy survey'));
  72. $form->display();
  73. Display::display_footer();