Пример #1
0
        private int CreateResultsSheetHeaderRow(ISheet sheet, ContestResultsViewModel contestResults)
        {
            var headerRow    = sheet.CreateRow(0);
            var columnNumber = 0;

            headerRow.CreateCell(columnNumber++).SetCellValue("Username");
            headerRow.CreateCell(columnNumber++).SetCellValue("Name");

            foreach (var problem in contestResults.Problems)
            {
                if (problem.IsExcludedFromHomework)
                {
                    problem.Name = $"(*){problem.Name}";
                }

                headerRow.CreateCell(columnNumber++).SetCellValue(problem.Name);
            }

            var maxPoints = this.contestsData.GetMaxPointsForExportById(contestResults.Id);

            var totalPointsCellTitle = $"Total (Max: {maxPoints})";

            headerRow.CreateCell(columnNumber++).SetCellValue(totalPointsCellTitle);

            return(columnNumber);
        }
Пример #2
0
        private void FillSheetWithParticipantResults(ISheet sheet, ContestResultsViewModel contestResults)
        {
            var rowNumber = 1;

            foreach (var result in contestResults.Results)
            {
                var colNumber = 0;
                var row       = sheet.CreateRow(rowNumber++);
                row.CreateCell(colNumber++).SetCellValue(result.ParticipantUsername);
                row.CreateCell(colNumber++).SetCellValue(result.ParticipantFullName);

                foreach (var problem in contestResults.Problems)
                {
                    var problemResult = result.ProblemResults.FirstOrDefault(pr => pr.ProblemId == problem.Id);

                    if (problemResult != null)
                    {
                        row.CreateCell(colNumber++).SetCellValue(problemResult.BestSubmission.Points);
                    }
                    else
                    {
                        row.CreateCell(colNumber++, CellType.Blank);
                    }
                }

                row.CreateCell(colNumber).SetCellValue(result.ExportTotal);
            }
        }
Пример #3
0
        private ContestResultsViewModel GetContestResults(
            Contest contest,
            bool official,
            bool isUserAdminOrLecturer,
            bool isFullResults,
            bool isExportResults = false)
        {
            var contestResults = new ContestResultsViewModel
            {
                Id                    = contest.Id,
                Name                  = contest.Name,
                IsCompete             = official,
                ContestCanBeCompeted  = contest.CanBeCompeted,
                ContestCanBePracticed = contest.CanBePracticed,
                UserHasContestRights  = isUserAdminOrLecturer,
                ContestType           = contest.Type,
                Problems              = contest.ProblemGroups
                                        .SelectMany(pg => pg.Problems)
                                        .AsQueryable()
                                        .Where(p => !p.IsDeleted)
                                        .OrderBy(p => p.OrderBy)
                                        .ThenBy(p => p.Name)
                                        .Select(ContestProblemListViewModel.FromProblem)
            };

            var participants = this.participantsData
                               .GetAllByContestAndIsOfficial(contest.Id, official);

            var participantResults = participants
                                     .Select(ParticipantResultViewModel.FromParticipantAsSimpleResultByContest(contest.Id))
                                     .OrderByDescending(parRes => parRes.ProblemResults
                                                        .Where(pr => pr.ShowResult)
                                                        .Sum(pr => pr.BestSubmission.Points));

            if (isFullResults)
            {
                participantResults = participants
                                     .Select(ParticipantResultViewModel.FromParticipantAsFullResultByContest(contest.Id))
                                     .OrderByDescending(parRes => parRes.ProblemResults
                                                        .Sum(pr => pr.BestSubmission.Points));
            }
            else if (isExportResults)
            {
                participantResults = participants
                                     .Select(ParticipantResultViewModel.FromParticipantAsExportResultByContest(contest.Id))
                                     .OrderByDescending(parRes => parRes.ProblemResults
                                                        .Where(pr => pr.ShowResult && !pr.IsExcludedFromHomework)
                                                        .Sum(pr => pr.BestSubmission.Points));
            }

            contestResults.Results = participantResults
                                     .ThenBy(parResult => parResult.ProblemResults
                                             .OrderByDescending(pr => pr.BestSubmission.Id)
                                             .Select(pr => pr.BestSubmission.Id)
                                             .FirstOrDefault());

            return(contestResults);
        }
