/// <summary>
		/// Returns a user.
		/// </summary>
		private ClassroomMembership GetStudent(Classroom classroom, string team)
		{
			return new ClassroomMembership()
			{
				Classroom = classroom,
				ClassroomId = classroom.Id,
				GitHubTeam = team
			};
		}
		public async Task<IActionResult> Edit(string className, Classroom classroom)
		{
			if (ModelState.IsValid)
			{
				await ClassroomService.UpdateClassroomAsync(classroom);

				return RedirectToAction("Index");
			}
			else
			{
				return View("CreateEdit", classroom);
			}
		}
		/// <summary>
		/// Returns a project.
		/// </summary>
		private Project GetProject(Classroom classroom)
		{
			return new Project()
			{
				Name = "Project1",
				Classroom = classroom,
				ClassroomId = classroom.Id,
				ImmutableFilePaths = Collections.CreateList
				(
					new ImmutableFilePath() { Path = "Immutable" }
				),
				PrivateFilePaths = Collections.CreateList
				(
					new PrivateFilePath() { Path = "Private" }
				)
			};
		}
		/// <summary>
		/// Returns a user.
		/// </summary>
		private User GetUser(Classroom classroom)
		{
			return new User()
			{
				ClassroomMemberships = Collections.CreateList
				(
					new ClassroomMembership()
					{
						Classroom = classroom,
						ClassroomId = classroom.Id,
						GitHubTeam = "LastNameFirstName"
					}
				)
			};
		}
		/// <summary>
		/// Returns the staff team, if any.
		/// </summary>
		private GitHubTeam GetStaffTeam(Classroom classroom, ICollection<GitHubTeam> allTeams)
		{
			return allTeams.FirstOrDefault(team => team.Name == $"{classroom.Name}_Staff");
		}
		/// <summary>
		/// Returns the given user's classroom membership for this classroom.
		/// </summary>
		private static ClassroomMembership GetClassroomMembership(
			User user, 
			Classroom classroom)
		{
			return user?.ClassroomMemberships
				?.SingleOrDefault(m => m.Classroom == classroom);
		}
		/// <summary>
		/// Returns a project.
		/// </summary>
		private Project GetProject(Classroom classroom)
		{
			return new Project()
			{
				Name = "Project1",
				Classroom = classroom,
				ClassroomId = classroom.Id
			};
		}
        /// <summary>
        /// Adds a classroom to the database.
        /// </summary>
        public TestDatabaseBuilder AddClassroom(string classroomName)
        {
            var classroom = new Classroom()
            {
                Name = classroomName,
                DisplayName = $"{classroomName} DisplayName",
                GitHubOrganization = $"{classroomName}GitHubOrg"
            };

            _buildContext.Classrooms.Add(classroom);
            _buildContext.SaveChanges();

            return this;
        }
		/// <summary>
		/// Returns the given user's classroom role for this classroom.
		/// </summary>
		private ClassroomRole GetClassroomRole(User user, Classroom classroom)
		{
			if (user?.SuperUser ?? false)
			{
				return ClassroomRole.Admin;
			}
			else
			{
				return ClassroomMembership?.Role ?? ClassroomRole.None;
			}
		}
		/// <summary>
		/// Updates a classroom.
		/// </summary>
		public async Task UpdateClassroomAsync(Classroom classroom)
		{
			var currentClassroom = await _dbContext.Classrooms
				.Where(c => c.Id == classroom.Id)
				.SingleOrDefaultAsync();

			_dbContext.Entry(currentClassroom).State = EntityState.Detached;

			UpdateClassroom(classroom);
			_dbContext.Update(classroom);

			await _dbContext.SaveChangesAsync();
		}
		/// <summary>
		/// Updates a classroom.
		/// </summary>
		private void UpdateClassroom(Classroom classroom)
		{
			UpdateClassroomGradebookOrder(classroom.ClassroomGradebooks);

			_dbContext.RemoveUnwantedObjects
			(
				_dbContext.ClassroomGradebooks,
				classroomGradebook => classroomGradebook.Id,
				classroomGradebook => classroomGradebook.ClassroomId == classroom.Id,
				classroom.ClassroomGradebooks
			);
		}
		/// <summary>
		/// Creates a classroom.
		/// </summary>
		public async Task CreateClassroomAsync(Classroom classroom)
		{
			_dbContext.Add(classroom);

			await _dbContext.SaveChangesAsync();
		}
Beispiel #13
0
		/// <summary>
		/// Returns a GitHub repository suffix that is unique class-wide.
		/// </summary>
		private async Task<string> GetNewGitHubTeamNameAsync(Classroom classroom, User user)
		{
			var proposedSuffix = new string
			(
				$"{user.LastName}{user.FirstName}"
					.Where(c => char.IsLetter(c))
					.ToArray()
			);

			var membersWithSuffix = await _dbContext.ClassroomMemberships
				.Where(classMember => classMember.ClassroomId == classroom.Id)
				.Where(classMember => classMember.GitHubTeam.StartsWith(proposedSuffix))
				.ToListAsync();

			if (membersWithSuffix.Count == 0)
				return proposedSuffix;

			const int maxMembersWithSuffix= 100;
			for (int i = 2; i < maxMembersWithSuffix; i++)
			{
				var newProposedSuffix = $"{proposedSuffix}{i}";
				var existingUser = membersWithSuffix.SingleOrDefault
				(
					classMember => classMember.GitHubTeam == newProposedSuffix
				);

				if (existingUser == null)
				{
					return newProposedSuffix;
				}
			}

			throw new InvalidOperationException("Unable to find GitHub repository suffix for user.");
		}
Beispiel #14
0
		/// <summary>
		/// Ensures a classroom membership exists for the given user and role.
		/// The caller is responsible for saving changes.
		/// </summary>
		private async Task<ClassroomMembership> EnsureClassroomMembershipAsync(
			User user,
			Classroom classroom,
			ClassroomRole role)
		{
			var classroomMembership = user?.ClassroomMemberships
				?.SingleOrDefault(m => m.ClassroomId == classroom.Id);

			if (user.ClassroomMemberships == null)
			{
				user.ClassroomMemberships = new List<ClassroomMembership>();
			}

			if (classroomMembership == null)
			{
				classroomMembership = new ClassroomMembership()
				{
					ClassroomId = classroom.Id,
					Classroom = classroom,
					InGitHubOrganization = false,
					GitHubTeam = await GetNewGitHubTeamNameAsync(classroom, user),
					Role = role
				};

				user.ClassroomMemberships.Add(classroomMembership);
			}
			else if (role > classroomMembership.Role)
			{
				classroomMembership.Role = role;
			}

			return classroomMembership;
		}
		/// <summary>
		/// Returns a list of students in the given classroom.
		/// </summary>
		private async Task<List<ClassroomMembership>> GetStudentsAsync(Classroom classroom)
		{
			return await _dbContext.ClassroomMemberships
				.Where
				(
					cm => cm.SectionMemberships.Any
					(
						sm => sm.ClassroomMembership.ClassroomId == classroom.Id
						   && sm.Role == SectionRole.Student
					)
				).Include(cm => cm.User)
				.ToListAsync();
		}
		/// <summary>
		/// Returns the assignments query.
		/// </summary>
		private IQueryable<Assignment> GetAssignmentsQuery(Classroom classroom)
		{
			return _dbContext.Assignments
				.Where(assignment => assignment.ClassroomId == classroom.Id)
				.Include(assignment => assignment.Classroom)
				.Include(assignment => assignment.Questions)
					.ThenInclude(assignmentQuestion => assignmentQuestion.Question)
				.Include(assignment => assignment.DueDates);
		}