/**
  * Save a single model item.
  *
  * @param array $newValues The values to store for a single model item.
  * @param array $filter If the filter contains old key values these are used
  * to decide on update versus insert.
  * @return array The values as they are after saving (they may change).
  */
 public function save(array $newValues, array $filter = null, array $saveTables = null)
 {
     // If the respondent id is not set, check using the
     // patient number and then the ssn
     if (!isset($newValues['grs_id_user'])) {
         $id = false;
         if (isset($newValues['gr2o_patient_nr'], $newValues['gr2o_id_organization'])) {
             $sql = 'SELECT gr2o_id_user
                     FROM gems__respondent2org
                     WHERE gr2o_patient_nr = ? AND gr2o_id_organization = ?';
             $id = $this->db->fetchOne($sql, array($newValues['gr2o_patient_nr'], $newValues['gr2o_id_organization']));
         }
         if (!$id && isset($newValues['grs_ssn']) && $this->hashSsn !== \Gems_Model_RespondentModel::SSN_HIDE) {
             if (\Gems_Model_RespondentModel::SSN_HASH === $this->hashSsn) {
                 $search = $this->saveSSN($newValues['grs_ssn']);
             } else {
                 $search = $newValues['grs_ssn'];
             }
             $sql = 'SELECT grs_id_user FROM gems__respondents WHERE grs_ssn = ?';
             $id = $this->db->fetchOne($sql, $search);
             // Check for change in patient ID
             if ($id && isset($newValues['gr2o_id_organization'])) {
                 $sql = 'SELECT gr2o_patient_nr
                         FROM gems__respondent2org
                         WHERE gr2o_id_user = ? AND gr2o_id_organization = ?';
                 $patientId = $this->db->fetchOne($sql, array($id, $newValues['gr2o_id_organization']));
                 if ($patientId) {
                     $copyId = $this->getKeyCopyName('gr2o_patient_nr');
                     $newValues[$copyId] = $patientId;
                 }
             }
         }
         if ($id) {
             $newValues['grs_id_user'] = $id;
             $newValues['gr2o_id_user'] = $id;
         }
     }
     $result = parent::save($newValues, $filter, $saveTables);
     if (isset($result['gr2o_id_organization']) && isset($result['grs_id_user'])) {
         // Tell the organization it has at least one user
         $org = $this->loader->getOrganization($result['gr2o_id_organization']);
         if ($org) {
             $org->setHasRespondents($this->loader->getCurrentUser()->getUserId());
         }
     }
     return $result;
 }
 /**
  * Save a single model item.
  *
  * @param array $newValues The values to store for a single model item.
  * @param array $filter If the filter contains old key values these are used
  * to decide on update versus insert.
  * @return array The values as they are after saving (they may change).
  */
 public function save(array $newValues, array $filter = null)
 {
     $keys = $this->getKeys();
     // This is the only key to save on, no matter
     // the keys used to initiate the model.
     $this->setKeys($this->_getKeysFor('gems__respondent2track'));
     // Change the end date until the end of the day
     if (isset($newValues['gr2t_end_date']) && $newValues['gr2t_end_date']) {
         $displayFormat = $this->get('gr2t_end_date', 'dateFormat');
         if (!$displayFormat) {
             $displayFormat = \MUtil_Model_Bridge_FormBridge::getFixedOption('date', 'dateFormat');
         }
         // Of course do not do so when we got a time format
         if (!\MUtil_Date_Format::getTimeFormat($displayFormat)) {
             $newValues['gr2t_end_date'] = new \MUtil_Date($newValues['gr2t_end_date'], $displayFormat);
             $newValues['gr2t_end_date']->setTimeToDayEnd();
         }
     }
     $newValues = parent::save($newValues, $filter);
     $this->setKeys($keys);
     return $newValues;
 }
 /**
  * Called after the check that all required registry values
  * have been set correctly has run.
  *
  * @return void
  */
 public function afterRegistry()
 {
     parent::afterRegistry();
     //If we are allowed to see who filled out a survey, modify the model accordingly
     if ($this->currentUser->hasPrivilege('pr.respondent.who')) {
         $this->addLeftTable('gems__staff', array('gto_by' => 'gems__staff_2.gsf_id_user'));
         $this->addColumn(new \Zend_Db_Expr('CASE
             WHEN gems__staff_2.gsf_id_user IS NULL THEN COALESCE(gems__track_fields.gtf_field_name, gems__groups.ggp_name)
             ELSE COALESCE(CONCAT_WS(
                 " ",
                 CONCAT(COALESCE(gems__staff_2.gsf_last_name, "-"), ","),
                 gems__staff_2.gsf_first_name,
                 gems__staff_2.gsf_surname_prefix
                 ))
             END'), 'ggp_name');
     } else {
         $this->set('ggp_name', 'column_expression', new \Zend_Db_Expr('COALESCE(gems__track_fields.gtf_field_name, gems__groups.ggp_name)'));
     }
     if ($this->currentUser->hasPrivilege('pr.respondent.result')) {
         $this->addColumn('gto_result', 'calc_result', 'gto_result');
     } else {
         $this->addColumn(new \Zend_Db_Expr('NULL'), 'calc_result', 'gto_result');
     }
 }