Beispiel #1
0
        public async Task <IActionResult> ShowTestsInTopicToUser(int topicId, string search, int page = 1, int size = 5)
        {
            string userEmail = User.Identity.Name;
            int    userId    = await _userManager.GetUserIdAsync(userEmail);

            if (!await _testManager.IsUserTopicExistsAsync(userId, topicId))
            {
                var userTopic = new DomainUserTopic
                {
                    UserId  = userId,
                    TopicId = topicId,
                    Status  = TopicStatus.NotStarted,
                    Points  = 0
                };

                await _testManager.CreateUserTopicAsync(userTopic);
            }

            List <int> topicTestIds = (await _testManager.GetTestsInTopicAsync(topicId))
                                      .Select(x => x.Id)
                                      .ToList();

            List <int> userTopicTestIds = (await _testManager.GetUserTestsInTopicAsync(topicId, userId))
                                          .Select(x => x.TestId).ToList();

            foreach (int testId in topicTestIds)
            {
                if (!userTopicTestIds.Contains(testId))
                {
                    var domainUserTest = new DomainUserTest
                    {
                        Status = TestStatus.NotStarted,
                        TestId = testId,
                        UserId = userId,
                    };

                    if (!await _testManager.IsUserTestExistsAsync(userId, testId))
                    {
                        await _testManager.CreateUserTestAsync(domainUserTest);
                    }
                }
            }

            IReadOnlyCollection <UserTestModel> userTopicTests = (await _testManager.GetUserTestsInTopicAsync(topicId, userId))
                                                                 .Select(x => _mapper.Map <UserTestModel>(x)).ToList();

            ViewData["TopicId"] = topicId;
            ViewData["Search"]  = search == null ? string.Empty : search;
            ViewData["Page"]    = page;
            ViewData["Size"]    = size;

            return(View(userTopicTests));
        }
Beispiel #2
0
        public async Task <ActionResult> TestTimeExpired(int topicId, int testId)
        {
            int userId = await _userManager.GetUserIdAsync(User.Identity.Name);

            DomainTest test = await _testManager.GetTestByIdAsync(testId);

            DomainUserTest userTest = await _testManager.GetUserTestAsync(userId, testId);

            int secondsLeft = test.Minutes * 60 - Convert.ToInt32(Math.Abs((userTest.StartTime - DateTime.Now).TotalSeconds));

            if (secondsLeft <= 0)
            {
                await FinishTest(userId, testId);

                await TryFinishTopicAndIfTopicIsFinishedSendEmail(userId, topicId);

                return(PartialView("_EndTest", new EndTestModel {
                    TopicId = topicId
                }));
            }

            return(BadRequest("TimeExpired"));
        }
