Beispiel #1
0
        private async Task <HashSet <string> > FilterUsersWhoNotSeeCourse(Notification notification, HashSet <string> recipientsIds)
        {
            if (notification.CourseId != "")
            {
                var course = await courseManager.FindCourseAsync(notification.CourseId);

                if (course != null)
                {
                    var visibleUnits = await unitsRepo.GetPublishedUnitIdsAsync(course);

                    if (!visibleUnits.Any())
                    {
                        var userIdsWithInstructorRoles = await courseRolesRepo.GetListOfUsersWithCourseRoleAsync(CourseRoleType.Tester, notification.CourseId, true);

                        var sysAdminsIds = await usersRepo.GetSysAdminsIdsAsync();

                        recipientsIds.IntersectWith(userIdsWithInstructorRoles.Concat(sysAdminsIds));
                    }
                }
            }
            return(recipientsIds);
        }
        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
            });
        }