public async Task <IActionResult> EditTest(int topicId, int testId) { List <QuestionModel> questions = (await _questionManager.GetQuestionsByTestIdSortedByStageAsync(testId)) .Select(question => { var questionModel = _mapper.Map <QuestionModel>(question); if (!string.IsNullOrWhiteSpace(question.ImageFullName)) { questionModel.ImageLocation = $"/{WebExtensions.ImagesFolderName}/" + Path.GetFileName(questionModel.ImageFullName); } return(questionModel); }) .ToList(); DomainTest test = await _testManager.GetTestByIdAsync(testId); ViewData["TopicId"] = topicId; ViewData["TestName"] = test.Name; ViewData["TestId"] = test.Id; return(View(questions)); }
public async Task <IActionResult> CreateQuestion(QuestionModel model) { if (ModelState.IsValid && await _testManager.IsTestExistsAsync(model.TestId)) { List <int> stages = (await _testManager.GetTestStagesAsync(model.TestId)).ToList(); if (model.Stage == 0) { ViewData["Stage"] = model.Stage; ModelState.AddModelError("", "Invalid Stage"); return(View(model)); } if (stages.Count > 0) { if (stages[stages.Count - 1] + 2 <= model.Stage) { ViewData["Stage"] = model.Stage; ModelState.AddModelError("", "Invalid Stage"); return(View(model)); } } IFormFile image = model.Image; if (image != null) { string imageFullName = Path.Combine(WebExtensions.ImagesFolderFullName, Guid.NewGuid() + Path.GetFileName(image.FileName)); image.CopyTo(new FileStream(imageFullName, FileMode.Create)); model.ImageFullName = imageFullName; } var domainQuestion = _mapper.Map <DomainQuestion>(model); await _questionManager.CreateQuestionAsync(domainQuestion); DomainTest test = await _testManager.GetTestByIdAsync(model.TestId); return(RedirectToAction("EditTest", "Test", new { @TopicId = test.TopicId, @TestId = model.TestId })); } ModelState.AddModelError("", "Invalid Question information"); return(View(model)); }
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")); }
public async Task <IActionResult> CreateAnswer(AnswerModel model) { if (ModelState.IsValid && await _questionManager.IsQuestionExistsAsync(model.QuestionId)) { var domainAnswer = _mapper.Map <DomainAnswer>(model); int answersCount = await _answersManager.GetAnswerCountByQuestionIdAsync(model.QuestionId); if (model.QuestionType == QuestionTypes.Text && answersCount >= 1) { return(BadRequest()); } await _answersManager.CreateAnswerAsync(domainAnswer); DomainTest test = await _testManager.GetTestByIdAsync(model.TestId); return(RedirectToAction("EditQuestion", "Test", new { @TopicId = test.TopicId, @TestId = model.TestId, @QuestionId = model.QuestionId, @QuestionType = model.QuestionType })); } ModelState.AddModelError("", "Invalid Question information"); return(View(model)); }
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)); }
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)); }