public ActionResult Add([Bind(Include = "student_ID,GrantDescription,GrantValue,DateOfIssue,KuhaFunds,grant_type_id")] StudentVoucher studentVoucher)
        {
            StudentRegistrationsModel db = new StudentRegistrationsModel();
            //lets make sure student exists
            if (this.studentExists(studentVoucher.student_ID) == false)
            {
                ModelState.AddModelError("Student_ID", "Student ID Does Not Exist");
                //return View(StudentRegistration);
            }

            //check the required grant requirements have been meet
            GrantType GrantType = db.GrantTypes.Find(studentVoucher.grant_type_id);

               if (GrantType.grant_description == true && (String.IsNullOrEmpty(studentVoucher.GrantDescription)))
               ModelState.AddModelError("GrantDescription", "Grant description must contain details it is required");

               if (GrantType.grant_value == true && studentVoucher.GrantValue <= 0)
               ModelState.AddModelError("GrantValue", "GrantValue needs to be greater than 0");

            if (ModelState.IsValid)
            {
                db.StudentVouchers.Add(studentVoucher);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            //pass back our data
            ViewBag.grant_type_id = db.GrantTypes;
            ViewBag.student_ID = (studentVoucher.student_ID != null) ? studentVoucher.student_ID : String.Empty;

            return View(studentVoucher);
        }
        public ActionResult Create([Bind(Include =
             "Student_ID,FirstName,LastName,Gender,DOB,Address1,Accomodition_Type,Phone,Mobile,Email,Marital_Status,Contact,Main_Ethnicity,id_faculty,id_courses,Detailed_Ethnicity,id_campus")] StudentRegistration studentRegistration)
        {
            StudentRegistrationsModel db = new StudentRegistrationsModel();
             //error checking goes here

             //lets make sure we don't already have this Student_ID
             if (this.studentExists(studentRegistration.Student_ID))
             {
                 ModelState.AddModelError("Student_ID", "Student ID Already Exists");
                 //return View(StudentRegistration);
             }
             if (studentRegistration.Student_ID == null || studentRegistration.Student_ID.ToString().Trim() == String.Empty)
             {
                 ModelState.AddModelError("Student_ID", "Can not be blank or empty");
                 //return View(StudentRegistration);
             }

             if (ModelState.IsValid)
             {

                 //administrator.UserType = "Admin";
                // db.Administrators.Add(administrator);

                 db.StudentRegistrations.Add(studentRegistration);
                 db.SaveChanges();
                 /*
                 try
                 {

                     // Could also be before try if you know the exception occurs in SaveChanges

                 }
                 catch (DbEntityValidationException e)
                 {
                     foreach (var eve in e.EntityValidationErrors)
                     {
                         System.Diagnostics.Debug.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                             eve.Entry.Entity.GetType().Name, eve.Entry.State);
                         foreach (var ve in eve.ValidationErrors)
                         {
                             System.Diagnostics.Debug.WriteLine("- Property: \"{0}\", Value: \"{1}\", Error: \"{2}\"",
                                 ve.PropertyName,
                                 eve.Entry.CurrentValues.GetValue<object>(ve.PropertyName),
                                 ve.ErrorMessage);
                         }
                     }
                     throw;
                 }
                 */

                 return RedirectToAction("Index");
             }

             ViewBag.id_courses = new SelectList(db.Courses, "id_courses", "course_name");
             ViewBag.id_faculty = new SelectList(db.Faculties, "id_faculty", "faculty_name");
             ViewBag.id_campus = new SelectList(db.Campus, "id_campus", "campus_name");
             return View(studentRegistration);
        }
        public ActionResult Edit(StudentRegistration theStudent)
        {
            StudentRegistrationsModel db = new StudentRegistrationsModel();
             //if id == return
             //StudentRegistration theStudent = (StudentRegistration)db.StudentRegistrations.Where(m => m.Student_ID == id);

             ViewBag.id_courses = new SelectList(db.Courses, "id_courses", "course_name");
             ViewBag.id_faculty = new SelectList(db.Faculties, "id_faculty", "faculty_name");
             ViewBag.id_campus = new SelectList(db.Campus, "id_campus", "campus_name");

             if (theStudent.Student_ID == null || theStudent.Student_ID.ToString().Trim() == String.Empty)
             {
                 ModelState.AddModelError("Student_ID", "Can not be blank or empty");
                 //return View(StudentRegistration);
             }

             if (ModelState.IsValid)
             {

                 //administrator.UserType = "Admin";
                 // db.Administrators.Add(administrator);
                 db.Entry(theStudent).State = EntityState.Modified;
                 db.SaveChanges();
                 return RedirectToAction("Index");

             }
             return View(theStudent);
        }
        public ActionResult Add(Administrator newAdmin)
        {
            StudentRegistrationsModel db = new StudentRegistrationsModel();
            //newAdmin.Roles = Request.Form["admin_roles"];

            ViewBag.role_type_id = new SelectList(db.RoleTypes, "role_type_id", "role_description");
            //ViewBag.Roles = Request.Form["admin_roles"];
            ViewBag.password_match = Request.Form["password_match"];

            //check for unique email address
            //if (ModelState.ContainsKey("Roles"))
              //  ModelState["Roles"].Errors.Clear();

            if (db.Administrators.Any(m => m.Email.ToLower() == newAdmin.Email.ToLower()))
            {
                ModelState.AddModelError("Email", "Admin email already exists!");

            }
            //check password match
            if (newAdmin.Password != Request.Form["password_match"])
            {
                //clear the viewbag password so they re-type
                ViewBag.password_match = String.Empty;
                ModelState.AddModelError("Password", "Passwords don't match");
            }

            //check mobile phone number if added

            if (!ModelState.IsValid)
            {
                return View(newAdmin);
            }

            //PasswordHashing passwordHash = new PasswordHashing();
            newAdmin.Password = PasswordHashing.Encrypt(newAdmin.Password);

            //add the admin
            db.Administrators.Add(newAdmin);
            db.SaveChanges();

            //we will now have admin id if saved
            //int theAdmin_id = newAdmin.UserId;
            //check roles that need to be added

            //store roles into a string array
            //this.addRoles(newAdmin, Request.Form["roles"]);

            return RedirectToAction("Admins");
        }
 public ActionResult Edit([Bind(Include = "UserId,Email,Password,FirstName,LastName,mobile,role_type_id")] Administrator administrator)
 {
     StudentRegistrationsModel db = new StudentRegistrationsModel();
     if (ModelState.IsValid)
     {
         db.Entry(administrator).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Admins");
     }
     ViewBag.role_type_id = new SelectList(db.RoleTypes, "role_type_id", "role_name", administrator.role_type_id);
     return View(administrator);
 }
 public ActionResult DeleteConfirmed(int id)
 {
     StudentRegistrationsModel db = new StudentRegistrationsModel();
     Administrator administrator = db.Administrators.Find(id);
     db.Administrators.Remove(administrator);
     db.SaveChanges();
     return RedirectToAction("Admins");
 }
        public ActionResult ChangePassword(AdministratorLogin theAdmin)
        {
            StudentRegistrationsModel db = new StudentRegistrationsModel();
            //passing back from session so no injection of userID or email can happen we also need to clear the model state and re-validate
            ModelState.Clear();
            theAdmin.Email = this.AdminSession().Email;
            theAdmin.UserId = this.AdminSession().UserId;
            TryValidateModel(theAdmin);

            //ModelState.Clear();
            //check password match
            if (theAdmin.Password != Request.Form["password_match"])
            {
                //clear the viewbag password so they re-type
                ViewBag.password_match = String.Empty;
                ModelState.AddModelError("Password", "Passwords don't match");
            }
            if (!ModelState.IsValid)
            {
                foreach (ModelState modelState in ViewData.ModelState.Values)
                {
                    foreach (ModelError error in modelState.Errors)
                    {
                        Console.Write(error);
                    }
                }
                return View(theAdmin);
            }

            //grab the current admin session and update password
            //process the update
            AdministratorLogin thisUser = this.AdminSession();

            var change = (from a in db.Administrators
                          where a.UserId == thisUser.UserId
                            select a).SingleOrDefault();
            //rehash password
            change.Password = PasswordHashing.Encrypt(theAdmin.Password);

            //clean up from recovery
            if (Session["AdministratorRecovery"] != null)
            {
                Session.Remove("AdministratorRecovery");
                //remove any recovery options that are set
                var recovery = (from b in db.Recoveries where b.UserId == change.UserId select b);
                foreach (var entry in recovery)
                    db.Recoveries.Remove(entry);
            }

            db.Entry(change).State = EntityState.Modified;
            db.SaveChanges();

            return RedirectToAction("Index");
        }
        /// <summary>
        ///  Sends the recovery code to the user via email or sms
        /// </summary>
        /// <param name="theRecovery">Recovery details</param>
        /// <returns>JSON if has passed Recovery checks</returns>
        public JsonResult sendRecoveryCode(Recovery theRecovery)
        {
            //session has been removed
                if (Session["AdministratorRecovery"] == null)
                {
                    return Json(new
                    {
                        success = false,
                        message = "Invalid Session please refresh the browser"
                    }, JsonRequestBehavior.AllowGet);
                }

                Recovery sessRecovery = (Recovery)Session["AdministratorRecovery"];

                theRecovery.Administrator.FirstName = sessRecovery.Administrator.FirstName;
                theRecovery.Administrator.Password = sessRecovery.Administrator.Password;
                theRecovery.UserId = sessRecovery.Administrator.UserId;
                //ModelState.Clear();
                StudentRegistrationsModel db = new StudentRegistrationsModel();

                if (theRecovery.recovery_option == "email")
                {
                    RecoveryComms theComms = new RecoveryComms();
                    if (theComms.sendEmail(ref sessRecovery) == true)
                    {

                        //success lets tell the user and save
                        var email = sessRecovery.Administrator.Email;
                        sessRecovery.Administrator = null;
                        db.Recoveries.Add(sessRecovery);
                        db.SaveChanges();

                        return Json(new
                        {
                            success = true,
                            message = String.Format("<b>Great!</b>, we have emailed you your recovery code to <b>{0}</b>", email)

                        }, JsonRequestBehavior.AllowGet);

                    }
                    else
                    {
                        return Json(new
                        {
                            success = false,
                            message = "Something bad happend"

                        }, JsonRequestBehavior.AllowGet);
                    }
                }

                //requesting for recovery code
                if (theRecovery.recovery_option == "mobile")
                {

                    string attemptGuess = theRecovery.Administrator.mobile;
                    //int.TryParse(theRecovery.Administrator.mobile,out guessNumber);

                    string correctAttempt = new String(sessRecovery.Administrator.mobile.
                                Where(x => Char.IsDigit(x)).Reverse().Take(4).Reverse().ToArray());

                    //success here if correct number
                    if (theRecovery.Administrator.mobile == correctAttempt)
                    {
                        //passover the correct mobile details

                        RecoveryComms theComms = new RecoveryComms();
                        if (theComms.sendSMS(ref sessRecovery) == true)
                        {

                            var mobile_number = sessRecovery.Administrator.mobile;
                            sessRecovery.Administrator = null;
                            db.Recoveries.Add(sessRecovery);
                            db.SaveChanges();

                            return Json(new
                            {
                                success = true,
                                message = String.Format("<b>Great!</b>, we have sent you a TEXT message with your recovery code to <b>{0}</b>", mobile_number)

                            }, JsonRequestBehavior.AllowGet);
                        }
                        else
                        {

                            return Json(new
                            {
                                success = false,
                                message = "Something bad happend"

                            }, JsonRequestBehavior.AllowGet);
                        }
                    }
                    else
                    {
                        return Json(new
                        {
                            success = false,
                            message = "Failed incorrect last digits"

                        }, JsonRequestBehavior.AllowGet);
                    }
            }

            return Json("", JsonRequestBehavior.AllowGet);
        }
        public ActionResult Edit([Bind(Include = "id_student_vouchers,student_ID,GrantDescription,GrantValue,DateOfIssue,KuhaFunds,grant_type_id")] StudentVoucher studentVoucher)
        {
            StudentRegistrationsModel db = new StudentRegistrationsModel();
            if (ModelState.IsValid)
            {
                StudentVoucher theVoucher = db.StudentVouchers.Find(studentVoucher.id_student_vouchers);
                theVoucher.KuhaFunds = studentVoucher.KuhaFunds;
                theVoucher.grant_type_id = studentVoucher.grant_type_id;
                theVoucher.GrantValue = studentVoucher.GrantValue;
                theVoucher.GrantDescription = studentVoucher.GrantDescription;
                theVoucher.DateOfIssue = studentVoucher.DateOfIssue;

               // db.StudentVouchers.Add(theVoucher);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.grant_type_id = db.GrantTypes;
            //ViewBag.student_ID = new SelectList(db.StudentRegistrations, "Student_ID", "FirstName", studentVoucher.student_ID);
            return View(studentVoucher);
        }
 // POST: StudentVouchers/Delete/5
 //[HttpPost, ActionName("Delete")]
 // [ValidateAntiForgeryToken]
 public ActionResult DeleteConfirmed(int id)
 {
     StudentRegistrationsModel db = new StudentRegistrationsModel();
     StudentVoucher studentVoucher = db.StudentVouchers.Find(id);
     db.StudentVouchers.Remove(studentVoucher);
     db.SaveChanges();
     return RedirectToAction("Index");
 }