public function __construct(\Gems_Tracker_Survey $survey, \Gems_Tracker_Source_SourceInterface $source)
 {
     parent::__construct($survey->getName(), 'gems__tokens', 'gto');
     $this->addTable('gems__reception_codes', array('gto_reception_code' => 'grc_id_reception_code'));
     $this->addColumn('CASE WHEN grc_success = 1 AND gto_valid_from <= CURRENT_TIMESTAMP AND gto_completion_time IS NULL AND (gto_valid_until IS NULL OR gto_valid_until >= CURRENT_TIMESTAMP) THEN 1 ELSE 0 END', 'can_be_taken');
     $this->addColumn("CASE WHEN grc_success = 1 THEN '' ELSE 'deleted' END", 'row_class');
     $this->source = $source;
     $this->survey = $survey;
     $this->addAnswersToModel();
 }
 /**
  * Hook that loads the form data from $_POST or the model
  *
  * Or from whatever other source you specify here.
  */
 protected function loadFormData()
 {
     parent::loadFormData();
     $surveyId = $this->request->getParam(\MUtil_Model::REQUEST_ID);
     if (isset($this->formData['survey']) && $this->formData['survey'] && !$this->_survey instanceof \Gems_Tracker_Survey) {
         $this->_survey = $this->loader->getTracker()->getSurvey($this->formData['survey']);
     }
     if ($this->_survey instanceof \Gems_Tracker_Survey) {
         // Add (optional) survey specific translators
         $extraTrans = $this->importLoader->getAnswerImporters($this->_survey);
         if ($extraTrans) {
             $this->importTranslators = $extraTrans + $this->importTranslators;
             $this->_translatorDescriptions = false;
             $this->importModel->set('trans', 'multiOptions', $this->getTranslatorDescriptions());
         }
     }
     if ($this->_survey instanceof \Gems_Tracker_Survey) {
         $this->targetModel = $this->_survey->getAnswerModel($this->locale->toString());
         $this->importer->setTargetModel($this->targetModel);
         $source = $this->menu->getParameterSource();
         $source->offsetSet('gsu_has_pdf', $this->_survey->hasPdf() ? 1 : 0);
         $source->offsetSet('gsu_active', $this->_survey->isActive() ? 1 : 0);
     }
     // \MUtil_Echo::track($this->formData);
 }
 public function __construct(\Gems_Tracker_Survey $survey, \Gems_Tracker_Source_SourceInterface $source)
 {
     parent::__construct($survey->getName(), 'gems__tokens', 'gto');
     $this->addTable('gems__reception_codes', array('gto_reception_code' => 'grc_id_reception_code'));
     // Add relations
     $this->addLeftTable('gems__track_fields', array('gto_id_relationfield' => 'gtf_id_field', 'gtf_field_type = "relation"'));
     // Add relation fields
     $this->addLeftTable('gems__respondent_relations', array('gto_id_relation' => 'grr_id', 'gto_id_respondent' => 'grr_id_respondent'));
     // Add relation
     $this->set('grr_name', 'column_expression', new Zend_Db_Expr('CONCAT_WS(" ", gems__respondent_relations.grr_first_name, gems__respondent_relations.grr_last_name)'));
     $this->addColumn('CASE WHEN grc_success = 1 AND gto_valid_from <= CURRENT_TIMESTAMP AND gto_completion_time IS NULL AND (gto_valid_until IS NULL OR gto_valid_until >= CURRENT_TIMESTAMP) THEN 1 ELSE 0 END', 'can_be_taken');
     $this->addColumn("CASE WHEN grc_success = 1 THEN '' ELSE 'deleted' END", 'row_class');
     $this->source = $source;
     $this->survey = $survey;
     $this->addAnswersToModel();
 }
 /**
  * This transform function performs the actual save (if any) of the transformer data and is called after
  * the saving of the data in the source model.
  *
  * @param \MUtil_Model_ModelAbstract $model The parent model
  * @param array $row Array containing row
  * @return array Row array containing (optionally) transformed data
  */
 public function transformRowAfterSave(\MUtil_Model_ModelAbstract $model, array $row)
 {
     $token = $this->source->getToken($row['gto_id_token']);
     $answers = $row;
     $surveyId = $this->survey->getSurveyId();
     if ($this->source->setRawTokenAnswers($token, $answers, $surveyId)) {
         $this->changed++;
     }
     // No changes
     return $row;
 }
 /**
  * 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()
 {
     // Apply translations
     if (!$this->showAnswersTranslated) {
         // Here, not in e.g. __construct as these vars may be set during initiation
         $this->showAnswersNone = $this->_($this->showAnswersNone);
         $this->showAnswersRemoved = $this->_($this->showAnswersRemoved);
         $this->showAnswersSeparator = $this->_($this->showAnswersSeparator);
         $this->showAnswersTranslated = true;
     }
     // Overrule any setting of these values from source
     $this->data = null;
     $this->repeater = null;
     if (!$this->surveyId) {
         if ($this->token instanceof \Gems_Tracker_Token && $this->token->exists) {
             $this->surveyId = $this->token->getSurveyId();
         } elseif ($this->trackData && !$this->trackId) {
             // Look up key values from trackData
             if (isset($this->trackData['gsu_id_survey'])) {
                 $this->surveyId = $this->trackData['gsu_id_survey'];
             } elseif (isset($this->trackData['gro_id_survey'])) {
                 $this->surveyId = $this->trackData['gro_id_survey'];
             } elseif (!$this->trackId) {
                 if (isset($this->trackData['gtr_id_track'])) {
                     $this->trackId = $this->trackData['gtr_id_track'];
                 } elseif (isset($this->trackData['gro_id_track'])) {
                     $this->trackId = $this->trackData['gro_id_track'];
                 }
             }
         }
         if (!$this->trackId && $this->trackEngine) {
             $this->trackId = $this->trackEngine->getTrackId();
         }
         if ($this->trackId && !$this->surveyId) {
             // Use the track ID to get the id of the first active survey
             $this->surveyId = $this->db->fetchOne('SELECT gro_id_survey FROM gems__rounds WHERE gro_active = 1 AND gro_id_track = ? ORDER BY gro_id_order', $this->trackId);
         }
     }
     // \MUtil_Echo::track($this->surveyId, $this->trackId);
     // Get the survey
     if ($this->surveyId && !$this->survey instanceof \Gems_Tracker_Survey) {
         $this->survey = $this->loader->getTracker()->getSurvey($this->surveyId);
     }
     // Load the data
     if ($this->survey instanceof \Gems_Tracker_Survey && $this->survey->exists) {
         $this->data = \MUtil_Ra::addKey($this->survey->getQuestionInformation($this->locale->getLanguage()), 'key');
         //\MUtil_Echo::track($this->data);
     }
     return parent::hasHtmlOutput();
 }
 /**
  * Load the settings for the survey
  */
 protected function loadTrackSettings()
 {
     $respTracks = $this->tracker->getRespondentTracks($this->formData['gto_id_respondent'], $this->formData['gto_id_organization']);
     $tracks = array();
     foreach ($respTracks as $respTrack) {
         if ($respTrack instanceof \Gems_Tracker_RespondentTrack) {
             if ($respTrack->hasSuccesCode()) {
                 $tracks[$respTrack->getRespondentTrackId()] = substr(sprintf($this->_('%s - %s'), $respTrack->getTrackEngine()->getTrackName(), $respTrack->getFieldsInfo()), 0, 100);
             }
         }
     }
     if ($tracks) {
         if (!isset($this->formData['gto_id_track'])) {
             reset($tracks);
             $this->formData['gto_id_track'] = key($tracks);
         }
     } else {
         $this->addMessageInvalid($this->_('Survey insertion impossible: respondent has no track!'));
         $tracks = $this->util->getTranslated()->getEmptyDropdownArray();
     }
     asort($tracks);
     $model = $this->getModel();
     $model->set('gto_id_track', 'multiOptions', $tracks);
     if (count($tracks) === 1) {
         $model->set('gto_id_track', 'elementClass', 'Exhibitor');
     }
     if (isset($this->formData['gto_id_track'])) {
         $this->respondentTrack = $respTracks[$this->formData['gto_id_track']];
         // Add relation field when survey is not for staff
         if ($this->survey && $this->survey->isTakenByStaff() === false) {
             $engine = $this->respondentTrack->getTrackEngine();
             if (method_exists($engine, 'getRespondentRelationFields')) {
                 $empty = array('-1' => $this->_('Patient'));
                 $relations = $empty + $engine->getRespondentRelationFields();
                 $model->set('gto_id_relationfield', 'label', $this->_('Fill out by'), 'multiOptions', $relations, 'elementClass', 'Select', 'required', true);
             }
         }
     }
 }
 /**
  * Get a name for the view
  *
  * @param \Gems_Tracker_Survey $survey
  * @return string
  */
 protected function getViewName(\Gems_Tracker_Survey $survey)
 {
     return 'T' . $survey->getSurveyId();
 }
 /**
  * Returns a model for the survey answers
  *
  * @param \Gems_Tracker_Survey $survey
  * @param string $language Optional (ISO) language string
  * @param string $sourceSurveyId Optional Survey Id used by source
  * @return \MUtil_Model_ModelAbstract
  */
 public function getSurveyAnswerModel(\Gems_Tracker_Survey $survey, $language = null, $sourceSurveyId = null)
 {
     if (null === $sourceSurveyId) {
         $sourceSurveyId = $this->_getSid($survey->getSurveyId());
     }
     $surveyModel = $this->getSurvey($survey->getSurveyId(), $sourceSurveyId)->getModel();
     $model = new \OpenRosa_Tracker_Source_OpenRosa_Model($survey, $this);
     $questionsList = $this->getQuestionList($language, $survey->getSurveyId(), $sourceSurveyId);
     foreach ($questionsList as $item => $question) {
         $allOptions = $surveyModel->get($item);
         $allowed = array_fill_keys(array('storageFormat', 'dateFormat', 'label', 'multiOptions', 'maxlength', 'type', 'itemDisplay', 'formatFunction'), 1);
         $options = array_intersect_key($allOptions, $allowed);
         $options['label'] = strip_tags($question);
         $options['survey_question'] = true;
         //Should also do something to get the better titles...
         $model->set($item, $options);
     }
     return $model;
 }