upload_corrections.php 6.8 KB

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