work.ajax.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 'delete_work':
  12. if ($isAllowedToEdit) {
  13. if (empty($_REQUEST['id'])) {
  14. return false;
  15. }
  16. $workList = explode(',', $_REQUEST['id']);
  17. foreach ($workList as $workId) {
  18. deleteDirWork($workId);
  19. }
  20. }
  21. break;
  22. case 'upload_correction_file':
  23. api_protect_course_script(true);
  24. // User access same as upload.php
  25. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  26. $itemId = isset($_GET['item_id']) ? intval($_GET['item_id']) : '';
  27. $result = array();
  28. if (!empty($_FILES) && !empty($itemId)) {
  29. $file = $_FILES['file'];
  30. $courseInfo = api_get_course_info();
  31. $workInfo = get_work_data_by_id($itemId);
  32. $workInfoParent = get_work_data_by_id($workInfo['parent_id']);
  33. $resultUpload = uploadWork($workInfoParent, $courseInfo, true, $workInfo);
  34. $work_table = Database:: get_course_table(
  35. TABLE_STUDENT_PUBLICATION
  36. );
  37. if (isset($resultUpload['url']) && !empty($resultUpload['url'])) {
  38. $title = isset($resultUpload['filename']) && !empty($resultUpload['filename']) ? $resultUpload['filename'] : get_lang('Untitled');
  39. $url = Database::escape_string($resultUpload['url']);
  40. $title = Database::escape_string($title);
  41. $sql = "UPDATE $work_table SET
  42. url_correction = '".$url."',
  43. title_correction = '".$title."'
  44. WHERE iid = $itemId";
  45. Database::query($sql);
  46. $result['title'] = $resultUpload['filename'];
  47. $result['url'] = 'view.php?'.api_get_cidreq().'&id='.$itemId;
  48. $json = array();
  49. $json['name'] = Display::url(
  50. api_htmlentities($result['title']),
  51. api_htmlentities($result['url']),
  52. array('target' => '_blank')
  53. );
  54. $json['type'] = api_htmlentities($file['type']);
  55. $json['size'] = format_file_size($file['size']);
  56. }
  57. if (isset($result['url'])) {
  58. $json['result'] = Display::return_icon(
  59. 'accept.png',
  60. get_lang('Uploaded')
  61. );
  62. } else {
  63. $json['result'] = Display::return_icon(
  64. 'exclamation.png',
  65. get_lang('Error')
  66. );
  67. }
  68. echo json_encode($json);
  69. }
  70. break;
  71. default:
  72. echo '';
  73. break;
  74. }
  75. exit;