/** * adds a risk to the patient * * @param Risk $risk * @param string $other * @throws Exception */ public function addRisk(Risk $risk, $other = null, $comments = null) { if ($risk->name == 'Other') { if (!$other) { throw new Exception("No 'other' risk specified"); } } else { if (PatientRiskAssignment::model()->exists('patient_id=? and risk_id=?', array($this->id, $risk->id))) { throw new Exception("Patient is already assigned risk '{$risk->name}'"); } } $transaction = Yii::app()->db->beginTransaction(); try { $pra = new PatientRiskAssignment(); $pra->patient_id = $this->id; $pra->risk_id = $risk->id; $pra->comments = $comments; $pra->other = $other; if (!$pra->save()) { throw new Exception('Unable to add patient risk assignment: ' . print_r($pra->getErrors(), true)); } $this->audit('patient', 'add-risk'); if ($this->no_risks_date) { $this->no_risks_date = null; if (!$this->save()) { throw new Exception('Could not remove no risk flag: ' . print_r($this->getErrors(), true)); } } $this->audit('patient', 'remove-noriskdate'); $transaction->commit(); } catch (Exception $e) { $transaction->rollback(); throw $e; } }