work.ajax.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. $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. true
  47. );
  48. $json = array();
  49. if (!empty($result) && is_array($result) && empty($result['error'])) {
  50. $json['name'] = Display::url(
  51. api_htmlentities($result['title']),
  52. api_htmlentities($result['view_url']),
  53. array('target' => '_blank')
  54. );
  55. $json['url'] = $result['view_url'];
  56. $json['size'] = '';
  57. $json['type'] = api_htmlentities($result['filetype']);
  58. $json['result'] = Display::return_icon(
  59. 'accept.png',
  60. get_lang('Uploaded')
  61. );
  62. } else {
  63. $json['url'] = '';
  64. $json['error'] = isset($result['error']) ? $result['error'] : get_lang('Error');
  65. }
  66. $resultList[] = $json;
  67. }
  68. echo json_encode(['files' => $resultList]);
  69. }
  70. break;
  71. case 'delete_work':
  72. if ($isAllowedToEdit) {
  73. if (empty($_REQUEST['id'])) {
  74. return false;
  75. }
  76. $workList = explode(',', $_REQUEST['id']);
  77. foreach ($workList as $workId) {
  78. deleteDirWork($workId);
  79. }
  80. }
  81. break;
  82. case 'upload_correction_file':
  83. api_protect_course_script(true);
  84. // User access same as upload.php
  85. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  86. $itemId = isset($_GET['item_id']) ? intval($_GET['item_id']) : '';
  87. $result = array();
  88. if (!empty($_FILES) && !empty($itemId)) {
  89. $file = $_FILES['file'];
  90. $courseInfo = api_get_course_info();
  91. $workInfo = get_work_data_by_id($itemId);
  92. $workInfoParent = get_work_data_by_id($workInfo['parent_id']);
  93. $resultUpload = uploadWork($workInfoParent, $courseInfo, true, $workInfo);
  94. $work_table = Database:: get_course_table(
  95. TABLE_STUDENT_PUBLICATION
  96. );
  97. if (isset($resultUpload['url']) && !empty($resultUpload['url'])) {
  98. $title = isset($resultUpload['filename']) && !empty($resultUpload['filename']) ? $resultUpload['filename'] : get_lang('Untitled');
  99. $url = Database::escape_string($resultUpload['url']);
  100. $title = Database::escape_string($title);
  101. $sql = "UPDATE $work_table SET
  102. url_correction = '".$url."',
  103. title_correction = '".$title."'
  104. WHERE iid = $itemId";
  105. Database::query($sql);
  106. $result['title'] = $resultUpload['filename'];
  107. $result['url'] = 'view.php?'.api_get_cidreq().'&id='.$itemId;
  108. $json = array();
  109. $json['name'] = Display::url(
  110. api_htmlentities($result['title']),
  111. api_htmlentities($result['url']),
  112. array('target' => '_blank')
  113. );
  114. $json['type'] = api_htmlentities($file['type']);
  115. $json['size'] = format_file_size($file['size']);
  116. }
  117. if (isset($result['url'])) {
  118. $json['result'] = Display::return_icon(
  119. 'accept.png',
  120. get_lang('Uploaded')
  121. );
  122. } else {
  123. $json['result'] = Display::return_icon(
  124. 'exclamation.png',
  125. get_lang('Error')
  126. );
  127. }
  128. header('Content-Type: application/json');
  129. echo json_encode($json);
  130. }
  131. break;
  132. default:
  133. echo '';
  134. break;
  135. }
  136. exit;