/**
  * Creates a model for getModel(). Called only for each new $action.
  *
  * The parameters allow you to easily adapt the model to the current action. The $detailed
  * parameter was added, because the most common use of action is a split between detailed
  * and summarized actions.
  *
  * @param boolean $detailed True when the current action is not in $summarizedActions.
  * @param string $action The current action.
  * @return \MUtil_Model_ModelAbstract
  */
 public function createModel($detailed, $action)
 {
     //
     $model = new \Gems_Model_JoinModel('resptrack', 'gems__respondent2track');
     $model->addTable('gems__respondent2org', array('gr2t_id_user' => 'gr2o_id_user', 'gr2t_id_organization' => 'gr2o_id_organization'));
     $model->addTable('gems__tracks', array('gr2t_id_track' => 'gtr_id_track'));
     $model->addTable('gems__reception_codes', array('gr2t_reception_code' => 'grc_id_reception_code'));
     $model->addFilter(array('grc_success' => 1));
     $model->resetOrder();
     $model->set('gr2o_patient_nr', 'label', $this->_('Respondent nr'));
     $model->set('gr2t_start_date', 'label', $this->_('Start date'), 'dateFormat', 'dd-MM-yyyy');
     $model->set('gr2t_end_date', 'label', $this->_('End date'), 'dateFormat', 'dd-MM-yyyy');
     $filter = $this->getSearchFilter($action !== 'export');
     if (!(isset($filter['gr2t_id_organization']) && $filter['gr2t_id_organization'])) {
         $model->addFilter(array('gr2t_id_organization' => $this->currentUser->getRespondentOrgFilter()));
     }
     if (!(isset($filter['gr2t_id_track']) && $filter['gr2t_id_track'])) {
         $model->setFilter(array('1=0'));
         $this->autofilterParameters['onEmpty'] = $this->_('No track selected...');
         return $model;
     }
     // Add the period filter - if any
     if ($where = \Gems_Snippets_AutosearchFormSnippet::getPeriodFilter($filter, $this->db)) {
         $model->addFilter(array($where));
     }
     $trackId = $filter['gr2t_id_track'];
     $engine = $this->loader->getTracker()->getTrackEngine($trackId);
     $engine->addFieldsToModel($model, false);
     return $model;
 }
 /**
  * Creates a model for getModel(). Called only for each new $action.
  *
  * The parameters allow you to easily adapt the model to the current action. The $detailed
  * parameter was added, because the most common use of action is a split between detailed
  * and summarized actions.
  *
  * @param boolean $detailed True when the current action is not in $summarizedActions.
  * @param string $action The current action.
  * @return \MUtil_Model_ModelAbstract
  */
 public function createModel($detailed, $action)
 {
     $model = new \Gems_Model_JoinModel('resptrack', 'gems__respondent2track');
     $model->addTable('gems__respondent2org', array('gr2t_id_user' => 'gr2o_id_user', 'gr2t_id_organization' => 'gr2o_id_organization'));
     $model->addTable('gems__tracks', array('gr2t_id_track' => 'gtr_id_track'));
     $model->addTable('gems__reception_codes', array('gr2t_reception_code' => 'grc_id_reception_code'));
     $model->addFilter(array('grc_success' => 1));
     $model->resetOrder();
     $model->set('gr2o_patient_nr', 'label', $this->_('Respondent nr'));
     $model->set('gr2t_start_date', 'label', $this->_('Start date'), 'dateFormat', 'dd-MM-yyyy');
     $model->set('gr2t_end_date', 'label', $this->_('End date'), 'dateFormat', 'dd-MM-yyyy');
     $filter = $this->getSearchFilter($action !== 'export');
     if (!(isset($filter['gr2t_id_organization']) && $filter['gr2t_id_organization'])) {
         $model->addFilter(array('gr2t_id_organization' => $this->currentUser->getRespondentOrgFilter()));
     }
     if (!(isset($filter['gr2t_id_track']) && $filter['gr2t_id_track'])) {
         $model->setFilter(array('1=0'));
         $this->autofilterParameters['onEmpty'] = $this->_('No track selected...');
         return $model;
     }
     // Add the period filter - if any
     if ($where = \Gems_Snippets_AutosearchFormSnippet::getPeriodFilter($filter, $this->db)) {
         $model->addFilter(array($where));
     }
     $select = $this->db->select();
     $select->from('gems__rounds', array('gro_id_round', 'gro_id_order', 'gro_round_description', 'gro_icon_file'))->joinInner('gems__surveys', 'gro_id_survey = gsu_id_survey', array('gsu_survey_name'))->where('gro_id_track = ?', $filter['gr2t_id_track'])->order('gro_id_order');
     if (isset($filter['gsu_id_primary_group']) && $filter['gsu_id_primary_group']) {
         $select->where('gsu_id_primary_group = ?', $filter['gsu_id_primary_group']);
     }
     $data = $this->db->fetchAll($select);
     if (!$data) {
         return $model;
     }
     $status = $this->util->getTokenData()->getStatusExpression();
     $select = $this->db->select();
     $select->from('gems__tokens', array('gto_id_respondent_track', 'gto_id_round', 'gto_id_token', 'status' => $status))->joinInner('gems__reception_codes', 'gto_reception_code = grc_id_reception_code', array())->where('gto_id_track = ?', $filter['gr2t_id_track'])->order('grc_success')->order('gto_id_respondent_track')->order('gto_round_order');
     // \MUtil_Echo::track($this->db->fetchAll($select));
     $newModel = new \MUtil_Model_SelectModel($select, 'tok');
     $newModel->setKeys(array('gto_id_respondent_track'));
     $transformer = new \MUtil_Model_Transform_CrossTabTransformer();
     $transformer->addCrosstabField('gto_id_round', 'status', 'stat_')->addCrosstabField('gto_id_round', 'gto_id_token', 'tok_');
     foreach ($data as $row) {
         $name = 'stat_' . $row['gro_id_round'];
         $transformer->set($name, 'label', \MUtil_Lazy::call('substr', $row['gsu_survey_name'], 0, 2), 'description', sprintf("%s\n[%s]", $row['gsu_survey_name'], $row['gro_round_description']), 'noSort', true, 'round', $row['gro_round_description'], 'roundIcon', $row['gro_icon_file']);
         $transformer->set('tok_' . $row['gro_id_round']);
     }
     $newModel->addTransformer($transformer);
     // \MUtil_Echo::track($data);
     $joinTrans = new \MUtil_Model_Transform_JoinTransformer();
     $joinTrans->addModel($newModel, array('gr2t_id_respondent_track' => 'gto_id_respondent_track'));
     $model->resetOrder();
     $model->set('gr2o_patient_nr');
     $model->set('gr2t_start_date');
     $model->addTransformer($joinTrans);
     return $model;
 }
 /**
  * Creates a model for getModel(). Called only for each new $action.
  *
  * The parameters allow you to easily adapt the model to the current action. The $detailed
  * parameter was added, because the most common use of action is a split between detailed
  * and summarized actions.
  *
  * @param boolean $detailed True when the current action is not in $summarizedActions.
  * @param string $action The current action.
  * @return \MUtil_Model_ModelAbstract
  */
 public function createModel($detailed, $action)
 {
     $filter = $this->getSearchFilter($action !== 'export');
     // Return empty model when no track sel;ected
     if (!(isset($filter['gtf_id_track']) && $filter['gtf_id_track'])) {
         $model = new \Gems_Model_JoinModel('trackfields', 'gems__track_fields');
         $model->set('gtf_field_name', 'label', $this->_('Name'));
         $model->setFilter(array('1=0'));
         $this->autofilterParameters['onEmpty'] = $this->_('No track selected...');
         return $model;
     }
     $this->trackId = $filter['gtf_id_track'];
     $tracker = $this->loader->getTracker();
     $this->engine = $tracker->getTrackEngine($this->trackId);
     $orgs = $this->currentUser->getRespondentOrgFilter();
     $this->orgWhere = " AND gr2t_id_organization IN (" . implode(", ", $orgs) . ")";
     $sql = "SELECT COUNT(*)\n            FROM gems__respondent2track INNER JOIN gems__reception_codes ON gr2t_reception_code = grc_id_reception_code\n            WHERE gr2t_id_track = ? AND grc_success = 1" . $this->orgWhere;
     $this->trackCount = $this->db->fetchOne($sql, $this->trackId);
     $model = $this->engine->getFieldsMaintenanceModel();
     //$model->setFilter($filter);
     // $model->addColumn(new \Zend_Db_Expr($trackCount), 'trackcount');
     // $model->addColumn(new \Zend_Db_Expr("(SELECT COUNT())"), 'fillcount');
     $model->set('trackcount', 'label', $this->_('Tracks'));
     $model->setOnLoad('trackcount', $this->trackCount);
     $model->set('fillcount', 'label', $this->_('Filled'));
     $model->setOnLoad('fillcount', array($this, 'fillCount'));
     $model->set('emptycount', 'label', $this->_('Empty'));
     $model->setOnLoad('emptycount', array($this, 'emptyCount'));
     $model->set('valuecount', 'label', $this->_('Unique values'));
     $model->setOnLoad('valuecount', array($this, 'valueCount'));
     return $model;
 }
 /**
  * Creates a model for getModel(). Called only for each new $action.
  *
  * The parameters allow you to easily adapt the model to the current action. The $detailed
  * parameter was added, because the most common use of action is a split between detailed
  * and summarized actions.
  *
  * @param boolean $detailed True when the current action is not in $summarizedActions.
  * @param string $action The current action.
  * @return \MUtil_Model_ModelAbstract
  */
 public function createModel($detailed, $action)
 {
     $select = $this->getSelect();
     // \MUtil_Model::$verbose = true;
     $model = new \MUtil_Model_SelectModel($select, 'summary');
     // Make sure of filter and sort for these fields
     $model->set('gro_id_order');
     $model->set('gsu_id_primary_group');
     $model->set('gto_id_track');
     $model->set('gto_id_organization');
     $model->resetOrder();
     $model->set('gro_round_description', 'label', $this->_('Round'));
     $model->set('gsu_survey_name', 'label', $this->_('Survey'));
     $model->set('answered', 'label', $this->_('Answered'), 'tdClass', 'centerAlign', 'thClass', 'centerAlign');
     $model->set('missed', 'label', $this->_('Missed'), 'tdClass', 'centerAlign', 'thClass', 'centerAlign');
     $model->set('open', 'label', $this->_('Open'), 'tdClass', 'centerAlign', 'thClass', 'centerAlign');
     $model->set('total', 'label', $this->_('Total'), 'tdClass', 'centerAlign', 'thClass', 'centerAlign');
     // $model->set('future',   'label', $this->_('Future'),   'tdClass', 'centerAlign', 'thClass', 'centerAlign');
     // $model->set('unknown',  'label', $this->_('Unknown'),  'tdClass', 'centerAlign', 'thClass', 'centerAlign');
     // $model->set('is',       'label', ' ',                  'tdClass', 'centerAlign', 'thClass', 'centerAlign');
     // $model->set('success',  'label', $this->_('Success'),    'tdClass', 'centerAlign', 'thClass', 'centerAlign');
     // $model->set('removed',  'label', $this->_('Removed'),  'tdClass', 'deleted centerAlign',
     //         'thClass', 'centerAlign');
     $model->set('gsu_id_primary_group', 'label', $this->_('Filler'), 'multiOptions', $this->util->getDbLookup()->getGroups());
     $filter = $this->getSearchFilter($action !== 'excel');
     if (!(isset($filter['gto_id_organization']) && $filter['gto_id_organization'])) {
         $model->addFilter(array('gto_id_organization' => $this->currentUser->getRespondentOrgFilter()));
     }
     if (isset($filter['gto_id_track']) && $filter['gto_id_track']) {
         // Add the period filter
         if ($where = \Gems_Snippets_AutosearchFormSnippet::getPeriodFilter($filter, $this->db)) {
             $select->joinInner('gems__respondent2track', 'gto_id_respondent_track = gr2t_id_respondent_track', array());
             $model->addFilter(array($where));
         }
     } else {
         $model->setFilter(array('1=0'));
         $this->autofilterParameters['onEmpty'] = $this->_('No track selected...');
     }
     return $model;
 }
 /**
  * Get the data to use for searching: the values passed in the request + any defaults
  * used in the search form (or any other search request mechanism).
  *
  * It does not return the actual filter used in the query.
  *
  * @see getSearchFilter()
  *
  * @param boolean $useRequest Use the request as source (when false, the session is used)
  * @return array
  */
 public function getSearchData($useRequest = true)
 {
     $data = parent::getSearchData($useRequest);
     if (isset($data[\MUtil_Model::REQUEST_ID2])) {
         $orgs = intval($data[\MUtil_Model::REQUEST_ID2]);
     } else {
         $orgs = $this->currentUser->getRespondentOrgFilter();
     }
     $activeTracks = $this->util->getTrackData()->getActiveTracks($orgs);
     if (isset($data['gr2t_id_track']) && $data['gr2t_id_track']) {
         switch ($data['gr2t_id_track']) {
             case 'show_without_track':
             case 'show_all':
             case 'show_with_track':
                 break;
             default:
                 if (!isset($activeTracks[$data['gr2t_id_track']])) {
                     $data['gr2t_id_track'] = 'show_with_track';
                 }
         }
     }
     $data['__active_tracks'] = $activeTracks;
     return $data;
 }
 /**
  * Get the filter to use with the model for searching
  *
  * @param boolean $useRequest Use the request as source (when false, the session is used)
  * @return array or false
  */
 public function getSearchFilter($useRequest = true)
 {
     $filter = parent::getSearchFilter($useRequest);
     unset($filter['AUTO_SEARCH_TEXT_BUTTON']);
     $where = \Gems_Snippets_AutosearchFormSnippet::getPeriodFilter($filter, $this->db, null, 'yyyy-MM-dd HH:mm:ss');
     if ($where) {
         $filter[] = $where;
     }
     if (!isset($filter['gto_id_organization'])) {
         $filter['gto_id_organization'] = $this->currentUser->getRespondentOrgFilter();
     }
     $filter['gsu_active'] = 1;
     $filter['grc_success'] = 1;
     // Overruled with success
     if (isset($filter['main_filter'])) {
         switch ($filter['main_filter']) {
             case 'answered':
                 $filter[] = 'gto_completion_time IS NOT NULL';
                 break;
             case 'hasnomail':
                 $filter[] = sprintf("(grs_email IS NULL OR grs_email = '' OR grs_email NOT RLIKE '%s') AND\n                                ggp_respondent_members = 1", str_replace('\'', '\\\'', trim(\MUtil_Validate_SimpleEmail::EMAIL_REGEX, '/')));
                 $filter[] = '(gto_valid_until IS NULL OR gto_valid_until >= CURRENT_TIMESTAMP)';
                 $filter['gto_completion_time'] = null;
                 break;
             case 'missed':
                 $filter[] = 'gto_valid_from <= CURRENT_TIMESTAMP';
                 $filter[] = 'gto_valid_until < CURRENT_TIMESTAMP';
                 $filter['gto_completion_time'] = null;
                 break;
             case 'notmailed':
                 $filter['gto_mail_sent_date'] = null;
                 $filter[] = '(gto_valid_until IS NULL OR gto_valid_until >= CURRENT_TIMESTAMP)';
                 $filter['gto_completion_time'] = null;
                 break;
             case 'open':
                 $filter['gto_completion_time'] = null;
                 $filter[] = 'gto_valid_from <= CURRENT_TIMESTAMP';
                 $filter[] = '(gto_valid_until >= CURRENT_TIMESTAMP OR gto_valid_until IS NULL)';
                 break;
                 // case 'other':
                 //    $filter[] = "grs_email IS NULL OR grs_email = '' OR ggp_respondent_members = 0";
                 //    $filter['can_email'] = 0;
                 //    $filter[] = '(gto_valid_until IS NULL OR gto_valid_until >= CURRENT_TIMESTAMP)';
                 //    break;
             // case 'other':
             //    $filter[] = "grs_email IS NULL OR grs_email = '' OR ggp_respondent_members = 0";
             //    $filter['can_email'] = 0;
             //    $filter[] = '(gto_valid_until IS NULL OR gto_valid_until >= CURRENT_TIMESTAMP)';
             //    break;
             case 'removed':
                 $filter['grc_success'] = 0;
                 break;
             case 'toanswer':
                 $filter[] = 'gto_completion_time IS NULL';
                 break;
             case 'tomail':
                 $filter[] = sprintf("grs_email IS NOT NULL AND\n                                grs_email != '' AND\n                                grs_email RLIKE '%s' AND\n                                ggp_respondent_members = 1", str_replace('\'', '\\\'', trim(\MUtil_Validate_SimpleEmail::EMAIL_REGEX, '/')));
                 //$filter[] = "grs_email IS NOT NULL AND grs_email != '' AND ggp_respondent_members = 1";
                 $filter['gto_mail_sent_date'] = null;
                 $filter[] = '(gto_valid_until IS NULL OR gto_valid_until >= CURRENT_TIMESTAMP)';
                 $filter['gto_completion_time'] = null;
                 break;
             case 'toremind':
                 // $filter['can_email'] = 1;
                 $filter[] = 'gto_mail_sent_date < CURRENT_TIMESTAMP';
                 $filter[] = '(gto_valid_until IS NULL OR gto_valid_until >= CURRENT_TIMESTAMP)';
                 $filter['gto_completion_time'] = null;
                 break;
             default:
                 break;
         }
         unset($filter['main_filter']);
     }
     return $filter;
 }