private CoursePageRecord GetCurrentCoursePageRecord(RockContext rockContext) { CourseRecordService courseRecordService = new CourseRecordService(rockContext); ChapterRecordService chapterRecordService = new ChapterRecordService(rockContext); CoursePageRecordService coursePageRecordService = new CoursePageRecordService(rockContext); CourseRecord courseRecord = GetCourseRecord(courseRecordService); if (externalRedirectDebug.IsNotNullOrWhiteSpace()) { return(null); } if (courseRecord == null) { throw new Exception("This course does not exist or you are not authorized"); } ChapterRecord chapterRecord = GetNextChapterRecord(courseRecord, chapterRecordService); while (chapterRecord != null) { CoursePageRecord coursePageRecord = GetNextCoursePageRecord(coursePageRecordService, chapterRecord); if (coursePageRecord != null) { ViewState["coursePageRecordId"] = coursePageRecord.Id; return(coursePageRecord); } else { chapterRecord.CompletionDateTime = RockDateTime.Now; chapterRecord.Passed = chapterRecord.CoursePageRecords.Where(p => p.Passed).Count() >= chapterRecord.Chapter.CoursePages.Count(); rockContext.SaveChanges(); if (chapterRecord.Passed == false) { ShowFailedChapter(); return(null); } chapterRecord = GetNextChapterRecord(courseRecord, chapterRecordService); } } //if we got to this place the course is complete courseRecord.CompletionDateTime = RockDateTime.Now; courseRecord.Passed = courseRecord.ChapterRecords.Where(c => c.Passed).Count() >= courseRecord.Course.Chapters.Count(); rockContext.SaveChanges(); CourseRequirementHelper.UpdateCourseStatuses(courseRecord.CourseId, courseRecord.PersonAliasId, courseRecord.Passed); DisplayCompletion(); return(null); }
private ChapterRecord GetNextChapterRecord(CourseRecord courseRecord, ChapterRecordService chapterRecordService) { var chapters = courseRecord.Course.Chapters.OrderBy(c => c.Order).ToList(); foreach (var chapter in chapters) { var chapterRecords = courseRecord.ChapterRecords .Where(cr => cr.ChapterId == chapter.Id); //If no records for this chapter make a new one if (!chapterRecords.Any()) { var chapterRecord = new ChapterRecord { Chapter = chapter, CourseRecord = courseRecord }; chapterRecordService.Add(chapterRecord); chapterRecordService.Context.SaveChanges(); return(chapterRecord); } //If there is an incomplete record select that one if (chapterRecords.Where(cr => cr.CompletionDateTime == null).Any()) { return(chapterRecords.Where(cr => cr.CompletionDateTime == null).FirstOrDefault()); } //If there are no passed records make a new one if (!chapterRecords.Where(cr => cr.Passed).Any()) { var chapterRecord = new ChapterRecord { Chapter = chapter, CourseRecord = courseRecord }; chapterRecordService.Add(chapterRecord); chapterRecordService.Context.SaveChanges(); return(chapterRecord); } //Continue loop because this chapter has been passed } return(null); }