Пример #1
0
        public async Task <ActionResult <FlashcardResponseByUnits> > Flashcards([FromRoute] Course course)
        {
            var userFlashcardsVisitsByCourse = await usersFlashcardsVisitsRepo.GetUserFlashcardsVisitsAsync(UserId, course.Id);

            var flashcardResponseByUnits = new FlashcardResponseByUnits();
            var visibleUnits             = await unitsRepo.GetVisibleUnitIdsAsync(course, UserId);

            foreach (var unit in course.GetUnits(visibleUnits))
            {
                var unitFlashcardsResponse = new UnitFlashcardsResponse();
                var unitFlashcards         = unit.Flashcards;
                if (unitFlashcards.Count == 0)
                {
                    continue;
                }

                var flashcardResponsesEnumerable = GetFlashcardResponses(unit, unitFlashcards, userFlashcardsVisitsByCourse);

                unitFlashcardsResponse.Flashcards.AddRange(flashcardResponsesEnumerable);
                unitFlashcardsResponse.UnitId    = unit.Id;
                unitFlashcardsResponse.UnitTitle = unit.Title;
                unitFlashcardsResponse.Unlocked  = await IsUnlocked(course, unit);

                flashcardResponseByUnits.Units.Add(unitFlashcardsResponse);
            }

            return(flashcardResponseByUnits);
        }
Пример #2
0
        public async Task <ActionResult <GroupScoringGroupsResponse> > ScoringGroups(int groupId)
        {
            var group = await groupsRepo.FindGroupByIdAsync(groupId).ConfigureAwait(false);

            var course = await courseManager.FindCourseAsync(@group.CourseId);

            if (course == null)
            {
                log.Error($"It's strange: group {groupId} exists, but course {group.CourseId} not. I will return 404");
                return(NotFound(new ErrorResponse("Group or course not found")));
            }

            var scoringGroups  = course.Settings.Scoring.Groups.Values.ToList();
            var visibleUnitIds = await unitsRepo.GetVisibleUnitIdsAsync(course, UserId);

            var scoringGroupsCanBeSetInSomeUnit = GetScoringGroupsCanBeSetInSomeUnit(course.GetUnits(visibleUnitIds));
            var enabledScoringGroups            = await groupsRepo.GetEnabledAdditionalScoringGroupsForGroupAsync(groupId).ConfigureAwait(false);

            return(new GroupScoringGroupsResponse
            {
                Scores = scoringGroups.Select(scoringGroup => BuildGroupScoringGroupInfo(scoringGroup, scoringGroupsCanBeSetInSomeUnit, enabledScoringGroups)).ToList(),
            });
        }
        private async Task <UserFlashcardStatistics> GetUserFlashcardStatistics(GroupMember member, Group group, Course course)
        {
            var userStat = new UserFlashcardStatistics
            {
                UserId    = member.UserId,
                UserName  = member.User.VisibleNameWithLastNameFirst,
                GroupId   = group.Id,
                GroupName = group.Name
            };
            var userVisits = await usersFlashcardsVisitsRepo.GetUserFlashcardsVisitsAsync(member.UserId, course.Id);

            var visitsByUnits = userVisits.GroupBy(x => x.UnitId).ToDictionary(x => x.Key);

            var visibleUnitsIds = await unitsRepo.GetVisibleUnitIdsAsync(course, UserId);

            foreach (var unit in course.GetUnits(visibleUnitsIds))
            {
                var unitStat = new UnitUserStatistic {
                    UnitId = unit.Id, UnitTitle = unit.Title, TotalFlashcardsCount = unit.Flashcards.Count
                };
                if (visitsByUnits.TryGetValue(unit.Id, out var unitGroup))
                {
                    var rate5FlashcardsIds  = new HashSet <string>();
                    var uniqueFlashcardsIds = new HashSet <string>();

                    foreach (var visit in unitGroup)
                    {
                        if (visit.Rate == Rate.Rate5)
                        {
                            rate5FlashcardsIds.Add(visit.FlashcardId);
                        }

                        uniqueFlashcardsIds.Add(visit.FlashcardId);
                    }

                    unitStat.Rate5Count            = rate5FlashcardsIds.Count;
                    unitStat.UniqueFlashcardVisits = uniqueFlashcardsIds.Count;
                    unitStat.TotalFlashcardVisits  = unitGroup.Count();
                }

                userStat.UnitUserStatistics.Add(unitStat);
            }

            return(userStat);
        }
