Esempio n. 1
0
        // Create a new student with input from the user
        private void NewStudent_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // Use the StudentDialog to get the details of the student from the user
                StudentDialog sd = new StudentDialog();

                // Display the form and get the details of the new student
                if (sd.ShowDialog().Value)
                {
                    // When the user closes the form, retrieve the details of the student from the form
                    // and use them to create a new Student object
                    Grades.DataModel.Student newStudent = new Grades.DataModel.Student();
                    newStudent.FirstName = sd.firstName.Text;
                    newStudent.LastName  = sd.lastName.Text;
                    newStudent.User      = new User();
                    if (!newStudent.User.SetPassword(Role.Student, sd.password.Text))
                    {
                        throw new Exception("Password must be at least 6 characters long. Student not created");
                    }

                    // Generate the UserName property - lastname with the initial letter of the first name all converted to lowercase
                    newStudent.User.UserName = (newStudent.LastName + newStudent.FirstName.Substring(0, 1)).ToLower();

                    // Generate a unique ID for the user
                    newStudent.UserId = Guid.NewGuid();

                    // Assign a value for the ImageName field
                    newStudent.ImageName = "No photo";

                    // Generate default values for remaining properties of user object
                    newStudent.User.ApplicationId    = (from Grades.DataModel.User u in SessionContext.DBContext.Users select u.ApplicationId).FirstOrDefault();
                    newStudent.User.IsAnonymous      = false;
                    newStudent.User.LastActivityDate = DateTime.Now;
                    newStudent.User.UserId           = newStudent.UserId;

                    // Add the student to the Students collection
                    // TODO: Exercise 2: Task 2i: Use the AddToStudents method to add a new student
                    SessionContext.DBContext.AddToStudents(newStudent);
                    SessionContext.Save();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error creating new student", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Esempio n. 2
0
        private void Student_Click(object sender, RoutedEventArgs e)
        {
            Button itemClicked = sender as Button;

            if (itemClicked != null)
            {
                // Find out which student was clicked
                Guid studentID = (Guid)itemClicked.Tag;
                if (StudentSelected != null)
                {
                    // Find the details of the student by examining the DataContext of the Button
                    Grades.DataModel.Student student = (Grades.DataModel.Student)itemClicked.DataContext;

                    // Raise the StudentSelected event (handled by MainWindow) to display the details for this student
                    StudentSelected(sender, new StudentEventArgs(student));
                }
            }
        }
        // Enroll a student in the teacher's class
        private void Student_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // Determine which student the user clicked using the StudentID held in the Tag property of the Button that the user clicked
                Button studentClicked = sender as Button;
                Guid   studentID      = (Guid)studentClicked.Tag;

                // Find this student in the Students collection
                Grades.DataModel.Student student = (from s in SessionContext.DBContext.Students
                                                    where s.UserId == studentID
                                                    select s).First();

                // Prompt the user to confirm that they wish to add this student to their class
                string           message = String.Format("Add {0} {1} to your class?", student.FirstName, student.LastName);
                MessageBoxResult reply   = MessageBox.Show(message, "Confirm", MessageBoxButton.YesNo, MessageBoxImage.Question);

                // If the user confirms, add the student to their class
                if (reply == MessageBoxResult.Yes)
                {
                    // Get the ID of the currently logged-on teacher
                    Guid teacherID = SessionContext.CurrentTeacher.UserId;

                    // TODO: Exercise 3: Task 1a: Call the EnrollInClass method to assign the student to this teacher's class.
                    SessionContext.CurrentTeacher.EnrollInClass(student);

                    // TODO: Exercise 3: Task 1b: Save the updated student/class information back to the database.
                    SessionContext.Save();



                    // Refresh the display - the newly assigned student should disappear from the list of unassigned students
                    Refresh();
                }
            }
            catch (Grades.DataModel.ClassFullException cfe)
            {
                MessageBox.Show(String.Format("{0}. Class: {1}", cfe.Message, cfe.ClassName), "Error enrolling student", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error enrolling student", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Esempio n. 4
0
 public StudentEventArgs(Grades.DataModel.Student s)
 {
     Child = s;
 }