public ContestRegistrationViewModel(Contest contest, ContestRegistrationModel userAnswers, bool isOfficial)
     : this(contest, isOfficial)
 {
     this.Questions = this.Questions.Select(x =>
     {
         var userAnswer = userAnswers.Questions.FirstOrDefault(y => y.QuestionId == x.QuestionId);
         return new QuestionViewModel
         {
             Answer = userAnswer == null ? null : userAnswer.Answer,
             QuestionId = x.QuestionId,
             Question = x.Question
         };
     });
 }
        public ActionResult Register(bool official, ContestRegistrationModel registrationData)
        {
            // check if the user has already registered for participation and redirect him to the correct action
            var participantFound = this.Data.Participants.Any(registrationData.ContestId, this.UserProfile.Id, official);

            if (participantFound)
            {
                return this.RedirectToAction("Index", new { id = registrationData.ContestId, official });
            }

            var contest = this.Data.Contests.GetById(registrationData.ContestId);
            ValidateContest(contest, official);

            if (official && contest.HasContestPassword && (contest.ContestPassword != registrationData.Password))
            {
                this.ModelState.AddModelError("Password", Resource.Views.CompeteRegister.Incorrect_password);
            }

            if (!official && contest.HasPracticePassword && (contest.PracticePassword != registrationData.Password))
            {
                this.ModelState.AddModelError("Password", Resource.Views.CompeteRegister.Incorrect_password);
            }

            var questionsToAnswerCount = official ?
                contest.Questions.Where(x => x.AskOfficialParticipants).Count() :
                contest.Questions.Where(x => x.AskPracticeParticipants).Count();

            if (questionsToAnswerCount != registrationData.Questions.Count())
            {
                this.ModelState.AddModelError("Questions", Resource.Views.CompeteRegister.Not_all_questions_answered);
            }

            if (!ModelState.IsValid)
            {
                return this.View(new ContestRegistrationViewModel(contest, registrationData, official));
            }

            var participant = new Participant(registrationData.ContestId, this.UserProfile.Id, official);
            this.Data.Participants.Add(participant);
            foreach (var question in registrationData.Questions)
            {
                participant.Answers.Add(new ParticipantAnswer
                                                {
                                                    ContestQuestionId = question.QuestionId,
                                                    Answer = question.Answer
                                                });
            }

            this.Data.SaveChanges();

            return this.RedirectToAction("Index", new { id = registrationData.ContestId, official });
        }
        public void RegisterActionWhenPostedDataPracticeHasPasswordAndQuestionsUnansweredShouldReturnView()
        {
            var contest = this.CreateAndSaveContest("testContest", this.InactiveContestOptions, this.ActiveContestWithPasswordAndQuestionsOptions);

            var contestRegistrationModel = new ContestRegistrationModel(this.EmptyOjsData);
            contestRegistrationModel.Password = this.DefaultPracticePassword;
            contestRegistrationModel.ContestId = contest.Id;

            this.TryValidateModel(contestRegistrationModel, this.CompeteController);
            var result = this.CompeteController.Register(this.IsPractice, contestRegistrationModel) as ViewResult;
            var resultModel = result.Model as ContestRegistrationViewModel;

            Assert.IsNotNull(resultModel);
            Assert.AreEqual(contest.Questions.Count, resultModel.Questions.Count());
            Assert.IsTrue(contest.HasPracticePassword);
        }
        public void RegisterActionWhenPostedDataPracticeHasPasswordAndProvidedIncorrectPasswordShouldReturnView()
        {
            var contest = this.CreateAndSaveContest("testContest", this.InactiveContestOptions, this.ActiveContestWithPasswordOptions);

            var contestRegistrationModel = new ContestRegistrationModel(this.EmptyOjsData);
            contestRegistrationModel.Password = "******";
            contestRegistrationModel.ContestId = contest.Id;

            var result = this.CompeteController.Register(this.IsPractice, contestRegistrationModel) as ViewResult;
            var model = result.Model as ContestRegistrationViewModel;

            Assert.IsNotNull(model);
            Assert.IsNull(model.Password);
            Assert.IsFalse(this.CompeteController.ModelState.IsValid);
            Assert.IsNotNull(this.CompeteController.ModelState["Password"]);
            Assert.AreEqual(contest.Name, model.ContestName);
            Assert.IsTrue(model.RequirePassword);
        }
        public void RegisterActionWhenPostedDataPracticeHasPasswordAndProvidedCorrectPasswordShouldRedirectToIndex()
        {
            var contest = this.CreateAndSaveContest("contestName", this.InactiveContestOptions, this.ActiveContestWithPasswordOptions);

            var contestRegistrationModel = new ContestRegistrationModel(this.EmptyOjsData);
            contestRegistrationModel.Password = this.DefaultPracticePassword;
            contestRegistrationModel.ContestId = contest.Id;

            var result = this.CompeteController.Register(this.IsPractice, contestRegistrationModel) as RedirectToRouteResult;

            Assert.IsNull(result.RouteValues["controller"]);
            Assert.AreEqual("Index", result.RouteValues["action"]);
            Assert.AreEqual(this.IsPractice, result.RouteValues["official"]);
            Assert.AreEqual(contest.Id, result.RouteValues["id"]);
        }
        public void RegisterActionWhenPostedDataContestHasPasswordAndAllQuestionsAnsweredShouldRedirectToIndex()
        {
            var contest = this.CreateAndSaveContest("testContest", this.ActiveContestWithQuestionsOptions, this.InactiveContestOptions);

            var contestRegistrationModel = new ContestRegistrationModel(this.EmptyOjsData);
            contestRegistrationModel.Password = this.DefaultCompetePassword;
            contestRegistrationModel.ContestId = contest.Id;
            contestRegistrationModel.Questions = contest.Questions.Select(x => new ContestQuestionAnswerModel
            {
                QuestionId = x.Id,
                Answer = "answer"
            });

            var result = this.CompeteController.Register(this.IsCompete, contestRegistrationModel) as RedirectToRouteResult;

            Assert.IsNull(result.RouteValues["controller"]);
            Assert.AreEqual("Index", result.RouteValues["action"]);
            Assert.AreEqual(contest.Id, result.RouteValues["id"]);
            Assert.AreEqual(this.IsCompete, result.RouteValues["official"]);
        }
        public void RegisterActionWhenPostedDataAndUserAlreadyRegisteredShouldRedirectToIndex()
        {
            var contest = this.CreateAndSaveContest("testContest", this.ActiveContestWithPasswordOptions, this.ActiveContestWithPasswordOptions);

            this.EmptyOjsData.Participants.Add(new Participant(contest.Id, this.FakeUserProfile.Id, this.IsCompete));
            this.EmptyOjsData.SaveChanges();

            var contestRegistrationModel = new ContestRegistrationModel(this.EmptyOjsData);
            contestRegistrationModel.ContestId = contest.Id;

            var result = this.CompeteController.Register(this.IsCompete, contestRegistrationModel) as RedirectToRouteResult;

            Assert.IsNull(result.RouteValues["controller"]);
            Assert.AreEqual("Index", result.RouteValues["action"]);
            Assert.AreEqual(contest.Id, result.RouteValues["id"]);
            Assert.AreEqual(this.IsCompete, result.RouteValues["official"]);
        }
        public void RegisterActionWhenPostedDataAndContestCannotBePracticedShouldThrowException()
        {
            var contest = this.CreateAndSaveContest("testContest", this.InactiveContestOptions, this.InactiveContestOptions);

            try
            {
                var contestRegistrationModel = new ContestRegistrationModel(this.EmptyOjsData);
                contestRegistrationModel.ContestId = contest.Id;

                this.CompeteController.Register(this.IsPractice, contestRegistrationModel);
                Assert.Fail("Expected exception trying to register to practice contest, when practice is not available");
            }
            catch (HttpException ex)
            {
                Assert.AreEqual((int)HttpStatusCode.Forbidden, ex.GetHttpCode());
            }
        }
        public void RegisterActionWhenPostedDataContestHasPasswordQuestionAnsweredWithEmptyStringShouldReturnView()
        {
            var contest = this.CreateAndSaveContest("testContest", this.ActiveContestWithQuestionsOptions, this.InactiveContestOptions);

            var registerModel = new ContestRegistrationModel
            {
                Password = this.DefaultCompetePassword,
                Questions = contest.Questions.Select(x => new ContestQuestionAnswerModel
                {
                    QuestionId = x.Id,
                    Answer = string.Empty
                })
            };

            var result = this.CompeteController.Register(contest.Id, this.IsCompete, registerModel) as ViewResult;
            var model = result.Model as ContestRegistrationViewModel;

            Assert.IsNotNull(model);
        }
        public ActionResult Register(bool official, ContestRegistrationModel registrationData)
        {
            // check if the user has already registered for participation and redirect him to the correct action
            var participantFound = this.Data.Participants.Any(registrationData.ContestId, this.UserProfile.Id, official);

            if (participantFound)
            {
                return this.RedirectToAction(GlobalConstants.Index, new { id = registrationData.ContestId, official });
            }

            var contest = this.Data.Contests.GetById(registrationData.ContestId);
            ValidateContest(contest, official);

            if (official && contest.HasContestPassword)
            {
                if (string.IsNullOrEmpty(registrationData.Password))
                {
                    this.ModelState.AddModelError("Password", Resource.Views.CompeteRegister.Empty_Password);
                }
                else if (contest.ContestPassword != registrationData.Password)
                {
                    this.ModelState.AddModelError("Password", Resource.Views.CompeteRegister.Incorrect_password);
                }
            }

            if (!official && contest.HasPracticePassword)
            {
                if (string.IsNullOrEmpty(registrationData.Password))
                {
                    this.ModelState.AddModelError("Password", Resource.Views.CompeteRegister.Empty_Password);
                }
                else if (contest.PracticePassword != registrationData.Password)
                {
                    this.ModelState.AddModelError("Password", Resource.Views.CompeteRegister.Incorrect_password);
                }
            }

            var questionsToAnswerCount = official ?
                contest.Questions.Count(x => x.AskOfficialParticipants) :
                contest.Questions.Count(x => x.AskPracticeParticipants);

            if (questionsToAnswerCount != registrationData.Questions.Count())
            {
                this.ModelState.AddModelError("Questions", Resource.Views.CompeteRegister.Not_all_questions_answered);
            }

            var contestQuestions = contest.Questions.ToList();

            var participant = new Participant(registrationData.ContestId, this.UserProfile.Id, official);
            this.Data.Participants.Add(participant);
            var counter = 0;
            foreach (var question in registrationData.Questions)
            {
                var contestQuestion = contestQuestions.FirstOrDefault(x => x.Id == question.QuestionId);

                var regularExpression = contestQuestion.RegularExpressionValidation;
                bool correctlyAnswered = false;

                if (!string.IsNullOrEmpty(regularExpression))
                {
                    correctlyAnswered = Regex.IsMatch(question.Answer, regularExpression);
                }

                if (contestQuestion.Type == ContestQuestionType.DropDown)
                {
                    int contestAnswerId;
                    if (int.TryParse(question.Answer, out contestAnswerId) && contestQuestion.Answers.Any(x => x.Id == contestAnswerId))
                    {
                        correctlyAnswered = true;
                    }

                    if (!correctlyAnswered)
                    {
                        this.ModelState.AddModelError(string.Format("Questions[{0}].Answer", counter), "Invalid selection");
                    }
                }

                participant.Answers.Add(new ParticipantAnswer
                                                {
                                                    ContestQuestionId = question.QuestionId,
                                                    Answer = question.Answer
                                                });

                counter++;
            }

            if (!this.ModelState.IsValid)
            {
                return this.View(new ContestRegistrationViewModel(contest, registrationData, official));
            }

            this.Data.SaveChanges();

            return this.RedirectToAction(GlobalConstants.Index, new { id = registrationData.ContestId, official });
        }
