// Gather the messages that are relevent to the connected user. public void Initialize(Person connectedPerson) { ConnectedPerson = connectedPerson; // Reset data first Messages = new List <DisplayedMessage>(); var schoolMessages = new SchoolEntities().Messages; // Start by gathering the messages that are for everyone in the school or directly to the connected user. schoolMessages.Where(message => message.forEveryone).ToList() .ForEach(message => Messages.Add(ModelMessageToDisplayedMessage(message, MessageRecipientsTypes.Everyone))); schoolMessages.Where(message => message.recipientID == ConnectedPerson.personID).ToList() .ForEach(message => Messages.Add(ModelMessageToDisplayedMessage(message, MessageRecipientsTypes.Person))); // Gather messages depending on the user permissions if (ConnectedPerson.isStudent) { // User is a student => Gather messages for their class, and messages for all students schoolMessages.Where(message => message.recipientClassID == ConnectedPerson.Student.classID) .ToList().ForEach(message => Messages.Add(ModelMessageToDisplayedMessage(message, MessageRecipientsTypes.Class))); schoolMessages.Where(message => message.forAllStudents).ToList() .ForEach(message => Messages.Add(ModelMessageToDisplayedMessage(message, MessageRecipientsTypes.Students))); } if (ConnectedPerson.isParent) { // Gather messages for the classes of the user's children (but not direct messages to them - keeping those private) schoolMessages.AsEnumerable().Where(message => ConnectedPerson.ChildrenStudents.Any(childStudent => childStudent.classID == message.recipientClassID)) .ToList().ForEach(message => Messages.Add(ModelMessageToDisplayedMessage(message, MessageRecipientsTypes.Class))); } if (ConnectedPerson.isTeacher) { // Gather messages for all teachers schoolMessages.Where(message => message.forAllTeachers).ToList() .ForEach(message => Messages.Add(ModelMessageToDisplayedMessage(message, MessageRecipientsTypes.Teachers))); if (ConnectedPerson.Teacher.classID != null) { // User is an homeroom teacher - gather messages for his class schoolMessages.Where(message => ConnectedPerson.Teacher.classID == message.recipientClassID).ToList() .ForEach(message => Messages.Add(ModelMessageToDisplayedMessage(message, MessageRecipientsTypes.Class))); } } if (ConnectedPerson.isSecretary || ConnectedPerson.isPrincipal) { // Gather messages for management schoolMessages.Where(message => message.forAllManagement).ToList() .ForEach(message => Messages.Add(ModelMessageToDisplayedMessage(message, MessageRecipientsTypes.Management))); } // Order messages by date (latest first) Messages = Messages.OrderByDescending(message => message.MessageDateTime).ToList(); }