protected override void Page_Load(object sender, EventArgs e) { // set dataset message if (!hasInstitutionDataset) { NewPatientInstitution.Visible = true; } if (!Page.IsPostBack) { int protocolId = int.Parse(BaseProtocolId); // Get an auto-generate screening id ScreeningID.Value = PatientProtocolController.GenerateScreeningId().ToString(); // study id string autoStudyId = PatientProtocolController.GenerateStudyId(protocolId); StudyID.Value = autoStudyId; bool calculateStudyIdBySite = ProtocolMgmtUtil.GenerateStudyIdByOrganization(); // custom auto-generation if (calculateStudyIdBySite) { OrganizationStudyIds.Visible = true; StudyIdTooltip.Attributes["title"] = "An Id created to identify a patient on a study in the form: Site Id (2 digits) + Protocol Id (3 digits) + Incremental Patient Id (3 digits)"; // get associated Project (to select organizations) Protocol protocol = new Protocol(); protocol.Get(protocolId); string protocolNum = protocol[Protocol.ProtocolNum].ToString(); // get a list of associated organizations if (!protocol.IsNull(Protocol.ProjectId) && !string.IsNullOrEmpty(protocolNum)) { ProjectManagementDa da = new ProjectManagementDa(); int projectId = (int)protocol[Protocol.ProjectId]; // get a list of organizations associated with project var projectOrganizations = BusinessObject.GetByFields <Project_ProjectOrganization>(new Dictionary <string, object> { { Project_ProjectOrganization.ProjectId, projectId } }); // get a list of user's organizations int userId = new UserController().GetUserId(); DataTable userOrganizations = new UserDa().GetUserOrganizations(userId); var userOrganizationIds = userOrganizations.AsEnumerable().Select(r => (int)r["OrganizationId"]); // get the intersect of user's sites and lead sites var userLeadSites = from po in projectOrganizations let organizationType = po["OrganizationType"].ToString() let organizationId = (int)po[Project_ProjectOrganization.OrganizationId] where organizationType.Contains("Lead Site") join userOrgId in userOrganizationIds on organizationId equals userOrgId select userOrgId; // if user is part of a lead site, allow to select organization if (userLeadSites.Count() > 0) { // TODO: centralize call to get project organizations var organizations = da.GetAllOrganizationsByProjectId(projectId); // get a list of organizations and calculate study id per organization var organizationToStudyId = from row in organizations.AsEnumerable() let orgName = row["Name"].ToString() let orgId = (int)row["OrganizationId"] let isAssociated = userOrganizationIds.Contains(orgId) ? 0 : 1 orderby //isAssociated ascending, orgName ascending select new { OrganizationName = orgName, OrganizationId = orgId, OrganizationStudyId = GenerateStudyIdByOrganization(protocolId, protocolNum, orgId) }; OrganizationStudyIds.DataSource = organizationToStudyId; OrganizationStudyIds.DataBind(); // a list of insitutions where institution = organization in project var allInstitutions = from inst in BOL.BusinessObject.GetAll <Institution>() let institutionId = (int)inst[BOL.Institution.InstitutionId] let institutionName = inst[BOL.Institution.Institution_Field].ToString() select new { InstitutionId = institutionId, Institution = institutionName }; var orgs = from org in userOrganizations.AsEnumerable() select new { Name = org["Name"].ToString(), ShortName = org["ShortName"].ToString() }; // get institution where name matches an organization's name or short name var institutions = from inst in allInstitutions let institutionName = inst.Institution let organization = orgs.Where(org => org.Name == institutionName || org.ShortName == institutionName).FirstOrDefault() where organization != null select new { Text = organization.Name, Value = inst.InstitutionId }; // build institutions NewPatientInstitutionSelect.DataSource = institutions; NewPatientInstitutionSelect.DataBind(); // set default study id: user's organization // get user's site int userLeadSiteId = userLeadSites.First(); // get study id for user's site var userSites = organizationToStudyId.Where(o => o.OrganizationId == userLeadSiteId); autoStudyId = userSites.Count() == 1 ? userSites.First().OrganizationStudyId : ""; // update field and select OrganizationStudyIds.Value = autoStudyId; StudyID.Value = autoStudyId; } // show a list of all user's organizations else { if (userOrganizations.Rows.Count > 0) { string organizationName = userOrganizations.Rows[0]["Name"].ToString(); int organizationId = (int)userOrganizations.Rows[0]["OrganizationId"]; // a list of orgaizations associated with the project var organizations = from o in userOrganizations.AsEnumerable() let orgId = (int)o["OrganizationId"] select new { OrganizationName = o["Name"].ToString(), OrganizationId = orgId, OrganizationStudyId = GenerateStudyIdByOrganization(protocolId, protocolNum, orgId) }; // a list of insitutions where institution = organization in project var allInstitutions = from inst in BOL.BusinessObject.GetAll <Institution>() let institutionId = (int)inst[BOL.Institution.InstitutionId] let institutionName = inst[BOL.Institution.Institution_Field].ToString() select new { InstitutionId = institutionId, Institution = institutionName }; var orgs = from org in userOrganizations.AsEnumerable() select new { Name = org["Name"].ToString(), ShortName = org["ShortName"].ToString() }; // get institution where name matches an organization's name or short name var institutions = from inst in allInstitutions let institutionName = inst.Institution let organization = orgs.Where(org => org.Name == institutionName || org.ShortName == institutionName).FirstOrDefault() where organization != null select new { Text = organization.Name, Value = inst.InstitutionId }; // build organizations OrganizationStudyIds.DataSource = organizations; OrganizationStudyIds.DataBind(); // build institutions NewPatientInstitutionSelect.DataSource = institutions; NewPatientInstitutionSelect.DataBind(); autoStudyId = organizations.Count() == 1? organizations.First().OrganizationStudyId : ""; OrganizationStudyIds.Value = autoStudyId; //OrganizationStudyIds.Enabled = false; StudyID.Value = autoStudyId; } } } if (string.IsNullOrEmpty(autoStudyId)) { StudyIdError.Visible = true; } } // default screeing date //ScreeningDate.Value = DateTime.Today.ToShortDateString(); PopulatePhysicians(); // set search by particpant id type SeachTypeParticipantID.Text = PROTOCOL_PARTICIPANT_ID_CODE; // default participant id field ParticipantIdField.Text = GetNextParticipantId(); // default radios and panel which can be seen by user DetermineUserSearchAccess(); } }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void DoSave(object sender, EventArgs e) { try { if (ValidateForm()) { // REQUIRED int protocolId = int.Parse(BaseProtocolId); // STEP 1: Get PatientId Patient patient = new Patient(); int ptId = -1; if (CanViewPatientInfo) { if (AddNewPatient.Checked) { patient[Patient.PtFirstName] = NewFirstName.Value; patient[Patient.PtLastName] = NewLastName.Value; patient[Patient.PtMRN] = NewMRN.Value; // If no name or mrn, enter notes to save record patient[Patient.PtNotes] = "Protocol-Patient"; // insert records using controller PatientController ct = new PatientController(); try { // insert new patient InsertNewPatientRecord(patient); } catch (InvalidScreeningException ex) { throw ex; } // if cannot insert patient, already exits, warn user catch (Exception ex) { throw new InvalidScreeningException("Select a new MRN, another patient with the same MRN already exists in the system."); } //if (patient.RecordCount > 0) if (!patient.IsEmpty) { ptId = int.Parse(patient[Patient.PatientId].ToString()); } } // if using existing patient else if (FindExistingPatient.Checked) { if (!string.IsNullOrEmpty(epid.Value)) { ptId = int.Parse(DecrypyValue(epid.Value)); } } } // if cannnot view PatientInfo, insert blinded info else { patient[Patient.PtFirstName] = string.Empty; patient[Patient.PtLastName] = string.Empty; patient[Patient.PtMRN] = string.Empty; patient[Patient.PtNotes] = "Blinded-Protocol-Patient"; // insert new patient InsertNewPatientRecord(patient); //if (patient.RecordCount > 0) if (!patient.IsEmpty) { ptId = int.Parse(patient[Patient.PatientId].ToString()); } } // STEP 2: Create ParticipantID (if needed) if (ptId != -1) { if (!PatientProtocolController.HasParticipantId(protocolId, ptId)) { PatientProtocolController.CreateParticipantIdIdentifier(protocolId, ptId); } } // VALIDATION: ensure non-duplicate PatientProtocol, via insert new patient int?ptProtocolId = null; var pp = BusinessObject.GetByFields <PatientProtocol>( new Dictionary <string, object> { { PatientProtocol.ProtocolId, protocolId }, { PatientProtocol.PatientId, ptId } } ); if (pp.Count() > 0) { ptProtocolId = (int)pp.First()[PatientProtocol.PatientProtocolId]; } // STEP 3: Determine Pass or Fail, and create relevent records // If screening failed, create empty PatientProtocol record to track screening if (ScreeningFailed.Checked) { PatientProtocol ptProtocol = new PatientProtocol(); if (ptProtocolId.HasValue) { ptProtocol.Get(ptProtocolId.Value); } else { ptProtocol[PatientProtocol.PatientId] = ptId; ptProtocol[PatientProtocol.ProtocolId] = protocolId; ptProtocol[PatientProtocol.PtProtocolScreeningId] = PatientProtocolController.GenerateScreeningId(); ptProtocol.Save(); } PatientProtocolStatus status = new PatientProtocolStatus(); status[PatientProtocolStatus.PatientProtocolId] = ptProtocol[PatientProtocol.PatientProtocolId]; status[PatientProtocolStatus.PtProtocolStatus] = PatientProtocolController.ScreeningFailed; DateTime statusDate = DateTime.Today; if (!string.IsNullOrEmpty(ScreeningDate.Value) && DateTime.TryParse(ScreeningDate.Value, out statusDate)) { status[PatientProtocolStatus.PtProtocolStatusDate] = statusDate; status[PatientProtocolStatus.PtProtocolStatusDateText] = statusDate.ToShortDateString(); } else { status[PatientProtocolStatus.PtProtocolStatusDate] = DateTime.Today; status[PatientProtocolStatus.PtProtocolStatusDateText] = DateTime.Today.ToShortDateString(); } status[PatientProtocolStatus.PtProtocolStatusReason] = ReasonFailed.Value; status[PatientProtocolStatus.PtProtocolStatusNotes] = ReasonFailedNotes.Value; status.Save(); } // If screening passed, create necessary records else if (ScreeningPassed.Checked) { // Create PatientProtocol for Patient PatientProtocol ptProtocol = new PatientProtocol(); if (ptProtocolId.HasValue) { ptProtocol.Get(ptProtocolId.Value); bool doUpdate = false; // update/ insert fields if (ptProtocol.IsNull(PatientProtocol.PtProtocolStudyId)) { ptProtocol[PatientProtocol.PtProtocolStudyId] = StudyID.Value; doUpdate = true; } if (ptProtocol.IsNull(PatientProtocol.PtProtocolScreeningId)) { ptProtocol[PatientProtocol.PtProtocolScreeningId] = PatientProtocolController.GenerateScreeningId(); doUpdate = true; } if (doUpdate) { ptProtocol.Save(); } } else { ptProtocol[PatientProtocol.ProtocolId] = protocolId; ptProtocol[PatientProtocol.PatientId] = ptId; ptProtocol[PatientProtocol.PtProtocolScreeningId] = PatientProtocolController.GenerateScreeningId(); ptProtocol[PatientProtocol.PtProtocolStudyId] = StudyID.Value; ptProtocol.Save(); } // CREATE STUDY ID IDENTIFIER PatientProtocolController.CreateStudyIdIdentifier((int)ptProtocol[ptProtocol.PrimaryKeyName]); // Create Registration Record PatientProtocolRegistration registrationRecord = new PatientProtocolRegistration(); registrationRecord[PatientProtocolRegistration.PatientProtocolId] = ptProtocol[PatientProtocol.PatientProtocolId]; registrationRecord[PatientProtocolRegistration.ConsentedTo] = " "; registrationRecord.Save(); PatientProtocolStatus status = new PatientProtocolStatus(); status[PatientProtocolStatus.PatientProtocolId] = ptProtocol[PatientProtocol.PatientProtocolId]; status[PatientProtocolStatus.PtProtocolStatus] = PatientProtocolController.ScreeningPassed; DateTime statusDate = DateTime.Today; if (!string.IsNullOrEmpty(ScreeningDate.Value) && DateTime.TryParse(ScreeningDate.Value, out statusDate)) { status[PatientProtocolStatus.PtProtocolStatusDate] = statusDate; status[PatientProtocolStatus.PtProtocolStatusDateText] = statusDate.ToShortDateString(); } else { status[PatientProtocolStatus.PtProtocolStatusDate] = DateTime.Today; status[PatientProtocolStatus.PtProtocolStatusDateText] = DateTime.Today.ToShortDateString(); } status[PatientProtocolStatus.PtProtocolStatusNotes] = StudyNodes.Value; status.Save(); } // STEP 4: Register Client Script events // Save and Close if (sender == SaveBtn) { Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "saveAndClose", "saveAndClose();", true); } // Save and Load Registration Interface else if (sender == ContinueBtn) { string script = "if(parent.patientAssigned) { parent.patientAssigned('" + protocolId + "','','" + EncryptValue(ptId.ToString()) + "'); }"; Page.ClientScript.RegisterStartupScript(this.GetType(), "OnPatientAssigned", script, true); } ErrorMessage.Text = ""; } } catch (InvalidScreeningException invalidFormException) { ErrorMessage.Text = "Error: " + invalidFormException.Message; } }