From 09b01b9be61cafc723cbd13d1ce666044a093fe2 Mon Sep 17 00:00:00 2001 From: Daniel Barreto Date: Thu, 2 Oct 2014 16:19:25 -0500 Subject: [PATCH] Add comments and minor refactor - refs #7255 --- main/forum/editpost.php | 4 +- main/forum/forumfunction.inc.php | 135 +++++++++++++++++++------ main/forum/newthread.php | 2 + main/forum/reply.php | 1 + main/forum/viewthread_flat.inc.php | 1 - main/forum/viewthread_nested.inc.php | 1 - main/forum/viewthread_threaded.inc.php | 1 - main/inc/ajax/forum.ajax.php | 35 +++++-- 8 files changed, 132 insertions(+), 48 deletions(-) diff --git a/main/forum/editpost.php b/main/forum/editpost.php index ad33393f9a..ccc3f79e37 100755 --- a/main/forum/editpost.php +++ b/main/forum/editpost.php @@ -193,8 +193,9 @@ echo ""; echo ""; echo ''; -// The form for the reply +// Set forum attachment data into $_SESSION getAttachedFiles($current_forum['forum_id'], $current_thread['thread_id'], $current_post['post_id']); +// The form for the reply $values = show_edit_post_form($forum_setting, $current_post, $current_thread, $current_forum, isset($_SESSION['formelements']) ? $_SESSION['formelements'] : ''); if (!empty($values) and isset($_POST['SubmitPost'])) { @@ -217,6 +218,7 @@ if (!empty($values) and isset($_POST['SubmitPost'])) { } } } else { + // Only show Forum attachment ajax form when do not pass form submit $attachmentAjaxForm = getAttachmentAjaxForm($current_forum['forum_id'], $current_thread['thread_id'], $current_post['post_id']); echo $attachmentAjaxForm; } diff --git a/main/forum/forumfunction.inc.php b/main/forum/forumfunction.inc.php index 689dd0cb59..d5bb3b3f69 100755 --- a/main/forum/forumfunction.inc.php +++ b/main/forum/forumfunction.inc.php @@ -56,7 +56,7 @@ function setFocus() { $("#title_file").focus(); } '; - +// The next javascript script is to manage ajax upload file $htmlHeadXtra[] = " "; +// Recover Thread ID, will be used to generate delete attachment URL to do ajax $threadId = isset($_REQUEST['thread']) ? intval($_REQUEST['thread']) : ''; - +// The next javascript script is to delete file by ajax $htmlHeadXtra[] = ''; } else { + // Only show Forum attachment ajax form when do not pass form submit $attachmentAjaxForm = getAttachmentAjaxForm($current_forum['forum_id'], $current_thread['thread_id'], 0); echo $attachmentAjaxForm; } diff --git a/main/forum/viewthread_flat.inc.php b/main/forum/viewthread_flat.inc.php index 78bcdae2a9..8ceea559f9 100755 --- a/main/forum/viewthread_flat.inc.php +++ b/main/forum/viewthread_flat.inc.php @@ -141,7 +141,6 @@ if (isset($current_thread['thread_id'])) { // The check if there is an attachment - $attachment_list = get_attachment($row['post_id']); $attachment_list = getAllAttachment($row['post_id']); if (!empty($attachment_list) && is_array($attachment_list)) { foreach ($attachment_list as $attachment) { diff --git a/main/forum/viewthread_nested.inc.php b/main/forum/viewthread_nested.inc.php index db7b91051f..2d8d78987d 100755 --- a/main/forum/viewthread_nested.inc.php +++ b/main/forum/viewthread_nested.inc.php @@ -147,7 +147,6 @@ foreach ($rows as $post) { // The check if there is an attachment $attachment_list = getAllAttachment($post['post_id']); - if (!empty($attachment_list) && is_array($attachment_list)) { foreach ($attachment_list as $attachment) { echo ''; diff --git a/main/forum/viewthread_threaded.inc.php b/main/forum/viewthread_threaded.inc.php index 3050682402..c4dbfd177e 100755 --- a/main/forum/viewthread_threaded.inc.php +++ b/main/forum/viewthread_threaded.inc.php @@ -288,7 +288,6 @@ echo ""; // The check if there is an attachment $attachment_list = getAllAttachment($display_post_id); - if (!empty($attachment_list) && is_array($attachment_list)) { foreach ($attachment_list as $attachment) { echo ''; diff --git a/main/inc/ajax/forum.ajax.php b/main/inc/ajax/forum.ajax.php index 93a8c0b7cd..a14605c9d9 100644 --- a/main/inc/ajax/forum.ajax.php +++ b/main/inc/ajax/forum.ajax.php @@ -1,7 +1,9 @@ */ /** @@ -13,16 +15,19 @@ require_once api_get_path(LIBRARY_PATH).'document.lib.php'; /** * Main code */ +// Create a default error response +$json = array( + 'error' => true, + 'errorMessage' => 'ERROR', +); $action = isset($_REQUEST['a']) ? $_REQUEST['a'] : null; +// Check if exist action if (!empty($action)) { require_once api_get_path(SYS_CODE_PATH) . 'forum/forumfunction.inc.php'; - $json = array( - 'error' => true, - 'errorMessage' => 'ERROR', - ); switch($action) { case 'upload_file': - api_protect_course_script(true); + // First, protect this script + api_protect_course_script(false); if (!empty($_FILES) && !empty($_REQUEST['forum'])) { // The user is not allowed here if // 1. the forum category, forum or thread is invisible (visibility==0) @@ -52,18 +57,25 @@ if (!empty($action)) { $postId = isset($_REQUEST['postId'])? intval($_REQUEST['postId']) : null; $json['postId'] = $postId; if (!empty($courseId) && !is_null($forumId) && !is_null($threadId) && !is_null($postId)) { - + // Save forum attachment $attachId = add_forum_attachment_file('', $postId); if ($attachId !== false) { - $json = getAttachedFiles($forumId, $threadId, $postId, $attachId, $courseId); - $json['error'] = false; - $json['errorMessage'] = 'Success'; - + // Get prepared array of attachment data + $array = getAttachedFiles($forumId, $threadId, $postId, $attachId, $courseId); + // Check if array data is consistent + if (isset($array['name'])) { + $json['error'] = false; + $json['errorMessage'] = 'Success'; + $json = array_merge($json, $array); + } } } } break; case 'delete_file': + // First, protect this script + api_protect_course_script(false); + // Check if set attachment ID and thread ID if (isset($_REQUEST['attachId']) && isset($_REQUEST['thread'])) { api_block_course_item_locked_by_gradebook($_REQUEST['thread'], LINK_FORUM_THREAD); // The user is not allowed here if @@ -93,6 +105,7 @@ if (!empty($action)) { // If pass all previous control, user can edit post $attachId = $_REQUEST['attachId']; $threadId = $_REQUEST['thread']; + // Delete forum attachment from database and file system $affectedRows = delete_attachment(0, $attachId, false); if ($affectedRows > 0) { $json['error'] = false;