upload.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once __DIR__.'/../inc/global.inc.php';
  4. $current_course_tool = TOOL_STUDENTPUBLICATION;
  5. api_protect_course_script(true);
  6. // Including necessary files
  7. require_once 'work.lib.php';
  8. $this_section = SECTION_COURSES;
  9. $work_id = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : null;
  10. $is_allowed_to_edit = api_is_allowed_to_edit();
  11. $course_id = api_get_course_int_id();
  12. $user_id = api_get_user_id();
  13. $userInfo = api_get_user_info();
  14. $session_id = api_get_session_id();
  15. $course_info = api_get_course_info();
  16. $course_code = $course_info['code'];
  17. $group_id = api_get_group_id();
  18. if (empty($work_id)) {
  19. api_not_allowed(true);
  20. }
  21. protectWork($course_info, $work_id);
  22. $workInfo = get_work_data_by_id($work_id);
  23. $is_course_member = CourseManager::is_user_subscribed_in_real_or_linked_course(
  24. $user_id,
  25. $course_id,
  26. $session_id
  27. );
  28. $is_course_member = $is_course_member || api_is_platform_admin();
  29. if ($is_course_member == false || api_is_invitee()) {
  30. api_not_allowed(true);
  31. }
  32. $check = Security::check_token('post');
  33. $token = Security::get_token();
  34. $student_can_edit_in_session = api_is_allowed_to_session_edit(false, true);
  35. $onlyOnePublication = api_get_configuration_value('allow_only_one_student_publication_per_user');
  36. if ($onlyOnePublication) {
  37. $count = get_work_count_by_student($user_id, $work_id);
  38. if ($count >= 1) {
  39. api_not_allowed(true);
  40. }
  41. }
  42. $homework = get_work_assignment_by_id($workInfo['id']);
  43. $validationStatus = getWorkDateValidationStatus($homework);
  44. $interbreadcrumb[] = [
  45. 'url' => api_get_path(WEB_CODE_PATH).'work/work.php?'.api_get_cidreq(),
  46. 'name' => get_lang('Assignments'),
  47. ];
  48. $interbreadcrumb[] = [
  49. 'url' => api_get_path(WEB_CODE_PATH).'work/work_list.php?'.api_get_cidreq().'&id='.$work_id,
  50. 'name' => $workInfo['title'],
  51. ];
  52. $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('Upload a document')];
  53. $form = new FormValidator(
  54. 'form-work',
  55. 'POST',
  56. api_get_self()."?".api_get_cidreq()."&id=".$work_id,
  57. '',
  58. ['enctype' => "multipart/form-data"]
  59. );
  60. setWorkUploadForm($form, $workInfo['allow_text_assignment']);
  61. $form->addHidden('id', $work_id);
  62. $form->addHidden('sec_token', $token);
  63. $allowRedirect = api_get_configuration_value('allow_redirect_to_main_page_after_work_upload');
  64. $urlToRedirect = '';
  65. if ($allowRedirect) {
  66. $urlToRedirect = api_get_path(WEB_CODE_PATH).'work/work.php?'.api_get_cidreq();
  67. }
  68. $succeed = false;
  69. if ($form->validate()) {
  70. if ($student_can_edit_in_session && $check) {
  71. $values = $form->getSubmitValues();
  72. // Process work
  73. $result = processWorkForm(
  74. $workInfo,
  75. $values,
  76. $course_info,
  77. $session_id,
  78. $group_id,
  79. $user_id,
  80. $_FILES['file'],
  81. api_get_configuration_value('assignment_prevent_duplicate_upload')
  82. );
  83. if ($allowRedirect) {
  84. header('Location: '.$urlToRedirect);
  85. exit;
  86. }
  87. $script = 'work_list.php';
  88. if ($is_allowed_to_edit) {
  89. $script = 'work_list_all.php';
  90. }
  91. header('Location: '.api_get_path(WEB_CODE_PATH).'work/'.$script.'?'.api_get_cidreq().'&id='.$work_id);
  92. exit;
  93. } else {
  94. // Bad token or can't add works
  95. Display::addFlash(
  96. Display::return_message(get_lang('Impossible to save the document'), 'error')
  97. );
  98. }
  99. }
  100. $url = api_get_path(WEB_AJAX_PATH).'work.ajax.php?'.api_get_cidreq().'&a=upload_file&id='.$work_id;
  101. $htmlHeadXtra[] = api_get_jquery_libraries_js(['jquery-ui', 'jquery-upload']);
  102. $htmlHeadXtra[] = to_javascript_work();
  103. Display::display_header(null);
  104. // Only text
  105. if ($workInfo['allow_text_assignment'] == 1) {
  106. $tabs = $form->returnForm();
  107. } else {
  108. $headers = [
  109. get_lang('Upload'),
  110. get_lang('Upload').' ('.get_lang('Simple').')',
  111. ];
  112. $multipleForm = new FormValidator('post');
  113. $multipleForm->addMultipleUpload($url, $urlToRedirect);
  114. $tabs = Display::tabs(
  115. $headers,
  116. [$multipleForm->returnForm(), $form->returnForm()],
  117. 'tabs'
  118. );
  119. }
  120. if (!empty($work_id)) {
  121. echo $validationStatus['message'];
  122. if ($is_allowed_to_edit) {
  123. if (api_resource_is_locked_by_gradebook($work_id, LINK_STUDENTPUBLICATION)) {
  124. echo Display::return_message(get_lang('This option is not available because this activity is contained by an assessment, which is currently locked. To unlock the assessment, ask your platform administrator.'), 'warning');
  125. } else {
  126. echo $tabs;
  127. }
  128. } elseif ($student_can_edit_in_session && $validationStatus['has_ended'] == false) {
  129. echo $tabs;
  130. } else {
  131. Display::addFlash(Display::return_message(get_lang('Action not allowed'), 'error'));
  132. }
  133. } else {
  134. Display::addFlash(Display::return_message(get_lang('Action not allowed'), 'error'));
  135. }
  136. Display :: display_footer();