record_audio_rtc.ajax.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. require_once __DIR__.'/../global.inc.php';
  5. // Add security from Chamilo
  6. api_block_anonymous_users();
  7. $courseInfo = api_get_course_info();
  8. /** @var string $tool document or exercise */
  9. $tool = isset($_REQUEST['tool']) ? $_REQUEST['tool'] : '';
  10. $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : 'document'; // can be document or message
  11. if ($type == 'document') {
  12. api_protect_course_script();
  13. }
  14. $userId = api_get_user_id();
  15. if (!isset($_FILES['audio_blob'], $_REQUEST['audio_dir'])) {
  16. if ($tool === 'exercise') {
  17. header('Content-Type: application/json');
  18. echo json_encode([
  19. 'error' => true,
  20. 'message' => Display::return_message(get_lang('Upload failed, please check maximum file size limits and folder rights.'), 'error'),
  21. ]);
  22. Display::cleanFlashMessages();
  23. exit;
  24. }
  25. Display::addFlash(
  26. Display::return_message(get_lang('Upload failed, please check maximum file size limits and folder rights.'), 'error')
  27. );
  28. exit;
  29. }
  30. $file = isset($_FILES['audio_blob']) ? $_FILES['audio_blob'] : [];
  31. $file['file'] = $file;
  32. $audioDir = Security::remove_XSS($_REQUEST['audio_dir']);
  33. switch ($type) {
  34. case 'document':
  35. /*$dirBaseDocuments = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document';
  36. $saveDir = $dirBaseDocuments.$audioDir;
  37. if (!is_dir($saveDir)) {
  38. mkdir($saveDir, api_get_permissions_for_new_directories(), true);
  39. }*/
  40. $uploadedDocument = DocumentManager::upload_document(
  41. $file,
  42. $audioDir,
  43. $file['name'],
  44. null,
  45. 0,
  46. 'overwrite',
  47. false,
  48. in_array($tool, ['document', 'exercise'])
  49. );
  50. $error = empty($uploadedDocument) || !is_array($uploadedDocument);
  51. if (!$error) {
  52. $newDocId = $uploadedDocument['id'];
  53. $courseId = $uploadedDocument['c_id'];
  54. /** @var learnpath $lp */
  55. $lp = Session::read('oLP');
  56. $lpItemId = isset($_REQUEST['lp_item_id']) && !empty($_REQUEST['lp_item_id']) ? $_REQUEST['lp_item_id'] : null;
  57. if (!empty($lp) && empty($lpItemId)) {
  58. $lp->set_modified_on();
  59. $lpItem = new learnpathItem($lpItemId);
  60. $lpItem->add_audio_from_documents($newDocId);
  61. }
  62. $data = DocumentManager::get_document_data_by_id($newDocId, $courseInfo['code']);
  63. if ($tool === 'exercise') {
  64. header('Content-Type: application/json');
  65. echo json_encode([
  66. 'error' => $error,
  67. 'message' => Display::getFlashToString(),
  68. 'fileUrl' => $data['document_url'],
  69. ]);
  70. Display::cleanFlashMessages();
  71. exit;
  72. }
  73. echo $data['document_url'];
  74. }
  75. break;
  76. case 'message':
  77. Session::write('current_audio_id', $file['name']);
  78. api_upload_file('audio_message', $file, api_get_user_id());
  79. break;
  80. }