Example #1
0
        public ActionResult RolloverStudents(int id)
        {
            var currentYear  = id - 1;
            var students     = repository.GetUsers(null, null, null, null, UserGroup.STUDENT, null, null, null, currentYear, null);
            var noclass      = 0;
            var noclassyear  = 0;
            var alreadyalloc = 0;

            foreach (var student in students)
            {
                if (student.classes_students_allocateds.Count == 0)
                {
                    noclass++;
                    continue;
                }
                // get current allocated class
                var allocated = student.classes_students_allocateds.SingleOrDefault(x => x.year == currentYear);
                if (allocated == null)
                {
                    noclassyear++;
                    continue;
                }

                // check that nothing already allocated
                var checkallocated = student.classes_students_allocateds.SingleOrDefault(x => x.year == id);
                if (checkallocated != null)
                {
                    alreadyalloc++;
                    continue;
                }

                // get next class
                var nextClass = allocated.school_class.nextclass;

                // allocate class
                if (nextClass.HasValue)
                {
                    var newalloc = new classes_students_allocated();
                    newalloc.classid   = nextClass.Value;
                    newalloc.studentid = student.id;
                    newalloc.year      = id;
                    student.classes_students_allocateds.Add(newalloc);

                    // set school id
                    student.schoolid = student.GetNewSchoolID();
                }
            }


            try
            {
                repository.Save();
            }
            catch (Exception ex)
            {
                return(SendJsonErrorResponse(ex));
            }

            return(Json("Students have been rolled over to the next year".ToJsonOKMessage()));
        }
