work.ajax.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Responses to AJAX calls.
  5. */
  6. require_once __DIR__.'/../global.inc.php';
  7. require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
  8. $action = isset($_REQUEST['a']) ? $_REQUEST['a'] : null;
  9. $isAllowedToEdit = api_is_allowed_to_edit();
  10. switch ($action) {
  11. case 'upload_file':
  12. api_protect_course_script(true);
  13. $workId = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
  14. $workInfo = get_work_data_by_id($workId);
  15. $courseInfo = api_get_course_info();
  16. $sessionId = api_get_session_id();
  17. $userId = api_get_user_id();
  18. $groupId = api_get_group_id();
  19. $onlyOnePublication = api_get_configuration_value('allow_only_one_student_publication_per_user');
  20. if ($onlyOnePublication) {
  21. $count = get_work_count_by_student($userId, $workId);
  22. if ($count >= 1) {
  23. exit;
  24. }
  25. }
  26. if (!empty($_FILES)) {
  27. $files = $_FILES['files'];
  28. $fileList = [];
  29. foreach ($files as $name => $array) {
  30. $counter = 0;
  31. foreach ($array as $data) {
  32. $fileList[$counter][$name] = $data;
  33. $counter++;
  34. }
  35. }
  36. $resultList = [];
  37. foreach ($fileList as $file) {
  38. $globalFile = [];
  39. $globalFile['files'] = $file;
  40. $values = [
  41. 'contains_file' => 1,
  42. 'title' => $file['name'],
  43. 'description' => '',
  44. ];
  45. $result = processWorkForm(
  46. $workInfo,
  47. $values,
  48. $courseInfo,
  49. $sessionId,
  50. $groupId,
  51. $userId,
  52. $file,
  53. api_get_configuration_value('assignment_prevent_duplicate_upload'),
  54. false
  55. );
  56. $json = [];
  57. if (!empty($result) && is_array($result) && empty($result['error'])) {
  58. $json['name'] = api_htmlentities($result['title']);
  59. $json['link'] = Display::url(
  60. api_htmlentities($result['title']),
  61. api_htmlentities($result['view_url']),
  62. ['target' => '_blank']
  63. );
  64. $json['url'] = $result['view_url'];
  65. $json['size'] = '';
  66. $json['type'] = api_htmlentities($result['filetype']);
  67. $json['result'] = Display::return_icon(
  68. 'accept.png',
  69. get_lang('Uploaded')
  70. );
  71. } else {
  72. $json['url'] = '';
  73. $json['error'] = isset($result['error']) ? $result['error'] : get_lang('Error');
  74. }
  75. $resultList[] = $json;
  76. }
  77. echo json_encode(['files' => $resultList]);
  78. }
  79. break;
  80. case 'delete_work':
  81. if ($isAllowedToEdit) {
  82. if (empty($_REQUEST['id'])) {
  83. return false;
  84. }
  85. $workList = explode(',', $_REQUEST['id']);
  86. foreach ($workList as $workId) {
  87. deleteDirWork($workId);
  88. }
  89. }
  90. break;
  91. case 'upload_correction_file':
  92. api_protect_course_script(true);
  93. // User access same as upload.php
  94. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  95. $itemId = isset($_GET['item_id']) ? intval($_GET['item_id']) : '';
  96. $result = [];
  97. if (!empty($_FILES) && !empty($itemId)) {
  98. $file = $_FILES['file'];
  99. $courseInfo = api_get_course_info();
  100. $workInfo = get_work_data_by_id($itemId);
  101. $workInfoParent = get_work_data_by_id($workInfo['parent_id']);
  102. $resultUpload = uploadWork($workInfoParent, $courseInfo, true, $workInfo);
  103. if (!$resultUpload) {
  104. echo 'false';
  105. break;
  106. }
  107. $work_table = Database::get_course_table(
  108. TABLE_STUDENT_PUBLICATION
  109. );
  110. if (isset($resultUpload['url']) && !empty($resultUpload['url'])) {
  111. $title = isset($resultUpload['filename']) && !empty($resultUpload['filename']) ? $resultUpload['filename'] : get_lang('Untitled');
  112. $url = Database::escape_string($resultUpload['url']);
  113. $title = Database::escape_string($title);
  114. $sql = "UPDATE $work_table SET
  115. url_correction = '".$url."',
  116. title_correction = '".$title."'
  117. WHERE iid = $itemId";
  118. Database::query($sql);
  119. $result['title'] = $resultUpload['filename'];
  120. $result['url'] = 'view.php?'.api_get_cidreq().'&id='.$itemId;
  121. $json = [];
  122. $json['name'] = Display::url(
  123. api_htmlentities($result['title']),
  124. api_htmlentities($result['url']),
  125. ['target' => '_blank']
  126. );
  127. $json['type'] = api_htmlentities($file['type']);
  128. $json['size'] = format_file_size($file['size']);
  129. }
  130. if (isset($result['url'])) {
  131. $json['result'] = Display::return_icon(
  132. 'accept.png',
  133. get_lang('Uploaded'),
  134. [],
  135. ICON_SIZE_TINY
  136. );
  137. } else {
  138. $json['result'] = Display::return_icon(
  139. 'exclamation.png',
  140. get_lang('Error'),
  141. [],
  142. ICON_SIZE_TINY
  143. );
  144. }
  145. header('Content-Type: application/json');
  146. echo json_encode($json);
  147. }
  148. break;
  149. default:
  150. echo '';
  151. break;
  152. }
  153. exit;