upload_corrections.php 6.8 KB

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