work.ajax.php 5.3 KB

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