/// <summary>
        /// Determines whether or not a Student can register for a course.
        /// </summary>
        /// <param name="student">The Student to register to the Course.</param>
        /// <param name="course">The Course to register the Student for.</param>
        /// <returns>True if the Student can register for the Course.</returns>
        private bool CanRegisterForCourse(Student student, CourseSchedule courseSchedule)
        {
            bool successful = true;

            TimeSpan startTime = courseSchedule.Schedule.StartTime;
            TimeSpan endTime   = ScheduleTime.GetEndTime(startTime, courseSchedule.Schedule.TimeBlocks);

            if (courseSchedule.ProfessorId == student.PersonId)
            {
                successful = false;
            }
            else if (GetNumberOfStudentsInCourse(courseSchedule) >= courseSchedule.Capacity)
            {
                successful = false;
            }
            else
            {
                foreach (StudentSchedule existingSchedule in student.StudentSchedules.Where(s => s.Enrolled))
                {
                    TimeSpan oldStartTime = existingSchedule.CourseSchedule.Schedule.StartTime;
                    TimeSpan oldEndTime   = ScheduleTime.GetEndTime(oldStartTime, existingSchedule.CourseSchedule.Schedule.TimeBlocks);

                    if (ScheduleTime.TimesOverlap(startTime, endTime, oldStartTime, oldEndTime))
                    {
                        successful = false;
                    }
                }
            }

            return(successful);
        }
        /// <summary>
        /// Create a new StudentSchedule with the given Enrolled value.
        /// </summary>
        /// <param name="student"></param>
        /// <param name="courseSchedule"></param>
        /// <param name="enrolled"></param>
        /// <returns></returns>
        private bool CreateStudentSchedule(Student student, CourseSchedule courseSchedule, bool enrolled)
        {
            bool successful = CanRegisterForCourse(student, courseSchedule);

            if (successful)
            {
                StudentSchedule existingSchedule = db.StudentSchedules.Where(s => s.StudentId == student.PersonId && s.CourseScheduleId == courseSchedule.CourseScheduleId).FirstOrDefault();

                if (existingSchedule == null)
                {
                    db.StudentSchedules.Add(new StudentSchedule
                    {
                        Student        = student,
                        CourseSchedule = courseSchedule,
                        Enrolled       = enrolled
                    });
                }
                else
                {
                    existingSchedule.Enrolled = enrolled;
                    var entry = db.Entry(existingSchedule);
                    entry.State = EntityState.Modified;
                }

                successful = db.SaveChanges() > 0;
            }

            return(successful);
        }
Example #3
0
        /// <summary>
        /// Modify the capacity of a Course.
        /// </summary>
        /// <param name="capacity">The capacity of the course.</param>
        /// <returns>True if the modifications were successful.</returns>
        public bool ModifyCourse(CourseSchedule courseSchedule, short capacity)
        {
            courseSchedule.Capacity = capacity;
            var entry = db.Entry(courseSchedule);

            entry.State = System.Data.Entity.EntityState.Modified;
            return(db.SaveChanges() > 0);
        }
Example #4
0
        /// <summary>
        /// Modify the time of a Course.
        /// </summary>
        /// <param name="startTime">The start time of the Course.</param>
        /// <param name="timeBlocks">The number of time blocks the Course spans.</param>
        /// <returns>True if the modifications were successful.</returns>
        public bool ModifyCourse(CourseSchedule courseSchedule, Schedule schedule)
        {
            courseSchedule.Schedule = schedule;
            var entry = db.Entry(courseSchedule);

            entry.State = System.Data.Entity.EntityState.Modified;
            return(db.SaveChanges() > 0);
        }
Example #5
0
        /// <summary>
        /// Gets a List of all Students enrolled to the Course.
        /// </summary>
        /// <param name="courseSchedule">The Course to get Students from.</param>
        /// <returns>The List of Students.</returns>
        public List <Student> ListEnrolledStudents(CourseSchedule courseSchedule)
        {
            List <Student> students = new List <Student>();

            foreach (StudentSchedule studentSchedule in courseSchedule.StudentSchedules.Where(s => s.Enrolled))
            {
                students.Add(studentSchedule.Student);
            }

            return(students);
        }
Example #6
0
 /// <summary>
 /// Cancel a Course.
 /// </summary>
 /// <param name="courseSchedule">The Course to cancel.</param>
 /// <returns>True if the cancellation was successful.</returns>
 public bool CancelCourse(CourseSchedule courseSchedule)
 {
     db.CourseSchedules.Remove(courseSchedule);
     return(db.SaveChanges() > 0);
 }
Example #7
0
 /// <summary>
 /// Gets the number of Students registered for a Course.
 /// </summary>
 /// <param name="courseSchedule">The CourseSchedule to check the number of Students on.</param>
 /// <returns>The number of Students registered for the Course.</returns>
 public int GetNumberOfStudentsInCourse(CourseSchedule courseSchedule)
 {
     return(courseSchedule.StudentSchedules.Where(s => s.Enrolled).ToList().Count);
 }
 /// <summary>
 /// Hold a Course for a Student.
 /// </summary>
 /// <param name="student">The Student to hold the class for.</param>
 /// <param name="courseSchedule">The Course to hold for the Student.</param>
 /// <returns>True if the hold was successful.</returns>
 public bool HoldCourse(Student student, CourseSchedule courseSchedule)
 {
     return(CreateStudentSchedule(student, courseSchedule, false));
 }
 /// <summary>
 /// Register a Student to a Course.
 /// </summary>
 /// <param name="student">The Student to register to the Course.</param>
 /// <param name="course">The Course to register the Student for.</param>
 /// <returns>True if the registration was successful.</returns>
 public bool RegisterForCourse(Student student, CourseSchedule courseSchedule)
 {
     return(CreateStudentSchedule(student, courseSchedule, true));
 }