public static List<PTProtocol> GetProtocolsForPatient(int patientID) { try { //Get all assigned protocols for a patient //TODO: needs testing //TODO: assumes only one doctor using (PTLinkDatabaseDataContext db = new PTLinkDatabaseDataContext()) { List<PTProtocol> protocols = new List<PTProtocol>(); PTProtocol protocol; PTExercise exercise; PTExerciseProgress progress; var dbProtocols = (from proto in db.Protocols where proto.PatientId == patientID select proto); if (dbProtocols != null) { foreach (Protocol dbProtocol in dbProtocols) { protocol = new PTProtocol(); protocol.name = dbProtocol.Exercises.FirstOrDefault().ProtocolTemplate.Name; protocol.ID = dbProtocol.Id; protocol.exercises = new List<PTExercise>(); protocol.doctor = new PTDoctor(); var dbDoctor = (from protoDXref in db.ProtocolDoctorXrefs join d in db.Persons on protoDXref.DoctorId equals d.Id where protoDXref.ProtocolId == protocol.ID select d).FirstOrDefault(); if (dbDoctor.Birthday.HasValue) { protocol.doctor.birthday = dbDoctor.Birthday.Value.ToShortDateString(); } if (dbDoctor.Division != null) { protocol.doctor.division = dbDoctor.Division.Name; if (dbDoctor.Division.Institution != null) { protocol.doctor.institution = dbDoctor.Division.Institution.Name; } } protocol.doctor.email = dbDoctor.User.Email; protocol.doctor.firstName = dbDoctor.FirstName; protocol.doctor.ID = dbDoctor.Id; protocol.doctor.lastName = dbDoctor.LastName; protocol.doctor.middleName = dbDoctor.MiddleName; foreach (Exercise dbExercise in dbProtocol.Exercises) { exercise = new PTExercise(); exercise.ID = dbExercise.Id; exercise.exerciseCategory = dbExercise.ExerciseTemplate.CategoryId; exercise.category = dbExercise.ExerciseTemplate.Category.Name; exercise.days = dbExercise.Days; exercise.specialInstructions = dbExercise.SpecialInstruction; exercise.value = dbExercise.Value; exercise.name = dbExercise.ExerciseTemplate.Name; if (dbExercise.EndDate.HasValue) { exercise.endTime = dbExercise.EndDate.Value; } if (dbExercise.StartDate.HasValue) { exercise.startTime = dbExercise.StartDate.Value; } //TODO: default values in general? dates? if (dbExercise.TimerDuration.HasValue) { exercise.timerDuration = dbExercise.TimerDuration.Value; } else { exercise.timerDuration = 0; } if (dbExercise.HasTimer.HasValue) { exercise.hasTimer = dbExercise.HasTimer.Value; } else { exercise.hasTimer = false; } if (dbExercise.RepetitionQuantity.HasValue) { exercise.repetitionQuantity = dbExercise.RepetitionQuantity.Value; } else { exercise.repetitionQuantity = 0; } if (dbExercise.SetQuantity.HasValue) { exercise.setQuantity = dbExercise.SetQuantity.Value; } //TODO: ignoring image and stuff for now //tmpExercise.imageURL; //tmpExercise.videoURL; exercise.progress = new List<PTExerciseProgress>(); foreach (ExerciseProgressXref dbProgress in dbExercise.ExerciseProgressXrefs) { progress = new PTExerciseProgress(); progress.ID = dbProgress.Id; progress.timestamp = dbProgress.Timestamp; progress.value = dbProgress.Progress.Id; exercise.progress.Add(progress); } foreach (Instruction instruction in dbExercise.ExerciseTemplate.Instructions) { exercise.instructions += instruction.Text; exercise.imageURL = instruction.InstructionImages.First().ImagePath; exercise.videoURL = instruction.VideoPath; } protocol.exercises.Add(exercise); } protocols.Add(protocol); } } return protocols; } } catch { return null; } }
public static PTPatient GetPatient(int doctorID, int patientID) { try { //TODO: get a specific patient with all their protocols, exercises, etc //TODO: needs testing using (PTLinkDatabaseDataContext db = new PTLinkDatabaseDataContext()) { PTPatient assignedPatient; PTProtocol tmpProtocol; PTExercise tmpExercise; PTExerciseProgress tmpProgress; var patient = (from doctorProtocols in db.ProtocolDoctorXrefs join protocols in db.Protocols on doctorProtocols.ProtocolId equals protocols.Id join patients in db.Persons on protocols.PatientId equals patients.Id where doctorProtocols.DoctorId == doctorID && patients.Id == patientID select patients).SingleOrDefault(); if (patient != null) { assignedPatient = new PTPatient(); if (patient.Birthday.HasValue) { assignedPatient.birthday = patient.Birthday.Value.ToShortDateString(); } assignedPatient.email = patient.User.Email; assignedPatient.firstName = patient.FirstName; assignedPatient.ID = patient.Id; assignedPatient.lastName = patient.LastName; assignedPatient.middleName = patient.MiddleName; assignedPatient.recentMessageCount = 0; //Check loads protocols and exercises assignedPatient.protocols = new List<PTProtocol>(); foreach (Protocol protocol in patient.Protocols) { tmpProtocol = new PTProtocol(); tmpProtocol.ID = protocol.Id; tmpProtocol.exercises = new List<PTExercise>(); tmpProtocol.name = protocol.Exercises.FirstOrDefault().ProtocolTemplate.Name; tmpProtocol.imageURL = protocol.ImagePath; //TODO: assuming exerciseCategory refers to CategoryId foreach (Exercise exercise in protocol.Exercises) { tmpExercise = new PTExercise(); tmpExercise.ID = exercise.Id; tmpExercise.exerciseCategory = exercise.ExerciseTemplate.CategoryId; tmpExercise.category = exercise.ExerciseTemplate.Category.Name; tmpExercise.days = exercise.Days; tmpExercise.specialInstructions = exercise.SpecialInstruction; tmpExercise.value = exercise.Value; tmpExercise.name = exercise.ExerciseTemplate.Name; if (exercise.EndDate.HasValue) { tmpExercise.endTime = exercise.EndDate.Value; } if (exercise.StartDate.HasValue) { tmpExercise.startTime = exercise.StartDate.Value; } //TODO: default values in general? if (exercise.TimerDuration.HasValue) { tmpExercise.timerDuration = exercise.TimerDuration.Value; } else { tmpExercise.timerDuration = 0; } if (exercise.HasTimer.HasValue) { tmpExercise.hasTimer = exercise.HasTimer.Value; } else { tmpExercise.hasTimer = false; } if (exercise.RepetitionQuantity.HasValue) { tmpExercise.repetitionQuantity = exercise.RepetitionQuantity.Value; } else { tmpExercise.repetitionQuantity = 0; } if (exercise.SetQuantity.HasValue) { tmpExercise.setQuantity = exercise.SetQuantity.Value; } tmpExercise.progress = new List<PTExerciseProgress>(); foreach (ExerciseProgressXref progress in exercise.ExerciseProgressXrefs) { tmpProgress = new PTExerciseProgress(); tmpProgress.ID = progress.Id; //TODO: what is Z expecting for the timestamp tmpProgress.timestamp = progress.Timestamp; tmpProgress.value = progress.Progress.Id; tmpExercise.progress.Add(tmpProgress); } //TODO: really there could be multiple images and stuff...blergh, just overwrite for now foreach (Instruction instruction in exercise.ExerciseTemplate.Instructions) { tmpExercise.instructions += instruction.Text; tmpExercise.imageURL = instruction.InstructionImages.First().ImagePath; tmpExercise.videoURL = instruction.VideoPath; } tmpProtocol.exercises.Add(tmpExercise); } assignedPatient.protocols.Add(tmpProtocol); } return assignedPatient; } } return null; } catch { return null; } }