/// <summary>
        /// Written: 18/11/2012
        /// Deletes event by querying the events table using the event Id
        /// </summary>
        /// <param name="eventId"></param>
        /// <returns></returns>
        public bool DeleteEvent(int eventId)
        {
            var deletedEvent = db.Events.SingleOrDefault(x =>x.EventId == eventId);

                var newSavedId = new RecycledId()
                {
                    TableName = "Event",
                    IdRecovered = eventId
                };

                db.RecycledIds.Add(newSavedId);

                db.Events.Remove(deletedEvent);

                db.SaveChanges();

                return true;
        }
        /// <summary>
        /// Invitations of staff to a room only event created
        /// </summary>
        /// <param name="staff"></param>
        /// <param name="eventId"></param>
        /// <returns></returns>
        public bool AddStaffInvitesToEvent(List<Staff> staff, int eventId)
        {
            if (eventId != 0)
            {
                //selection of all current invites for the event created
                var allInvites = _dBase.StaffInvites.Where(x => x.EventId == eventId).ToList();

                if (allInvites.Count > 0)
                {
                    foreach (var i in allInvites)
                    { //old invitations removed
                        var recycledId = new RecycledId
                        {
                            IdRecovered = i.StaffInviteId,
                            DateAdded = DateTime.Now.AddSeconds(1),
                            TableName = "Staff Invite"
                        };
                        _dBase.StaffInvites.Remove(i);
                        _dBase.RecycledIds.Add(recycledId);
                        _dBase.SaveChanges();

                    }
                }
                //new invitations creatde and adde to the database
                foreach (var s in staff)
                {   //staff invite id generation
                    var staffInviteId = StaffInvitesIdGeneration();
                    //new staff invite created
                    var staffInvite = new StaffInvite
                    {
                        EventId = eventId,
                        StaffId = s.StaffId,
                        StaffInviteId = staffInviteId,
                        Attending = false
                    };
                    _dBase.StaffInvites.Add(staffInvite);
                    _dBase.SaveChanges();
                 }

                return true;
            }
            return false;
        }
        /// <summary>
        /// studnet invitations for event created
        /// </summary>
        /// <param name="students"></param>
        /// <param name="eventId"></param>
        /// <returns></returns>
        public bool AddStudentInvitesToEvent(List<Student> students, int eventId)
        {
            if (eventId != 0)
            {   //current invites selceted
                var allInvites = _dBase.StudentInvites.Where(x => x.EventId == eventId).ToList();
                //current invites removed
                if (allInvites.Count > 0)
                {
                    foreach (var i in allInvites)
                    {
                        var recycledId = new RecycledId
                        {
                            IdRecovered = i.StudentInviteId,
                            DateAdded = DateTime.Now.AddSeconds(1),
                            TableName = "Student Invite"
                        };
                        _dBase.StudentInvites.Remove(i);
                        _dBase.RecycledIds.Add(recycledId);
                        _dBase.SaveChanges();

                    }
                }
                //new invited created
                foreach (var s in students)
                {
                    //new invite id generated
                    var studentInviteId = StudentInvitesIdGeneration();
                    //new student invite object created
                    var studentInvite = new StudentInvite
                    {
                        EventId = eventId,
                        StudentId = s.StudentId,
                        StudentInviteId = studentInviteId,
                        Attending = false
                    };
                    //Invite added to databse
                    _dBase.StudentInvites.Add(studentInvite);
                    _dBase.SaveChanges();
                 }

                return true;
            }
            return false;
        }
        /// <summary>
        /// Edited event detials passed in from teh client and event selected
        /// from databse and changes applied
        /// </summary>
        /// <param name="editedEventId"></param>
        /// <param name="userId"></param>
        /// <param name="eventTitle"></param>
        /// <param name="eventDescription"></param>
        /// <param name="eventType"></param>
        /// <param name="eventDuration"></param>
        /// <param name="startDate"></param>
        /// <param name="eventTime"></param>
        /// <param name="roomName"></param>
        /// <param name="courseName"></param>
        /// <param name="moduleName"></param>
        /// <returns></returns>
        public bool EditEvent(int editedEventId, int userId, string eventTitle, string eventDescription,
            string eventType, int eventDuration, DateTime startDate, string eventTime,
            string roomName, string courseName, string moduleName)
        {
            //edited event seleted from databse
            var editedEvent = _dBase.Events.SingleOrDefault(x => x.EventId == editedEventId);
            //as long as edited event exists changes will be made otherwise changes saved failed
            if (editedEvent == null)
            {
                return false;
            }

            #region Removal of Previous Invites and Attendees
            //staff attendees selected to be deleted
            var staffAtendees = _dBase.StaffEvents.Where(x => x.EventId == editedEventId).ToList();
            if (staffAtendees.Any())
            {
                //each staff attendee reomved and Ids recycled
                foreach (var sa in staffAtendees)
                {//recycled id created
                    var recycledId = new RecycledId
                    {
                        DateAdded = DateTime.Now,
                        IdRecovered = sa.StaffEventId,
                        TableName = "Staff Event"
                    };
                    _dBase.StaffEvents.Remove(sa);
                    _dBase.RecycledIds.Add(recycledId);
                    _dBase.SaveChanges();
                }
            }
            //module attendees selected for deletion
            var moduleAttendees = _dBase.ModuleEvents.Where(x => x.EventId == editedEventId).ToList();
            if (moduleAttendees.Any())
            {
                //each module deleted individually
                foreach (var m in moduleAttendees)
                {//id of module event is recycled
                    var recycledId = new RecycledId
                    {
                        DateAdded = DateTime.Now,
                        IdRecovered = m.EventModule,
                        TableName = "Module Event"
                    };
                    _dBase.ModuleEvents.Remove(m);
                    _dBase.RecycledIds.Add(recycledId);
                    _dBase.SaveChanges();
                }
            }
            //staff invitations returned for deletion
            var staffInvites = _dBase.StaffInvites.Where(x => x.EventId == editedEventId).ToList();
            if (staffInvites.Any())
            { //staff Ids deleted individuallys
                foreach (var si in staffInvites)
                {//ids of each staff invited are returned and recycled
                    var recycledId = new RecycledId
                    {
                        DateAdded = DateTime.Now,
                        IdRecovered = si.StaffInviteId,
                        TableName = "Staff Invite"
                    };
                    _dBase.StaffInvites.Remove(si);
                    _dBase.RecycledIds.Add(recycledId);
                    _dBase.SaveChanges();
                }
            }
            //student invites retunred for deletion
            var studentInvites = _dBase.StudentInvites.Where(x => x.EventId == editedEventId).ToList();
            if (studentInvites.Any())
            { //each invite is deleted individually
                foreach (var st in studentInvites)
                {// ids are recycled
                    var recycledId = new RecycledId
                    {
                        DateAdded = DateTime.Now,
                        IdRecovered = st.StudentInviteId,
                        TableName = "Student Invite"
                    };
                    _dBase.StudentInvites.Remove(st);
                    _dBase.RecycledIds.Add(recycledId);
                    _dBase.SaveChanges();
                }
            }

            #endregion

            //repeat Id set to first value as default as repeats not implemented
            var repeatId = _dBase.RepeatTypes.First().RepeatTypeId;

            var roomId = 0;
            //room selceted and saved to event
            var room = _dBase.Rooms.SingleOrDefault(x => x.RoomName == roomName);
            if (roomName != "0" && room != null)
            {
                roomId = room.RoomId;
            }

            var timeId = 0;
            //time of event changed according to the information passed in from client
            var time = _dBase.Times.SingleOrDefault(x => x.TimeLiteral == eventTime);
            if (eventTime != "0" && time != null)
            {
                timeId = time.TimeId;
            }

            var courseId = 0;
            //course informatino changes accoding to the clinets information passed in
            var course = _dBase.Courses.SingleOrDefault(x => x.CourseName == courseName);
            if (courseName != "0" && course != null)
            {
                courseId = course.CourseId;
            }

            var moduleId = 0;
            //module Id passed in from the client added ot the event
            var module = _dBase.Modules.SingleOrDefault(x => x.ModuleName == moduleName);
            if (moduleName != "0" && module != null)
            {
                moduleId = module.ModuleId;
                //new moduke event created
                ModuleEvent(editedEventId, moduleId, courseId);
            }

            var typeId = 0;
            //event type set according to type id
            var type = _dBase.EventTypes.SingleOrDefault(x => x.TypeName == eventType);
            if (eventType != "0" && type != null)
            {
                typeId = type.TypeId;
            }

            //details of event passed in from client added to event
            editedEvent.EventTitle = eventTitle;
            editedEvent.EventType = typeId;
            editedEvent.LastUserEdited = userId;
            editedEvent.EventDescription = eventDescription;
            editedEvent.Repeats = repeatId;
            editedEvent.Duration = eventDuration;
            editedEvent.CreateDate = DateTime.Now;
            editedEvent.Status = "New";
            editedEvent.StartDate = startDate;
            editedEvent.LastDateEdited = DateTime.Now;
            editedEvent.Time = timeId;
            editedEvent.Room = roomId;
            editedEvent.Module = moduleId;
            editedEvent.Course = courseId;
            //change of event saved
            _dBase.SaveChanges();
            //edit changes successfull
            return true;
        }
        /// <summary>
        /// Staff added to events in general, no staff has to be adde to an event
        /// to be confirmed
        /// </summary>
        /// <param name="eventId"></param>
        /// <param name="staffId"></param>
        /// <returns></returns>
        public bool StaffEvent(int eventId, int staffId)
        {
            //as long as valid ids passed in attendee added
            if (eventId != 0 && staffId != 0)
            {
                //current staff allocation removed
                var idRecovery = _dBase.StaffEvents.SingleOrDefault(x => x.EventId == eventId);
                //Ids of current staff allocation recycled
                if (idRecovery != null)
                {
                    //recycled Id maintained
                    var recoveredId = new RecycledId
                    {
                        IdRecovered = idRecovery.StaffEventId,
                        TableName = "Staff Event",
                        DateAdded = DateTime.Now
                    };
                    _dBase.RecycledIds.Add(recoveredId);
                    _dBase.StaffEvents.Remove(idRecovery);
                    _dBase.SaveChanges();
                }

                //new staff attendee created
                var inviteId = StaffEventsIdGeneration();
                var staffEvent = new StaffEvent
                {
                    StaffEventId = inviteId,
                    StaffId = staffId,
                    EventId = eventId
                };
                _dBase.StaffEvents.Add(staffEvent);
                _dBase.SaveChanges();
                return true;
            }
            return false;
        }
        /// <summary>
        /// student delete function
        /// </summary>
        /// <param name="studentId"></param>
        /// <returns></returns>
        public bool DeleteStudent(int studentId)
        {
            if (studentId != 0)
            {//studnet Id recovered
                var recoveredId = new RecycledId
                {
                    DateAdded = DateTime.Now,
                    IdRecovered = studentId,
                    TableName = "Student"
                };

                //fields associated with students selected for removal or updating
                var student = _dBase.Students.SingleOrDefault(x => x.StudentId == studentId);
                var studentModules = _dBase.StudentModules.Where(x => x.StudentId == studentId).ToList();
                var studentEvents = _dBase.StudentEvents.Where(x => x.StudentId == studentId).ToList();
                var studentInvites = _dBase.StudentInvites.Where(x => x.StudentId == studentId).ToList();

                if (student != null)
                {//studnet removed
                    _dBase.Students.Remove(student);
                    _dBase.RecycledIds.Add(recoveredId);
                }

                //Student modules removed and id recycled
                if (studentModules.Count() != 0)
                {
                    foreach (var m in studentModules)
                    {
                        _dBase.StudentModules.Remove(m);

                        var recoveredRoomId = new RecycledId
                        {
                            DateAdded = DateTime.Now,
                            IdRecovered = m.StudentModuleId,
                            TableName = "Student Module"
                        };

                        _dBase.RecycledIds.Add(recoveredRoomId);
                    }
                }
                //studnet Events updated
                if (studentEvents.Count() != 0)
                {
                    foreach (var m in studentEvents)
                    {
                        _dBase.StudentEvents.Remove(m);

                        var recoveredRoomId = new RecycledId
                        {
                            DateAdded = DateTime.Now,
                            IdRecovered = m.StudentEventId,
                            TableName = "Student Event"
                        };

                        _dBase.RecycledIds.Add(recoveredRoomId);
                    }
                }
                //studnet Invites  updated
                if (studentInvites.Count() != 0)
                {
                    foreach (var m in studentInvites)
                    {
                        _dBase.StudentInvites.Remove(m);
                        var recoveredRoomId = new RecycledId
                        {
                            DateAdded = DateTime.Now,
                            IdRecovered = m.StudentInviteId,
                            TableName = "Student Invite"
                        };

                        _dBase.RecycledIds.Add(recoveredRoomId);
                    }
                }

                _dBase.SaveChanges();
                return true;
            }
            return false;
        }
        /// <summary>
        /// Deletion of a student invite
        /// </summary>
        /// <param name="inviteId"></param>
        /// <returns></returns>
        public bool DeleteStudentInvite(int inviteId)
        {
            //selected invite selected from database
            var inviteSelected = _dBase.StudentEvents.SingleOrDefault(x => x.StudentEventId == inviteId);
            // as longa as invite exists it is removed
            if (inviteSelected != null)
            {
                //record removed from database
                _dBase.StudentEvents.Remove(inviteSelected);

                //Id recycled
                var savedId = new RecycledId
                {
                    IdRecovered = inviteId,
                    TableName = "Student Event",
                    DateAdded = DateTime.Now
                };
                //Id saved
                _dBase.RecycledIds.Add(savedId);
                _dBase.SaveChanges();

                return true;
            }
            return false;
        }
        /// <summary>
        /// Deletion of rooom according to id passed in for cleint
        /// </summary>
        /// <param name="roomId"></param>
        /// <returns></returns>
        public bool DeleteRoom(int roomId)
        {
            if (roomId != 0)
            {
                //Id recycled
                var recoveredId = new RecycledId
                {
                    DateAdded = DateTime.Now,
                    IdRecovered = roomId,
                    TableName = "Room"
                };

                var room = _dBase.Rooms.SingleOrDefault(x => x.RoomId == roomId);
                var roomsEvents = _dBase.Events.Where(x => x.Room == roomId).ToList();
                //All rooms that have the id passed in removed
                if (room != null)
                {
                    _dBase.Rooms.Remove(room);
                    _dBase.RecycledIds.Add(recoveredId);
                }
                //all events that use this room are modified for both room and status
                if (roomsEvents.Count() != 0)
                {
                    foreach (var e in roomsEvents)
                    {
                        e.Room = 0;
                        e.Status = "Denied";
                    }
                }
                //database changes saved
                _dBase.SaveChanges();
                return true;
            }
            return false;
        }
        /// <summary>
        /// Deletion of selected staff member
        /// </summary>
        /// <param name="staffId"></param>
        /// <returns></returns>
        public bool DeleteStaff(int staffId)
        {
            if(staffId != 0)
            {
                //Staff id recovered
            var recoveredId = new RecycledId
                {
                    DateAdded = DateTime.Now,
                    IdRecovered = staffId,
                    TableName = "Staff"
                };
                //associated staff firelds collected for deletion or updating
                var staff = _dBase.Staffs.SingleOrDefault(x => x.StaffId == staffId);
                var staffModules = _dBase.Modules.Where(x => x.Staff == staffId);
                var staffEvents = _dBase.StaffEvents.Where(x => x.StaffId == staffId).ToList();
                var staffInvites = _dBase.StaffInvites.Where(x => x.StaffId == staffId).ToList();

                if (staff != null)
                {
                    //staff removed and id recovered
                    _dBase.Staffs.Remove(staff);
                    _dBase.RecycledIds.Add(recoveredId);
                }

                //staf modules updataed
                if (staffModules.Count() != 0)
                {
                    foreach (var m in staffModules)
                    {
                        m.Staff = 0;
                    }
                }

                //staff events updated to relect staff removal
                if (staffEvents.Count() != 0)
                {
                    foreach (var m in staffEvents)
                    {
                        _dBase.StaffEvents.Remove(m);
                        //Id recycled
                        var recoveredRoomId = new RecycledId
                        {
                            DateAdded = DateTime.Now,
                            IdRecovered = m.StaffId,
                            TableName = "Staff Event"
                        };

                        _dBase.RecycledIds.Add(recoveredRoomId);
                    }
                }
                //staff invitatino for events removeed
                if (staffInvites.Count() != 0)
                {
                    foreach (var m in staffInvites)
                    {
                        _dBase.StaffInvites.Remove(m);
                        var recoveredRoomId = new RecycledId
                        {
                            DateAdded = DateTime.Now,
                            IdRecovered = m.StaffId,
                            TableName = "Staff Invite"
                        };
                        //Id reccycled
                        _dBase.RecycledIds.Add(recoveredRoomId);
                    }
                }

                _dBase.SaveChanges();
                return true;
            }
            return false;
        }
        /// <summary>
        /// Deletion of module Event allocation
        /// Used to help allocated students to events
        /// </summary>
        /// <param name="inviteId"></param>
        /// <returns></returns>
        public bool DeleteModuleEvent(int inviteId)
        {
            //selected module event from database
            var inviteSelected = _dBase.ModuleEvents.SingleOrDefault(x => x.EventModule == inviteId);
            if (inviteSelected != null)
            {
                //module event removed
                _dBase.ModuleEvents.Remove(inviteSelected);
                //recycled Id
                var savedId = new RecycledId
                {
                    IdRecovered = inviteId,
                    TableName = "Module Event",
                    DateAdded = DateTime.Now
                };
                _dBase.RecycledIds.Add(savedId);
                _dBase.SaveChanges();

                return true;
            }
            return false;
        }
        /// <summary>
        /// deletion of module
        /// </summary>
        /// <param name="moduleId"></param>
        /// <returns></returns>
        public bool DeleteModule(int moduleId)
        {
            if (moduleId != 0)
            {
                //moudle Id recovered
                var recoveredId = new RecycledId
                {
                    DateAdded = DateTime.Now,
                    IdRecovered = moduleId,
                    TableName = "Module"
                };
                //module and module events selected
                var module = _dBase.Modules.SingleOrDefault(x => x.ModuleId == moduleId);
                var moduleEvents = _dBase.Events.Where(x => x.Module == moduleId).ToList();

                if (module != null)
                {
                    //modue removed
                    _dBase.Modules.Remove(module);
                    _dBase.RecycledIds.Add(recoveredId);
                }

                if (moduleEvents.Count() != 0)
                {//module events updated to relect removal of module
                    foreach (var e in moduleEvents)
                    {
                        e.Module = 0;
                        e.Status = "Denied";
                    }
                }

                _dBase.SaveChanges();
                return true;
            }
            return false;
        }
        /// <summary>
        /// Deletes event by querying the events table using the event Id
        /// </summary>
        /// <param name="eventId"></param>
        /// <returns></returns>
        public bool DeleteEvent(int eventId)
        {
            //delete event selected from database
            var deletedEvent = _dBase.Events.SingleOrDefault(x => x.EventId == eventId);
            //as long as event exists
            if (deletedEvent != null)
            {
                #region Removal of Previous Invites and Attendees

                var staffAtendees = _dBase.StaffEvents.Where(x => x.EventId == eventId).ToList();

                if (staffAtendees.Count > 0)
                {
                    foreach (var sa in staffAtendees)
                    {
                        var recycledId = new RecycledId
                        {
                            DateAdded = DateTime.Now,
                            IdRecovered = sa.StaffEventId,
                            TableName = "Staff Event"
                        };
                        _dBase.StaffEvents.Remove(sa);
                        _dBase.RecycledIds.Add(recycledId);
                        _dBase.SaveChanges();
                    }
                }

                var moduleAttendees = _dBase.ModuleEvents.Where(x => x.EventId == eventId).ToList();

                if (moduleAttendees.Count > 0)
                {
                    foreach (var m in moduleAttendees)
                    {
                        var recycledId = new RecycledId
                        {
                            DateAdded = DateTime.Now,
                            IdRecovered = m.EventModule,
                            TableName = "Module Event"
                        };
                        _dBase.ModuleEvents.Remove(m);
                        _dBase.RecycledIds.Add(recycledId);
                        _dBase.SaveChanges();
                    }
                }
                var staffInvites = _dBase.StaffInvites.Where(x => x.EventId == eventId).ToList();

                if (staffInvites.Count > 0)
                {
                    foreach (var si in staffInvites)
                    {
                        var recycledId = new RecycledId
                        {
                            DateAdded = DateTime.Now,
                            IdRecovered = si.StaffInviteId,
                            TableName = "Staff Invite"
                        };
                        _dBase.StaffInvites.Remove(si);
                        _dBase.RecycledIds.Add(recycledId);
                        _dBase.SaveChanges();
                    }
                }

                var studentInvites = _dBase.StudentInvites.Where(x => x.EventId == eventId).ToList();

                if (studentInvites.Count > 0)
                {
                    foreach (var st in studentInvites)
                    {
                        var recycledId = new RecycledId
                        {
                            DateAdded = DateTime.Now,
                            IdRecovered = st.StudentInviteId,
                            TableName = "Student Invite"
                        };
                        _dBase.StudentInvites.Remove(st);
                        _dBase.RecycledIds.Add(recycledId);
                        _dBase.SaveChanges();
                    }
                }

                #endregion

                //event id recycled
                var newSavedId = new RecycledId
                {
                    TableName = "Event",
                    IdRecovered = eventId,
                    DateAdded = DateTime.Now
                };

                //Id recycled
                _dBase.RecycledIds.Add(newSavedId);
                //event deleted
                _dBase.Events.Remove(deletedEvent);
                _dBase.SaveChanges();

                return true;
            }
            //delete failed
            return false;
        }
        /// <summary>
        /// Deletion of course seleted by Id
        /// </summary>
        /// <param name="courseId"></param>
        /// <returns></returns>
        public bool DeleteCourse(int courseId)
        {
            if (courseId != 0)
            {
                var recoveredId = new RecycledId
                {
                    DateAdded = DateTime.Now,
                    IdRecovered = courseId,
                    TableName = "Course"
                };
                //course and course modules allocations slected
                var course = _dBase.Courses.SingleOrDefault(x => x.CourseId == courseId);
                var courseModules = _dBase.CourseModules.Where(x => x.Course == courseId).ToList();

                if (course != null)
                {//removal of course
                    _dBase.Courses.Remove(course);
                    _dBase.RecycledIds.Add(recoveredId);
                }

                if (courseModules.Count() != 0)
                {
                    //removal of course moudule associations
                    foreach (var m in courseModules)
                    {
                        _dBase.CourseModules.Remove(m);
                        //course module id recycled
                        var recoveredRoomId = new RecycledId
                        {
                            DateAdded = DateTime.Now,
                            IdRecovered = m.Module,
                            TableName = "Module"
                        };

                        _dBase.RecycledIds.Add(recoveredRoomId);
                        //module events updated to reflect removal of modules
                        var moduleEvents = _dBase.Events.Where(x => x.Module == m.Module).ToList();

                        foreach (var e in moduleEvents)
                        {
                            e.Module = 0;
                            e.Status = "Denied";
                        }
                    }
                }

                _dBase.SaveChanges();
                return true;
            }
            return false;
        }
        /// <summary>
        /// Building deleted according to the Id passed in
        /// Also cleanu of rooms associations and events managaed
        /// </summary>
        /// <param name="buildingId"></param>
        /// <returns></returns>
        public bool DeleteBuilding(int buildingId)
        {
            if (buildingId != 0)
            {//building id recycled
                var recoveredId = new RecycledId
                {
                    DateAdded = DateTime.Now,
                    IdRecovered = buildingId,
                    TableName = "Building"
                };
                //Building and room objects selected
                var building = _dBase.Buildings.SingleOrDefault(x => x.BuildingId == buildingId);
                var buildingRooms = _dBase.Rooms.Where(x => x.Building == buildingId).ToList();

                if (building != null)
                {//building removed and id recycled
                    _dBase.Buildings.Remove(building);
                    _dBase.RecycledIds.Add(recoveredId);
                }

                //Rooms that were reviously associcated with building are updated
                if (buildingRooms.Count() != 0)
                {
                    //Rooms taht are associated with building removed
                    foreach (var r in buildingRooms)
                    {
                        _dBase.Rooms.Remove(r);
                        //room ids recycled
                        var recoveredRoomId = new RecycledId
                        {
                            DateAdded = DateTime.Now,
                            IdRecovered = r.RoomId,
                            TableName = "Room"
                        };
                        _dBase.RecycledIds.Add(recoveredRoomId);
                        //evenst of associated rooms updated
                        var roomEvents = _dBase.Events.Where(x => x.Room == r.RoomId).ToList();
                        foreach (var e in roomEvents)
                        {
                           e.Room = 0;
                           e.Status = "Denied";
                        }
                    }
                }
                //changes saved
                _dBase.SaveChanges();
                return true;
            }
            return false;
        }