/** * Get measure range with given patient patient details * * @return boolean */ public static function getRange($patient, $measureId) { $age = $patient->getAge('Y'); $measureRange = MeasureRange::where('measure_id', '=', $measureId)->where('age_min', '<=', $age)->where('age_max', '>=', $age); if (count($measureRange->get()) >= 1) { if (count($measureRange->get()) == 1) { $lowerUpper = $measureRange->first(); } else { if (count($measureRange->get()) > 1) { $measureRange = $measureRange->where('gender', '=', $patient->gender); if (count($measureRange->get()) == 1) { $lowerUpper = $measureRange->first(); } else { return null; } } } return "(" . $lowerUpper->range_lower . " - " . $lowerUpper->range_upper . ")"; } return null; }
/** * Update the specified resource in storage. * * @param int $id * @return Response */ public function update($measure) { foreach ($measure as $id => $data) { $measureTypeId = $data['measure_type_id']; $measure = Measure::find($id); $measure->name = trim($data['name']); $measure->measure_type_id = $measureTypeId; $measure->unit = $data['unit']; $measure->description = $data['description']; $measure->save(); if ($measureTypeId != Measure::FREETEXT) { if ($measureTypeId == Measure::NUMERIC) { $val['agemin'] = $data['agemin']; $val['agemax'] = $data['agemax']; $val['gender'] = $data['gender']; $val['rangemin'] = $data['rangemin']; $val['rangemax'] = $data['rangemax']; } else { $val['val'] = $data['val']; } $val['measurerangeid'] = $data['measurerangeid']; $val['interpretation'] = $data['interpretation']; $allRangeIDs = array(); for ($i = 0; $i < count(isset($data['agemin']) ? $val['agemin'] : $val['val']); $i++) { if ($val['measurerangeid'][$i] == 0) { $measurerange = new MeasureRange(); } else { $measurerange = MeasureRange::find($val['measurerangeid'][$i]); } $measurerange->measure_id = $measure->id; if ($measureTypeId == Measure::NUMERIC) { $measurerange->age_min = $val['agemin'][$i]; $measurerange->age_max = $val['agemax'][$i]; $measurerange->gender = $val['gender'][$i]; $measurerange->range_lower = $val['rangemin'][$i]; $measurerange->range_upper = $val['rangemax'][$i]; } else { $measurerange->alphanumeric = $val['val'][$i]; } $measurerange->interpretation = $val['interpretation'][$i]; $measurerange->save(); $allRangeIDs[] = $measurerange->id; } // Delete any pre-existing ranges for this measure_id that were not captured in the above loop. $allMeasureRanges = MeasureRange::where('measure_id', '=', $measure->id)->get(array('id')); $deleteRanges = array(); foreach ($allMeasureRanges as $key => $value) { if (!in_array($value->id, $allRangeIDs)) { $deleteRanges[] = $value->id; } } if (count($deleteRanges) > 0) { MeasureRange::destroy($deleteRanges); } } else { // Since this id has no ranges, delete any references to this id in the measure_range table MeasureRange::where('measure_id', '=', $measure->id)->delete(); } } }