Example #2
0
        private ActionResult ImportStudent(bool?commit)
        {
            var          fs = System.IO.File.OpenRead(AppDomain.CurrentDomain.BaseDirectory + "/Content/media/student.xls");
            HSSFWorkbook templateWorkbook = new HSSFWorkbook(fs, true);
            var          sheet            = templateWorkbook.GetSheet("student");
            var          count            = 1; // skips header

            try
            {
                while (true)
                {
                    var row = sheet.GetRow(count++);
                    if (row == null)
                    {
                        break;
                    }

                    var name = GetCellValueAsString(row.GetCell(1, MissingCellPolicy.RETURN_NULL_AND_BLANK));

                    if (string.IsNullOrEmpty(name))
                    {
                        continue;
                    }

                    var leavingDate           = GetCellValueAsString(row.GetCell(2, MissingCellPolicy.RETURN_NULL_AND_BLANK));
                    var leavingState          = GetCellValueAsString(row.GetCell(3, MissingCellPolicy.RETURN_NULL_AND_BLANK));
                    var currentYear           = GetCellValueAsString(row.GetCell(4, MissingCellPolicy.RETURN_NULL_AND_BLANK));
                    var dob                   = GetCellValueAsString(row.GetCell(5, MissingCellPolicy.RETURN_NULL_AND_BLANK));
                    var nricpassport          = GetCellValueAsString(row.GetCell(6, MissingCellPolicy.RETURN_NULL_AND_BLANK));
                    var admissiondate         = GetCellValueAsString(row.GetCell(7, MissingCellPolicy.RETURN_NULL_AND_BLANK));
                    var schoolclass           = GetCellValueAsString(row.GetCell(11, MissingCellPolicy.RETURN_NULL_AND_BLANK));
                    var rank                  = GetCellValueAsString(row.GetCell(12, MissingCellPolicy.RETURN_NULL_AND_BLANK));
                    var sex                   = GetCellValueAsString(row.GetCell(13, MissingCellPolicy.RETURN_NULL_AND_BLANK));
                    var race                  = GetCellValueAsString(row.GetCell(14, MissingCellPolicy.RETURN_NULL_AND_BLANK));
                    var nationality           = GetCellValueAsString(row.GetCell(15, MissingCellPolicy.RETURN_NULL_AND_BLANK));
                    var religion              = GetCellValueAsString(row.GetCell(16, MissingCellPolicy.RETURN_NULL_AND_BLANK));
                    var phone_house           = GetCellValueAsString(row.GetCell(17, MissingCellPolicy.RETURN_NULL_AND_BLANK));
                    var father_name           = GetCellValueAsString(row.GetCell(18, MissingCellPolicy.RETURN_NULL_AND_BLANK));
                    var father_nricpassport   = GetCellValueAsString(row.GetCell(19, MissingCellPolicy.RETURN_NULL_AND_BLANK));
                    var father_phone_cell     = GetCellValueAsString(row.GetCell(20, MissingCellPolicy.RETURN_NULL_AND_BLANK));
                    var father_phone_office   = GetCellValueAsString(row.GetCell(21, MissingCellPolicy.RETURN_NULL_AND_BLANK));
                    var father_occupation     = GetCellValueAsString(row.GetCell(22, MissingCellPolicy.RETURN_NULL_AND_BLANK));
                    var mother_name           = GetCellValueAsString(row.GetCell(23, MissingCellPolicy.RETURN_NULL_AND_BLANK));
                    var mother_nricpassport   = GetCellValueAsString(row.GetCell(24, MissingCellPolicy.RETURN_NULL_AND_BLANK));
                    var mother_phone_cell     = GetCellValueAsString(row.GetCell(25, MissingCellPolicy.RETURN_NULL_AND_BLANK));
                    var mother_phone_office   = GetCellValueAsString(row.GetCell(26, MissingCellPolicy.RETURN_NULL_AND_BLANK));
                    var mother_occupation     = GetCellValueAsString(row.GetCell(27, MissingCellPolicy.RETURN_NULL_AND_BLANK));
                    var address1              = GetCellValueAsString(row.GetCell(28, MissingCellPolicy.RETURN_NULL_AND_BLANK));
                    var address2              = GetCellValueAsString(row.GetCell(29, MissingCellPolicy.RETURN_NULL_AND_BLANK));
                    var postcode              = GetCellValueAsString(row.GetCell(30, MissingCellPolicy.RETURN_NULL_AND_BLANK));
                    var guardian_name         = GetCellValueAsString(row.GetCell(31, MissingCellPolicy.RETURN_NULL_AND_BLANK));
                    var guardian_nricpassport = GetCellValueAsString(row.GetCell(32, MissingCellPolicy.RETURN_NULL_AND_BLANK));
                    var guardian_phone_hand   = GetCellValueAsString(row.GetCell(33, MissingCellPolicy.RETURN_NULL_AND_BLANK));
                    var guardian_phone_home   = GetCellValueAsString(row.GetCell(34, MissingCellPolicy.RETURN_NULL_AND_BLANK));

                    var student = new user();
                    student.designation = "";
                    student.usergroup   = (int)UserGroup.STUDENT;
                    student.name        = name;
                    if (leavingState == "0")
                    {
                        student.settings = (int)UserSettings.INACTIVE;
                    }
                    else
                    {
                        student.settings = (int)UserSettings.NONE;
                    }
                    if (!string.IsNullOrEmpty(sex))
                    {
                        if (sex == "M")
                        {
                            student.gender = Gender.MALE.ToString();
                        }
                        else if (sex == "F")
                        {
                            student.gender = Gender.FEMALE.ToString();
                        }
                        else
                        {
                            return(Content("Unrecognised gender row " + count));
                        }
                    }

                    if (!string.IsNullOrEmpty(race))
                    {
                        if (race == "C")
                        {
                            student.race = "Chinese";
                        }
                        else
                        {
                            student.race = race;
                        }
                    }
                    if (!string.IsNullOrEmpty(nationality))
                    {
                        student.citizenship = nationality;
                    }
                    if (!string.IsNullOrEmpty(religion))
                    {
                        student.religion = religion;
                    }
                    student.dob = ParseDate(dob);
                    if (!string.IsNullOrEmpty(nricpassport))
                    {
                        switch (GetIDType(nricpassport))
                        {
                        case IDTYPE.PASSPORT:
                            student.passportno = nricpassport;
                            break;

                        case IDTYPE.NEWIC:
                            student.nric_new = nricpassport;
                            break;

                        case IDTYPE.BIRTHCERT:
                            student.birthcertno = nricpassport;
                            break;
                        }
                    }

                    var registration = new registration();
                    registration.admissionDate = ParseDate(admissiondate);
                    registration.leftDate      = ParseDate(leavingDate);
                    student.registrations.Add(registration);

                    if (!string.IsNullOrEmpty(schoolclass))
                    {
                        var school_class = repository.GetSchoolClasses().SingleOrDefault(x => x.name == schoolclass);
                        if (school_class == null)
                        {
                            return(Content("unrecognised school: row " + count));
                        }
                        var student_class_allocated = new classes_students_allocated();
                        student_class_allocated.classid = school_class.id;
                        if (string.IsNullOrEmpty(currentYear))
                        {
                            student_class_allocated.year = 2011;
                        }
                        else
                        {
                            student_class_allocated.year = int.Parse(currentYear);
                        }
                        student.classes_students_allocateds.Add(student_class_allocated);
                    }

                    repository.AddUser(student);

                    var address = string.Concat(address1, Environment.NewLine, address2, Environment.NewLine, postcode);

                    user father = null;
                    if (!string.IsNullOrEmpty(father_name))
                    {
                        father = new user();
                        bool foundDuplicate = false;
                        if (!string.IsNullOrEmpty(father_nricpassport))
                        {
                            user found = null;
                            switch (GetIDType(father_nricpassport))
                            {
                            case IDTYPE.PASSPORT:
                                father.passportno = father_nricpassport;
                                found             =
                                    repository.GetUsers().SingleOrDefault(x => x.passportno == father_nricpassport);
                                if (found != null)
                                {
                                    foundDuplicate = true;
                                    father         = found;
                                }
                                break;

                            case IDTYPE.NEWIC:
                                father.nric_new = father_nricpassport;
                                found           =
                                    repository.GetUsers().SingleOrDefault(x => x.nric_new == father_nricpassport);
                                if (found != null)
                                {
                                    foundDuplicate = true;
                                    father         = found;
                                }
                                break;

                            case IDTYPE.BIRTHCERT:
                                father.birthcertno = father_nricpassport;
                                found =
                                    repository.GetUsers().SingleOrDefault(x => x.birthcertno == father_nricpassport);
                                if (found != null)
                                {
                                    foundDuplicate = true;
                                    father         = found;
                                }
                                break;
                            }
                        }
                        if (!foundDuplicate)
                        {
                            father.designation  = "";
                            father.usergroup    = (int)UserGroup.GUARDIAN;
                            father.user_parents = new user_parent();
                            father.settings     = (int)UserSettings.NONE;
                            father.gender       = Gender.MALE.ToString();
                            father.name         = father_name;
                            father.address      = address;
                            if (!string.IsNullOrEmpty(phone_house))
                            {
                                father.phone_home = phone_house;
                            }
                            if (!string.IsNullOrEmpty(father_phone_cell))
                            {
                                father.phone_cell = father_phone_cell;
                            }
                            if (!string.IsNullOrEmpty(father_phone_office))
                            {
                                father.user_parents.phone_office = father_phone_office;
                            }
                            if (!string.IsNullOrEmpty(father_occupation))
                            {
                                father.user_parents.occupation = father_occupation;
                            }
                            repository.AddUser(father);
                        }
                    }

                    user mother = null;
                    if (!string.IsNullOrEmpty(mother_name))
                    {
                        mother = new user();
                        bool foundDuplicate = false;
                        if (!string.IsNullOrEmpty(mother_nricpassport))
                        {
                            user found = null;
                            switch (GetIDType(mother_nricpassport))
                            {
                            case IDTYPE.PASSPORT:
                                mother.passportno = mother_nricpassport;
                                found             =
                                    repository.GetUsers().SingleOrDefault(x => x.passportno == mother_nricpassport);
                                if (found != null)
                                {
                                    foundDuplicate = true;
                                    mother         = found;
                                }
                                break;

                            case IDTYPE.NEWIC:
                                mother.nric_new = mother_nricpassport;
                                found           =
                                    repository.GetUsers().SingleOrDefault(x => x.nric_new == mother_nricpassport);
                                if (found != null)
                                {
                                    foundDuplicate = true;
                                    mother         = found;
                                }
                                break;

                            case IDTYPE.BIRTHCERT:
                                mother.birthcertno = mother_nricpassport;
                                found =
                                    repository.GetUsers().SingleOrDefault(x => x.birthcertno == mother_nricpassport);
                                if (found != null)
                                {
                                    foundDuplicate = true;
                                    mother         = found;
                                }
                                break;
                            }
                        }
                        if (!foundDuplicate)
                        {
                            mother.designation  = "";
                            mother.usergroup    = (int)UserGroup.GUARDIAN;
                            mother.user_parents = new user_parent();
                            mother.settings     = (int)UserSettings.NONE;
                            mother.gender       = Gender.FEMALE.ToString();
                            mother.name         = mother_name;
                            mother.address      = address;

                            if (!string.IsNullOrEmpty(phone_house))
                            {
                                mother.phone_home = phone_house;
                            }
                            if (!string.IsNullOrEmpty(mother_phone_cell))
                            {
                                mother.phone_cell = mother_phone_cell;
                            }
                            if (!string.IsNullOrEmpty(mother_phone_office))
                            {
                                mother.user_parents.phone_office = mother_phone_office;
                            }
                            if (!string.IsNullOrEmpty(mother_occupation))
                            {
                                mother.user_parents.occupation = mother_occupation;
                            }
                            repository.AddUser(mother);
                        }
                    }

                    user guardian = null;
                    if (!string.IsNullOrEmpty(guardian_name))
                    {
                        guardian = new user();
                        bool foundDuplicate = false;
                        if (!string.IsNullOrEmpty(guardian_nricpassport))
                        {
                            user found = null;
                            switch (GetIDType(guardian_nricpassport))
                            {
                            case IDTYPE.PASSPORT:
                                guardian.passportno = guardian_nricpassport;
                                found =
                                    repository.GetUsers().SingleOrDefault(x => x.passportno == guardian_nricpassport);
                                if (found != null)
                                {
                                    foundDuplicate = true;
                                    guardian       = found;
                                }
                                break;

                            case IDTYPE.NEWIC:
                                guardian.nric_new = guardian_nricpassport;
                                found             =
                                    repository.GetUsers().SingleOrDefault(x => x.nric_new == guardian_nricpassport);
                                if (found != null)
                                {
                                    foundDuplicate = true;
                                    guardian       = found;
                                }
                                break;

                            case IDTYPE.BIRTHCERT:
                                guardian.birthcertno = guardian_nricpassport;
                                found =
                                    repository.GetUsers().SingleOrDefault(x => x.birthcertno == guardian_nricpassport);
                                if (found != null)
                                {
                                    foundDuplicate = true;
                                    guardian       = found;
                                }
                                break;
                            }
                        }

                        if (!foundDuplicate)
                        {
                            guardian.designation  = "";
                            guardian.usergroup    = (int)UserGroup.GUARDIAN;
                            guardian.user_parents = new user_parent();
                            guardian.name         = guardian_name;
                            guardian.settings     = (int)UserSettings.NONE;
                            guardian.address      = address;

                            if (!string.IsNullOrEmpty(guardian_phone_home))
                            {
                                guardian.phone_home = guardian_phone_home;
                            }
                            if (!string.IsNullOrEmpty(guardian_phone_hand))
                            {
                                guardian.phone_cell = guardian_phone_hand;
                            }
                            repository.AddUser(guardian);
                        }
                    }

                    if (commit.HasValue && commit.Value)
                    {
                        // save new users
                        repository.Save();

                        // add relationship
                        if (father != null)
                        {
                            var f = new students_guardian();
                            f.parentid = father.id;
                            f.type     = (byte)GuardianType.FATHER;
                            student.students_guardians.Add(f);
                        }

                        if (mother != null)
                        {
                            var m = new students_guardian();
                            m.parentid = mother.id;
                            m.type     = (byte)GuardianType.MOTHER;
                            student.students_guardians.Add(m);
                        }

                        if (guardian != null)
                        {
                            var g = new students_guardian();
                            g.parentid = guardian.id;
                            g.type     = (byte)GuardianType.GUARDIAN;
                            student.students_guardians.Add(g);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Syslog.Write(ex);
                return(Content(count + ":" + ex.Message));
            }

            if (commit.HasValue && commit.Value)
            {
                repository.Save();
                return(Content("commited rows " + count));
            }
            return(Content("done rows " + count));
        }
Example #3
0
        public ActionResult Save(long?id, string designation, string name, string email, Schools?uschool,
                                 int[] day, int[] year, int?[] school, int?[] schoolclass, string[] subject,
                                 long[] parent, int[] parentrel, long[] child, int[] childrel,
                                 UserGroup?ugroup, long?thumbnailid, int[] start_hour, int[] start_minutes, int[] end_hour, int[] end_minutes,
                                 string race, string dialect, int dob_day, int dob_month, int?dob_year,
                                 string pob, string citizenship, string birthcertno, string passport, bool bumi, string nric_new,
                                 string homephone, string cellphone, string address, string religion, Gender gender, MaritalStatus marital_status,
                                 string occupation, string officephone, string employer, string notes,
                                 // staff stuff
                                 string staff_socso, string staff_salary_grade, string staff_epf, string staff_income_tax,
                                 string staff_spouse_phone_cell, string staff_spouse_phone_office, string staff_spouse_employer_address,
                                 string staff_spouse_employer, string staff_spouse_name
                                 )
        {
            if (email == null)
            {
                email = "";
            }
            email = email.Trim().ToLower();

            // TODO check that staff / student id is unique
            var emailchanged = true;
            var u            = new user();

            if (id.HasValue)
            {
                u = repository.GetUser(id.Value);
                if (u == null)
                {
                    return(Json("Unable to find user".ToJsonFail()));
                }
                if (u.email == email)
                {
                    emailchanged = false;
                }
            }
            else
            {
                // can we create new user?
                if (!auth.perms.HasFlag(Permission.USERS_CREATE))
                {
                    return(SendJsonNoPermission());
                }

                // dont allow change of usergroups for the moment because there are specific actions
                // that need to be performed when a certain type of user is added
                // only set when user is created
                if (ugroup.HasValue)
                {
                    u.usergroup   = (int)ugroup.Value;
                    u.permissions = (long)UserHelper.GetDefaultPermission(ugroup.Value);
                }
                u.settings = (int)UserSettings.NONE;
            }

            // check that email is unique
            if (!string.IsNullOrEmpty(email))
            {
                var duplicate = repository.GetUsers().SingleOrDefault(x => string.Compare(x.email, email) == 0);
                if (duplicate != null && duplicate.id != u.id)
                {
                    return(Json("Email address is already in use".ToJsonFail()));
                }
            }

            // check that nric is unique
            if (!string.IsNullOrEmpty(nric_new))
            {
                var duplicate = repository.GetUsers().FirstOrDefault(x => string.Compare(x.nric_new, nric_new) == 0);
                if (duplicate != null && duplicate.id != u.id)
                {
                    return(Json("NRIC is already in use".ToJsonFail()));
                }
            }

            if (uschool.HasValue)
            {
                u.schoolid = uschool.Value.ToInt();
            }

            u.gender      = gender.ToString();
            u.designation = designation;
            u.name        = name;
            u.email       = email;
            u.photo       = thumbnailid;
            u.race        = race;
            u.dialect     = dialect;
            if (dob_year.HasValue)
            {
                try
                {
                    u.dob = new DateTime(dob_year.Value, dob_month, dob_day);
                }
                catch
                {
                    return(Json("Invalid Date of Birth".ToJsonFail()));
                }
            }
            u.pob            = pob;
            u.citizenship    = citizenship;
            u.birthcertno    = birthcertno;
            u.passportno     = passport;
            u.isbumi         = bumi;
            u.nric_new       = nric_new;
            u.phone_home     = homephone;
            u.phone_cell     = cellphone;
            u.address        = address;
            u.religion       = religion;
            u.notes          = notes;
            u.marital_status = marital_status.ToString();

            if (!ugroup.HasValue)
            {
                ugroup = (UserGroup)u.usergroup;
            }

            switch (ugroup)
            {
            case UserGroup.GUARDIAN:
                if (u.user_parents == null)
                {
                    u.user_parents = new user_parent();
                }
                if (!string.IsNullOrEmpty(employer))
                {
                    employer = employer.Trim();
                }
                u.user_parents.employer     = employer;
                u.user_parents.phone_office = officephone;
                u.user_parents.occupation   = occupation;

                if (child != null)
                {
                    for (int i = 0; i < child.Length; i++)
                    {
                        var student = new students_guardian();
                        student.studentid = child[i];
                        student.type      = Convert.ToByte(childrel[i]);
                        u.students_guardians1.Add(student);
                    }
                }
                break;

            case UserGroup.HEAD:
            case UserGroup.TEACHER:
                if (schoolclass != null)
                {
                    for (int i = 0; i < schoolclass.Length; i++)
                    {
                        var assigned = new classes_teachers_allocated();
                        assigned.day  = day[i];
                        assigned.year = year[i];
                        if (school[i] == null)
                        {
                            return(Json("School not specified".ToJsonFail()));
                        }
                        assigned.schoolid = school[i].Value;
                        if (schoolclass[i] == null)
                        {
                            return(Json("Class is not specified".ToJsonFail()));
                        }
                        assigned.classid = schoolclass[i].Value;

                        // allow NULL subject for kindy classes as they don't have subjects
                        if (subject != null && !string.IsNullOrEmpty(subject[i]))
                        {
                            assigned.subjectid = long.Parse(subject[i]);
                        }

                        assigned.time_start = new TimeSpan(start_hour[i], start_minutes[i], 0);
                        assigned.time_end   = new TimeSpan(end_hour[i], end_minutes[i], 0);

                        // check that period is not already assigned
                        var period = repository.GetClassPeriod(assigned.year, assigned.day, assigned.schoolid, assigned.classid,
                                                               assigned.time_start, assigned.time_end);
                        if (period != null)
                        {
                            // only give warning if class allocated is owner's own as we want to allow
                            // assistants to share the same period
                            if (id.HasValue && period.teacherid == id.Value)
                            {
                                return
                                    (Json(
                                         string.Format(
                                             "A class from {0} to {1} has already been assigned to {2} for {3}",
                                             period.time_start,
                                             period.time_end,
                                             period.user.ToName(),
                                             period.subject == null ? "" : period.subject.name).
                                         ToJsonFail()));
                            }
                        }
                        u.classes_teachers_allocateds.Add(assigned);
                    }
                }
                break;

            case UserGroup.STUDENT:
                if (schoolclass != null)
                {
                    for (int i = 0; i < schoolclass.Length; i++)
                    {
                        var assigned = new classes_students_allocated();
                        assigned.year = year[i];
                        if (schoolclass[i] == null)
                        {
                            return(Json("Class is not specified".ToJsonFail()));
                        }
                        assigned.classid = schoolclass[i].Value;

                        // check that class is not already assigned
                        var exist =
                            u.classes_students_allocateds.SingleOrDefault(x => x.year == assigned.year);
                        if (exist == null)
                        {
                            u.classes_students_allocateds.Add(assigned);
                        }
                        else
                        {
                            return(Json(string.Format("A class for the year {0} has already been allocated.", exist.year).ToJsonFail()));
                        }
                    }
                }
                if (parent != null)
                {
                    for (int i = 0; i < parent.Length; i++)
                    {
                        var guardian = new students_guardian();
                        guardian.parentid = parent[i];
                        guardian.type     = Convert.ToByte(parentrel[i]);
                        u.students_guardians.Add(guardian);
                    }

                    // validate not more than 1 mother or father
                    if (u.students_guardians.Count(x => x.type.HasValue && x.type == GuardianType.FATHER.ToInt()) > 1)
                    {
                        return(Json("Cannot add more than 1 father".ToJsonFail()));
                    }

                    if (u.students_guardians.Count(x => x.type.HasValue && x.type == GuardianType.MOTHER.ToInt()) > 1)
                    {
                        return(Json("Cannot add more than 1 mother".ToJsonFail()));
                    }

                    if (u.students_guardians.Count(x => x.type.HasValue && x.type == GuardianType.GUARDIAN.ToInt()) > 1)
                    {
                        return(Json("Cannot add more than 1 guardian".ToJsonFail()));
                    }
                }
                break;
            } // end switch

            // do STAFF only actions
            if (UserSuperGroups.STAFF.HasFlag(ugroup.Value) &&
                UserSuperGroups.SUPERSTAFF.HasFlag(auth.group))
            {
                if (u.user_staffs == null)
                {
                    u.user_staffs = new user_staff();
                }
                u.user_staffs.socso                   = staff_socso;
                u.user_staffs.salary_grade            = staff_salary_grade;
                u.user_staffs.epf                     = staff_epf;
                u.user_staffs.income_tax              = staff_income_tax;
                u.user_staffs.spouse_phone_cell       = staff_spouse_phone_cell;
                u.user_staffs.spouse_phone_work       = staff_spouse_phone_office;
                u.user_staffs.spouse_employer_address = staff_spouse_employer_address;
                u.user_staffs.spouse_employer         = staff_spouse_employer;
                u.user_staffs.spouse_name             = staff_spouse_name;
            }

            // check if we can actually edit
            var canedit = u.GetCanEdit(sessionid.Value, auth);

            if (!canedit)
            {
                return(SendJsonNoPermission());
            }

            if (!id.HasValue)
            {
                repository.AddUser(u);
            }

            // log changes
            EntityLogging.LogChanges(db, u, u.name, sessionid.Value);

            try
            {
                repository.Save();
            }
            catch (Exception ex)
            {
                return(SendJsonErrorResponse(ex));
            }

            // try to update school
            if (!u.schoolid.HasValue)
            {
                u.schoolid = u.GetNewSchoolID();
            }
            repository.Save();

            // resend password email if email has been changed OR a user has been created
            if (emailchanged && !string.IsNullOrEmpty(email))
            {
                var password = tradelr.Crypto.Utility.GetRandomString(uppercase: true);
                var hash     = Utility.GeneratePasswordHash(email, password);
                u.passwordhash = hash;
                u.settings     = u.SetFlag(UserSettings.PASSWORD_RESET);
                repository.Save();
                var credentials = new UserCredentials {
                    password = password, email = email
                };
                this.SendEmailNow(EmailViewType.PASSWORD_RESET, credentials, "New Account Password", email, u.ToName());
            }

            LuceneUtil.UpdateLuceneIndex(u);

            var jsonmodel = "User successfully saved".ToJsonOKMessage();

            jsonmodel.data = u.id;

            return(Json(jsonmodel));
        }