/**
 * This function handles all the forum and forum categories actions. This is a wrapper for the
 * forum and forum categories. All this code code could go into the section where this function is
 * called but this make the code there cleaner.
 * @param int $lp_id Learning path Id
 *
 * @return void
 * @author Patrick Cool <*****@*****.**>, Ghent University
 * @author Juan Carlos Raña Trabado (return to lp_id)
 * @version may 2011, Chamilo 1.8.8
 */
function handle_forum_and_forumcategories($lp_id = null)
{
    $action_forum_cat = isset($_GET['action']) ? $_GET['action'] : '';
    $get_content = isset($_GET['content']) ? $_GET['content'] : '';
    $post_submit_cat = isset($_POST['SubmitForumCategory']) ? true : false;
    $post_submit_forum = isset($_POST['SubmitForum']) ? true : false;
    $get_id = isset($_GET['id']) ? intval($_GET['id']) : '';
    $forum_categories_list = get_forum_categories();
    //Verify if forum category exists
    if (empty($forum_categories_list)) {
        $get_content = 'forumcategory';
    }
    // Adding a forum category
    if ($action_forum_cat == 'add' && $get_content == 'forumcategory' || $post_submit_cat) {
        show_add_forumcategory_form(array(), $lp_id);
        //$lp_id when is called from learning path
    }
    // Adding a forum
    if (($action_forum_cat == 'add' || $action_forum_cat == 'edit') && $get_content == 'forum' || $post_submit_forum) {
        if ($action_forum_cat == 'edit' && $get_id || $post_submit_forum) {
            $inputvalues = get_forums($get_id);
        } else {
            $inputvalues = array();
        }
        show_add_forum_form($inputvalues, $lp_id);
    }
    // Edit a forum category
    if ($action_forum_cat == 'edit' && $get_content == 'forumcategory' || isset($_POST['SubmitEditForumCategory']) ? true : false) {
        $forum_category = get_forum_categories($get_id);
        show_edit_forumcategory_form($forum_category);
    }
    // Delete a forum category
    if ($action_forum_cat == 'delete') {
        $id_forum = intval($get_id);
        $list_threads = get_threads($id_forum);
        for ($i = 0; $i < count($list_threads); $i++) {
            deleteForumCategoryThread('thread', $list_threads[$i]['thread_id']);
            $link_info = GradebookUtils::is_resource_in_course_gradebook(api_get_course_id(), 5, intval($list_threads[$i]['thread_id']), api_get_session_id());
            if ($link_info !== false) {
                GradebookUtils::remove_resource_from_course_gradebook($link_info['id']);
            }
        }
        $return_message = deleteForumCategoryThread($get_content, $get_id);
        Display::display_confirmation_message($return_message, false);
    }
    // Change visibility of a forum or a forum category.
    if ($action_forum_cat == 'invisible' || $action_forum_cat == 'visible') {
        $return_message = change_visibility($get_content, $get_id, $action_forum_cat);
        Display::display_confirmation_message($return_message, false);
    }
    // Change lock status of a forum or a forum category.
    if ($action_forum_cat == 'lock' || $action_forum_cat == 'unlock') {
        $return_message = change_lock_status($get_content, $get_id, $action_forum_cat);
        Display::display_confirmation_message($return_message, false);
    }
    // Move a forum or a forum category.
    if ($action_forum_cat == 'move' && isset($_GET['direction'])) {
        $return_message = move_up_down($get_content, $_GET['direction'], $get_id);
        Display::display_confirmation_message($return_message, false);
    }
}
/**
 * This function handles all the forum and forumcategories actions. This is a wrapper for the
 * forum and forum categories. All this code code could go into the section where this function is
 * called but this make the code there cleaner.
 * @param   int Learning path ID
 * @return void
 * @author Patrick Cool <*****@*****.**>, Ghent University
 * @author Juan Carlos Raña Trabado (return to lp_id)
 * @version may 2011, Chamilo 1.8.8
 */
function handle_forum_and_forumcategories($lp_id = null)
{
    $action_forum_cat = isset($_GET['action']) ? $_GET['action'] : '';
    $post_submit_cat = isset($_POST['SubmitForumCategory']) ? true : false;
    $post_submit_forum = isset($_POST['SubmitForum']) ? true : false;
    $get_id = isset($_GET['id']) ? $_GET['id'] : '';
    // Adding a forum category
    if ($action_forum_cat == 'add' && $_GET['content'] == 'forumcategory' || $post_submit_cat) {
        show_add_forumcategory_form(array(), $lp_id);
        //$lp_id when is called from learning path
    }
    // Adding a forum
    if (($action_forum_cat == 'add' || $action_forum_cat == 'edit') && $_GET['content'] == 'forum' || $post_submit_forum) {
        if ($action_forum_cat == 'edit' && $get_id || $post_submit_forum) {
            $inputvalues = get_forums(intval($get_id));
            // Note: This has to be cleaned first.
        } else {
            $inputvalues = array();
        }
        show_add_forum_form($inputvalues, $lp_id);
    }
    // Edit a forum category
    if ($action_forum_cat == 'edit' && $_GET['content'] == 'forumcategory' && isset($_GET['id']) || isset($_POST['SubmitEditForumCategory']) ? true : false) {
        $forum_category = get_forum_categories(strval(intval($_GET['id'])));
        // Note: This has to be cleaned first.
        show_edit_forumcategory_form($forum_category);
    }
    // Delete a forum category
    if (isset($_GET['action']) && $_GET['action'] == 'delete' && isset($_GET['content']) && $get_id) {
        $id_forum = intval($get_id);
        $list_threads = get_threads($id_forum);
        for ($i = 0; $i < count($list_threads); $i++) {
            delete_forum_forumcategory_thread('thread', $list_threads[$i]['thread_id']);
            require_once api_get_path(SYS_CODE_PATH) . 'gradebook/lib/gradebook_functions.inc.php';
            $link_info = is_resource_in_course_gradebook(api_get_course_id(), 5, intval($list_threads[$i]['thread_id']), api_get_session_id());
            if ($link_info !== false) {
                remove_resource_from_course_gradebook($link_info['id']);
            }
        }
        $return_message = delete_forum_forumcategory_thread($_GET['content'], $_GET['id']);
        Display::display_confirmation_message($return_message, false);
    }
    // Change visibility of a forum or a forum category.
    if (($action_forum_cat == 'invisible' || $action_forum_cat == 'visible') && isset($_GET['content']) && isset($_GET['id'])) {
        $return_message = change_visibility($_GET['content'], $_GET['id'], $_GET['action']);
        // Note: This has to be cleaned first.
        Display::display_confirmation_message($return_message, false);
    }
    // Change lock status of a forum or a forum category.
    if (($action_forum_cat == 'lock' || $action_forum_cat == 'unlock') && isset($_GET['content']) && isset($_GET['id'])) {
        $return_message = change_lock_status($_GET['content'], $_GET['id'], $_GET['action']);
        // Note: This has to be cleaned first.
        Display::display_confirmation_message($return_message, false);
    }
    // Move a forum or a forum category.
    if ($action_forum_cat == 'move' && isset($_GET['content']) && isset($_GET['id']) && isset($_GET['direction'])) {
        $return_message = move_up_down($_GET['content'], $_GET['direction'], $_GET['id']);
        // Note: This has to be cleaned first.
        Display::display_confirmation_message($return_message, false);
    }
}