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)); }
public AdmissionStatus Process(Gender child_sex, int enrol_year, int school, int year, string child_name, string child_race, string child_dialect, string child_address, int child_dob_day, int child_dob_month, int child_dob_year, string child_pob, string child_citizenship, string child_birthcertno, string child_passportnric, bool child_bumi, string child_religion, HttpPostedFileBase child_photo, string child_previous_school, HttpPostedFileBase child_report, string child_previous_class, string child_leaving_reason, bool?child_handicap, bool?child_learning_problems, string child_disability_details, // parents fields string parent1_designation, string parent1_name, string parent1_passportnric, string parent1_occupation, string parent1_employer, string parent1_race, string parent1_dialect, bool?parent1_bumi, string parent1_marital, string parent1_citizenship, string parent1_religion, string parent1_officephone, string parent1_homephone, string parent1_handphone, string parent1_email, string parent1_address, string parent2_designation, string parent2_name, string parent2_passportnric, string parent2_occupation, string parent2_employer, string parent2_race, string parent2_dialect, bool?parent2_bumi, string parent2_marital, string parent2_citizenship, string parent2_religion, string parent2_officephone, string parent2_homephone, string parent2_handphone, string parent2_email, string parent2_address, // guardian fields string guardian_designation, string guardian_name, Gender?guardian_sex, string guardian_passportnric, string guardian_occupation, string guardian_employer, string guardian_race, string guardian_dialect, bool?guardian_bumi, string guardian_marital, string guardian_citizenship, string guardian_religion, string guardian_officephone, string guardian_homephone, string guardian_handphone, string guardian_email, string guardian_address, // other siblings string[] sibling_name, string[] sibling_nric, GuardianType?applicant_relationship, bool internalsubmission) { var noemail = true; // to ensure at least parent/guardian has email var emails = new List <string>(); // sanitize inputs parent1_email = (parent1_email ?? "").Trim().ToLower(); parent2_email = (parent2_email ?? "").Trim().ToLower(); guardian_email = (guardian_email ?? "").Trim().ToLower(); if (!string.IsNullOrEmpty(child_passportnric)) { child_passportnric = child_passportnric.Trim().Replace("-", ""); } if (!string.IsNullOrEmpty(parent1_passportnric)) { parent1_passportnric = parent1_passportnric.Trim().Replace("-", ""); } if (!string.IsNullOrEmpty(parent2_passportnric)) { parent2_passportnric = parent2_passportnric.Trim().Replace("-", ""); } if (!string.IsNullOrEmpty(guardian_passportnric)) { guardian_passportnric = guardian_passportnric.Trim().Replace("-", ""); } // check that student does not already exist if (!string.IsNullOrEmpty(child_passportnric)) { student = repository.GetUserByNewNRIC(child_passportnric); // only match student usergroup to prevent accidently matching with parents or teachers if (student != null && student.usergroup != (int)UserGroup.STUDENT) { Syslog.Write(ErrorLevel.WARNING, "NRIC incorrect match: " + child_passportnric); return(AdmissionStatus.INCORRECT_NRIC_PASSPORT); } } if (student == null && !string.IsNullOrEmpty(child_passportnric)) { student = repository.GetUsers().SingleOrDefault(x => string.Compare(x.passportno, child_passportnric, true) == 0); // only match student usergroup to prevent accidently matching with parents or teachers if (student != null && student.usergroup != (int)UserGroup.STUDENT) { Syslog.Write(ErrorLevel.WARNING, "NRIC incorrect match: " + child_passportnric); return(AdmissionStatus.INCORRECT_NRIC_PASSPORT); } } if (student == null) { // student student = new ioschools.DB.user(); student.usergroup = (int)UserGroup.STUDENT; student.settings = (int)UserSettings.INACTIVE; student.email = ""; student.name = child_name; student.gender = child_sex.ToString(); student.race = child_race; student.dialect = child_dialect; student.dob = new DateTime(child_dob_year, child_dob_month, child_dob_day); student.pob = child_pob; student.citizenship = child_citizenship; student.birthcertno = child_birthcertno; student.religion = child_religion; student.isbumi = child_bumi; student.address = child_address; var childidtype = GetIDType(child_passportnric); switch (childidtype) { case IdType.NEWIC: student.nric_new = child_passportnric; break; case IdType.PASSPORT: student.passportno = child_passportnric; break; default: throw new NotImplementedException(); } repository.AddUser(student); } // father password_father = tradelr.Crypto.Utility.GetRandomString(uppercase: true); if (!string.IsNullOrEmpty(parent1_name)) { // see if we can find this parent if (!string.IsNullOrEmpty(parent1_passportnric)) { father = repository.GetUserByNewNRIC(parent1_passportnric); } if (father == null && !string.IsNullOrEmpty(parent1_passportnric)) { father = repository.GetUsers().SingleOrDefault(x => string.Compare(x.passportno, parent1_passportnric, true) == 0); } if (father == null) { father = new ioschools.DB.user(); father.usergroup = (int)UserGroup.GUARDIAN; father.settings = (int)UserSettings.NONE; father.designation = parent1_designation; father.name = parent1_name; father.gender = Gender.MALE.ToString(); father.race = parent1_race; father.dialect = parent1_dialect; father.citizenship = parent1_citizenship; father.address = parent1_address; father.religion = parent1_religion; father.isbumi = parent1_bumi; var fatheridtype = GetIDType(parent1_passportnric); switch (fatheridtype) { case IdType.PASSPORT: father.passportno = parent1_passportnric; break; case IdType.NEWIC: father.nric_new = parent1_passportnric; father.dob = parent1_passportnric.ToDOB(); break; default: throw new NotImplementedException(); } if (!string.IsNullOrEmpty(parent1_email)) { // checks that email address is not in use if (repository.GetUsers().SingleOrDefault(x => string.Compare(x.email, parent1_email, true) == 0) != null) { Syslog.Write(ErrorLevel.WARNING, "Enrolment using existing email " + parent1_email); return(AdmissionStatus.DUPLICATEEMAIL); } emails.Add(parent1_email); father.email = parent1_email; father.passwordhash = Utility.GeneratePasswordHash(parent1_email, password_father); noemail = false; } father.phone_home = parent1_homephone; father.phone_cell = parent1_handphone; father.user_parents = new user_parent(); father.marital_status = parent1_marital; father.user_parents.phone_office = parent1_officephone; father.user_parents.occupation = parent1_occupation; father.user_parents.employer = string.IsNullOrEmpty(parent1_employer)?"":parent1_employer.Trim(); repository.AddUser(father); } else { if (string.IsNullOrEmpty(father.email)) { // let's see if we can update email information if (!string.IsNullOrEmpty(parent1_email)) { // checks that email address is not in use if (repository.GetUsers().SingleOrDefault(x => string.Compare(x.email, parent1_email, true) == 0) != null) { Syslog.Write(ErrorLevel.WARNING, "Enrolment using existing email " + parent1_email); return(AdmissionStatus.DUPLICATEEMAIL); } emails.Add(parent1_email); father.email = parent1_email; father.passwordhash = Utility.GeneratePasswordHash(parent1_email, password_father); noemail = false; } } else { noemail = false; } } } password_mother = tradelr.Crypto.Utility.GetRandomString(uppercase: true); if (!string.IsNullOrEmpty(parent2_name)) { // see if we can find this parent if (!string.IsNullOrEmpty(parent2_passportnric)) { mother = repository.GetUserByNewNRIC(parent2_passportnric); } if (mother == null && !string.IsNullOrEmpty(parent2_passportnric)) { mother = repository.GetUsers().SingleOrDefault(x => string.Compare(x.passportno, parent2_passportnric, true) == 0); } if (mother == null) { mother = new ioschools.DB.user(); mother.usergroup = (int)UserGroup.GUARDIAN; mother.settings = (int)UserSettings.NONE; mother.designation = parent2_designation; mother.name = parent2_name; mother.gender = Gender.FEMALE.ToString(); mother.race = parent2_race; mother.dialect = parent2_dialect; mother.citizenship = parent2_citizenship; mother.address = parent2_address; mother.religion = parent2_religion; mother.isbumi = parent2_bumi; var motheridtype = GetIDType(parent2_passportnric); switch (motheridtype) { case IdType.PASSPORT: mother.passportno = parent2_passportnric; break; case IdType.NEWIC: mother.nric_new = parent2_passportnric; mother.dob = parent2_passportnric.ToDOB(); break; default: throw new NotImplementedException(); } if (!string.IsNullOrEmpty(parent2_email)) { // checks that email address is not in use if (repository.GetUsers().SingleOrDefault(x => string.Compare(x.email, parent2_email, true) == 0) != null) { Syslog.Write(ErrorLevel.WARNING, "Enrolment using existing email " + parent2_email); return(AdmissionStatus.DUPLICATEEMAIL); } if (!emails.Contains(parent2_email)) { emails.Add(parent2_email); mother.email = parent2_email; mother.passwordhash = Utility.GeneratePasswordHash(parent2_email, password_mother); noemail = false; } } mother.phone_home = parent2_homephone; mother.phone_cell = parent2_handphone; mother.user_parents = new user_parent(); mother.marital_status = parent2_marital; mother.user_parents.phone_office = parent2_officephone; mother.user_parents.occupation = parent2_occupation; mother.user_parents.employer = string.IsNullOrEmpty(parent2_employer) ? "" : parent2_employer.Trim(); repository.AddUser(mother); } else { if (string.IsNullOrEmpty(mother.email)) { // let's see if we can update email information if (!string.IsNullOrEmpty(parent2_email)) { // checks that email address is not in use if (repository.GetUsers().SingleOrDefault(x => string.Compare(x.email, parent2_email, true) == 0) != null) { Syslog.Write(ErrorLevel.WARNING, "Enrolment using existing email " + parent2_email); return(AdmissionStatus.DUPLICATEEMAIL); } emails.Add(parent2_email); mother.email = parent2_email; mother.passwordhash = Utility.GeneratePasswordHash(parent2_email, password_mother); noemail = false; } } else { noemail = false; } } } password_guardian = tradelr.Crypto.Utility.GetRandomString(uppercase: true); if (!string.IsNullOrEmpty(guardian_name)) { // see if we can find this parent if (!string.IsNullOrEmpty(guardian_passportnric)) { guardian = repository.GetUserByNewNRIC(guardian_passportnric); } if (guardian == null && !string.IsNullOrEmpty(guardian_passportnric)) { guardian = repository.GetUsers().SingleOrDefault(x => string.Compare(x.passportno, guardian_passportnric) == 0); } if (guardian == null) { guardian = new ioschools.DB.user(); guardian.usergroup = (int)UserGroup.GUARDIAN; guardian.settings = (int)UserSettings.NONE; guardian.designation = guardian_designation; guardian.name = guardian_name; guardian.gender = guardian_sex.ToString(); guardian.race = guardian_race; guardian.dialect = guardian_dialect; guardian.citizenship = guardian_citizenship; guardian.address = guardian_address; guardian.religion = guardian_religion; guardian.isbumi = guardian_bumi; var guardianidtype = GetIDType(guardian_passportnric); switch (guardianidtype) { case IdType.NEWIC: guardian.nric_new = guardian_passportnric; guardian.dob = guardian_passportnric.ToDOB(); break; case IdType.PASSPORT: guardian.passportno = guardian_passportnric; break; default: throw new NotImplementedException(); } if (!string.IsNullOrEmpty(guardian_email)) { // checks that email address is not in use if (repository.GetUsers().SingleOrDefault(x => string.Compare(x.email, guardian_email) == 0) != null) { Syslog.Write(ErrorLevel.WARNING, "Enrolment using existing email " + guardian_email); return(AdmissionStatus.DUPLICATEEMAIL); } if (!emails.Contains(guardian_email)) { guardian.email = guardian_email; guardian.passwordhash = Utility.GeneratePasswordHash(guardian_email, password_guardian); noemail = false; } } guardian.phone_home = guardian_homephone; guardian.phone_cell = guardian_handphone; guardian.user_parents = new user_parent(); guardian.marital_status = guardian_marital; guardian.user_parents.phone_office = guardian_officephone; guardian.user_parents.occupation = guardian_occupation; guardian.user_parents.employer = string.IsNullOrEmpty(guardian_employer) ? "" : guardian_employer.Trim(); repository.AddUser(guardian); } else { if (string.IsNullOrEmpty(guardian.email)) { // let's see if we can update email information if (!string.IsNullOrEmpty(guardian_email)) { // checks that email address is not in use if (repository.GetUsers().SingleOrDefault(x => string.Compare(x.email, guardian_email, true) == 0) != null) { Syslog.Write(ErrorLevel.WARNING, "Enrolment using existing email " + guardian_email); return(AdmissionStatus.DUPLICATEEMAIL); } emails.Add(guardian_email); guardian.email = guardian_email; guardian.passwordhash = Utility.GeneratePasswordHash(guardian_email, password_guardian); noemail = false; } } else { noemail = false; } } } // check that there's an email if (noemail && !internalsubmission) { Syslog.Write(ErrorLevel.WARNING, string.Format("No email specified: 1:{0} 2:{1} 3:{2}", parent1_email, parent2_email, guardian_email)); return(AdmissionStatus.NOEMAIL); } // check that student is not already enrol for the same year // should we? possiblity that student may leave and come back in the same year /////////////////////// SAVE //////////////////// repository.Save(); // save photo if (child_photo != null) { var photouploader = new FileHandler(child_photo.FileName, UploaderType.PHOTO, null); photouploader.Save(child_photo.InputStream); var image = new user_image(); image.url = photouploader.url; student.user_image = image; } // save child report if (child_report != null) { var reportuploader = new FileHandler(child_report.FileName, UploaderType.REGISTRATION, student.id); reportuploader.Save(child_report.InputStream); var file = new user_file(); file.filename = child_report.FileName; file.url = reportuploader.url; student.user_files.Add(file); } // siblings for (int i = 0; i < sibling_name.Length; i++) { var name = sibling_name[i]; var nric = sibling_nric[i]; if (!string.IsNullOrEmpty(nric)) { nric = nric.Trim().Replace("-", ""); // try to find user var sibling = repository.GetUserByNewNRIC(nric); if ((sibling != null && sibling.usergroup != (int)UserGroup.STUDENT) || sibling == null) { // try pasport sibling = repository.GetUsers().SingleOrDefault(x => string.Compare(x.passportno, nric, true) == 0); } if (sibling != null) { var s = new sibling(); s.otherid = student.id; sibling.siblings1.Add(s); } } } // relationship // parent if (father != null) { var f = new students_guardian(); f.parentid = father.id; f.type = (byte)GuardianType.FATHER; if (!student.students_guardians.Any(x => x.parentid == f.parentid && x.type == f.type)) { student.students_guardians.Add(f); } } if (mother != null) { var m = new students_guardian(); m.parentid = mother.id; m.type = (byte)GuardianType.MOTHER; if (!student.students_guardians.Any(x => x.parentid == m.parentid && x.type == m.type)) { student.students_guardians.Add(m); } } if (guardian != null) { var g = new students_guardian(); g.parentid = guardian.id; g.type = (byte)GuardianType.GUARDIAN; if (!student.students_guardians.Any(x => x.parentid == g.parentid && x.type == g.type)) { student.students_guardians.Add(g); } } // registration var r = new registration(); r.enrollingYear = enrol_year; r.created = DateTime.Now; r.schoolid = school; r.schoolyearid = year; r.studentid = student.id; r.status = RegistrationStatus.PENDING.ToString(); r.schoolid = school; r.schoolyearid = year; r.previous_school = child_previous_school; r.leaving_reason = child_leaving_reason; r.disability_details = child_disability_details; r.hasLearningProblem = child_learning_problems; r.isHandicap = child_handicap; r.previous_class = child_previous_class; if (applicant_relationship.HasValue) { switch (applicant_relationship) { case GuardianType.FATHER: r.applicantid = father.id; break; case GuardianType.MOTHER: r.applicantid = mother.id; break; case GuardianType.GUARDIAN: r.applicantid = guardian.id; break; } } repository.AddRegistration(r); // success try { repository.Save(); } catch (Exception ex) { Syslog.Write(ex); return(AdmissionStatus.UNKNOWN); } return(AdmissionStatus.SUCCESS); }
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)); }