コード例 #1
0
        public async Task <ActionResult> ModeratorCourseTestResults(int?courseId, string userEmail)
        {
            try
            {
                // I. Checks.
                //  Get course Id for the selected course and check User email.
                if (!int.TryParse(courseId.ToString(), out int intCourseId) && userEmail != null)
                {
                    return(RedirectToAction("Index"));
                }
                // Get CourseDTO object.
                CourseDTO courseDTO = await CourseService.GetAsync(intCourseId);

                if (courseDTO == null)
                {
                    return(RedirectToAction("Index"));
                }

                // II. Set ViewBag properties.
                ViewBag.CourseTitle = courseDTO.CourseTitle;
                ViewBag.ParentId    = intCourseId;
                ViewBag.Trainee     = userEmail;

                // III. AutoMapper Setup.
                var config = new MapperConfiguration(cfg =>
                {
                    cfg.CreateMap <TestResultDTO, TestResultViewModel>()
                    .ForMember("Id", opt => opt.MapFrom(tr => tr.TestResultId))
                    .ForMember("TestTitle", opt => opt.MapFrom(tr => tr.TestResultDetails.FirstOrDefault().Question.Topic.Course.CourseTitle))
                    .ForMember("UserEmail", opt => opt.MapFrom(tr => tr.UserProfile.Email));
                    cfg.CreateMap <TestResultDetailDTO, TestResultDetailViewModel>()
                    .ForMember("Question", opt => opt.MapFrom(trd => trd.Question.QuestionText))
                    .ForMember("Topic", opt => opt.MapFrom(trd => trd.Question.Topic.TopicTitle));
                });
                IMapper iMapper = config.CreateMapper();

                // IV. Get data for a view.
                IEnumerable <SubscriptionDTO> courseSubscriptions = SubscriptionService.Find(dto =>
                                                                                             dto.CourseId == intCourseId &&
                                                                                             dto.Email == userEmail);
                IEnumerable <TestResultDTO> source = TestResultService.Find(tr => !tr.IsTopicTest)
                                                     .Where(tr => tr.TestResultDetails.FirstOrDefault().Question.Topic.CourseId == intCourseId)
                                                     .Join(courseSubscriptions,
                                                           tr => tr.UserProfileId,
                                                           cs => cs.UserProfileId,
                                                           (tr, cs) => tr).Distinct();                //.ToList();
                IEnumerable <TestResultViewModel> courseTestResults = iMapper.Map <IEnumerable <TestResultDTO>, IEnumerable <TestResultViewModel> >(source);

                // V.
                Session["PreviousAction"] = "ModeratorCourseTestResults";
                Session["UserEmail"]      = userEmail;
                return(View(courseTestResults));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
コード例 #2
0
        public async Task <ActionResult> ModeratorCourseSubscribers(int?id)
        {
            // I. Checks.
            //  Get Id for the selected course.
            if (!int.TryParse(id.ToString(), out int intId))
            {
                return(RedirectToAction("Index"));
            }
            // Get CourseDTO object.
            CourseDTO courseDTO = await CourseService.GetAsync(intId);

            if (courseDTO == null)
            {
                return(RedirectToAction("Index"));
            }

            // II. Set ViewBag properties.
            ViewBag.CourseName = courseDTO.CourseTitle;
            ViewBag.CourseId   = intId;

            // III. AutoMapper Setup.
            var config = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap <TestResultDTO, TestResultViewModel>()
                .ForMember("Id", opt => opt.MapFrom(tr => tr.TestResultId))
                .ForMember("TestTitle", opt => opt.MapFrom(tr => tr.TestResultDetails.FirstOrDefault().Question.Topic.Course.CourseTitle))
                .ForMember("UserEmail", opt => opt.MapFrom(tr => tr.UserProfile.Email));
                cfg.CreateMap <TestResultDetailDTO, TestResultDetailViewModel>()
                .ForMember("Question", opt => opt.MapFrom(trd => trd.Question.QuestionText))
                .ForMember("Topic", opt => opt.MapFrom(trd => trd.Question.Topic.TopicTitle));
            });
            IMapper iMapper = config.CreateMapper();

            // IV. Get data for a view.
            IEnumerable <SubscriptionDTO> courseSubscriptions = SubscriptionService.Find(dto =>
                                                                                         dto.CourseId == intId &&
                                                                                         dto.IsApproved);
            //IEnumerable<TestResultDTO> source = TestResultService.Find(tr => !tr.IsTopicTest)
            IEnumerable <TestResultDTO> source = TestResultService.GetAll()
                                                 .Where(tr => tr.TestResultDetails.FirstOrDefault().Question.Topic.CourseId == intId)
                                                 .Join(courseSubscriptions,
                                                       tr => tr.UserProfileId,
                                                       cs => cs.UserProfileId,
                                                       (tr, cs) => tr).Distinct();                //.ToList();
            IEnumerable <TestResultViewModel>           courseTestResults = iMapper.Map <IEnumerable <TestResultDTO>, IEnumerable <TestResultViewModel> >(source);
            IEnumerable <SubscriberTestResultViewModel> resultView        = courseTestResults.GroupBy(ctr => ctr.UserEmail)
                                                                            .Select(ctr => new SubscriberTestResultViewModel
            {
                UserEmail      = ctr.Key,
                AttemptsNumber = ctr.Count(item => !item.IsTopicTest),
                BestResult     = ctr.Max((item) =>
                {
                    if (!item.IsTopicTest)
                    {
                        return(item.Result * 1000 / item.MaxScore);
                    }
                    return(0);
                }),
                IsPassedTest = ctr.Max((item) =>
                {
                    if (!item.IsTopicTest)
                    {
                        return(item.IsPassedTest);
                    }
                    return(false);
                })
            });

            // V.
            return(View(resultView));
        }
