コード例 #1
0
        public ActionResult SpellTest(FormCollection collection, TestViewModel model)
        {
            var words = _repository.GetWordsForTest(model.SpellingBeeTestId);
            ////skapa testwords
            //var testWords = words.Select(w =>
            //    new TestWord
            //    {
            //        WordId = w.WordId
            //    }).ToList();

            //TODO: Flytta skapandet av testet hit. Då slipper man tomma test om anv avbryter testet utan att spara.
            // TODO: hantera browser-navigering.
            var testWords = new List <TestWord>();

            foreach (var word in model.Words)
            {
                var testWord = new TestWord();
                testWord.WordId = word.WordId;

                var right = string.IsNullOrEmpty(word.Input) ? false : word.EnglishText.Equals(word.Input.ToLower());
                if (right)
                {
                    testWord.Correct = true;
                }
                testWords.Add(testWord);
                //_repository.CreateTestWord(testWord);
            }
            var appUser = SpellUserManager.FindByName(User.Identity.Name);
            //skapa usertest
            var userTest = new UserTest()
            {
                ApplicationUserId = appUser.Id,
                SpellingBeeTestId = model.SpellingBeeTestId,
                TestDate          = DateTime.Now,
                TestWords         = testWords
            };
            //Creates UserTest and TestWord in one call.
            var userTestId = _repository.CreateUserTest(userTest);

            var percentage          = Convert.ToInt32(Decimal.Round((Convert.ToDecimal(model.TestRights) / Convert.ToDecimal(model.Words.Count)), 2) * 100);
            var testResultViewModel = new TestResultViewModel()
            {
                RightPercentage = percentage
            };

            //Uppdatera UserTest med procenten
            _repository.UpdateUserTestscore(model.SpellingBeeTestId, percentage);

            return(RedirectToAction("SpellTestResult", testResultViewModel));
        }