public function copyToOrg($fromOrgId, $fromPid, $toOrgId, $toPid, $keepConsent = false)
    {
        // Maybe we should disable masking, just to be sure
        $this->currentUser->disableMask();

        // Do some sanity checks
        $fromPatient = $this->loadFirst(['gr2o_id_organization' => $fromOrgId, 'gr2o_patient_nr' => $fromPid]);
        if (empty($fromPatient)) {
            throw new \Gems_Exception($this->_('Respondent not found in sending organization.'));
        }

        $toPatientByPid        = $this->loadFirst(['gr2o_id_organization' => $toOrgId, 'gr2o_patient_nr' => $toPid]);
        $toPatientByRespondent = $this->loadFirst(['gr2o_id_organization' => $toOrgId, 'gr2o_id_user' => $fromPatient['gr2o_id_user']]);
        if (!empty($toPatientByPid) && $toPatientByPid['gr2o_id_user'] = $fromPatient['gr2o_id_user']) {
            // We already have the same respondent, nothing to do
            throw new \Gems_Exception($this->_('Respondent already exists in destination organization.'));
        }
        if (!empty($toPatientByPid) && empty($toPatientByRespondent)) {
            // Could be the same, or someone else... just return an error for now maybe offer to mark as duplicate and merge records later on
            throw new \Gems_Exception($this->_('Respondent with requested respondent number already exists in receiving organization.'));
        }
        if (!empty($toPatientByRespondent)) {
            // No action needed, maybe also report the number we found?
            throw new \Gems_Exception($this->_('Respondent already exists in destination organization, but with different respondent number.'));
        }

        // Ready to go, unset consent if needed
        $toPatient = $fromPatient;
        if (!$keepConsent) {
            unset($toPatient['gr2o_consent']);
        }

        // And save the record
        $toPatient['gr2o_patient_nr']      = $toPid;
        $toPatient['gr2o_id_organization'] = $toOrgId;
        $toPatient['gr2o_reception_code']  = \GemsEscort::RECEPTION_OK;

        $loginToSaveTable = false;
        if (isset($this->_saveTables['gems__user_logins'])) {
            $loginToSaveTable = $this->_saveTables['gems__user_logins'];
            unset($this->_saveTables['gems__user_logins']);
        }
        $result = $this->save($toPatient);

        if ($loginToSaveTable) {
            $this->_saveTables['gems__user_logins'] = $loginToSaveTable;
        }

        // Now re-enable the mask feature
        $this->currentUser->enableMask();

        return $result;
    }
public function copyToOrg($fromOrgId, $fromPid, $toOrgId, $toPid, $keepConsent = false)
    {
        // Maybe we should disable masking, just to be sure
        $this->currentUser->disableMask();

        // Do some sanity checks
        $fromPatient = $this->loadFirst(['gr2o_id_organization' => $fromOrgId, 'gr2o_patient_nr' => $fromPid]);
        if (empty($fromPatient)) {
            throw new \Gems_Exception($this->_('Respondent not found in sending organization.'));
        }

        $toPatientByPid        = $this->loadFirst(['gr2o_id_organization' => $toOrgId, 'gr2o_patient_nr' => $toPid]);
        $toPatientByRespondent = $this->loadFirst(['gr2o_id_organization' => $toOrgId, 'gr2o_id_user' => $fromPatient['gr2o_id_user']]);
        if (!empty($toPatientByPid) && $toPatientByPid['gr2o_id_user'] = $fromPatient['gr2o_id_user']) {
            // We already have the same respondent, nothing to do
            throw new \Gems_Exception($this->_('Respondent already exists in destination organization.'));
        }
        if (!empty($toPatientByPid) && empty($toPatientByRespondent)) {
            // Could be the same, or someone else... just return an error for now maybe offer to mark as duplicate and merge records later on
            throw new \Gems_Exception($this->_('Respondent with requested respondent number already exists in receiving organization.'));
        }
        if (!empty($toPatientByRespondent)) {
            // No action needed, maybe also report the number we found?
            throw new \Gems_Exception($this->_('Respondent already exists in destination organization, but with different respondent number.'));
        }

        // Ready to go, unset consent if needed
        $toPatient = $fromPatient;
        if (!$keepConsent) {
            unset($toPatient['gr2o_consent']);
        }

        // And save the record
        $toPatient['gr2o_patient_nr']      = $toPid;
        $toPatient['gr2o_id_organization'] = $toOrgId;
        $toPatient['gr2o_reception_code']  = \GemsEscort::RECEPTION_OK;

        $loginToSaveTable = false;
        if (isset($this->_saveTables['gems__user_logins'])) {
            $loginToSaveTable = $this->_saveTables['gems__user_logins'];
            unset($this->_saveTables['gems__user_logins']);
        }
        $result = $this->save($toPatient);

        if ($loginToSaveTable) {
            $this->_saveTables['gems__user_logins'] = $loginToSaveTable;
        }

        // Now re-enable the mask feature
        $this->currentUser->enableMask();

        return $result;
    }
