/**
 * This function stores the edit of a post in the forum_post table.
 *
 * @param array
 * @return void HTML
 *
 * @author Patrick Cool <*****@*****.**>, Ghent University
 * @version february 2006, dokeos 1.8
 */
function store_edit_post($values)
{
    $threadTable = Database::get_course_table(TABLE_FORUM_THREAD);
    $table_posts = Database::get_course_table(TABLE_FORUM_POST);
    $course_id = api_get_course_int_id();
    //check if this post is the first of the thread
    // First we check if the change affects the thread and if so we commit
    // the changes (sticky and post_title=thread_title are relevant).
    $posts = getPosts($values['thread_id']);
    $first_post = null;
    if (!empty($posts) && count($posts) > 0 && isset($posts[0])) {
        $first_post = $posts[0];
    }
    if (!empty($first_post) && $first_post['post_id'] == $values['post_id']) {
        $params = ['thread_title' => $values['post_title'], 'thread_sticky' => isset($values['thread_sticky']) ? $values['thread_sticky'] : null, 'thread_title_qualify' => $values['calification_notebook_title'], 'thread_qualify_max' => $values['numeric_calification'], 'thread_weight' => $values['weight_calification'], 'thread_peer_qualify' => $values['thread_peer_qualify']];
        $where = ['c_id = ? AND thread_id = ?' => [$course_id, $values['thread_id']]];
        Database::update($threadTable, $params, $where);
    }
    // Update the post_title and the post_text.
    $params = ['post_title' => $values['post_title'], 'post_text' => $values['post_text'], 'post_notification' => isset($values['post_notification']) ? $values['post_notification'] : ''];
    $where = ['c_id = ? AND post_id = ?' => [$course_id, $values['post_id']]];
    Database::update($table_posts, $params, $where);
    // Update attached files
    if (!empty($_POST['file_ids']) && is_array($_POST['file_ids'])) {
        foreach ($_POST['file_ids'] as $key => $id) {
            editAttachedFile(array('comment' => $_POST['file_comments'][$key], 'post_id' => $values['post_id']), $id);
        }
    }
    if (!empty($values['remove_attach'])) {
        delete_attachment($values['post_id']);
    }
    if (empty($values['id_attach'])) {
        add_forum_attachment_file(isset($values['file_comment']) ? $values['file_comment'] : null, $values['post_id']);
    } else {
        edit_forum_attachment_file(isset($values['file_comment']) ? $values['file_comment'] : null, $values['post_id'], $values['id_attach']);
    }
    if (api_is_course_admin() == true) {
        $ccode = api_get_course_id();
        $sid = api_get_session_id();
        $link_info = GradebookUtils::is_resource_in_course_gradebook($ccode, 5, $values['thread_id'], $sid);
        $link_id = $link_info['id'];
        $thread_qualify_gradebook = isset($values['thread_qualify_gradebook']) ? $values['thread_qualify_gradebook'] : null;
        if ($thread_qualify_gradebook != 1) {
            if ($link_info !== false) {
                GradebookUtils::remove_resource_from_course_gradebook($link_id);
            }
        } else {
            if ($link_info === false && !$_GET['thread']) {
                $weigthqualify = $values['weight_calification'];
                GradebookUtils::add_resource_to_course_gradebook($values['category_id'], $ccode, 5, $values['thread_id'], Database::escape_string(stripslashes($values['calification_notebook_title'])), $weigthqualify, $values['numeric_calification'], null, 0, $sid);
            }
        }
    }
    // Storing the attachments if any.
    //update_added_resources('forum_post', $values['post_id']);
    $message = get_lang('EditPostStored') . '<br />';
    $message .= get_lang('ReturnTo') . ' <a href="viewforum.php?' . api_get_cidreq() . '&forum=' . intval($_GET['forum']) . '&">
            ' . get_lang('Forum') . '</a><br />';
    $message .= get_lang('ReturnTo') . ' <a href="viewthread.php?' . api_get_cidreq() . '&forum=' . intval($_GET['forum']) . '&thread=' . $values['thread_id'] . '&post=' . intval($_GET['post']) . '">' . get_lang('Message') . '</a>';
    Session::erase('formelements');
    Session::erase('origin');
    Session::erase('breadcrumbs');
    Session::erase('addedresource');
    Session::erase('addedresourceid');
    Display::display_confirmation_message($message, false);
}
/**
 * This function stores the edit of a post in the forum_post table.
 *
 * @param array
 * @return void HTML
 *
 * @author Patrick Cool <*****@*****.**>, Ghent University
 * @version february 2006, dokeos 1.8
 */
