upload_corrections.php 6.5 KB

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