Skip to content

Commit

Permalink
Editor : Add upload image for students - BT18997
Browse files Browse the repository at this point in the history
  • Loading branch information
cfasanando committed Aug 12, 2021
1 parent 6557283 commit 736e323
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 22 deletions.
68 changes: 47 additions & 21 deletions main/inc/ajax/document.ajax.php
Expand Up @@ -85,18 +85,13 @@
}

if (!empty($_FILES)) {
$isCkUploadImage = ($_COOKIE['ckCsrfToken'] == $_POST['ckCsrfToken']); // it comes from uploaimage drag and drop ckeditor
$files = $_FILES['files'];
$fileList = [];
if ($isCkUploadImage) {
$fileList[0] = $_FILES['upload'];
} else {
$files = $_FILES['files'];
foreach ($files as $name => $array) {
$counter = 0;
foreach ($array as $data) {
$fileList[$counter][$name] = $data;
$counter++;
}
foreach ($files as $name => $array) {
$counter = 0;
foreach ($array as $data) {
$fileList[$counter][$name] = $data;
$counter++;
}
}

Expand Down Expand Up @@ -139,19 +134,50 @@
$resultList[] = $json;
}

if ($isCkUploadImage) {
$ckResult = $resultList[0];
$courseInfo = api_get_course_info();
$courseDir = $courseInfo['path'].'/document';
$webCoursePath = api_get_path(WEB_COURSE_PATH);
$url = $webCoursePath.$courseDir.$currentDirectory.$ckResult['name'];
$data = ['uploaded' => 1, 'fileName' => $ckResult['name'], 'url' => $url];
echo json_encode($data);
echo json_encode(['files' => $resultList]);
}
exit;
break;
case 'ck_uploadimage':
api_protect_course_script(true);
$isCkUploadImage = ($_COOKIE['ckCsrfToken'] == $_POST['ckCsrfToken']); // it comes from uploaimage drag and drop ckeditor
if ($isCkUploadImage) {
$data = [];
$fileUpload = $_FILES['upload'];
$currentDirectory = $_REQUEST['curdirpath'];
$isAllowedToEdit = api_is_allowed_to_edit(null, true);
if ($isAllowedToEdit) {
$globalFile['files'] = $fileUpload;
$result = DocumentManager::upload_document(
$globalFile,
$currentDirectory,
'',
'', // comment
0,
'',
false,
false,
'files'
);
if ($result) {
$courseInfo = api_get_course_info();
$courseDir = $courseInfo['path'].'/document';
$webCoursePath = api_get_path(WEB_COURSE_PATH);
$url = $webCoursePath.$courseDir.$currentDirectory.$fileUpload['name'];
$data = ['uploaded' => 1, 'fileName' => $fileUpload['name'], 'url' => $url];
}
} else {
echo json_encode(['files' => $resultList]);
$userId = api_get_user_id();
$syspath = UserManager::getUserPathById($userId, 'system').'my_files'.$currentDirectory;
$webpath = UserManager::getUserPathById($userId, 'web').'my_files'.$currentDirectory;
if (move_uploaded_file($fileUpload['tmp_name'], $syspath.$fileUpload['name'])) {
$url = $webpath.$fileUpload['name'];
$data = ['uploaded' => 1, 'fileName' => $fileUpload['name'], 'url' => $url];
}
}
echo json_encode($data);
exit;
}
exit;
break;
case 'document_preview':
$courseInfo = api_get_course_info_by_id($_REQUEST['course_id']);
Expand Down
Expand Up @@ -177,7 +177,7 @@ public function getConfig()
$config['flash_flvPlayer'] = api_get_path(WEB_LIBRARY_JS_PATH).'ckeditor/plugins/flash/swf/player.swf';

if (api_get_configuration_value('enable_uploadimage_editor')) {
$config['imageUploadUrl'] = api_get_path(WEB_AJAX_PATH).'document.ajax.php?'.api_get_cidreq().'&a=upload_file&curdirpath=/';
$config['imageUploadUrl'] = api_get_path(WEB_AJAX_PATH).'document.ajax.php?'.api_get_cidreq().'&a=ck_uploadimage&curdirpath=/';
}
/*filebrowserFlashBrowseUrl
filebrowserFlashUploadUrl
Expand Down

0 comments on commit 736e323

Please sign in to comment.