/**
  *
  * @return \Gems_Task_TaskRunnerBatch
  */
 protected function getExportBatch($load = true)
 {
     if ($this->_batch) {
         return $this->_batch;
     }
     $this->_batch = $this->loader->getTaskRunnerBatch('track_export_' . $this->trackEngine->getTrackId());
     if (!$load || $this->_batch->isFinished()) {
         return $this->_batch;
     }
     if (!$this->_batch->isLoaded()) {
         $filename = \MUtil_File::createTemporaryIn(GEMS_ROOT_DIR . '/var/tmp/export/track');
         $trackId = $this->trackEngine->getTrackId();
         $this->_batch->setSessionVariable('filename', $filename);
         $this->_batch->addTask('Tracker\\Export\\MainTrackExportTask', $this->trackEngine->getTrackId(), $this->formData['orgs']);
         // \MUtil_Echo::track($this->formData['fields']);
         foreach ($this->formData['fields'] as $fieldId) {
             $this->_batch->addTask('Tracker\\Export\\TrackFieldExportTask', $trackId, $fieldId);
         }
         $model = $this->getModel();
         foreach ($model->getCol('surveyId') as $surveyId) {
             $this->_batch->addTask('Tracker\\Export\\TrackSurveyExportTask', $trackId, $surveyId);
         }
         foreach ($this->formData['rounds'] as $roundId) {
             $this->_batch->addTask('Tracker\\Export\\TrackRoundExportTask', $trackId, $roundId);
         }
     } else {
         $filename = $this->_batch->getSessionVariable('filename');
     }
     $this->_batch->setVariable('file', fopen($filename, 'a'));
     return $this->_batch;
 }
 /**
  * Updates the gems__tokens table so all tokens stick to the (possibly) new token name rules.
  *
  * @param int $userId    Id of the user who takes the action (for logging)
  * @return int The number of tokens changed
  */
 protected function updateTokens($userId, $updateTokens = true)
 {
     $tokenLib = $this->tracker->getTokenLibrary();
     $sql = 'UPDATE gems__tokens
                 SET gto_id_token = ' . $this->_getTokenFromToSql($tokenLib->getFrom(), $tokenLib->getTo(), 'gto_id_token') . ',
                     gto_changed = CURRENT_TIMESTAMP,
                     gto_changed_by = ' . $this->_gemsDb->quote($userId) . '
                 WHERE ' . $this->_getTokenFromSqlWhere($tokenLib->getFrom(), 'gto_id_token') . ' AND
                     gto_id_survey IN (SELECT gsu_id_survey FROM gems__surveys WHERE gsu_id_source = ' . $this->_gemsDb->quote($this->getId()) . ')';
     return $this->_gemsDb->query($sql)->rowCount();
 }
 /**
  * Add a message to the stack
  *
  * @param string $message
  */
 protected function addMessage($message)
 {
     $this->_batch->addMessage($message);
 }
 /**
  * Check for the existence of all tokens and create them otherwise
  *
  * @param \Gems_Tracker_RespondentTrack $respTrack The respondent track to check
  * @param int $userId Id of the user who takes the action (for logging)
  * @param \Gems_Task_TaskRunnerBatch $changes batch for counters
  */
 public function checkRoundsFor(\Gems_Tracker_RespondentTrack $respTrack, $userId, \Gems_Task_TaskRunnerBatch $batch = null)
 {
     if (null === $batch) {
         $batch = new \Gems_Task_TaskRunnerBatch('tmp-tack-' . $respTrack->getRespondentTrackId());
     }
     // Step one: update existing tokens
     $i = $batch->addToCounter('roundChangeUpdates', $this->checkExistingRoundsFor($respTrack, $userId));
     $batch->setMessage('roundChangeUpdates', sprintf($this->_('Round changes propagated to %d tokens.'), $i));
     // Step two: deactivate inactive rounds
     $i = $batch->addToCounter('deletedTokens', $this->removeInactiveRounds($respTrack, $userId));
     $batch->setMessage('deletedTokens', sprintf($this->_('%d tokens deleted by round changes.'), $i));
     // Step three: create lacking tokens
     $i = $batch->addToCounter('createdTokens', $this->addNewTokens($respTrack, $userId));
     $batch->setMessage('createdTokens', sprintf($this->_('%d tokens created to by round changes.'), $i));
     // Step four: set the dates and times
     //$changed = $this->checkTokensFromStart($respTrack, $userId);
     $changed = $respTrack->checkTrackTokens($userId);
     $ica = $batch->addToCounter('tokenDateCauses', $changed ? 1 : 0);
     $ich = $batch->addToCounter('tokenDateChanges', $changed);
     $batch->setMessage('tokenDateChanges', sprintf($this->_('%2$d token date changes in %1$d tracks.'), $ica, $ich));
     $i = $batch->addToCounter('checkedRespondentTracks');
     $batch->setMessage('checkedRespondentTracks', sprintf($this->_('Checked %d tracks.'), $i));
 }