Beispiel #3
0
        public async Task <IActionResult> StartTest()
        {
            Dictionary <string, string> dictReq = Request.Form.ToDictionary(x => x.Key, x => x.Value.ToString());
            string     submitButtonKey          = dictReq.Keys.Single(x => x.Contains("SubmitButton"));
            string     submitButtonValue        = dictReq[submitButtonKey];
            int        topicId = int.Parse(dictReq["TopicId"]);
            int        testId  = int.Parse(dictReq["TestId"]);
            int        userId  = int.Parse(dictReq["UserId"]);
            DomainTest test    = await _testManager.GetTestByIdAsync(testId);

            DomainUserTest userTest = await _testManager.GetUserTestAsync(userId, testId);

            if (userTest.Status == TestStatus.Finished)
            {
                return(PartialView("_EndTest", new EndTestModel {
                    TopicId = topicId
                }));                                                                    // to redirect from ajax post
            }
            int           stagesCount               = int.Parse(dictReq["StagesCount"]);
            int           currQuestionId            = int.Parse(dictReq["CurrQuestionId"]);
            int           currQuestionStage         = int.Parse(dictReq["CurrQuestionStage"]);
            QuestionTypes currQuestionType          = (QuestionTypes)Enum.Parse(typeof(QuestionTypes), dictReq["CurrQuestionType"]);
            List <int>    userQuestionIds           = dictReq["UserQuestionIds"].Split(",").Select(x => int.Parse(x)).ToList();
            var           answerKeySubStr           = "AnswerId";
            var           currQuestionImageLocation = string.Empty;

            ViewData["TopicId"] = topicId;

            int secondsLeft = test.Minutes * 60 - Convert.ToInt32(Math.Abs((userTest.StartTime - DateTime.Now).TotalSeconds));

            if (secondsLeft <= 0)
            {
                await FinishTest(userId, testId);

                await TryFinishTopicAndIfTopicIsFinishedSendEmail(userId, topicId);

                return(PartialView("_EndTest", new EndTestModel {
                    TopicId = topicId
                }));                                                                    // to redirect from ajax post
            }

            if (currQuestionType == QuestionTypes.Option)
            {
                List <string> answersKeys = dictReq.Keys.Where(x => x.Contains(answerKeySubStr)).ToList();

                foreach (string answerKey in answersKeys)
                {
                    int  answerId = int.Parse(answerKey.Substring(answerKeySubStr.Length));
                    bool isValid  = dictReq[answerKey].Contains("true") ? true : false;

                    await _answersManager.UpdateUserAnswerValidAsync(userId, answerId, isValid);
                }
            }
            else if (currQuestionType == QuestionTypes.Text)
            {
                string answerKey      = dictReq.Keys.First(x => x.Contains(answerKeySubStr));
                int    answerId       = int.Parse(answerKey.Substring(answerKeySubStr.Length));
                string userAnswerText = dictReq[answerKey];

                await _answersManager.UpdateUserAnswerTextAsync(userId, answerId, userAnswerText);
            }

            ViewData["SubmitButton_1"] = "Next";
            ViewData["SubmitButton_2"] = "Back";
            SetSubmitButtonsTextAndChangeCurrQuestionStage(submitButtonValue, stagesCount, ref currQuestionStage);

            if (submitButtonValue == "Finish" && currQuestionStage == stagesCount)
            {
                await FinishTest(userId, testId);

                await TryFinishTopicAndIfTopicIsFinishedSendEmail(userId, topicId);

                return(PartialView("_EndTest", new EndTestModel {
                    TopicId = topicId
                }));
            }

            DomainQuestion currQuestion = await _questionManager.GetQuestionByIdAsync(userQuestionIds[currQuestionStage - 1]);

            List <UserAnswerModel> currQuestionUserAnswers = (await _answersManager.GetUserAnswersByQuestionIdAsync(userId, currQuestion.Id))
                                                             .Select(x => _mapper.Map <UserAnswerModel>(x))
                                                             .ToList();

            if (!string.IsNullOrWhiteSpace(currQuestion.ImageFullName))
            {
                currQuestionImageLocation = $"/{WebExtensions.ImagesFolderName}/" + Path.GetFileName(currQuestion.ImageFullName);
            }

            var startTest = new StartTestModel()
            {
                TopicId         = topicId,
                TestId          = testId,
                UserId          = userId,
                StagesCount     = stagesCount,
                SecondsLeft     = secondsLeft,
                UserQuestionIds = dictReq["UserQuestionIds"],

                CurrQuestionId            = currQuestion.Id,
                CurrQuestionText          = currQuestion.Text,
                CurrQuestionStage         = currQuestion.Stage,
                CurrQuestionType          = currQuestion.QuestionType,
                CurrQuestionUserAnswers   = currQuestionUserAnswers,
                CurrQuestionImageLocation = currQuestionImageLocation
            };

            return(PartialView("_StartTest", startTest));
        }
