public ActionResult Simple(int id, bool official)
        {
            var contest = this.Data.Contests.GetById(id);

            if (contest == null)
            {
                throw new HttpException((int)HttpStatusCode.NotFound, Resource.Contest_not_found);
            }

            // if the results are not visible and the participant is not registered for the contest
            // then he is not authorized to view the results
            if (!contest.ResultsArePubliclyVisible &&
                !this.Data.Participants.Any(id, this.UserProfile.Id, official))
            {
                throw new HttpException((int)HttpStatusCode.Forbidden, Resource.Contest_results_not_available);
            }

            // TODO: Extract choosing the best submission logic to separate repository method?
            var contestModel = new ContestResultsViewModel
            {
                Id = contest.Id,
                Name = contest.Name,
                Problems = contest.Problems.AsQueryable().Select(ContestProblemViewModel.FromProblem).OrderBy(x => x.Name),
                Results = this.Data.Participants.All()
                    .Where(participant => participant.ContestId == contest.Id && participant.IsOfficial == official)
                    .Select(participant => new ParticipantResultViewModel
                    {
                        ParticipantName = participant.User.UserName,
                        ProblemResults = participant.Contest.Problems
                            .Select(problem =>
                                new ProblemResultPairViewModel
                                {
                                    Id = problem.Id,
                                    ProblemName = problem.Name,
                                    ShowResult = problem.ShowResults,
                                    BestSubmission = problem.Submissions
                                                        .Where(z => z.ParticipantId == participant.Id)
                                                        .OrderByDescending(z => z.Points).ThenByDescending(z => z.Id)
                                                        .Select(z => new BestSubmissionViewModel { Id = z.Id, Points = z.Points })
                                                        .FirstOrDefault()
                                })
                                .OrderBy(res => res.ProblemName)
                    })
                    .ToList()
                    .OrderByDescending(x => x.Total)
            };

            this.ViewBag.IsOfficial = official;

            return this.View(contestModel);
        }
        public ActionResult Simple(int id, bool official, int? page)
        {
            var contest = this.Data.Contests.GetById(id);

            if (contest == null)
            {
                throw new HttpException((int)HttpStatusCode.NotFound, Resource.Contest_not_found);
            }

            // if the results are not visible and the participant is not registered for the contest
            // then he is not authorized to view the results
            if (!contest.ResultsArePubliclyVisible &&
                !this.Data.Participants.Any(id, this.UserProfile.Id, official) &&
                !this.User.IsAdmin())
            {
                throw new HttpException((int)HttpStatusCode.Forbidden, Resource.Contest_results_not_available);
            }

            // TODO: Extract choosing the best submission logic to separate repository method?
            var contestModel = new ContestResultsViewModel
            {
                Id = contest.Id,
                Name = contest.Name,
                ContestCanBeCompeted = contest.CanBeCompeted,
                ContestCanBePracticed = contest.CanBePracticed,
                Problems = contest.Problems.AsQueryable().Where(x => !x.IsDeleted)
                                        .Select(ContestProblemViewModel.FromProblem).OrderBy(x => x.Name),
                Results = this.Data.Participants.All()
                    .Where(participant => participant.ContestId == contest.Id && participant.IsOfficial == official)
                    .Select(participant => new ParticipantResultViewModel
                    {
                        ParticipantName = participant.User.UserName,
                        ProblemResults = participant.Contest.Problems
                            .Where(x => !x.IsDeleted)
                            .Select(problem =>
                                new ProblemResultPairViewModel
                                {
                                    Id = problem.Id,
                                    ProblemName = problem.Name,
                                    ShowResult = problem.ShowResults,
                                    BestSubmission = problem.Submissions
                                                        .Where(z => z.ParticipantId == participant.Id && !z.IsDeleted)
                                                        .OrderByDescending(z => z.Points).ThenByDescending(z => z.Id)
                                                        .Select(z => new BestSubmissionViewModel { Id = z.Id, Points = z.Points })
                                                        .FirstOrDefault()
                                })
                                .OrderBy(res => res.ProblemName)
                    })
                    .ToList()
                    .OrderByDescending(x => x.Total)
            };

            // calculate page information
            contestModel.TotalResults = contestModel.Results.Count();
            int totalResults = contestModel.TotalResults;
            int totalPages = totalResults % ResultsPageSize == 0 ? totalResults / ResultsPageSize : (totalResults / ResultsPageSize) + 1;

            if (page == null || page < 1)
            {
                page = 1;
            }
            else if (page > totalPages)
            {
                page = totalPages;
            }

            // TODO: optimize if possible
            // query the paged result
            contestModel.Results = contestModel.Results
                    .Skip((page.Value - 1) * ResultsPageSize)
                    .Take(ResultsPageSize);

            // add page info to View Model
            contestModel.CurrentPage = page.Value;
            contestModel.AllPages = totalPages;

            if (User.IsAdmin())
            {
                contestModel.Results = contestModel.Results.OrderByDescending(x => x.AdminTotal);
            }

            this.ViewBag.IsOfficial = official;

            return this.View(contestModel);
        }
        public ActionResult Simple(int id, bool official)
        {
            var contest = this.Data.Contests.GetById(id);

            if (contest == null)
            {
                throw new HttpException((int)HttpStatusCode.NotFound, "Invalid contest Id was provided.");
            }

            // if the results are not visible and the participant is not registered for the contest
            // then he is not authorized to view the results
            if (!contest.ResultsArePubliclyVisible &&
                !this.Data.Participants.Any(id, this.UserProfile.Id, official))
            {
                throw new HttpException((int)HttpStatusCode.Forbidden, "The results for this contest are not available");
            }

            var contestModel = new ContestResultsViewModel
            {
                Name = contest.Name,
                Problems = contest.Problems.AsQueryable().Select(ContestProblemViewModel.FromProblem).OrderBy(x => x.Name),
                Results = this.Data.Participants.All()
                    .Where(participant => participant.ContestId == contest.Id && participant.IsOfficial == official)
                    .Select(participant => new ParticipantResultViewModel
                    {
                        ParticipantName = participant.User.UserName,
                        ProblemResults = participant.Contest.Problems
                            .Select(problem =>
                                new ProblemResultPairViewModel
                                {
                                    Id = problem.Id,
                                    ProblemName = problem.Name,
                                    Result = problem.Submissions
                                                        .Where(z => z.ParticipantId == participant.Id)
                                                        .OrderByDescending(z => z.Points).ThenBy(z => z.Id)
                                                        .Select(z => z.Points)
                                                        .FirstOrDefault()
                                })
                                .OrderBy(res => res.ProblemName)
                    })
                    .ToList()
                    .OrderByDescending(x => x.Total)
            };

            return this.View(contestModel);
        }