upload_corrections.php 6.6 KB

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