Пример #4
0
        public async Task <ActionResult <CourseInfo> > CourseInfo([FromRoute] string courseId, [FromQuery][CanBeNull] int?groupId = null)
        {
            if (!await courseManager.HasCourseAsync(courseId))
            {
                return(NotFound(new ErrorResponse("Course not found")));
            }

            var course = await courseManager.FindCourseAsync(courseId);

            List <UnitInfo> units;
            var             visibleUnitsIds = await unitsRepo.GetVisibleUnitIdsAsync(course, UserId);

            var visibleUnits = course.GetUnits(visibleUnitsIds);

            if (groupId == null)
            {
                var isInstructor = await courseRolesRepo.HasUserAccessToCourseAsync(UserId, course.Id, CourseRoleType.Instructor).ConfigureAwait(false);

                if (!isInstructor && visibleUnits.Count == 0)
                {
                    return(NotFound(new ErrorResponse("Course not found")));
                }

                var unitAppearances = !isInstructor
                                        ? new Dictionary <Guid, UnitAppearance>()
                                        : (await unitsRepo.GetUnitAppearancesAsync(course)).ToDictionary(a => a.UnitId, a => a);
                var publishedUnitIds     = new HashSet <Guid>(!isInstructor ? visibleUnitsIds : await unitsRepo.GetPublishedUnitIdsAsync(course));
                var getSlideMaxScoreFunc = await BuildGetSlideMaxScoreFunc(solutionsRepo, userQuizzesRepo, visitsRepo, groupsRepo, course, UserId);

                var getGitEditLinkFunc = await BuildGetGitEditLinkFunc(User.GetUserId(), course, courseRolesRepo, coursesRepo);

                units = visibleUnits.Select(unit => BuildUnitInfo(course.Id, unit,
                                                                  !publishedUnitIds.Contains(unit.Id), publishedUnitIds.Contains(unit.Id) ? null : unitAppearances.GetOrDefault(unit.Id)?.PublishTime,
                                                                  isInstructor, getSlideMaxScoreFunc, getGitEditLinkFunc)).ToList();
            }
            else
            {
                var group = await groupsRepo.FindGroupByIdAsync(groupId.Value).ConfigureAwait(false);

                if (group == null)
                {
                    return(NotFound(new ErrorResponse("Group not found")));
                }

                async Task <bool> IsUserMemberOfGroup() => await groupMembersRepo.IsUserMemberOfGroup(groupId.Value, UserId).ConfigureAwait(false);
                async Task <bool> IsGroupVisibleForUserAsync() => await groupAccessesRepo.IsGroupVisibleForUserAsync(groupId.Value, UserId).ConfigureAwait(false);

                var isGroupAvailableForUser = await IsUserMemberOfGroup() || await IsGroupVisibleForUserAsync();

                if (!isGroupAvailableForUser)
                {
                    return(NotFound(new ErrorResponse("Group not found")));
                }

                if (visibleUnits.Count == 0)
                {
                    return(NotFound(new ErrorResponse("Course not found")));
                }

                var getSlideMaxScoreFunc = BuildGetSlideMaxScoreFunc(course, group);
                var getGitEditLinkFunc   = await BuildGetGitEditLinkFunc(User.GetUserId(), course, courseRolesRepo, coursesRepo);

                units = visibleUnits.Select(unit => BuildUnitInfo(course.Id, unit, false, null, false, getSlideMaxScoreFunc, getGitEditLinkFunc)).ToList();
            }

            var containsFlashcards = visibleUnits.Any(x => x.GetSlides(true).OfType <FlashcardSlide>().Any());
            var scoringSettings    = GetScoringSettings(course);
            var tempCourseError    = (await tempCoursesRepo.GetCourseErrorAsync(courseId))?.Error;
            var isTempCourse       = await tempCoursesRepo.FindAsync(courseId) != null;

            return(new CourseInfo
            {
                Id = course.Id,
                Title = isTempCourse ? "Временный - " + course.Title : course.Title,
                Description = course.Settings.Description,
                Scoring = scoringSettings,
                NextUnitPublishTime = unitsRepo.GetNextUnitPublishTime(course.Id),
                Units = units,
                ContainsFlashcards = containsFlashcards,
                IsTempCourse = isTempCourse,
                TempCourseError = tempCourseError
            });
        }