upload_from_template.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. require_once __DIR__.'/../inc/global.inc.php';
  5. $current_course_tool = TOOL_STUDENTPUBLICATION;
  6. api_protect_course_script(true);
  7. // Including necessary files
  8. require_once 'work.lib.php';
  9. $this_section = SECTION_COURSES;
  10. $work_id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : null;
  11. $documentId = isset($_REQUEST['document_id']) ? intval($_REQUEST['document_id']) : null;
  12. $is_allowed_to_edit = api_is_allowed_to_edit();
  13. $course_id = api_get_course_int_id();
  14. $user_id = api_get_user_id();
  15. $userInfo = api_get_user_info();
  16. $session_id = api_get_session_id();
  17. $course_info = api_get_course_info();
  18. $course_code = $course_info['code'];
  19. $group_id = api_get_group_id();
  20. if (empty($work_id)) {
  21. api_not_allowed(true);
  22. }
  23. protectWork($course_info, $work_id);
  24. $workInfo = get_work_data_by_id($work_id);
  25. $is_course_member = CourseManager::is_user_subscribed_in_real_or_linked_course(
  26. $user_id,
  27. $course_id,
  28. $session_id
  29. );
  30. $is_course_member = $is_course_member || api_is_platform_admin();
  31. if ($is_course_member == false) {
  32. api_not_allowed(true);
  33. }
  34. $check = Security::check_token('post');
  35. $token = Security::get_token();
  36. $student_can_edit_in_session = api_is_allowed_to_session_edit(false, true);
  37. $homework = get_work_assignment_by_id($workInfo['id']);
  38. $validationStatus = getWorkDateValidationStatus($homework);
  39. $interbreadcrumb[] = array(
  40. 'url' => api_get_path(WEB_CODE_PATH).'work/work.php?'.api_get_cidreq(),
  41. 'name' => get_lang('StudentPublications'),
  42. );
  43. $interbreadcrumb[] = array(
  44. 'url' => api_get_path(WEB_CODE_PATH).'work/work_list.php?'.api_get_cidreq().'&id='.$work_id,
  45. 'name' => $workInfo['title'],
  46. );
  47. $interbreadcrumb[] = array('url' => '#', 'name' => get_lang('UploadFromTemplate'));
  48. $form = new FormValidator(
  49. 'form',
  50. 'POST',
  51. api_get_self()."?".api_get_cidreq()."&id=".$work_id,
  52. '',
  53. array('enctype' => "multipart/form-data")
  54. );
  55. setWorkUploadForm($form, $workInfo['allow_text_assignment']);
  56. $form->addElement('hidden', 'document_id', $documentId);
  57. $form->addElement('hidden', 'id', $work_id);
  58. $form->addElement('hidden', 'sec_token', $token);
  59. $documentTemplateData = getDocumentTemplateFromWork($work_id, $course_info, $documentId);
  60. $defaults = [];
  61. if (!empty($documentTemplateData)) {
  62. $defaults['title'] = $userInfo['complete_name'].'_'.$documentTemplateData['title'].'_'.substr(api_get_utc_datetime(), 0, 10);
  63. $defaults['description'] = $documentTemplateData['file_content'];
  64. }
  65. $form->setDefaults($defaults);
  66. $succeed = false;
  67. if ($form->validate()) {
  68. if ($student_can_edit_in_session && $check) {
  69. $values = $form->getSubmitValues();
  70. // Process work
  71. $error_message = processWorkForm(
  72. $workInfo,
  73. $values,
  74. $course_info,
  75. $id_session,
  76. $group_id,
  77. $user_id,
  78. [],
  79. api_get_configuration_value('assignment_prevent_duplicate_upload')
  80. );
  81. $script = 'work_list.php';
  82. if ($is_allowed_to_edit) {
  83. $script = 'work_list_all.php';
  84. }
  85. Display::addFlash($error_message);
  86. header('Location: '.api_get_path(WEB_CODE_PATH).'work/'.$script.'?'.api_get_cidreq().'&id='.$work_id);
  87. exit;
  88. } else {
  89. // Bad token or can't add works
  90. Display::addFlash(Display::return_message(get_lang('IsNotPosibleSaveTheDocument'), 'error'));
  91. }
  92. }
  93. $htmlHeadXtra[] = to_javascript_work();
  94. Display :: display_header(null);
  95. if (!empty($work_id)) {
  96. echo $validationStatus['message'];
  97. if ($is_allowed_to_edit) {
  98. if (api_resource_is_locked_by_gradebook($work_id, LINK_STUDENTPUBLICATION)) {
  99. echo Display::display_warning_message(get_lang('ResourceLockedByGradebook'));
  100. } else {
  101. $form->display();
  102. }
  103. } elseif ($student_can_edit_in_session && $validationStatus['has_ended'] == false) {
  104. $form->display();
  105. } else {
  106. api_not_allowed();
  107. }
  108. } else {
  109. api_not_allowed();
  110. }
  111. Display :: display_footer();