upload.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. $is_allowed_to_edit = api_is_allowed_to_edit();
  12. $course_id = api_get_course_int_id();
  13. $user_id = api_get_user_id();
  14. $userInfo = api_get_user_info();
  15. $session_id = api_get_session_id();
  16. $course_info = api_get_course_info();
  17. $course_code = $course_info['code'];
  18. $group_id = api_get_group_id();
  19. if (empty($work_id)) {
  20. api_not_allowed(true);
  21. }
  22. protectWork($course_info, $work_id);
  23. $workInfo = get_work_data_by_id($work_id);
  24. $is_course_member = CourseManager::is_user_subscribed_in_real_or_linked_course(
  25. $user_id,
  26. $course_id,
  27. $session_id
  28. );
  29. $is_course_member = $is_course_member || api_is_platform_admin();
  30. if ($is_course_member == false || api_is_invitee()) {
  31. api_not_allowed(true);
  32. }
  33. $check = Security::check_token('post');
  34. $token = Security::get_token();
  35. $student_can_edit_in_session = api_is_allowed_to_session_edit(false, true);
  36. // @todo add an option to allow/block multiple attempts.
  37. /*
  38. if (!empty($workInfo) && !empty($workInfo['qualification'])) {
  39. $count = get_work_count_by_student($user_id, $work_id);
  40. if ($count >= 1) {
  41. Display::display_header();
  42. if (api_get_course_setting('student_delete_own_publication') == '1') {
  43. echo Display::return_message(get_lang('CantUploadDeleteYourPaperFirst'), 'warning');
  44. } else {
  45. echo Display::return_message(get_lang('YouAlreadySentAPaperYouCantUpload'), 'warning');
  46. }
  47. Display::display_footer();
  48. exit;
  49. }
  50. }*/
  51. $homework = get_work_assignment_by_id($workInfo['id']);
  52. $validationStatus = getWorkDateValidationStatus($homework);
  53. $interbreadcrumb[] = array(
  54. 'url' => api_get_path(WEB_CODE_PATH).'work/work.php?'.api_get_cidreq(),
  55. 'name' => get_lang('StudentPublications')
  56. );
  57. $interbreadcrumb[] = array(
  58. 'url' => api_get_path(WEB_CODE_PATH).'work/work_list.php?'.api_get_cidreq().'&id='.$work_id,
  59. 'name' => $workInfo['title']
  60. );
  61. $interbreadcrumb[] = array('url' => '#', 'name' => get_lang('UploadADocument'));
  62. $form = new FormValidator(
  63. 'form-work',
  64. 'POST',
  65. api_get_self()."?".api_get_cidreq()."&id=".$work_id,
  66. '',
  67. array('enctype' => "multipart/form-data")
  68. );
  69. setWorkUploadForm($form, $workInfo['allow_text_assignment']);
  70. $form->addElement('hidden', 'id', $work_id);
  71. $form->addElement('hidden', 'sec_token', $token);
  72. $allowRedirect = api_get_configuration_value('allow_redirect_to_main_page_after_work_upload');
  73. $urlToRedirect = '';
  74. if ($allowRedirect) {
  75. $urlToRedirect = api_get_path(WEB_CODE_PATH).'work/work.php?'.api_get_cidreq();
  76. }
  77. $succeed = false;
  78. if ($form->validate()) {
  79. if ($student_can_edit_in_session && $check) {
  80. $values = $form->getSubmitValues();
  81. // Process work
  82. $result = processWorkForm(
  83. $workInfo,
  84. $values,
  85. $course_info,
  86. $session_id,
  87. $group_id,
  88. $user_id,
  89. $_FILES['file'],
  90. api_get_configuration_value('assignment_prevent_duplicate_upload')
  91. );
  92. if ($allowRedirect) {
  93. header('Location: '.$urlToRedirect);
  94. exit;
  95. }
  96. $script = 'work_list.php';
  97. if ($is_allowed_to_edit) {
  98. $script = 'work_list_all.php';
  99. }
  100. header('Location: '.api_get_path(WEB_CODE_PATH).'work/'.$script.'?'.api_get_cidreq().'&id='.$work_id);
  101. exit;
  102. } else {
  103. // Bad token or can't add works
  104. Display::addFlash(
  105. Display::return_message(get_lang('ImpossibleToSaveTheDocument'), 'error')
  106. );
  107. }
  108. }
  109. $url = api_get_path(WEB_AJAX_PATH).'work.ajax.php?'.api_get_cidreq().'&a=upload_file&id='.$work_id;
  110. $htmlHeadXtra[] = api_get_jquery_libraries_js(array('jquery-ui', 'jquery-upload'));
  111. $htmlHeadXtra[] = to_javascript_work();
  112. Display :: display_header(null);
  113. // Only text
  114. if ($workInfo['allow_text_assignment'] == 1) {
  115. $tabs = $form->returnForm();
  116. } else {
  117. $headers = array(
  118. get_lang('Upload'),
  119. get_lang('Upload').' ('.get_lang('Simple').')',
  120. );
  121. $multipleForm = new FormValidator('post');
  122. $multipleForm->addMultipleUpload($url, $urlToRedirect);
  123. $tabs = Display::tabs(
  124. $headers,
  125. array($multipleForm->returnForm(), $form->returnForm()),
  126. 'tabs'
  127. );
  128. }
  129. if (!empty($work_id)) {
  130. echo $validationStatus['message'];
  131. if ($is_allowed_to_edit) {
  132. if (api_resource_is_locked_by_gradebook($work_id, LINK_STUDENTPUBLICATION)) {
  133. echo Display::return_message(get_lang('ResourceLockedByGradebook'), 'warning');
  134. } else {
  135. echo $tabs;
  136. }
  137. } elseif ($student_can_edit_in_session && $validationStatus['has_ended'] == false) {
  138. echo $tabs;
  139. } else {
  140. Display::addFlash(Display::return_message(get_lang('ActionNotAllowed'), 'error'));
  141. }
  142. } else {
  143. Display::addFlash(Display::return_message(get_lang('ActionNotAllowed'), 'error'));
  144. }
  145. Display :: display_footer();