Exemplo n.º 11
0
        public ActionResult Register(int id, bool official, ContestRegistrationModel registrationData)
        {
            // check if the user has already registered for participation and redirect him to the correct
            // action
            var participantFound = this.Data.Participants.Any(id, this.UserProfile.Id, official);

            if (participantFound)
            {
                return this.RedirectToAction("Index", new { id, official });
            }

            var contest = this.Data.Contests.GetById(id);

            ValidateContest(contest, official);

            // check if the contest is official, has a password and if the user entered the correct password
            if (official && contest.HasContestPassword && !contest.ContestPassword.Equals(registrationData.Password))
            {
                this.ModelState.AddModelError("Password", "Incorrect password!");
            }

            // check if the contest is practice, has a password and if the user entered the correct password
            if (!official && contest.HasPracticePassword && !contest.PracticePassword.Equals(registrationData.Password))
            {
                this.ModelState.AddModelError("Password", "Incorrect password!");
            }

            var questionsToAnswerCount = official ?
                contest.Questions.Where(x => x.AskOfficialParticipants).Count() :
                contest.Questions.Where(x => x.AskPracticeParticipants).Count();

            if (questionsToAnswerCount != registrationData.Questions.Count())
            {
                // TODO: add an appropriate model error
                this.ModelState.AddModelError(string.Empty, string.Empty);
            }

            registrationData.Questions.Each((x, i) =>
            {
                if (string.IsNullOrEmpty(x.Answer))
                {
                    this.ModelState.AddModelError(string.Format("Questions[{0}].Answer", i), "Answer is required");
                }
            });

            if (!ModelState.IsValid)
            {
                return this.View(new ContestRegistrationViewModel(contest, registrationData, official));
            }

            registrationData.Questions.Each(q =>
            {
                var contestQuestion = contest.Questions.FirstOrDefault(x => x.Id == q.QuestionId);

                contestQuestion.Answers.Add(new ContestQuestionAnswer
                {
                    QuestionId = q.QuestionId,
                    Text = q.Answer
                });
            });

            var participant = new Participant(id, this.UserProfile.Id, official);
            this.Data.Participants.Add(participant);
            this.Data.SaveChanges();

            return this.RedirectToAction("Index", new { id, official });
        }