Beispiel #4
0
        public async Task <IActionResult> StartTest(int topicId, int testId)
        {
            string userEmail = User.Identity.Name;
            int    userId    = await _userManager.GetUserIdAsync(userEmail);

            DomainTest test = await _testManager.GetTestByIdAsync(testId);

            DomainUserTest userTest = await _testManager.GetUserTestAsync(userId, testId);

            List <int>             testStages              = (await _questionManager.GetTestStagesByTestIdAsync(testId)).ToList();
            int                    stagesCount             = testStages.Count;
            var                    userQuestionIds         = new List <int>();
            DomainQuestion         currQuestion            = null;
            List <UserAnswerModel> currQuestionUserAnswers = null;
            QuestionTypes          currQuestionType;
            string                 currQuestionImageLocation = string.Empty;
            int                    currQuestionStage         = 0;
            int                    secondsLeft = 0;
            DateTime               startTime   = default;

            ViewData["TopicId"] = topicId;

            if (userTest.Status == TestStatus.Finished)
            {
                return(RedirectToAction("ShowTestsInTopicToUser", "Test", new { @TopicId = topicId }));
            }
            else if (userTest.Status == TestStatus.NotStarted)
            {
                await _testManager.UpdateUserTestStatusAsync(userId, testId, TestStatus.NotFinished);

                userQuestionIds = await CreateUserAnswersAndGetQuestionIdsSortedByStageAsync(testId, userId);

                currQuestion = await _questionManager.GetQuestionByIdAsync(userQuestionIds[0]);

                secondsLeft = test.Minutes * _toSecondsConstant;
                startTime   = DateTime.Now;

                await _testManager.UpdateUserTestStartTimeAsync(userId, testId, startTime);

                await _testManager.UpdateUserTopicStatus(userId, topicId, TopicStatus.NotFinished);
            }
            else if (userTest.Status == TestStatus.NotFinished)
            {
                secondsLeft = test.Minutes * _toSecondsConstant - Convert.ToInt32(Math.Abs((userTest.StartTime - DateTime.Now).TotalSeconds));

                if (secondsLeft <= 0)
                {
                    await FinishTest(userId, testId);

                    await TryFinishTopicAndIfTopicIsFinishedSendEmail(userId, topicId);

                    return(RedirectToAction("ShowTestsInTopicToUser", "Test", new { @TopicId = topicId }));
                }

                (await _questionManager.GetUserQuestionsByTestIdSortedByStageAsync(userId, testId)).ToList()
                .ForEach(question => userQuestionIds.Add(question.Id));

                currQuestion = await _questionManager.GetQuestionByIdAsync(userQuestionIds[0]);
            }

            currQuestionUserAnswers = (await _answersManager.GetUserAnswersByQuestionIdAsync(userId, currQuestion.Id))
                                      .Select(x => _mapper.Map <UserAnswerModel>(x)).ToList();
            currQuestionStage = currQuestion.Stage;
            currQuestionType  = currQuestion.QuestionType;

            if (!string.IsNullOrWhiteSpace(currQuestion.ImageFullName))
            {
                currQuestionImageLocation = $"/{WebExtensions.ImagesFolderName}/" + Path.GetFileName(currQuestion.ImageFullName);
            }

            var startTest = new StartTestModel()
            {
                TopicId         = topicId,
                UserId          = userId,
                TestId          = testId,
                StagesCount     = stagesCount,
                StartTime       = startTime,
                SecondsLeft     = secondsLeft,
                UserQuestionIds = string.Join(",", userQuestionIds),

                CurrQuestionId            = currQuestion.Id,
                CurrQuestionText          = currQuestion.Text,
                CurrQuestionStage         = currQuestion.Stage,
                CurrQuestionType          = currQuestionType,
                CurrQuestionUserAnswers   = currQuestionUserAnswers,
                CurrQuestionImageLocation = currQuestionImageLocation
            };

            ViewData["SubmitButton_1"] = "Next";
            ViewData["SubmitButton_2"] = string.Empty;

            if (stagesCount == 1)
            {
                ViewData["SubmitButton_1"] = "Finish";
            }

            return(View(startTest));
        }