public ActionResult CreateKnowledgeBaseItem()
 {
     SiteUser siteUser = (SiteUser)Session["SiteUser"];
     ModelServices modelService = new ModelServices();
     ViewBag.RoleId = new SelectList(modelService.GetRolesForRole((int)(siteUser.Role)), "RoleId", "RoleDesc");
     return View();
 }
 private void FillUserExtendedCommanData(ModelServices modelService, tblUserExt tbluserExtended)
 {
     ViewBag.RoleId = new SelectList(modelService.GetRolesForRole((int)(siteUser.Role)), "RoleId", "RoleDesc", tbluserExtended.RoleId);
     ViewBag.SchoolYear = schoolService.GetSchoolYearDownData((int)tbluserExtended.SchoolYearId);
     int currentSchoolYear = Convert.ToInt32(schoolService.GetCurrentSchoolYear());
     ViewBag.CurrentSchoolYearId = modelService.GetSchoolYearId(currentSchoolYear);
     var classData = modelService.GetClassesByTeacher((int)tbluserExtended.SchoolYearId, new[] { (int)tbluserExtended.UserId });
     tbluserExtended.SchoolClasses = new List<SchoolClass>();
     foreach (var classItem in classData)
     {
         SchoolClass classItems = new SchoolClass() { ClassDesc = classItem.Name, SchoolClassId = classItem.Id };
         tbluserExtended.SchoolClasses.Add(classItems);
     }
 }
        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 CreateKnowledgeBaseItem(KnowledgeBase model)
        {
            try
            {
                SiteUser siteUser = (SiteUser)Session["SiteUser"];
                ModelServices modelService = new ModelServices();
                List<HttpPostedFileBase> httpPostedFileBases = null;
                if (model.Files.ToList()[0] != null)
                {
                    httpPostedFileBases = model.Files.ToList();
                    var allowedFileSize = ConfigurationManager.AppSettings["FileSize"].ToString();
                    if (!DocumentManagementService.ValidateAttachmentSize(httpPostedFileBases))
                    {
                        ModelState.AddModelError("Files", "Attachment Size exceeds allowed limit of " + allowedFileSize + " MB");
                    }
                }

                if (ModelState.IsValid)
                {
                    var db = new dbTIREntities();
                    KnowledgeBaseService kbService = new KnowledgeBaseService(siteUser, db);

                    if (!kbService.IsKnowledgeBaseExists(model.Title))
                    {
                        //TODO: Till nowwe are picking first district Id. Need to refactor code when a user belongs to more than 1 district.
                        model.DistrictId = siteUser.Districts[0].Id;
                        model.RoleId = model.RoleId;
                        model.CreatedDateTime = DateTime.Now;
                        model.CreatedUserId = siteUser.EdsUserId;
                        model.FileDetails = model.Files.ToList()[0] != null ? DocumentManagementService.GetFileDetail(model.Files) : null;
                        kbService.SaveKnowledgeBase(model);
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        ModelState.AddModelError("Title", "Duplicate Title - please choose a unique title.");
                    }
                }
                ViewBag.RoleId = new SelectList(modelService.GetRolesForRole((int)(siteUser.Role)), "RoleId", "RoleDesc");
                return View();
            }
            catch (Exception ex)
            {
                Logging log = new Logging();
                log.LogException(ex);
                return View("GeneralError");
            }
        }