function store_edit_post($values)
{
    $table_threads = Database::get_course_table(TABLE_FORUM_THREAD);
    $table_posts = Database::get_course_table(TABLE_FORUM_POST);
    $gradebook = Security::remove_XSS($_GET['gradebook']);
    $course_id = api_get_course_int_id();
    //check if this post is the first of the thread
    // First we check if the change affects the thread and if so we commit the changes (sticky and post_title=thread_title are relevant).
    $posts = get_posts($values['thread_id']);
    $first_post = null;
    if (!empty($posts)) {
        $first_post = $posts[0];
    }
    if (!empty($first_post) && $first_post['post_id'] == $values['post_id']) {
        //if (array_key_exists('is_first_post_of_thread', $values) AND $values['is_first_post_of_thread'] == '1') {
        $sql = "UPDATE {$table_threads} SET\n                thread_title            ='" . Database::escape_string($values['post_title']) . "',\n                thread_sticky           ='" . Database::escape_string(isset($values['thread_sticky']) ? $values['thread_sticky'] : null) . "'," . "thread_title_qualify   ='" . Database::escape_string($values['calification_notebook_title']) . "'," . "thread_qualify_max     ='" . Database::escape_string($values['numeric_calification']) . "'," . "thread_weight          ='" . Database::escape_string($values['weight_calification']) . "'" . " WHERE c_id = {$course_id} AND thread_id='" . intval($values['thread_id']) . "'";
        Database::query($sql);
    }
    // Update the post_title and the post_text.
    $sql = "UPDATE {$table_posts} SET\n                post_title          ='" . Database::escape_string($values['post_title']) . "',\n                post_text           ='" . Database::escape_string($values['post_text']) . "',\n                post_notification   ='" . Database::escape_string(isset($values['post_notification']) ? $values['post_notification'] : null) . "'\n                WHERE c_id = {$course_id} AND post_id = '" . intval($values['post_id']) . "'";
    Database::query($sql);
    // Update attached files
    if (!empty($_POST['file_ids']) && is_array($_POST['file_ids'])) {
        foreach ($_POST['file_ids'] as $key => $id) {
            editAttachedFile(array('comment' => $_POST['file_comments'][$key], 'post_id' => $values['post_id']), $id);
        }
    }
    if (!empty($values['remove_attach'])) {
        delete_attachment($values['post_id']);
    }
    if (empty($values['id_attach'])) {
        add_forum_attachment_file($values['file_comment'], $values['post_id']);
    } else {
        edit_forum_attachment_file($values['file_comment'], $values['post_id'], $values['id_attach']);
    }
    if (api_is_course_admin() == true) {
        $ccode = api_get_course_id();
        $sid = api_get_session_id();
        $link_info = is_resource_in_course_gradebook($ccode, 5, $values['thread_id'], $sid);
        $link_id = $link_info['id'];
        $thread_qualify_gradebook = isset($values['thread_qualify_gradebook']) ? $values['thread_qualify_gradebook'] : null;
        if ($thread_qualify_gradebook != 1) {
            if ($link_info !== false) {
                remove_resource_from_course_gradebook($link_id);
            }
        } else {
            if ($link_info === false && !$_GET['thread']) {
                $weigthqualify = $values['weight_calification'];
                add_resource_to_course_gradebook($values['category_id'], $ccode, 5, $values['thread_id'], Database::escape_string(stripslashes($values['calification_notebook_title'])), $weigthqualify, $values['numeric_calification'], null, 0, $sid);
            }
        }
    }
    // Storing the attachments if any.
    //update_added_resources('forum_post', $values['post_id']);
    $message = get_lang('EditPostStored') . '<br />';
    $message .= get_lang('ReturnTo') . ' <a href="viewforum.php?' . api_get_cidreq() . '&forum=' . Security::remove_XSS($_GET['forum']) . '&">' . get_lang('Forum') . '</a><br />';
    $message .= get_lang('ReturnTo') . ' <a href="viewthread.php?' . api_get_cidreq() . '&forum=' . Security::remove_XSS($_GET['forum']) . '&gradebook=' . $gradebook . '&thread=' . $values['thread_id'] . '&post=' . Security::remove_XSS($_GET['post']) . '">' . get_lang('Message') . '</a>';
    Session::erase('formelements');
    Session::erase('origin');
    Session::erase('breadcrumbs');
    Session::erase('addedresource');
    Session::erase('addedresourceid');
    Display::display_confirmation_message($message, false);
}