/**
  * 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();
 }
 /**
  * Checks all existing tokens and updates any changes to the original rounds (when necessary)
  *
  * @param \Gems_Tracker_RespondentTrack $respTrack The respondent track to check
  * @param int $userId Id of the user who takes the action (for logging)
  * @return int The number of tokens changed by this code
  */
 protected function checkExistingRoundsFor(\Gems_Tracker_RespondentTrack $respTrack, $userId)
 {
     // Quote here, I like to keep bound parameters limited to the WHERE
     // Besides, these statements are not order dependent while parameters are and do not repeat
     $qOrgId = $this->db->quote($respTrack->getOrganizationId());
     $qRespId = $this->db->quote($respTrack->getRespondentId());
     $qTrackId = $this->db->quote($this->_trackId);
     $qUserId = $this->db->quote($userId);
     $respTrackId = $respTrack->getRespondentTrackId();
     $sql = "UPDATE gems__tokens, gems__rounds, gems__reception_codes\n            SET gto_id_respondent = {$qRespId},\n                gto_id_organization = {$qOrgId},\n                gto_id_track = {$qTrackId},\n                gto_id_survey = CASE WHEN gto_start_time IS NULL AND grc_success = 1 THEN gro_id_survey ELSE gto_id_survey END,\n                gto_round_order = gro_id_order,\n                gto_icon_file = gro_icon_file,\n                gto_round_description = gro_round_description,\n                gto_changed = CURRENT_TIMESTAMP,\n                gto_changed_by = {$qUserId}\n            WHERE gto_id_round = gro_id_round AND\n                gto_reception_code = grc_id_reception_code AND\n                gto_id_round != 0 AND\n                gro_active = 1 AND\n                (\n                    gto_id_respondent != {$qRespId} OR\n                    gto_id_organization != {$qOrgId} OR\n                    gto_id_track != {$qTrackId} OR\n                    gto_id_survey != CASE WHEN gto_start_time IS NULL AND grc_success = 1 THEN gro_id_survey ELSE gto_id_survey END OR\n                    gto_round_order != gro_id_order OR\n                    (gto_round_order IS NULL AND gro_id_order IS NOT NULL) OR\n                    (gto_round_order IS NOT NULL AND gro_id_order IS NULL) OR\n                    gto_icon_file != gro_icon_file OR\n                    (gto_icon_file IS NULL AND gro_icon_file IS NOT NULL) OR\n                    (gto_icon_file IS NOT NULL AND gro_icon_file IS NULL) OR\n                    gto_round_description != gro_round_description OR\n                    (gto_round_description IS NULL AND gro_round_description IS NOT NULL) OR\n                    (gto_round_description IS NOT NULL AND gro_round_description IS NULL)\n                ) AND\n                    gto_id_respondent_track = ?";
     $stmt = $this->db->query($sql, array($respTrackId));
     return $stmt->rowCount();
 }
 /**
  * Determines if this particular track should be included
  * in the report
  *
  * @param  \Gems_Tracker_RespondentTrack $track
  * @return boolean This dummy implementation always returns true
  */
 protected function _isTrackInFilter(\Gems_Tracker_RespondentTrack $track)
 {
     $result = false;
     $trackInfo = array('code' => $track->getCode(), 'trackid' => $track->getTrackId(), 'resptrackid' => $track->getRespondentTrackId(), 'respid' => $track->getRespondentId());
     if (empty($this->trackFilter)) {
         $result = true;
     } else {
         // Now read the filter and split by track code or track id
         foreach ($this->trackFilter as $filter) {
             $remaining = array_diff_assoc($filter, $trackInfo);
             if (empty($remaining)) {
                 $result = true;
                 break;
             }
         }
     }
     // Only if track has a success code
     if ($result && $track->getReceptionCode()->isSuccess()) {
         return true;
     }
     return false;
 }