/**
  * Called after the check that all required registry values
  * have been set correctly has run.
  *
  * @return void
  */
 public function afterRegistry()
 {
     parent::afterRegistry();
     $orgId = $this->respondent->getOrganizationId();
     // These values are set for the generic table snippet and
     // should be reset for this snippet
     $this->browse = false;
     $this->extraFilter = array("gtr_organizations LIKE '%|{$orgId}|%'");
     $this->menuEditActions = 'view';
     $this->menuShowActions = 'create';
 }
 /**
  * Get the respondent object
  *
  * @return \Gems_Tracker_Respondent
  */
 protected function getRespondent()
 {
     if (!$this->_respondent) {
         $patientNumber = $this->_getParam(\MUtil_Model::REQUEST_ID1);
         $organizationId = $this->_getParam(\MUtil_Model::REQUEST_ID2);
         $this->_respondent = $this->loader->getRespondent($patientNumber, $organizationId);
         if (!$this->_respondent->exists && $patientNumber && $organizationId) {
             throw new \Gems_Exception($this->_('Unknown respondent.'));
         }
         $this->_respondent->applyToMenuSource($this->menu->getParameterSource());
     }
     return $this->_respondent;
 }
 /**
  * Process the respondent and return true when data has changed.
  *
  * The event has to handle the actual storage of the changes.
  *
  * @param \Gems_Tracker_Respondent $respondent
  * @param int $userId The current user
  * @return boolean True when something changed
  */
 public function processChangedRespondent(\Gems_Tracker_Respondent $respondent)
 {
     $changes = 0;
     $tracker = $this->loader->getTracker();
     $respTracks = $tracker->getRespondentTracks($respondent->getId(), $respondent->getOrganizationId());
     $userId = $this->currentUser->getUserId();
     foreach ($respTracks as $respondentTrack) {
         if ($respondentTrack instanceof \Gems_Tracker_RespondentTrack) {
             $changes += $respondentTrack->checkTrackTokens($userId);
         }
     }
     // \MUtil_Echo::track('Hi there! ' . $changes);
     return (bool) $changes;
 }
 /**
  * Hook performing actual save
  *
  * @param string $newCode
  * @param int $userId
  * @return $changed
  */
 public function setReceptionCode($newCode, $userId)
 {
     $model = $this->getModel();
     $oldCode = $this->respondent->getReceptionCode()->getCode();
     $code = $model->setReceptionCode($this->formData['gr2o_patient_nr'], $this->formData['gr2o_id_organization'], $newCode, $userId, $oldCode);
     // Is the respondent really removed
     if ($code->isSuccess()) {
         $this->addMessage($this->_('Respondent restored.'));
         if ($this->formData['restore_tracks']) {
             $count = 0;
             $respTracks = $this->loader->getTracker()->getRespondentTracks($this->formData['grs_id_user'], $this->formData['gr2o_id_organization']);
             foreach ($respTracks as $respTrack) {
                 if ($respTrack instanceof \Gems_Tracker_RespondentTrack) {
                     if ($oldCode == $respTrack->getReceptionCode()->getCode()) {
                         $respTrack->setReceptionCode($code, null, $userId);
                         $count++;
                     }
                 }
             }
             $this->addMessage(sprintf($this->plural('Restored %d track.', 'Restored %d tracks.', $count), $count));
         }
     } else {
         // Perform actual save, but not simple stop codes.
         if ($code->isForRespondents()) {
             $this->addMessage($this->_('Respondent deleted.'));
             $this->afterSaveRouteKeys = false;
             $this->resetRoute = true;
             $this->routeAction = 'index';
         } else {
             // Just a stop code
             $this->addMessage(sprintf($this->plural('Stopped %d track.', 'Stopped %d tracks.', $count), $count));
         }
     }
     return 1;
 }
 /**
  * Overrule to implement snippet specific filtering and sorting.
  *
  * @param \MUtil_Model_ModelAbstract $model
  */
 protected function processFilterAndSort(\MUtil_Model_ModelAbstract $model)
 {
     $filter['gto_id_respondent'] = $this->respondent->getId();
     if (is_array($this->forOtherOrgs)) {
         $filter['gto_id_organization'] = $this->forOtherOrgs;
     } elseif (true !== $this->forOtherOrgs) {
         $filter['gto_id_organization'] = $this->respondent->getOrganizationId();
     }
     // Filter for valid track reception codes
     $filter[] = 'gr2t_reception_code IN (SELECT grc_id_reception_code FROM gems__reception_codes WHERE grc_success = 1)';
     $filter['grc_success'] = 1;
     // Active round
     // or
     // no round
     // or
     // token is success and completed
     $filter[] = 'gro_active = 1 OR gro_active IS NULL OR (grc_success=1 AND gto_completion_time IS NOT NULL)';
     $filter['gsu_active'] = 1;
     // NOTE! $this->model does not need to be the token model, but $model is a token model
     $tabFilter = $this->model->getMeta('tab_filter');
     if ($tabFilter) {
         $model->addFilter($tabFilter);
     }
     $model->addFilter($filter);
     // \MUtil_Echo::track($model->getFilter());
     $this->processSortOnly($model);
 }
 /**
  * Hook performing actual save
  *
  * @param string $newCode
  * @param int $userId
  * @return $changed
  */
 public function setReceptionCode($newCode, $userId)
 {
     $oldCode = $this->respondent->getReceptionCode();
     $code = $this->respondent->setReceptionCode($newCode);
     // Is the respondent really removed
     if ($code->isSuccess()) {
         $this->addMessage($this->_('Respondent restored.'));
         if ($this->formData['restore_tracks']) {
             $count = $this->respondent->restoreTracks($oldCode, $code);
             $this->addMessage(sprintf($this->plural('Restored %d track.', 'Restored %d tracks.', $count), $count));
         }
     } else {
         // Perform actual save, but not simple stop codes.
         if ($code->isForRespondents()) {
             $this->addMessage($this->_('Respondent deleted.'));
             $this->afterSaveRouteKeys = false;
             $this->resetRoute = true;
             $this->routeAction = 'index';
         } else {
             // Just a stop code
             $this->addMessage(sprintf($this->plural('Stopped %d track.', 'Stopped %d tracks.', $count), $count));
         }
     }
     return 1;
 }
 /**
  * Function used to fill the tab bar
  *
  * @return array tabId => label
  */
 protected function getTabs()
 {
     $user = $this->loader->getCurrentUser();
     $sql = "SELECT gr2o_id_organization, gr2o_patient_nr FROM gems__respondent2org WHERE gr2o_id_user = ?";
     $this->defaultTab = $user->getCurrentOrganizationId();
     $this->currentTab = $this->request->getParam(\MUtil_Model::REQUEST_ID2);
     $allowedOrgs = $user->getRespondentOrganizations();
     $existingOrgs = $this->db->fetchPairs($sql, $this->respondent->getId());
     $tabs = array();
     foreach ($allowedOrgs as $orgId => $name) {
         if (isset($existingOrgs[$orgId])) {
             $tabs[$orgId] = $name;
             $this->hrefs[$orgId] = array(\MUtil_Model::REQUEST_ID1 => $existingOrgs[$orgId], \MUtil_Model::REQUEST_ID2 => $orgId, 'RouteReset' => true);
         }
     }
     return $tabs;
 }
 /**
  * Get the respondent linked to this token
  *
  * @return \Gems_Tracker_Respondent
  */
 public function getRespondent()
 {
     $patientNumber = $this->getPatientNumber();
     $organizationId = $this->getOrganizationId();
     if (!$this->_respondentObject instanceof \Gems_Tracker_Respondent || $this->_respondentObject->getPatientNumber() !== $patientNumber || $this->_respondentObject->getOrganizationId() !== $organizationId) {
         $this->_respondentObject = $this->loader->getRespondent($patientNumber, $organizationId);
     }
     return $this->_respondentObject;
 }
 /**
  * The place to check if the data set in the snippet is valid
  * to generate the snippet.
  *
  * When invalid data should result in an error, you can throw it
  * here but you can also perform the check in the
  * checkRegistryRequestsAnswers() function from the
  * {@see \MUtil_Registry_TargetInterface}.
  *
  * @return boolean
  */
 public function hasHtmlOutput()
 {
     if ($this->respondent instanceof \Gems_Tracker_Respondent) {
         if (!$this->patientId) {
             $this->patientId = $this->respondent->getPatientNumber();
         }
         if (!$this->organizationId) {
             $this->organizationId = $this->respondent->getOrganizationId();
         }
     }
     // Try to get $this->respondentTrackId filled
     if (!$this->respondentTrackId) {
         if ($this->respondentTrack) {
             $this->respondentTrackId = $this->respondentTrack->getRespondentTrackId();
         } else {
             $this->respondentTrackId = $this->request->getParam(\Gems_Model::RESPONDENT_TRACK);
         }
     }
     // Try to get $this->respondentTrack filled
     if ($this->respondentTrackId && !$this->respondentTrack) {
         $this->respondentTrack = $this->loader->getTracker()->getRespondentTrack($this->respondentTrackId);
     }
     // Set the user id
     if (!$this->userId) {
         $this->userId = $this->loader->getCurrentUser()->getUserId();
     }
     if ($this->respondentTrack) {
         // We are updating
         $this->createData = false;
         // Try to get $this->trackEngine filled
         if (!$this->trackEngine) {
             // Set the engine used
             $this->trackEngine = $this->respondentTrack->getTrackEngine();
         }
     } else {
         // We are inserting
         $this->createData = true;
         $this->saveLabel = $this->_($this->_('Add track'));
         // Try to get $this->trackId filled
         if (!$this->trackId) {
             if ($this->trackEngine) {
                 $this->trackId = $this->trackEngine->getTrackId();
             } else {
                 $this->trackId = $this->request->getParam(\Gems_Model::TRACK_ID);
             }
         }
         // Try to get $this->trackEngine filled
         if ($this->trackId && !$this->trackEngine) {
             $this->trackEngine = $this->loader->getTracker()->getTrackEngine($this->trackId);
         }
         if (!($this->trackEngine && $this->patientId && $this->organizationId && $this->userId)) {
             throw new \Gems_Exception_Coding('Missing parameter for ' . __CLASS__ . ': could not find data for editing a respondent track nor the track engine, patientId and organizationId needed for creating one.');
         }
     }
     return parent::hasHtmlOutput();
 }
 /**
  * Should be called after answering the request to allow the Target
  * to check if all required registry values have been set correctly.
  *
  * @return boolean False if required are missing.
  */
 public function checkRegistryRequestsAnswers()
 {
     if (!$this->organizationId) {
         if ($this->respondent) {
             $this->organizationId = $this->respondent->getOrganizationId();
         }
         if (!$this->organizationId) {
             $this->organizationId = $this->request->getParam(\MUtil_Model::REQUEST_ID2);
         }
     }
     if ($this->organizationId && !$this->respondentId) {
         if ($this->respondent) {
             $this->respondentId = $this->respondent->getId();
         }
         if (!$this->respondentId) {
             $this->respondentId = $this->util->getDbLookup()->getRespondentId($this->request->getParam(\MUtil_Model::REQUEST_ID1), $this->organizationId);
         }
     }
     return $this->organizationId && $this->respondentId && $this->db && $this->model;
 }
 protected function loadExport()
 {
     $this->export = $this->loader->getRespondentExport();
     if ($this->token instanceof \Gems_Tracker_Token) {
         $this->addRespondent($this->token->getPatientNumber(), $this->token->getOrganizationId());
         $this->export->addRespondentTrackFilter($this->token->getRespondentTrackId());
         $this->export->addTokenFilter($this->token->getTokenId());
     } elseif ($this->respondentTrack instanceof \Gems_Tracker_RespondentTrack) {
         $this->addRespondent($this->respondentTrack->getPatientNumber(), $this->respondentTrack->getOrganizationId());
         $this->export->addRespondentTrackFilter($this->respondentTrack->getRespondentTrackId());
     } elseif ($this->respondent instanceof \Gems_Tracker_Respondent) {
         $this->addRespondent($this->respondent->getPatientNumber(), $this->respondent->getOrganizationId());
     }
 }
 /**
  * The place to check if the data set in the snippet is valid
  * to generate the snippet.
  *
  * When invalid data should result in an error, you can throw it
  * here but you can also perform the check in the
  * checkRegistryRequestsAnswers() function from the
  * {@see \MUtil_Registry_TargetInterface}.
  *
  * @return boolean
  */
 public function hasHtmlOutput()
 {
     if (!$this->multiTracks) {
         return false;
     }
     $this->tracker = $this->loader->getTracker();
     if (!$this->respondentTrackId) {
         $this->respondentTrackId = $this->request->getParam(\Gems_Model::RESPONDENT_TRACK);
     }
     if ($this->respondentTrackId) {
         if (!$this->respondentTrack instanceof \Gems_Tracker_RespondentTrack) {
             $this->respondentTrack = $this->tracker->getRespondentTrack($this->respondentTrackId);
         }
     }
     if ($this->respondentTrack instanceof \Gems_Tracker_RespondentTrack) {
         if (!$this->respondentTrackId) {
             $this->respondentTrackId = $this->respondentTrack->getRespondentTrackId();
         }
         $this->trackId = $this->respondentTrack->getTrackId();
         if (!$this->respondentId) {
             $this->respondentId = $this->respondentTrack->getRespondentId();
         }
         if (!$this->organizationId) {
             $this->organizationId = $this->respondentTrack->getOrganizationId();
         }
         $this->caption = $this->_('Other assignments of this track to this respondent.');
         $this->onEmpty = $this->_('This track is assigned only once to this respondent.');
     } else {
         if ($this->respondent instanceof \Gems_Tracker_Respondent) {
             if (!$this->respondentId) {
                 $this->respondentId = $this->respondent->getId();
             }
             if (!$this->organizationId) {
                 $this->organizationId = $this->respondent->getOrganizationId();
             }
         }
         $this->caption = $this->_('Existing assignments of this track to this respondent.');
         $this->onEmpty = $this->_('This track is not assigned to this respondent.');
     }
     if (!$this->trackId) {
         $this->trackId = $this->request->getParam(\Gems_Model::TRACK_ID);
     }
     if (!$this->trackId && $this->trackEngine instanceof \Gems_Tracker_Engine_TrackEngineInterface) {
         $this->trackId = $this->trackEngine->getTrackId();
     }
     return $this->trackId && $this->respondentId && $this->organizationId && parent::hasHtmlOutput();
 }
 /**
  * Get the respondent object
  *
  * @return \Gems_Tracker_Respondent
  */
 protected function getRespondent()
 {
     if (!$this->_respondent) {
         $id = $this->_getParam(\Gems_Model::APPOINTMENT_ID);
         if ($id && !($this->_getParam(\MUtil_Model::REQUEST_ID1) || $this->_getParam(\MUtil_Model::REQUEST_ID2))) {
             $appointment = $this->loader->getAgenda()->getAppointment($id);
             $this->_respondent = $appointment->getRespondent();
             if (!$this->_respondent->exists) {
                 throw new \Gems_Exception($this->_('Unknown respondent.'));
             }
             $this->_respondent->applyToMenuSource($this->menu->getParameterSource());
         } else {
             $this->_respondent = parent::getRespondent();
         }
     }
     return $this->_respondent;
 }
 /**
  * The place to check if the data set in the snippet is valid
  * to generate the snippet.
  *
  * When invalid data should result in an error, you can throw it
  * here but you can also perform the check in the
  * checkRegistryRequestsAnswers() function from the
  * {@see \MUtil_Registry_TargetInterface}.
  *
  * @return boolean
  */
 public function hasHtmlOutput()
 {
     if ($this->model) {
         $this->model->setIfExists('grs_email', 'itemDisplay', array('MUtil_Html_AElement', 'ifmail'));
         $this->model->setIfExists('gr2o_comments', 'rowspan', 2);
         if ($this->showConsentWarning && $this->model->has('gr2o_consent')) {
             $this->model->set('gr2o_consent', 'formatFunction', array($this, 'checkConsent'));
         }
         if (!$this->repeater) {
             if (!$this->respondent) {
                 $this->repeater = $this->model->loadRepeatable();
             } else {
                 $data = array($this->respondent->getArrayCopy());
                 $this->repeater = \MUtil_Lazy::repeat($data);
             }
         }
         return true;
     }
     return false;
 }
 /**
  * Creates the model
  *
  * @return \MUtil_Model_ModelAbstract
  */
 protected function createModel()
 {
     if ($this->model instanceof \Gems_Model_AppointmentModel) {
         $model = $this->model;
     } else {
         $model = $this->loader->getModels()->createAppointmentModel();
         $model->applyBrowseSettings();
     }
     $model->addColumn(new \Zend_Db_Expr("CONVERT(gap_admission_time, DATE)"), 'date_only');
     $model->set('date_only', 'formatFunction', array($this, 'formatDate'));
     // 'dateFormat', \Zend_Date::DAY_SHORT . ' ' . \Zend_Date::MONTH_NAME . ' ' . \Zend_Date::YEAR);
     $model->set('gap_admission_time', 'label', $this->_('Time'), 'formatFunction', array($this, 'formatTime'));
     // 'dateFormat', 'HH:mm ' . \Zend_Date::WEEKDAY);
     $this->_dateStorageFormat = $model->get('gap_admission_time', 'storageFormat');
     // $this->_timeImg           = \MUtil_Html::create('img', array('src' => 'stopwatch.png', 'alt' => ''));
     $model->set('gr2o_patient_nr', 'label', $this->_('Respondent nr'));
     if ($this->respondent instanceof \Gems_Tracker_Respondent) {
         $model->addFilter(array('gap_id_user' => $this->respondent->getId(), 'gap_id_organization' => $this->respondent->getOrganizationId()));
     }
     return $model;
 }
 /**
  * Get the respondent object
  *
  * @return \Gems_Tracker_Respondent
  */
 protected function getRespondent()
 {
     if (!$this->_respondent instanceof \Gems_Tracker_Respondent) {
         if ($this->_getParam(\MUtil_Model::REQUEST_ID1) && $this->_getParam(\MUtil_Model::REQUEST_ID2)) {
             $this->_respondent = parent::getRespondent();
         } else {
             $id = $this->_getParam(\MUtil_Model::REQUEST_ID);
             if ($id) {
                 $model = $this->getModel();
                 $row = $model->loadFirst(array('grco_id_action' => $id));
                 if ($row) {
                     $this->_respondent = $this->loader->getRespondent($row['gr2o_patient_nr'], $row['gr2o_id_organization']);
                     if (!$this->_respondent->exists && $patientNumber && $organizationId) {
                         throw new \Gems_Exception($this->_('Unknown respondent.'));
                     }
                     $this->_respondent->applyToMenuSource($this->menu->getParameterSource());
                 }
             }
         }
     }
     return $this->_respondent;
 }
 /**
  * Get the respondent mailfields
  * @return array
  */
 protected function getRespondentMailfields()
 {
     if ($this->respondent) {
         $result = array();
         $result['bcc'] = $this->mailFields['project_bcc'];
         $result['email'] = $this->respondent->getEmailAddress();
         $result['from'] = '';
         $result['first_name'] = $this->respondent->getFirstName();
         $result['full_name'] = $this->respondent->getFullName();
         $result['greeting'] = $this->respondent->getGreeting();
         $result['last_name'] = $this->respondent->getLastName();
         $result['name'] = $this->respondent->getName();
     } else {
         $result = array('email' => '', 'from' => '', 'first_name' => '', 'full_name' => '', 'greeting' => '', 'last_name' => '', 'name' => '');
     }
     $result['reply_to'] = $result['from'];
     $result['to'] = $result['email'];
     $result['reset_ask'] = '';
     if ($this->mailFields['organization_login_url']) {
         $result['reset_ask'] = $this->mailFields['organization_login_url'] . '/index/resetpassword';
     }
     return $result;
 }