public async Task SetCurrentTestResults(int?id) { try { // I. Clear controller properties for processing a test period. //TestPeriod = 0; //StartTestTime = null; // II. Check. OperationDetails operationDetails; string currentUserId = System.Web.HttpContext.Current.User.Identity.GetUserId(); if (currentUserId == null || AllAnswers.Count == 0 || UserAnswers.Count == 0) { return; } // III.Get the test result. operationDetails = PLRepository.CalculateTestResults(AllAnswers, UserAnswers, TestQuetions, out TestResultViewModel testResult); if (!operationDetails.Succedeed) { return; } // Is the test passed? int firstQuestionId = AllAnswers.FirstOrDefault().Key; CourseDTO courseDTO = (await QuestionService.GetAsync(firstQuestionId)).Topic.Course; testResult.IsPassedTest = testResult.Result > testResult.MaxScore * courseDTO.PassingScore / 100; // IV. Write the test results to DB. operationDetails = await SaveCurrentTestResults(id, currentUserId, testResult); } catch (Exception ex) { throw new Exception(ex.Message); } }
// Subject specialities list. public async Task <ActionResult> SubjectSpecialities(int?id) { try { // I. Checks. // Get Id for the current user. string currentUserId = System.Web.HttpContext.Current.User.Identity.GetUserId(); if (currentUserId == null) { return(RedirectToAction("Login", "Account")); } // Check id. if (!int.TryParse(id.ToString(), out int intId)) { return(RedirectToAction("Index")); } // Get SubjectDTO. SubjectDTO subjectDTO = await SubjectService.GetAsync(intId); if (subjectDTO == null) { return(RedirectToAction("Index")); } // II. Set ViewBag properties. PLRepository.SetViewBagProperiesForModeratorSubscription(UserService, currentUserId, SubscriptionForModeratorService, out bool?viewBagIsAllowedToSuggest, out bool?viewBagGoToTrialSubscription, out bool?viewBagGoToSubscription); ViewBag.IsAllowedToSuggest = viewBagIsAllowedToSuggest; ViewBag.GoToTrialSubscription = viewBagGoToTrialSubscription; ViewBag.GoToSubscription = viewBagGoToSubscription; ViewBag.ParentId = intId; ViewBag.SubjectName = subjectDTO.SubjectName; // III. AutoMapper Setup. var config = new MapperConfiguration(cfg => { cfg.CreateMap <SpecialityDTO, SpecialityViewModel>() .ForMember("Id", opt => opt.MapFrom(obj => obj.SpecialityId)) .ForMember("Name", opt => opt.MapFrom(obj => obj.SpecialityName)); }); IMapper iMapper = config.CreateMapper(); // IV. Get data for a view. List <SpecialityDTO> source = SpecialityService.Find(dto => dto.SubjectId == intId && dto.IsApproved).OrderBy(o => o.SpecialityName).ToList(); IEnumerable <SpecialityViewModel> specialityOrderedList = iMapper.Map <IEnumerable <SpecialityDTO>, IEnumerable <SpecialityViewModel> >(source); // V. return(View(specialityOrderedList)); } catch (Exception ex) { throw new Exception(ex.Message); } }
// Get test results. public async Task <ActionResult> GetCurrentTestResults(int?id) { try { // I. Clear controller properties for processing a test period. TestPeriod = 0; StartTestTime = null; // II. Check. if (AllAnswers.Count == 0 || UserAnswers.Count == 0) { return(RedirectToAction("Index")); } // III.Get the test result. OperationDetails operationDetails = PLRepository.CalculateTestResults(AllAnswers, UserAnswers, TestQuetions, out TestResultViewModel testResult); if (!operationDetails.Succedeed) { return(View("Report", operationDetails)); } // Is the test passed? int firstQuestionId = AllAnswers.FirstOrDefault().Key; CourseDTO courseDTO = (await QuestionService.GetAsync(firstQuestionId)).Topic.Course; testResult.IsPassedTest = testResult.Result > testResult.MaxScore * courseDTO.PassingScore / 100; // Set ViewBag property for a View. ViewBag.CourseName = courseDTO.CourseTitle; // IV.Set testResult properies (Result, MaxScore and TestResultDetails). testResult.Result = testResult.Result * 1000 / testResult.MaxScore; testResult.MaxScore = 1000; foreach (var item in testResult.TestResultDetails) { item.Topic = (await QuestionService.GetAsync(item.QuestionId)).Topic.TopicTitle; item.Question = (await QuestionService.GetAsync(item.QuestionId)).QuestionText; } // V. return(View(testResult)); } catch (Exception ex) { throw new Exception(ex.Message); } }
// GET: ModeratorSuscription/Index public ActionResult Index() { try { //I. Check. // Get Id for the current user. string currentUserId = System.Web.HttpContext.Current.User.Identity.GetUserId(); if (currentUserId == null) { return(RedirectToAction("Login", "Account")); } // II. Set ViewBag properties. PLRepository.SetViewBagProperiesForModeratorSubscription(UserService, currentUserId, SubscriptionForModeratorService, out bool?viewBagIsAllowedToSuggest, out bool?viewBagGoToTrialSubscription, out bool?viewBagGoToSubscription); ViewBag.IsAllowedToSuggest = viewBagIsAllowedToSuggest; ViewBag.GoToTrialSubscription = viewBagGoToTrialSubscription; ViewBag.GoToSubscription = viewBagGoToSubscription; // III. AutoMapper Setup. var config = new MapperConfiguration(cfg => { cfg.CreateMap <SubjectDTO, SubjectViewModel>() .ForMember("Id", opt => opt.MapFrom(obj => obj.SubjectId)) .ForMember("Name", opt => opt.MapFrom(obj => obj.SubjectName)); }); IMapper iMapper = config.CreateMapper(); // IV. Get data for a view. IEnumerable <SubjectViewModel> subjects = iMapper.Map <IEnumerable <SubjectDTO>, IEnumerable <SubjectViewModel> >(SubjectService.Find(subject => subject.IsApproved)) .OrderBy(obj => obj.Name); // V. return(View(subjects)); } catch (Exception ex) { throw new Exception(ex.Message); } }
// Speciality courses list. public async Task <ActionResult> SpecialityCourses(int?id) { try { // I. Checks. // Get Id for the current user. string currentUserId = System.Web.HttpContext.Current.User.Identity.GetUserId(); if (currentUserId == null) { return(RedirectToAction("Login", "Account")); } // Check id. if (!int.TryParse(id.ToString(), out int intId)) { return(RedirectToAction("Index")); } // Get SubjectDTO. SpecialityDTO specialityDTO = await SpecialityService.GetAsync(intId); if (specialityDTO == null) { return(RedirectToAction("Index")); } // II. Set ViewBag properties. ViewBag.ParentId = intId; ViewBag.SpecialityName = specialityDTO.SpecialityName; ViewBag.ParentParentId = specialityDTO.SubjectId; // If a current user is Admin. if (UserService.FindUserRoleById(currentUserId).ToLower() == "admin") { ViewBag.IsAllowToAddCourses = true; ViewBag.IsAdmin = true; } else { ViewBag.IsAdmin = false; PLRepository.SetViewBagProperiesForModeratorSubscription(UserService, currentUserId, SubscriptionForModeratorService, out bool?viewBagIsAllowedToSuggest, out bool?viewBagGoToTrialSubscription, out bool?viewBagGoToSubscription); ViewBag.GoToTrialSubscription = viewBagGoToTrialSubscription; ViewBag.GoToSubscription = viewBagGoToSubscription; // Count of existed courses. int existedCourseCount = CourseService.Find(dto => dto.UserProfileId == currentUserId).Count(); // Get count of allowed courses according to the active subscription. IEnumerable <SubscriptionForModeratorDTO> subscriptions = SubscriptionForModeratorService.Find(obj => obj.UserProfileId == currentUserId && obj.IsApproved); if (subscriptions.Count() == 0) { ViewBag.IsAllowToAddCourses = false; } else { int allowedCourseCount = (from sub in subscriptions where sub.StartDate < DateTime.Now && (DateTime.Now - sub.StartDate < TimeSpan.FromDays(sub.SubscriptionPeriod) && sub.IsApproved) select sub).FirstOrDefault().CourseCount; bool isAllowToAddCourses = allowedCourseCount > existedCourseCount; if (isAllowToAddCourses) { ViewBag.MessageAboutAllowedCourses = string.Format("According to terms of your subscription you are allowed {0} course(s). You can add {1} more course(s).", allowedCourseCount, allowedCourseCount - existedCourseCount); } else { ViewBag.MessageAboutAllowedCourses = "According to terms of your subscription you can't add a new course. Delete an unnecessary course or upgrade to a new subscription."; } ViewBag.IsAllowToAddCourses = isAllowToAddCourses; } } // III. AutoMapper Setup. var config = new MapperConfiguration(cfg => { cfg.CreateMap <CourseDTO, CourseViewModel>() .ForMember("Id", opt => opt.MapFrom(obj => obj.CourseId)) .ForMember("Name", opt => opt.MapFrom(obj => obj.CourseTitle)); }); IMapper iMapper = config.CreateMapper(); // IV. Get data for a view. IEnumerable <CourseDTO> source = CourseService.Find(dto => dto.SpecialityId == intId && dto.UserProfileId == currentUserId) .OrderBy(o => o.CourseTitle) .OrderBy(o => o.IsApproved); IEnumerable <CourseViewModel> courseOrderedList = iMapper.Map <IEnumerable <CourseDTO>, IEnumerable <CourseViewModel> >(source); // V. return(View(courseOrderedList)); } catch (Exception ex) { throw new Exception(ex.Message); } }