Пример #4
0
        public ActionResult Results(int id, bool official)
        {
            var contest = this.Data.Contests.GetById(id);

            // 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(contest, official);

            return(this.View(contestModel));
        }
Пример #5
0
        public ActionResult SimplePartial(
            int contestId,
            bool official,
            bool isUserAdminOrLecturerInContest,
            int page,
            int resultsInPage)
        {
            var cacheKey = $"{this.Request.Url?.AbsolutePath}?{nameof(page)}={page}";

            ContestResultsViewModel contestResults = null;

            if (!official && !isUserAdminOrLecturerInContest)
            {
                contestResults = this.HttpContext.Cache[cacheKey] as ContestResultsViewModel;
            }

            if (contestResults == null)
            {
                var contest = this.contestsData.GetByIdWithProblems(contestId);

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

                contestResults = this
                                 .GetContestResults(contest, official, isUserAdminOrLecturerInContest, isFullResults: false)
                                 .ToPagedResults(page, resultsInPage);

                if (!official && !isUserAdminOrLecturerInContest)
                {
                    this.HttpContext.Cache.Add(
                        key: cacheKey,
                        value: contestResults,
                        dependencies: null,
                        absoluteExpiration: DateTime.Now.AddMinutes(2),
                        slidingExpiration: Cache.NoSlidingExpiration,
                        priority: CacheItemPriority.Normal,
                        onRemoveCallback: null);
                }
            }

            return(this.PartialView("_SimpleResultsPagedList", contestResults));
        }
Пример #6
0
        private FileResult ExportResultsToExcel(ContestResultsViewModel contestResults, string fileName)
        {
            var workbook = new HSSFWorkbook();
            var sheet    = workbook.CreateSheet();

            var columnsCount = this.CreateResultsSheetHeaderRow(sheet, contestResults);

            this.FillSheetWithParticipantResults(sheet, contestResults);

            sheet.AutoSizeColumns(columnsCount);

            // Write the workbook to a memory stream
            var outputStream = new MemoryStream();

            workbook.Write(outputStream);

            // Return the result to the end user
            return(this.File(
                       outputStream.ToArray(),        // The binary data of the XLS file
                       GlobalConstants.ExcelMimeType, // MIME type of Excel files
                       fileName));
        }
Пример #7
0
        public ActionResult Simple(int id, bool official, int?page)
        {
            var contest = this.Data.Contests.All().Include(x => x.Problems).FirstOrDefault(x => x.Id == 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 &&
                official &&
                !this.Data.Participants.Any(id, this.UserProfile.Id, official) &&
                !this.User.IsAdmin())
            {
                throw new HttpException((int)HttpStatusCode.Forbidden, Resource.Contest_results_not_available);
            }

            var isUserLecturerInContest =
                this.Data.Users.All().Any(x => x.Id == this.UserProfile.Id && x.LecturerInContests.Any(y => y.ContestId == id));

            var isUserAdminOrLecturerInContest = isUserLecturerInContest || this.User.IsAdmin();

            var cacheKey = this.Request.Url.ToString();

            ContestResultsViewModel contestResults = null;

            if (!official && !isUserAdminOrLecturerInContest)
            {
                contestResults = this.HttpContext.Cache[cacheKey] as ContestResultsViewModel;
            }

            if (contestResults == null)
            {
                contestResults = this.GetContestResults(contest, official, isUserAdminOrLecturerInContest);

                var resultsInPage = NotOfficialResultsPageSize;
                if (official)
                {
                    resultsInPage = OfficialResultsPageSize;
                }

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

                // query the paged result
                contestResults.Results = contestResults
                                         .Results
                                         .Skip((page.Value - 1) * resultsInPage)
                                         .Take(resultsInPage)
                                         .ToArray();

                // add page info to View Model
                contestResults.CurrentPage = page.Value;

                if (!official && !isUserAdminOrLecturerInContest)
                {
                    this.HttpContext.Cache.Add(
                        key: cacheKey,
                        value: contestResults,
                        dependencies: null,
                        absoluteExpiration: DateTime.Now.AddMinutes(2),
                        slidingExpiration: Cache.NoSlidingExpiration,
                        priority: CacheItemPriority.Normal,
                        onRemoveCallback: null);
                }
            }

            contestResults.UserIsLecturerInContest = isUserLecturerInContest;
            this.ViewBag.IsOfficial = official;

            return(this.View(contestResults));
        }
