function execute($request)
{
    $language_info = api_autograder_get_language_info($request['path_parts'][1]);
    if ($language_info == null) {
        return build_response_not_found("Language not found.");
    }
    $language_key = $language_info['key'];
    $language_id = $language_info['language_id'];
    $problems = api_autograder_menu_get_problems($request['user_id'], 'practice', 0, $language_id);
    debug_print($problems);
    if ($problems['OK']) {
        $output = array('<h1>' . htmlspecialchars($language_info['name']) . ' Practice Problems</h1>');
        if (count($problems['ordered_problem_ids']) == 0) {
            array_push($output, "<div>Empty!</div>");
        }
        foreach ($problems['ordered_problem_ids'] as $problem_id) {
            $problem_info = $problems['problem_' . $problem_id];
            array_push($output, '<div><a href="/practice/' . $language_key . '/' . $problem_id . '">');
            array_push($output, htmlspecialchars($problem_info['title']));
            array_push($output, '</a></div>');
        }
        return build_response_ok($language_info['title'] . " Practice Problems", implode("\n", $output));
    } else {
        if ($problems['message'] == 'INVALID_LANGUAGE') {
            return build_response_not_found("Language not found.");
        }
    }
}
Exemple #2
0
function execute($request)
{
    if ($request['user_id'] == 0) {
        return build_response_forbidden('You must be <a href="/login">logged in</a> to post.');
    }
    $category_key = $request['path_parts'][1];
    $category_info = api_forum_get_category_info($request['user_id'], $request['is_admin'], $category_key, true);
    if ($category_info['ERROR']) {
        return build_response_not_found('Forum category not found.');
    }
    $path_parts = $request['path_parts'];
    if ($path_parts[3] == 'reply') {
        $type = 'reply';
        $thread_id = intval($path_parts[2]);
        $thread_info = api_forum_get_thread_info($request['user_id'], $request['is_admin'], $thread_id);
        if ($thread_info['is_locked']) {
            return build_response_forbidden("Cannot post in a locked thread.");
        }
    } else {
        if ($path_parts[2] == 'post') {
            $type = 'create';
        } else {
            throw new Exception("Invalid post type.");
        }
    }
    $thread_title = '';
    $post_body = '';
    $error_message = null;
    if ($request['method'] == "POST") {
        $thread_title = trim($request['form']['thread_title']);
        $post_body = trim($request['form']['post_body']);
        if ($type == 'create') {
            $result = api_forum_create_post($request['user_id'], $request['is_admin'], $category_info['category_id'], $thread_title, 0, null, $post_body);
        } else {
            if ($type == 'reply') {
                $result = api_forum_create_post($request['user_id'], $request['is_admin'], $category_info['category_id'], '', $thread_id, null, $post_body);
            } else {
                throw new Exception("Invalid post type.");
            }
        }
        if ($result['OK']) {
            return build_response_moved_temporarily('/forum/' . $category_key . '/' . $result['thread_id'] . '/new');
        } else {
            switch ($result['message']) {
                case 'BLANK_POST':
                    $error_message = "Post cannot be blank.";
                    break;
                case 'THREAD_TITLE_BLANK':
                    $error_message = "Thread title cannot be blank.";
                    break;
                default:
                    $error_message = "Server returned error: " . $result['message'];
                    break;
            }
        }
    }
    $html = array($error_message != null ? '<div style="color:#f00;">' . htmlspecialchars($error_message) . '</div>' : '', '<form action="/' . implode('/', $request['path_parts']) . '" method="post">', '<div>', $thread_id == 0 ? 'Title: <input type="text" name="thread_title" value="' . htmlspecialchars($thread_title) . '" />' : '', '</div>', '<div>', '<textarea name="post_body" rows="12" style="width:900px">' . htmlspecialchars($post_body) . '</textarea>', '</div>', '<div>', '<input type="submit" name="submit" value="Be Nice" />', '</div>', '</form>');
    return build_response_ok("New Post", implode("\n", $html));
}
function execute($request)
{
    $problem_id = intval($request['path_parts'][1]);
    $problem = api_autograder_menu_get_problem($request['user_id'], 'golf', 0, $problem_id);
    if ($problem == null) {
        return build_response_not_found("Golf Problem not found.");
    }
    $output = array('<h1>', '<a href="/golf">Code Golf</a> &gt; ', htmlspecialchars($problem['title']), '</h1>', '<div>', nl2br(htmlspecialchars($problem['statement'])), '</div>', api_autograder_generate_client_html('golf', null, '', $problem_id));
    return build_response_ok("Code Golf: " . $problem['title'], implode("\n", $output), array('js' => 'autograder.js', 'onload' => "ag_init('golf')"));
}
Exemple #4
0
function execute($request)
{
    $language_info = api_autograder_get_language_info($request['path_parts'][1]);
    $problem_id = intval($request['path_parts'][2]);
    if ($problem_id == 0 || $language_info == null) {
        return build_response_not_found("Problem not found.");
    }
    $problem = api_autograder_menu_get_problem($request['user_id'], $language_info['language_id'], 'practice', 0, $problem_id);
    if ($problem == null) {
        return build_response_not_found("Problem not found.");
    }
    $output = array('<h1>' . htmlspecialchars($problem['title']) . '</h1>', '<div>', nl2br(htmlspecialchars($problem['statement'])), '</div>', api_autograder_generate_client_html('practice', $language_info, $problem['template'], $problem_id));
    return build_response_ok('Problem', implode("\n", $output), array('js' => 'autograder.js', 'onload' => "ag_init('practice')"));
}
Exemple #5
0
function execute($request)
{
    $user_info = api_account_lookup_user_by_name($request['path_parts'][1]);
    if ($user_info == null) {
        return build_response_not_found("Not account by that name exists.");
    }
    $user_id = $user_info['user_id'];
    $output = array('<h1>' . htmlspecialchars($user_info['name']) . '</h1>');
    $profile = sql_query_item("SELECT * FROM `user_profiles` WHERE `user_id` = {$user_id} LIMIT 1");
    if ($profile == null) {
        $profile = array();
    }
    if (strlen($user_info['image_id']) > 0) {
        array_push($output, '<div>', '<img src="/uploads/avatars/' . $user_info['image_id'] . '" />', '</div>');
    }
    $blurb = trim($profile['blurb']);
    if (strlen($blurb) > 0) {
        array_push($output, '<div>', nl2br(htmlspecialchars($blurb)), '</div>');
    }
    array_push($output, '<div style="padding-top:100px; font-style:italic; color:#888;">', "More interesting stuff will be put here, I promise.", '</div>');
    return build_response_ok($user_info['name'], implode("\n", $output));
}
function execute($request)
{
    return build_response_not_found("I'm sorry. I could not find that page.");
}
Exemple #7
0
function execute($request)
{
    $path_parts = $request['path_parts'];
    $category_key = $path_parts[1];
    $thread_id = intval($path_parts[2]);
    $page_id = 0;
    // returns null for admin-only threads if not an admin
    $thread_info = api_forum_get_thread_info($request['user_id'], $request['is_admin'], $thread_id, true);
    if ($thread_info == null || $thread_info['category_info']['key'] != $category_key) {
        return build_response_not_found("Thread not found.");
    }
    $total_posts = $thread_info['post_count'];
    if (count($path_parts) > 3 && substr($path_parts[3], 0, strlen('page')) == 'page') {
        $page_id = intval(substr($path_parts[3], strlen('page'))) - 1;
        if ($page_id < 0) {
            $page_id = 0;
        }
    } else {
        if ($path_parts[3] == 'new') {
            // TODO: per-user new post tracking
        }
    }
    $current_page = $page_id + 1;
    $total_pages = intval(($total_posts - 1) / 25) + 1;
    // List of integers including 1-indexed page numbers or -1 for ellipses.
    // Links to first 3 pages and last 3 pages should always be available, along with pages within 2 of the current page.
    $paginator_links = array();
    $first_range = 3;
    $end_range = $total_pages - 2;
    $mid_begin_range = $current_page - 2;
    $mid_end_range = $current_page + 2;
    $last_item_is_ellipsis = false;
    for ($i = 1; $i <= $total_pages; ++$i) {
        if ($i <= $first_range || $i >= $end_range || $i >= $mid_begin_range && $i <= $mid_end_range) {
            array_push($paginator_links, $i);
            $last_item_is_ellipses = false;
        } else {
            if (!$last_item_is_ellipses) {
                array_push($paginator_links, -1);
                $last_item_is_ellipses = true;
            }
        }
    }
    $starting_post_index = $page_id * 25;
    if ($starting_post_index >= $total_posts) {
    }
    $forum_posts = api_forum_fetch_posts_for_thread($request['user_id'], $category_key, $thread_id, $page_id);
    $post_ids = $forum_posts['ordered_post_ids'];
    $thread_info = $forum_posts['thread_' . $thread_id];
    $category_info = $forum_posts['category_' . $thread_info['category_id']];
    if ($category_info == null) {
        return build_response_not_found("Thread not found.");
    }
    $output = array();
    array_push($output, '<h1 style="font-size:16px;">', '<a href="/forum">Forum</a> &gt; ', '<a href="/forum/' . $category_key . '">' . htmlspecialchars($category_info['name']) . '</a> &gt; ', htmlspecialchars($thread_info['title']), '</h1>');
    $paginator_html = array();
    if ($total_pages > 1) {
        array_push($paginator_html, '<div style="text-align:right;">');
        if ($current_page > 1) {
            array_push($paginator_html, '<a href="/forum/' . $category_key . '/' . $thread_id . '/page' . ($current_page - 1) . '">Prev</a> ');
        }
        foreach ($paginator_links as $page) {
            if ($page === -1) {
                array_push($paginator_html, ' ... ');
            } else {
                if ($page == $current_page) {
                    array_push($paginator_html, '[' . $current_page . ']');
                } else {
                    array_push($paginator_html, '<a href="/forum/' . $category_key . '/' . $thread_id . '/page' . $page . '">' . $page . '</a>');
                }
            }
        }
        if ($current_page < $total_pages) {
            array_push($paginator_html, '<a href="/forum/' . $category_key . '/' . $thread_id . '/page' . ($current_page + 1) . '">Next</a> ');
        }
        array_push($paginator_html, '</div>');
    }
    $paginator_html = implode("\n", $paginator_html);
    array_push($output, $paginator_html);
    if (count($post_ids) == 0) {
        return build_response_not_found("No posts found.");
    }
    array_push($output, '</div>');
    //array_push($output, '<div style="margin-bottom:20px;">');
    foreach ($post_ids as $post_id) {
        $post = $forum_posts['post_' . $post_id];
        $user = $forum_posts['user_' . $post['user_id']];
        array_push($output, '<div style="clear:both; padding-top:20px;">', '<div class="block" style="float:left; width:120px; margin-right:20px;">', '<div style="text-align:center;">', strlen($user['image_id']) > 0 ? '<img src="/uploads/avatars/' . $user['image_id'] . '" />' : ":'(", '</div>', '</div>', '<div style="float:left; width:780px; background-color:#fff;">', '<div style="background-color:#ddd;font-weight:bold; padding:8px; font-size:12px;">', '<div title="' . date("M j, Y g:i:s A", $post['time']) . '" style="float:right;width:300px;text-align:right;font-weight:normal;color:#555;">', unix_to_scaling_time($post['time']), '</div>', '<a href="/profiles/' . $user['login_id'] . '">' . htmlspecialchars($user['name']) . '</a>', '</div>', '<div style="clear:right;padding:20px;">', nl2br(htmlspecialchars($post['content_raw'])), '</div>', '</div>', '</div>');
    }
    //array_push($output, '</div>');
    array_push($output, '<div class="fullblock" style="clear:both;margin-top:20px;">');
    array_push($output, $paginator_html);
    array_push($output, '<div><a href="/forum/' . $category_key . '/' . $thread_id . '/reply">Reply</a></div>');
    return build_response_ok("Forum thread", implode("\n", $output));
}