public List <UserAchievement> UpdateUserAchievements(User user,
                                                             Course course,
                                                             AchievementTypesToUpdate achievementTypesToUpdate,
                                                             ICollection <Achievement> systemAchievements,
                                                             DateTime utcNow)
        {
            var userAchievements       = user.Achievements;
            var achievementsUpdatedNow = new List <UserAchievement>();

            foreach (var notCompletedAchievement in userAchievements
                     .Where(x => !x.IsCompleted && x.Achievement.Type != AchievementType.CompleteCourse))
            {
                if (LessonOrChapterTypeAchievementsMustBeUpdates(achievementTypesToUpdate, notCompletedAchievement.Achievement.Type))
                {
                    UpdateAchievement(notCompletedAchievement, achievementsUpdatedNow, utcNow);
                }
            }

            if (achievementTypesToUpdate.CourseCompleted)
            {
                var completedCourseAchievement = new UserAchievement
                {
                    Achievement = systemAchievements.First(x => x.Type == AchievementType.CompleteCourse),
                    IsCompleted = true,
                    CompletedOn = utcNow,
                    Progress    = 1,
                    Course      = course
                };
                user.Achievements.Add(completedCourseAchievement);
                achievementsUpdatedNow.Add(completedCourseAchievement);
            }

            return(achievementsUpdatedNow);
        }
        public AchievementTypesToUpdate CalculateAchievementTypesToUpdate(ICollection <UserLesson> userCompletedLessons, Course currentCourse, int chapterId)
        {
            var achievementTypesToUpdate = new AchievementTypesToUpdate
            {
                LessonCompleted = true
            };
            var currentChapter = currentCourse.Chapters.First(x => x.Id == chapterId);

            if (UserHasCompletedAllLessonsForCurrentChapter(userCompletedLessons, currentChapter))
            {
                achievementTypesToUpdate.ChapterCompleted = true;
            }
            if (UserHasCompletedAllLessonsForCurrentCourse(userCompletedLessons, currentCourse))
            {
                achievementTypesToUpdate.CourseCompleted = true;
            }
            return(achievementTypesToUpdate);
        }
 private bool LessonOrChapterTypeAchievementsMustBeUpdates(AchievementTypesToUpdate achievementTypesToUpdate, AchievementType userAchievementType)
 {
     return((achievementTypesToUpdate.LessonCompleted && userAchievementType == AchievementType.CompleteLesson) ||
            (achievementTypesToUpdate.ChapterCompleted && userAchievementType == AchievementType.CompleteChapter));
 }