Ejemplo n.º 1
0
        public ActionResult ImportStudents(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            ApplicationUser user = UserManager.FindByIdAsync(User.Identity.GetUserId()).Result;

            if (UserManager.IsInRole(user.Id, "Guardian"))
            {
                return(RedirectToAction("Index", "Home"));
            }

            ClassRoom classRoom = db.ClassRooms.Find(id);

            if (classRoom == null)
            {
                return(HttpNotFound());
            }
            if (!UserManager.IsInRole(user.Id, "System Admin"))
            {
                //If User is not part of classes school - redirect home
                //if(user.SchoolID != classRoom.SchoolID)
                //{
                //	return RedirectToAction("Index", "Home");
                //}
            }

            ClassRoomImport classRoomImport = new ClassRoomImport((int)id, classRoom.SchoolID, classRoom.School.SchoolName, classRoom.AspNetUser.FullName, classRoom.RoomNumber);

            return(View(classRoomImport));
        }
Ejemplo n.º 2
0
        public ActionResult ImportStudents([Bind(Include = "ID,SchoolID,SchoolName,TeacherName,RoomNumber,Students")] ClassRoomImport classRoom)
        {
            ActionResult         result = View(classRoom);
            DbContextTransaction xactn  = null;

            try
            {
                if (ModelState.IsValid)
                {
                    xactn = db.Database.BeginTransaction();

                    foreach (StudentImport s in classRoom.Students)
                    {
                        db.Students.Add(new Student()
                        {
                            SchoolID           = classRoom.SchoolID,
                            ClassRoomID        = classRoom.ID,
                            FullName           = s.FullName,
                            StudentNumber      = s.StudentNumber,
                            Guardian1TempEmail = s.Guardian1TempEmail,
                            Guardian2TempEmail = s.Guardian2TempEmail
                        });
                    }

                    ClassRoom c = db.ClassRooms.Find(classRoom.ID);
                    c.SetModified();
                    if (db.Entry(c).State != EntityState.Modified)
                    {
                        db.Entry(c).State = EntityState.Modified;
                    }

                    db.SaveChanges();

                    //////////////////////////////////////////////////////
                    ///	Send Emails to each of the registered guardians
                    //////////////////////////////////////////////////////
                    foreach (StudentImport s in classRoom.Students)
                    {
                        try
                        {
                            if (s.Guardian1TempEmail != null)
                            {
                                SendGuardianRegistrationEmail(classRoom.ID, s.ID, s.Guardian1TempEmail);
                            }
                            if (s.Guardian2TempEmail != null)
                            {
                                SendGuardianRegistrationEmail(classRoom.ID, s.ID, s.Guardian2TempEmail);
                            }
                        }
                        catch (Exception innerEx) { throw new Exception("Unable to send Guardian Emails", innerEx); }
                    }

                    xactn.Commit();
                    xactn.Dispose();
                    xactn = null;

                    result = RedirectToAction("Edit", "ClassRooms", new { id = classRoom.ID });
                }
            }
            catch (Exception ex)
            {
                string    Message      = "Error Saving Changes";
                string    InnerMessage = "";
                Exception inner        = ex.InnerException;
                while (inner != null)
                {
                    InnerMessage = " :: " + inner.Message;
                    inner        = inner.InnerException;
                }
                Message += InnerMessage;

                ModelState.AddModelError("", Message);
                if (xactn != null)
                {
                    xactn.Rollback();
                    xactn.Dispose();
                    xactn = null;
                }
            }
            return(result);
        }