work.ajax.php 5.6 KB

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