/**
  * Returns the data model based on the primary key given in the GET variable.
  * If the data model is not found, an HTTP exception will be raised.
  * @param integer $id the ID of the model to be loaded
  * @return SatisfactionData the loaded model
  * @throws CHttpException
  */
 public function loadModel($id)
 {
     $model = SatisfactionData::model()->findByPk($id);
     if ($model === null) {
         throw new CHttpException(404, 'The requested page does not exist.');
     }
     return $model;
 }
 public function actionSurvey($id)
 {
     $model = $this->loadModel($id);
     if (isset($_POST['SatisfactionData'])) {
         foreach ($_POST['SatisfactionData'] as $sid => $value) {
             $sd = SatisfactionData::model()->findByAttributes(array('family_id' => $id, 'satisfaction_item_id' => $sid));
             if (!$sd) {
                 $sd = new SatisfactionData();
             }
             $sd->attributes = $value;
             $sd->family_id = $id;
             $sd->satisfaction_item_id = $sid;
             $sd->save();
         }
     }
     if (isset($_POST['NeedData'])) {
         foreach ($_POST['NeedData'] as $nid => $value) {
             $nd = NeedData::model()->findByAttributes(array('family_id' => $id, 'need_id' => $nid));
             if (!$nd) {
                 $nd = new NeedData();
             }
             $nd->attributes = $value;
             $nd->family_id = $id;
             $nd->need_id = $nid;
             $nd->save();
         }
     }
     if (isset($_POST['AwarenessData'])) {
         Yii::trace("FC.survey awareness reached", 'application.controllers.FamilyController');
         $awarenessItems = AwarenessItem::model()->findAll();
         foreach ($awarenessItems as $aRow) {
             $aid = $aRow->id;
             $value = isset($_POST['AwarenessData'][$aid]) ? $_POST['AwarenessData'][$aid] : 1;
             $ad = AwarenessData::model()->findByAttributes(array('family_id' => $id, 'awareness_id' => $aid));
             if (!$ad) {
                 $ad = new AwarenessData();
             }
             $ad->value = $value;
             $ad->family_id = $id;
             $ad->awareness_id = $aid;
             $ad->save();
         }
     }
     if (isset($_POST['OpenData'])) {
         foreach ($_POST['OpenData'] as $qid => $value) {
             $od = OpenData::model()->findByAttributes(array('family_id' => $id, 'question_id' => $qid));
             if (!$od) {
                 $od = new OpenData();
             }
             $od->attributes = $value;
             $od->family_id = $id;
             $od->question_id = $qid;
             $od->save();
         }
     }
     $satisfactionItems = SatisfactionItem::model()->findAll();
     $needItems = NeedItem::model()->findAll();
     $openQuestions = OpenQuestion::model()->findAll(array('order' => 'seq'));
     $awarenessItems = AwarenessItem::model()->findAll();
     $this->render('survey', array('model' => $model, 'satisfactionItems' => $satisfactionItems, 'satisfactionData' => Families::satisfactionData($id), 'needItems' => $needItems, 'needData' => Families::needData($id), 'awarenessItems' => $awarenessItems, 'awarenessData' => Families::awarenessData($id), 'openQuestions' => $openQuestions));
 }
 public function actionSatisfaction()
 {
     $satisfactionItems = SatisfactionItem::model()->findAll();
     if (isset($_GET['satisfaction_item_id'])) {
         $satisfaction_item_id = $_GET['satisfaction_item_id'];
         Yii::trace("SR.satisfaction called for {$satisfaction_item_id}", 'application.controllers.SurveyReportsController');
         if (strlen($satisfaction_item_id) > 0) {
             $crit = array('satisfaction_item_id' => $satisfaction_item_id);
         } else {
             $crit = array();
         }
         $satisfactionDist = SatisfactionData::model()->findAllByAttributes($crit, array('select' => 'satisfaction_item_id, satisfaction_value, count(id) val_count', 'group' => 'satisfaction_item_id, satisfaction_value'));
         $this->renderPartial('satisfaction_report', array('satisfactionDist' => $satisfactionDist, 'satisfactionItems' => $satisfactionItems));
         return;
     }
     $this->render('satisfaction', array('satisfactionItems' => $satisfactionItems));
 }