// GET: /Students/
        public ActionResult Index()
        {
            try
            {
                dbTIREntities db = new dbTIREntities();
                SiteUser siteUser = ((SiteUser)Session["SiteUser"]);
                StudentService studentService = new StudentService(siteUser, db);
                SchoolService schoolService = new SchoolService(siteUser, db);
                ModelServices modelService = new ModelServices();
                string currentSchoolYear = schoolService.GetCurrentSchoolYear();
                ViewBag.DistrictDesc = siteUser.Districts[0].Name;
                int schoolYearId = modelService.SchoolYearId();
                ViewBag.SchoolId = modelService.DropDownDataSchool("", siteUser.EdsUserId, schoolYearId, true);
                ViewBag.AllowEdit = HelperService.AllowUiEdits(siteUser.RoleDesc, "STUDENT");
                //ViewBag.SchoolYear = HelperService.SchoolYearDescription(db);
                ViewBag.SchoolYearList = schoolService.DropDownDataSchoolYear(currentSchoolYear);
                ViewBag.AllowEdit = HelperService.AllowUiEdits(siteUser.RoleDesc, "STUDENT");
                ViewBag.SchoolYear = currentSchoolYear;

                return View(new SiteModels()
                {
                    Students = studentService.GetViewData(currentSchoolYear, "", "")
                });
            }
            catch (Exception ex)
            {
                Logging log = new Logging();
                log.LogException(ex);
                return View("GeneralError");
            }
        }
        public ActionResult UpdateGrid(string hiddenSchoolFilter, string hiddenSchoolYearFilter)
        {
            try
            {
                dbTIREntities db = new dbTIREntities();
                SiteUser siteUser = ((SiteUser)Session["SiteUser"]);
                StudentService studentService = new StudentService(siteUser, db);
                SchoolService schoolService = new SchoolService(siteUser, db);
                ModelServices modelService = new ModelServices();
                ViewBag.DistrictDesc = siteUser.Districts[0].Name;
                int schoolYearId = modelService.GetSchoolYearId(Convert.ToInt32(hiddenSchoolYearFilter));
                ViewBag.SchoolId = modelService.DropDownDataSchool(hiddenSchoolFilter, siteUser.EdsUserId, schoolYearId, true);
                ViewBag.AllowEdit = HelperService.AllowUiEdits(siteUser.RoleDesc, "STUDENT");
                ViewBag.SchoolYearList = schoolService.DropDownDataSchoolYear(hiddenSchoolYearFilter);

                ViewBag.SchoolYear = hiddenSchoolYearFilter;

                return View("Index", new SiteModels()
                {
                    Students = studentService.GetViewData(hiddenSchoolYearFilter, hiddenSchoolFilter)
                });
            }
            catch (Exception ex)
            {
                Logging log = new Logging();
                log.LogException(ex);
                return View("GeneralError");
            }
        }
 public ActionResult UpdateGrid(string hiddenSchoolFilter, string schoolYearFilter)
 {
     try
     {
         db = new dbTIREntities();
         siteUser = ((SiteUser)Session["SiteUser"]);
         userService = new UserService(siteUser, db);
         schoolService = new SchoolService(siteUser, db);
         modelService = new ModelServices();
         ViewBag.SchoolYearList = schoolService.DropDownDataSchoolYear(schoolYearFilter);
         int schoolYearId = modelService.GetSchoolYearId(Convert.ToInt32(schoolYearFilter));
         FillViewBagValues(siteUser.Districts[0].Name, hiddenSchoolFilter, siteUser.RoleDesc, schoolYearId);
         return View("Index", userService.GetViewData(schoolYearFilter, hiddenSchoolFilter, ""));
     }
     catch (Exception ex)
     {
         Logging log = new Logging();
         log.LogException(ex);
         return View("GeneralError");
     }
 }
 public ActionResult Index()
 {
     try
     {
         db = new dbTIREntities();
         siteUser = ((SiteUser)Session["SiteUser"]);
         userService = new UserService(siteUser, db);
         schoolService = new SchoolService(siteUser, db);
         modelService = new ModelServices();
         int schoolYearId = modelService.SchoolYearId();
         string currentSchoolYear = schoolService.GetCurrentSchoolYear();
         ViewBag.SchoolYearList = schoolService.DropDownDataSchoolYear(currentSchoolYear);
         FillViewBagValues(siteUser.Districts[0].Name, string.Empty, siteUser.RoleDesc, schoolYearId);
         return View(userService.GetViewData(currentSchoolYear, "", ""));
     }
     catch (Exception ex)
     {
         Logging log = new Logging();
         log.LogException(ex);
         return View("GeneralError");
         //throw;
     }
 }
        public ActionResult Edit(tblUserExt tblUserExtended)
        {
            try
            {
                db = new dbTIREntities();
                modelService = new ModelServices();
                siteUser = ((SiteUser)Session["SiteUser"]);
                userService = new UserService(siteUser, db);
                schoolService = new SchoolService(siteUser, db);
                int userAssignedDistrict = siteUser.Districts[0].Id;

                if (ModelState.IsValid)
                {
                    if (tblUserExtended.SelectedSchools != null && tblUserExtended.SelectedSchools.Count() > 0)
                    {
                        bool isEmailAddressExist = db.tblUsers.Where(x => x.UserEmail == tblUserExtended.UserEmail && x.UserId != tblUserExtended.UserId).Count() > 0 ? true : false;
                        bool isStateIdExist = db.tblUsers.Where(x => x.StateId == tblUserExtended.StateId && x.UserId != tblUserExtended.UserId).Count() > 0 ? true : false;

                        if ((!isEmailAddressExist) && (!isStateIdExist))
                        {
                            userService.UpdateUser(tblUserExtended);
                            HelperService.UpdateSiteUserProfile(siteUser, db);
                            return RedirectToAction("Index");
                        }
                        else
                        {
                            if (isEmailAddressExist)
                                ModelState.AddModelError("UserEmail", "Duplicate email - please choose a unique email.");
                            if (isStateIdExist)
                                ModelState.AddModelError("StateId", "Duplicate state id - please choose a unique state.");
                        }
                    }
                    else
                    {
                        ViewBag.SchoolMessage = "Required";
                    }
                }
                tblUserExtended.Schools = userService.GetSelectedSchoolCheckBoxes(tblUserExtended);
                FillViewBagValues(siteUser.Districts[0].Name, string.Empty, siteUser.RoleDesc, tblUserExtended.SchoolYearId);
                FillUserExtendedCommanData(modelService, tblUserExtended);
                return View(tblUserExtended);
            }
            catch (Exception ex)
            {
                Logging log = new Logging();
                log.LogException(ex);
                return View("GeneralError");
            }
        }
        public ActionResult Edit(int? id, int schoolYearId)
        {

            try
            {
                db = new dbTIREntities();
                modelService = new ModelServices();
                siteUser = ((SiteUser)Session["SiteUser"]);
                schoolService = new SchoolService(siteUser, db);
                userService = new UserService(siteUser, db);
                //tblUserExt tbluserExtended = null;
                if (id == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }

                tblUser tbluser = db.tblUsers.Find(id);

                if (tbluser == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }

                var context = new Models.ApplicationDbContext();
                var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));

                string aspNetUserName = "******";
                if (!String.IsNullOrEmpty(tbluser.AspNetUserId))
                {
                    ApplicationUser aspNetUser = userManager.FindById(tbluser.AspNetUserId);
                    if (aspNetUser != null)
                    {
                        aspNetUserName = aspNetUser.UserName;
                    }
                }
                //Get RoleId from tblUserDistrict instead of tblUser
                int roleId = userService.GetRoleId(id, schoolYearId);
                tblUserExt tbluserExtended = new tblUserExt()
                {
                    UserId = tbluser.UserId,
                    UserName = aspNetUserName,
                    FirstName = tbluser.FirstName,
                    LastName = tbluser.LastName,
                    UserEmail = tbluser.UserEmail,
                    StateId = tbluser.StateId,
                    Schools = tbluser.Schools,
                    SchoolYearId = schoolYearId,
                    RoleId = roleId
                };

                //Check that edited user's school must be from EDSUser schools or edsUser must have permissions to view user school
                bool isUserHasPermissionForSchool = userService.IsUserHasPermissionForSchool(tbluserExtended);
                if (!isUserHasPermissionForSchool)
                {
                    return RedirectToAction("Index");
                }

                //Get User schools
                tbluserExtended.Schools = userService.GetUserSchoolWithCheckBoxes(tbluserExtended);
                var dropDownEmpty = Enumerable.Repeat(new SelectListItem { Value = "", Text = "" }, count: 1);
                FillViewBagValues(siteUser.Districts[0].Name, string.Empty, siteUser.RoleDesc, schoolYearId);
                FillUserExtendedCommanData(modelService, tbluserExtended);
                return View(tbluserExtended);
            }
            catch (Exception ex)
            {
                Logging log = new Logging();
                log.LogException(ex);
                return View("GeneralError");
            }
        }
        public async Task<ActionResult> Create(tblUserExt tblUserExtended)
        {
            try
            {
                db = new dbTIREntities();
                modelService = new ModelServices();
                siteUser = ((SiteUser)Session["SiteUser"]);
                userService = new UserService(siteUser, db);
                schoolService = new SchoolService(siteUser, db);

                int userAssignedDistrict = siteUser.Districts[0].Id;
                string currentSchoolYear = schoolService.GetCurrentSchoolYear();

                if (ModelState.IsValid)
                {
                    if (tblUserExtended.SelectedSchools != null && tblUserExtended.SelectedSchools.Count() > 0)
                    {
                        var context = new Models.ApplicationDbContext();
                        var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));

                        // 1. Create ASPNET user
                        string userName = tblUserExtended.UserName;
                        string password = tblUserExtended.Password;

                        var isPasswordValid = password != null && password.Length >= 6 ? true : false;
                        var isUserNameExist = userManager.FindByName(userName);
                        bool isEmailAddressExist = db.tblUsers.Where(x => x.UserEmail == tblUserExtended.UserEmail).Count() > 0 ? true : false;
                        bool isStateIdExist = db.tblUsers.Where(x => x.StateId == tblUserExtended.StateId).Count() > 0 ? true : false;

                        if ((isUserNameExist == null) && (!isEmailAddressExist) && (!isStateIdExist) && (isPasswordValid))
                        {
                            var user = new ApplicationUser() { UserName = userName };
                            var result = await userManager.CreateAsync(user, password);
                            if (result.Succeeded)
                            {
                                // 2. Create EDS user
                                ApplicationUser newAspNetUser = userManager.FindByName(userName);
                                if (newAspNetUser != null)
                                {
                                    userService.CreateEdsUser(newAspNetUser.Id, tblUserExtended);
                                }
                            }
                            else
                            {
                                throw new Exception(String.Format("ERROR: {0}", result.Errors));
                            }
                            return RedirectToAction("Index");
                        }
                        else
                        {
                            if (isUserNameExist != null)
                                ModelState.AddModelError("UserName", "Duplicate name - please choose a unique name.");
                            if (isEmailAddressExist)
                                ModelState.AddModelError("UserEmail", "Duplicate email - please choose a unique email.");
                            if (isStateIdExist)
                                ModelState.AddModelError("StateId", "Duplicate state id - please choose a unique state.");
                            if (!isPasswordValid)
                                ModelState.AddModelError("Password", "Please enter password at least 6 characters.");
                        }
                    }
                    else
                    {
                        ViewBag.SchoolMessage = "Required";
                    }
                }

                tblUserExtended.Schools = userService.GetSelectedSchoolCheckBoxes(tblUserExtended);
                ViewBag.RoleId = new SelectList(modelService.GetRolesForRole((int)(siteUser.Role)), "RoleId", "RoleDesc", tblUserExtended.RoleId);
                FillViewBagValues(siteUser.Districts[0].Name, string.Empty, siteUser.RoleDesc, tblUserExtended.SchoolYearId);
                return View(tblUserExtended);
            }
            catch (Exception ex)
            {
                Logging log = new Logging();
                log.LogException(ex);
                return View("GeneralError");
            }
        }
        // GET: /User/Create
        public ActionResult Create()
        {
            try
            {
                db = new dbTIREntities();
                siteUser = ((SiteUser)Session["SiteUser"]);
                modelService = new ModelServices();
                schoolService = new SchoolService(siteUser, db);
                userService = new UserService(siteUser, db);

                int userAssignedDistrict = siteUser.Districts[0].Id;
                int schoolYearId = modelService.SchoolYearId();

                tblUserExt userExtended = new tblUserExt();
                userExtended.SchoolYearId = schoolYearId;
                userExtended.Schools = userService.GetSchoolWithCheckBoxes(userExtended);
                ViewBag.RoleId = new SelectList(modelService.GetRolesForRole((int)(siteUser.Role)), "RoleId", "RoleDesc");
                FillViewBagValues(siteUser.Districts[0].Name, string.Empty, siteUser.RoleDesc, schoolYearId);
                return View(userExtended);
            }
            catch (Exception ex)
            {
                Logging log = new Logging();
                log.LogException(ex);
                return View("GeneralError");
            }
        }
        public ActionResult Search(string searchClass, string hiddenSchoolFilterSearch, string hiddenSchoolYearFilterSearch)
        {
            try
            {
                dbTIREntities db = new dbTIREntities();

                SiteUser siteUser = ((SiteUser)Session["SiteUser"]);
                ClassService classService = new ClassService(siteUser, db);
                SchoolService schoolService = new SchoolService(siteUser, db);
                ModelServices modelService = new ModelServices();
                ViewBag.DistrictDesc = siteUser.Districts[0].Name;
                int schoolYearId = modelService.GetSchoolYearId(Convert.ToInt32(hiddenSchoolYearFilterSearch));
                ViewBag.SchoolId = modelService.DropDownDataSchool(hiddenSchoolFilterSearch, siteUser.EdsUserId, schoolYearId, true);
                ViewBag.SchoolYear = schoolService.DropDownDataSchoolYear(hiddenSchoolYearFilterSearch);
                ViewBag.AllowEdit = HelperService.AllowUiEdits(siteUser.RoleDesc, "CLASS");
                return View("Index", new SiteModels()
                {
                    SchoolClasses = classService.GetViewData(hiddenSchoolYearFilterSearch, hiddenSchoolFilterSearch, searchClass)
                });
            }
            catch (Exception ex)
            {
                Logging log = new Logging();
                log.LogException(ex);
                return View("GeneralError");
            }
        }
        private void PopulateViewData(tblClass tblclass, ClassWizardSteps? defaultWizardStep, bool? showTeachersForDistrict, bool? showStudentsForDistrict)
        {
            ViewBag.DefaultWizardStep = defaultWizardStep;
            ViewBag.ShowTeachersForDistrict = !showTeachersForDistrict.GetValueOrDefault();
            ViewBag.ShowStudentsForDistrict = !showStudentsForDistrict.GetValueOrDefault();

            var db = new dbTIREntities();
            SiteUser siteUser = (SiteUser)Session["SiteUser"];
            SchoolService schoolService = new SchoolService(siteUser, db);
            ClassService classService = new ClassService(siteUser, db);
            ModelServices modelService = new ModelServices();

            int userAssignedDistrict = siteUser.Districts[0].Id;
            bool filterTeachersByDistrict = showTeachersForDistrict.GetValueOrDefault();
            int districtId = userAssignedDistrict;
            // Get teachers for this class
            var teachersForThisClass = classService.GetTeachersForThisClass(tblclass);

            // Get teachers not in this class
            var teachersNotForThisClass = classService.GetTeachersNotForThisClass(tblclass, filterTeachersByDistrict, districtId);

            //Get students for this class
            var studentsForThisClass = classService.GetStudentForThisClass(tblclass);

            // Get students not for this class
            var availableStudents = classService.GetStudentNotForThisClass(tblclass, districtId);

            var availableStudentsForSchool = classService.GetStudentForSchool(tblclass, districtId);

            // Add data to ViewBag for form
            ViewBag.TeachersForThisClass = new MultiSelectList(teachersForThisClass, "TeacherId", "FullName", null);
            ViewBag.AvailableTeachers = new MultiSelectList(teachersNotForThisClass, "TeacherId", "FullName", null);
            if (showStudentsForDistrict.GetValueOrDefault())
            {
                ViewBag.AvailableStudents = new MultiSelectList(availableStudents, "StudentId", "FullName", null);
            }
            else
            {
                ViewBag.AvailableStudents = new MultiSelectList(availableStudentsForSchool, "StudentId", "FullName", null);
            }
            ViewBag.StudentsForThisClass = new MultiSelectList(studentsForThisClass, "StudentId", "FullName", null);
            ViewBag.StudentsForThisClassCount = studentsForThisClass.Count();
            ViewBag.SchoolId = modelService.DropDownDataSchool(Convert.ToString(tblclass.SchoolId), siteUser.EdsUserId, tblclass.SchoolYearId, false);
            ViewBag.SchoolYearId = modelService.GetUserSchoolYear(siteUser.EdsUserId, siteUser.Districts[0].Id, tblclass.SchoolYearId); 
            ViewBag.ClassDesc = db.tblClasses.Where(x => x.ClassId == tblclass.ClassId).Select(x => x.ClassDesc).SingleOrDefault();
            ViewBag.SubjectId = new SelectList(db.tblSubjects.OrderBy(x => x.SubjectDesc), "SubjectId", "SubjectDesc", tblclass.SubjectId);
            ViewBag.AllowEdit = HelperService.AllowUiEdits(siteUser.RoleDesc, "CLASS");
        }
 public ActionResult Create([Bind(Include = "ClassId,SchoolId,ClassDesc,SchoolYearId,SubjectId,Grade,CreateDatetime,ChangeDatetime")] tblClass tblclass)
 {
     try
     {
         var db = new dbTIREntities();
         SiteUser su = (SiteUser)Session["SiteUser"];
         SchoolService schoolService = new SchoolService(su, db);
         ModelServices modelService = new ModelServices();
         if (ModelState.IsValid)
         {
             ClassService classService = new ClassService((SiteUser)Session["SiteUser"], db);
             if (!(classService.IsClassExists(tblclass)))
             {
                 classService.AddClass(newClass: tblclass);
                 return RedirectToAction("Edit", new { id = tblclass.ClassId, defaultWizardStep = ClassWizardSteps.Teachers });
             }
             else
             {
                 int defaultDistrict = su.Districts[0].Id;
                 ViewBag.DistrictDesc = su.Districts[0].Name;
                 ModelState.AddModelError("ClassDesc", "Duplicate class name - please choose a unique name.");
             }
         }
         ViewBag.SchoolId = modelService.DropDownDataSchool("", su.EdsUserId, tblclass.SchoolYearId, false);
         ViewBag.SchoolYearId = modelService.GetUserSchoolYear(su.EdsUserId, su.Districts[0].Id, tblclass.SchoolYearId);
         ViewBag.SubjectId = new SelectList(db.tblSubjects.OrderBy(x => x.SubjectDesc), "SubjectId", "SubjectDesc", tblclass.SubjectId);
         return View(tblclass);
     }
     catch (Exception ex)
     {
         Logging log = new Logging();
         log.LogException(ex);
         return View("GeneralError");
     }
 }
 // GET: /Classes/Create
 public ActionResult Create()
 {
     try
     {
         SiteUser siteUser = (SiteUser)Session["SiteUser"];
         var db = new dbTIREntities();
         SchoolService schoolService = new SchoolService(siteUser, db);
         ModelServices modelService = new ModelServices();
         int defaultDistrict = siteUser.Districts[0].Id;
         ViewBag.DistrictDesc = siteUser.Districts[0].Name;
         int schoolYearId = modelService.SchoolYearId();
         ViewBag.SchoolId = modelService.DropDownDataSchool("", siteUser.EdsUserId, schoolYearId, false);
         ViewBag.SchoolYearId = modelService.GetUserSchoolYear(siteUser.EdsUserId, defaultDistrict, schoolYearId);
         ViewBag.SubjectId = new SelectList(db.tblSubjects.OrderBy(x => x.SubjectDesc), "SubjectId", "SubjectDesc");
         return View();
     }
     catch (Exception ex)
     {
         Logging log = new Logging();
         log.LogException(ex);
         return View("GeneralError");
     }
 }
 public ActionResult Edit([Bind(Include = "ClassId,SchoolId,ClassDesc,SchoolYearId,SubjectId,Grade")] tblClass tblclass)
 {
     try
     {
         var db = new dbTIREntities();
         SiteUser siteUser = (SiteUser)Session["SiteUser"];
         SchoolService schoolService = new SchoolService(siteUser, db);
         ModelServices modelService = new ModelServices();
         if (ModelState.IsValid)
         {
             ClassService classService = new ClassService((SiteUser)Session["SiteUser"], db);
             if (!(classService.IsEditClassExists(tblclass)))
             {
                 classService.EditClass(editClass: tblclass);
                 return RedirectToAction("Edit", new { id = tblclass.ClassId, defaultWizardStep = ClassWizardSteps.Teachers });
             }
             else
             {
                 PopulateViewData(tblclass, null, null, null);
                 ModelState.AddModelError("ClassDesc", "Duplicate class name - please choose a unique name.");
                 return View(tblclass);
             }
         }
         ViewBag.SchoolYearId = modelService.GetUserSchoolYear(siteUser.EdsUserId, siteUser.Districts[0].Id, tblclass.SchoolYearId); 
         ViewBag.SchoolId = modelService.DropDownDataSchool(tblclass.SchoolId.ToString(), siteUser.EdsUserId, tblclass.SchoolYearId, true);
         ViewBag.SubjectId = new SelectList(db.tblSubjects, "SubjectId", "SubjectDesc", tblclass.SubjectId);
         return View(tblclass);
     }
     catch (Exception ex)
     {
         Logging log = new Logging();
         log.LogException(ex);
         return View("GeneralError");
     }
 }