upload_corrections.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Symfony\Component\Finder\Finder;
  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. $workId = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : 0;
  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. $courseInfo = api_get_course_info();
  17. $course_code = $courseInfo['code'];
  18. $group_id = api_get_group_id();
  19. if (empty($workId)) {
  20. api_not_allowed(true);
  21. }
  22. protectWork($courseInfo, $workId);
  23. $workInfo = get_work_data_by_id($workId);
  24. if (empty($workInfo)) {
  25. api_not_allowed(true);
  26. }
  27. $student_can_edit_in_session = api_is_allowed_to_session_edit(false, true);
  28. $homework = get_work_assignment_by_id($workInfo['id']);
  29. $validationStatus = getWorkDateValidationStatus($homework);
  30. $interbreadcrumb[] = [
  31. 'url' => api_get_path(WEB_CODE_PATH).'work/work.php?'.api_get_cidreq(),
  32. 'name' => get_lang('StudentPublications'),
  33. ];
  34. $interbreadcrumb[] = [
  35. 'url' => api_get_path(WEB_CODE_PATH).'work/work_list.php?'.api_get_cidreq().'&id='.$workId,
  36. 'name' => $workInfo['title'],
  37. ];
  38. $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('UploadCorrections')];
  39. $downloadLink = api_get_path(WEB_CODE_PATH).'work/downloadfolder.inc.php?id='.$workId.'&'.api_get_cidreq();
  40. $form = new FormValidator(
  41. 'form',
  42. 'POST',
  43. api_get_self()."?".api_get_cidreq()."&id=".$workId,
  44. '',
  45. ['enctype' => "multipart/form-data"]
  46. );
  47. $form->addElement('header', get_lang('UploadCorrections'));
  48. $form->addHtml(Display::return_message(
  49. sprintf(
  50. get_lang('UploadCorrectionsExplanationWithDownloadLinkX'),
  51. $downloadLink
  52. ),
  53. 'normal',
  54. false
  55. ));
  56. $form->addElement('file', 'file', get_lang('UploadADocument'));
  57. $form->addProgress();
  58. $form->addRule('file', get_lang('ThisFieldIsRequired'), 'required');
  59. $form->addElement('hidden', 'id', $workId);
  60. $form->addButtonUpload(get_lang('Upload'));
  61. $succeed = false;
  62. if ($form->validate()) {
  63. $values = $form->getSubmitValues();
  64. $upload = process_uploaded_file($_FILES['file'], false);
  65. if ($upload) {
  66. $zip = new PclZip($_FILES['file']['tmp_name']);
  67. // Check the zip content (real size and file extension)
  68. $zipFileList = (array) $zip->listContent();
  69. $realSize = 0;
  70. foreach ($zipFileList as &$this_content) {
  71. $realSize += $this_content['size'];
  72. }
  73. $maxSpace = DocumentManager::get_course_quota();
  74. if (!DocumentManager::enough_space($realSize, $maxSpace)) {
  75. Display::addFlash(
  76. Display::return_message(
  77. get_lang('UplNotEnoughSpace'),
  78. 'warning'
  79. )
  80. );
  81. }
  82. $folder = api_get_unique_id();
  83. $destinationDir = api_get_path(SYS_ARCHIVE_PATH).$folder;
  84. mkdir($destinationDir, api_get_permissions_for_new_directories(), true);
  85. // Uncompress zip file
  86. // We extract using a callback function that "cleans" the path
  87. $zip->extract(
  88. PCLZIP_OPT_PATH,
  89. $destinationDir,
  90. PCLZIP_CB_PRE_EXTRACT,
  91. 'clean_up_files_in_zip',
  92. PCLZIP_OPT_REPLACE_NEWER
  93. );
  94. $result = get_work_user_list(null, null, null, null, $workId);
  95. if (empty($result)) {
  96. Display::addFlash(
  97. Display::return_message(
  98. get_lang('NoDataAvailable'),
  99. 'warning'
  100. )
  101. );
  102. }
  103. $finalResult = [];
  104. foreach ($result as $item) {
  105. $title = $item['title_clean'];
  106. $insert_date = str_replace([':', '-', ' '], '_', api_get_local_time($item['sent_date_from_db']));
  107. $title = api_replace_dangerous_char($insert_date.'_'.$item['username'].'_'.$title);
  108. $finalResult[$title] = $item['id'];
  109. }
  110. $coursePath = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/';
  111. $workDir = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/work/';
  112. $workDir .= basename($workInfo['url']).'/';
  113. $finder = new Finder();
  114. $finder->files()->in($destinationDir);
  115. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  116. //var_dump($finalResult);
  117. /** @var SplFileInfo $file */
  118. foreach ($finder as $file) {
  119. $fileName = $file->getBasename();
  120. if (isset($finalResult[$fileName])) {
  121. $workStudentId = $finalResult[$fileName];
  122. $workStudent = get_work_data_by_id($workStudentId);
  123. if ($workStudent) {
  124. if (!empty($workStudent['url_correction'])) {
  125. $correctionFilePath = $coursePath.$workStudent['url_correction'];
  126. $correctionTitle = $workStudent['title_correction'];
  127. } else {
  128. if (!empty($workStudent['url'])) {
  129. $correctionFilePath = $coursePath.$workStudent['url'].'_correction';
  130. $correctionTitle = $fileName;
  131. }
  132. }
  133. if (!empty($correctionFilePath)) {
  134. $result = copy(
  135. $file->getRealPath(),
  136. $correctionFilePath
  137. );
  138. $correctionTitle = Database::escape_string(
  139. $correctionTitle
  140. );
  141. $correctionFilePath = Database::escape_string(
  142. 'work/'.basename($workInfo['url']).'/'.basename($correctionFilePath)
  143. );
  144. if ($result) {
  145. $sql = "UPDATE $table SET
  146. url_correction = '".$correctionFilePath."',
  147. title_correction = '".$correctionTitle."'
  148. WHERE iid = $workStudentId";
  149. Database::query($sql);
  150. }
  151. }
  152. }
  153. }
  154. }
  155. Display::addFlash(
  156. Display::return_message(
  157. get_lang('Uploaded')
  158. )
  159. );
  160. }
  161. header('Location: '.api_get_path(WEB_CODE_PATH).'work/work_list_all.php?'.api_get_cidreq().'&id='.$workId);
  162. exit;
  163. }
  164. $htmlHeadXtra[] = to_javascript_work();
  165. Display :: display_header(null);
  166. if (!empty($workId)) {
  167. echo $validationStatus['message'];
  168. if ($is_allowed_to_edit) {
  169. $form->display();
  170. } else {
  171. api_not_allowed();
  172. }
  173. } else {
  174. api_not_allowed();
  175. }
  176. Display :: display_footer();