upload.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. $succeed = false;
  73. if ($form->validate()) {
  74. if ($student_can_edit_in_session && $check) {
  75. $values = $form->getSubmitValues();
  76. // Process work
  77. $result = processWorkForm(
  78. $workInfo,
  79. $values,
  80. $course_info,
  81. $session_id,
  82. $group_id,
  83. $user_id,
  84. $_FILES['file'],
  85. api_get_configuration_value('assignment_prevent_duplicate_upload')
  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('IsNotPosibleSaveTheDocument'), '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(array('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 = array(
  109. get_lang('Upload'),
  110. get_lang('Upload').' ('.get_lang('Simple').')',
  111. );
  112. $multipleForm = new FormValidator('post');
  113. $multipleForm->addMultipleUpload($url);
  114. $tabs = Display::tabs(
  115. $headers,
  116. array($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('ResourceLockedByGradebook'), '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('ActionNotAllowed'), 'error'));
  132. }
  133. } else {
  134. Display::addFlash(Display::return_message(get_lang('ActionNotAllowed'), 'error'));
  135. }
  136. Display :: display_footer();