Пример #8
0
        private ContestResultsViewModel GetContestResults(Contest contest, bool official, bool isUserAdminOrLecturer)
        {
            var contestResults = 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.OrderBy)
                           .ThenBy(x => x.Name),
                Results = this.Data.Participants
                          .All()
                          .Where(participant => participant.ContestId == contest.Id && participant.IsOfficial == official)
                          .Select(participant => new ParticipantResultViewModel
                {
                    ParticipantUsername  = participant.User.UserName,
                    ParticipantFirstName = participant.User.UserSettings.FirstName,
                    ParticipantLastName  = participant.User.UserSettings.LastName,
                    ProblemResults       = participant.Contest.Problems
                                           .Where(x => !x.IsDeleted)
                                           .Select(problem => new ProblemResultPairViewModel
                    {
                        Id             = problem.Id,
                        ProblemName    = problem.Name,
                        ProblemOrderBy = problem.OrderBy,
                        ShowResult     = problem.ShowResults,
                        BestSubmission = problem.ParticipantScores
                                         .Where(z => z.ParticipantId == participant.Id && z.IsOfficial == official)
                                         .Select(z => new BestSubmissionViewModel {
                            Id = z.SubmissionId, Points = z.Points
                        })
                                         .FirstOrDefault()
                    })
                                           .OrderBy(res => res.ProblemOrderBy)
                                           .ThenBy(res => res.ProblemName)
                })
            };

            IOrderedEnumerable <ParticipantResultViewModel> result;

            if (isUserAdminOrLecturer)
            {
                result = contestResults.Results
                         .OrderByDescending(x => x.AdminTotal);
            }
            else
            {
                result = contestResults.Results
                         .OrderByDescending(x => x.Total);
            }

            contestResults.Results = result
                                     .ThenByDescending(x => x.ProblemResults
                                                       .OrderBy(y => y.BestSubmission?.Id)
                                                       .Select(y => y.BestSubmission?.Id)
                                                       .FirstOrDefault());

            return(contestResults);
        }
Пример #9
0
        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));
        }
Пример #10
0
        private ContestResultsViewModel GetContestResults(Contest contest, bool official, bool isUserAdminOrLecturer)
        {
            var contestStartTime = official ? contest.StartTime : contest.PracticeStartTime;

            var contestResults = 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.OrderBy)
                           .ThenBy(x => x.Name),
                Results = this.Data.Participants
                          .All()
                          .Where(participant => participant.ContestId == contest.Id && participant.IsOfficial == official)
                          .Select(participant => new ParticipantResultViewModel
                {
                    ParticipantUsername  = participant.User.UserName,
                    ParticipantFirstName = participant.User.UserSettings.FirstName,
                    ParticipantLastName  = participant.User.UserSettings.LastName,
                    ProblemResults       = participant.Contest.Problems
                                           .Where(x => !x.IsDeleted)
                                           .Select(problem => new ProblemResultPairViewModel
                    {
                        Id             = problem.Id,
                        ProblemName    = problem.Name,
                        ProblemOrderBy = problem.OrderBy,
                        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, CreatedOn = z.CreatedOn
                        })
                                         .FirstOrDefault()
                    })
                                           .OrderBy(res => res.ProblemOrderBy)
                                           .ThenBy(res => res.ProblemName)
                })
                          .ToList()
            };

            if (isUserAdminOrLecturer)
            {
                contestResults.Results = contestResults.Results
                                         .OrderByDescending(x => x.AdminTotal)
                                         .ThenBy(x => x.GetContestTimeInSeconds(contestStartTime));
            }
            else
            {
                contestResults.Results = contestResults.Results
                                         .OrderByDescending(x => x.Total)
                                         .ThenBy(x => x.GetContestTimeInSeconds(contestStartTime));
            }

            return(contestResults);
        }