public async Task <ActionResult> Respond(int id, int?pageId)
        {
            var userSection = await _surveyManager.GetCurrentUserSectionAsync(User.Identity.GetUserId(), id);

            var userPages = (await _surveyManager.GetUserPagesAsync(User.Identity.GetUserId(), userSection.Id.GetValueOrDefault())).ToList();
            var userTest  = await _testManager.GetAsync(User.Identity.GetUserId(), id);

            var userPage = pageId.HasValue
                ? userPages.Single(p => p.Page.Id == pageId.Value)
                : await _surveyManager.GetCurrentUserPageAsync(User.Identity.GetUserId(), id);

            if (userPage == null)
            {
                return(RedirectToAction("SectionReview", "Test", new { id }));
            }

            var viewModel = _mapper.Map <ResponseViewModel>(userPage);

            var pageIndex = userPages.FindIndex(up => up.Page.Id == userPage.Page.Id);

            viewModel.TestId      = userTest.Id;
            viewModel.PriorPageId = pageIndex != 0
                ? userPages[pageIndex - 1].Page.Id
                : (int?)null;

            return(View((viewModel.Page.GetType().BaseType ?? viewModel.Page.GetType()).Name, viewModel));
        }
示例#2
0
        public async Task <UserSurvey> CompleteCurrentSectionAsync(string userId, int surveyId, CancellationToken cancellationToken = default(CancellationToken))
        {
            var section = await _surveyManager.GetCurrentUserSectionAsync(userId, surveyId, cancellationToken);

            var sectionMarker = await TestSectionMarkers
                                .SingleOrDefaultAsync(tsm => tsm.TestId == section.TestId && tsm.SectionId == section.Id, cancellationToken)
                                ?? throw new SectionMarkerNotFoundException(section.TestId.GetValueOrDefault(), section.Id.GetValueOrDefault());

            sectionMarker.Completed = DateTime.UtcNow;

            // Check if the survey is complete
            var sections = await _surveyManager.GetUserSectionsAsync(userId, surveyId, cancellationToken);

            if (sections.Where(s => s.Id != section.Id).All(s => s.Completed.HasValue))
            {
                // Close the test if all the section markers are complete.
                var test = Tests.SingleOrDefault(t => t.UserId.Equals(userId, StringComparison.OrdinalIgnoreCase) && t.SurveyId == surveyId) ?? throw new TestNotFoundException(userId, surveyId);
                test.Completed = DateTime.UtcNow;
            }

            await _dbContext.SaveChangesAsync(cancellationToken);

            var result = await _surveyManager.GetUserSurveyAsync(userId, surveyId, cancellationToken);

            return(result);
        }