public function copyToOrg ( $ fromOrgId , $ fromPid , $ toOrgId , $ toPid , $ keepConsent = false ) { // Maybe we should disable masking, just to be sure $ this -> currentUser -> disableMask ( ) ; // Do some sanity checks $ fromPatient = $ this -> loadFirst ( [ 'gr2o_id_organization' => $ fromOrgId , 'gr2o_patient_nr' => $ fromPid ] ) ; if ( empty ( $ fromPatient ) ) { throw new \ Gems_Exception ( $ this -> _ ( 'Respondent not found in sending organization.' ) ) ; } $ toPatientByPid = $ this -> loadFirst ( [ 'gr2o_id_organization' => $ toOrgId , 'gr2o_patient_nr' => $ toPid ] ) ; $ toPatientByRespondent = $ this -> loadFirst ( [ 'gr2o_id_organization' => $ toOrgId , 'gr2o_id_user' => $ fromPatient [ 'gr2o_id_user' ] ] ) ; if ( ! empty ( $ toPatientByPid ) && $ toPatientByPid [ 'gr2o_id_user' ] = $ fromPatient [ 'gr2o_id_user' ] ) { // We already have the same respondent, nothing to do throw new \ Gems_Exception ( $ this -> _ ( 'Respondent already exists in destination organization.' ) ) ; } if ( ! empty ( $ toPatientByPid ) && empty ( $ toPatientByRespondent ) ) { // Could be the same, or someone else... just return an error for now maybe offer to mark as duplicate and merge records later on throw new \ Gems_Exception ( $ this -> _ ( 'Respondent with requested respondent number already exists in receiving organization.' ) ) ; } if ( ! empty ( $ toPatientByRespondent ) ) { // No action needed, maybe also report the number we found? throw new \ Gems_Exception ( $ this -> _ ( 'Respondent already exists in destination organization, but with different respondent number.' ) ) ; } // Ready to go, unset consent if needed $ toPatient = $ fromPatient ; if ( ! $ keepConsent ) { unset ( $ toPatient [ 'gr2o_consent' ] ) ; } // And save the record $ toPatient [ 'gr2o_patient_nr' ] = $ toPid ; $ toPatient [ 'gr2o_id_organization' ] = $ toOrgId ; $ toPatient [ 'gr2o_reception_code' ] = \ GemsEscort :: RECEPTION_OK ; $ loginToSaveTable = false ; if ( isset ( $ this -> _saveTables [ 'gems__user_logins' ] ) ) { $ loginToSaveTable = $ this -> _saveTables [ 'gems__user_logins' ] ; unset ( $ this -> _saveTables [ 'gems__user_logins' ] ) ; } $ result = $ this -> save ( $ toPatient ) ; if ( $ loginToSaveTable ) { $ this -> _saveTables [ 'gems__user_logins' ] = $ loginToSaveTable ; } // Now re-enable the mask feature $ this -> currentUser -> enableMask ( ) ; return $ result ; }
Copy a respondent to a new organization

If you want to share a respondent(id) with another organization use this
method.

@param int $fromOrgId            Id of the sending organization
@param string $fromPid           Respondent number of the sending organization
@param int $toOrgId              Id of the receiving organization
@param string $toPid             Respondent number of the sending organization
@param bool $keepConsent         Should new organization inherit the consent of the old organization or not?
@return array The new respondent
@throws \Gems_Exception
Copy a respondent to a new organization