コード例 #3
0
        // Course topics list.
        public async Task <ActionResult> CourseTopics(int?id)
        {
            try
            {
                // I.Checks.
                // Check id.
                if (!int.TryParse(id.ToString(), out int intId))
                {
                    return(RedirectToAction("Index"));
                }
                // Get courseDTO object.
                CourseDTO courseDTO = await CourseService.GetAsync(intId);

                if (courseDTO == null)
                {
                    return(RedirectToAction("Index"));
                }

                // II. AutoMapper Setup.
                var config = new MapperConfiguration(cfg =>
                {
                    cfg.CreateMap <TestResultDTO, TestResultViewModel>()
                    .ForMember("Id", opt => opt.MapFrom(tr => tr.TestResultId))
                    .ForMember("TestTitle", opt => opt.MapFrom(tr => tr.TestResultDetails.FirstOrDefault().Question.Topic.TopicTitle));
                    cfg.CreateMap <TestResultDetailDTO, TestResultDetailViewModel>()
                    .ForMember("Question", opt => opt.MapFrom(trd => trd.Question.QuestionText))
                    .ForMember("Topic", opt => opt.MapFrom(trd => trd.Question.Topic.TopicTitle));
                    cfg.CreateMap <TopicDTO, TopicViewModel>()
                    .ForMember("ID", opt => opt.MapFrom(obj => obj.TopicId))
                    .ForMember("Name", opt => opt.MapFrom(obj => obj.TopicTitle));
                });
                IMapper iMapper = config.CreateMapper();

                // III. Set ViewBag properties.
                // Get a role name for the current user.
                string currentUserId = System.Web.HttpContext.Current.User.Identity.GetUserId();
                if (currentUserId != null)
                {
                    ViewBag.RoleName = UserService.FindUserRoleById(currentUserId);
                }
                else
                {
                    ViewBag.RoleName = null;
                }
                // Check: is this user subscribed to this course?
                SubscriptionDTO activeSubscription = SubscriptionService.Find(obj =>
                                                                              obj.UserProfileId == currentUserId &&
                                                                              obj.CourseId == intId &&
                                                                              (DateTime.Now - obj.StartDate < TimeSpan.FromDays(obj.SubscriptionPeriod)))
                                                     .FirstOrDefault();
                ViewBag.IsInApproving = false;
                ViewBag.IsSubscribed  = false;
                if (activeSubscription != null)
                {
                    if (activeSubscription.IsApproved)
                    {
                        ViewBag.IsSubscribed = true;
                    }
                    else
                    {
                        ViewBag.IsInApproving = true;
                    }
                }
                ViewBag.ParentId       = intId;
                ViewBag.CourseName     = courseDTO.CourseTitle;
                ViewBag.ParentParentId = courseDTO.SpecialityId;
                IEnumerable <TestResultDTO> testResultDTOs = TestResultService.Find(tr => tr.UserProfileId == currentUserId &&
                                                                                    tr.IsTopicTest);
                IEnumerable <TestResultViewModel> userTestResults = iMapper.Map <IEnumerable <TestResultDTO>, IEnumerable <TestResultViewModel> >(testResultDTOs);
                ViewBag.UserTestResults = userTestResults;
                ViewBag.AttemptsNumber  = courseDTO.AttemptsNumber;

                // IV. Get data for a view.
                List <TopicDTO> source = TopicService.Find(dto => dto.CourseId == intId).OrderBy(o => o.TopicNumber).ToList();
                IEnumerable <TopicViewModel> topicOrderedList = iMapper.Map <IEnumerable <TopicDTO>, IEnumerable <TopicViewModel> >(source);

                // VI.
                return(View(topicOrderedList));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }