コード例 #1
0
 /**
  * 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;
 }
コード例 #2
0
 /**
  * Creates the base model.
  *
  * @return \MUtil_Model_SelectModel
  */
 protected function createModel()
 {
     $groupby['period_1'] = new \Zend_Db_Expr("YEAR({$this->dateFrom})");
     $date = new \MUtil_Date();
     switch ($this->dateType) {
         case 'D':
             $keyCount = 1;
             $groupby['period_1'] = new \Zend_Db_Expr("CONVERT({$this->dateFrom}, DATE)");
             $date->setTime(0);
             $date->addDay($this->dateFactor - $this->dateRange);
             $start = $date->getIso();
             for ($i = -$this->dateRange; $i <= $this->dateRange; $i++) {
                 if (0 == $i) {
                     $this->dateCurrentStart = clone $date;
                     $this->dateCurrentEnd = clone $date;
                     $this->dateCurrentEnd->setTimeToDayEnd();
                 }
                 $values = array();
                 $values['period_1'] = $date->get('yyyy-MM-dd');
                 $values['range'] = $i;
                 $requiredRows[$i] = $values;
                 $date->addDay(1);
             }
             $date->subSecond(1);
             $end = $date->getIso();
             break;
         case 'W':
             $keyCount = 2;
             // Use MONDAY as start of week
             $groupby['period_1'] = new \Zend_Db_Expr("substr(YEARWEEK(gto_valid_from, 3),1,4)");
             //$groupby['period_1'] = new \Zend_Db_Expr("YEAR($this->dateFrom) - CASE WHEN WEEK($this->dateFrom, 1) = 0 THEN 1 ELSE 0 END");
             $groupby['period_2'] = new \Zend_Db_Expr("WEEK({$this->dateFrom}, 3)");
             $date->setWeekday(1);
             $date->setTime(0);
             $date->addWeek($this->dateFactor - $this->dateRange);
             $start = $date->getIso();
             for ($i = -$this->dateRange; $i <= $this->dateRange; $i++) {
                 if (0 == $i) {
                     $this->dateCurrentStart = clone $date;
                     $this->dateCurrentEnd = clone $date;
                     $this->dateCurrentEnd->addWeek(1)->subSecond(1);
                 }
                 $values = array();
                 $values['period_1'] = $date->get(\Zend_Date::YEAR);
                 $values['period_2'] = $date->get(\Zend_Date::WEEK);
                 // When monday is in the previous year, add one to the year
                 if ($date->get(\Zend_Date::DAY_OF_YEAR) > 14 && $date->get(\Zend_Date::WEEK) == 1) {
                     $values['period_1'] = $values['period_1'] + 1;
                 }
                 $values['range'] = $i;
                 $requiredRows[$i] = $values;
                 $date->addWeek(1);
             }
             $date->subSecond(1);
             $end = $date->getIso();
             break;
         case 'M':
             $keyCount = 2;
             $groupby['period_2'] = new \Zend_Db_Expr("MONTH({$this->dateFrom})");
             $date->setDay(1);
             $date->setTime(0);
             $date->addMonth($this->dateFactor - $this->dateRange);
             $start = $date->getIso();
             for ($i = -$this->dateRange; $i <= $this->dateRange; $i++) {
                 if (0 == $i) {
                     $this->dateCurrentStart = clone $date;
                     $this->dateCurrentEnd = clone $date;
                     $this->dateCurrentEnd->addMonth(1)->subSecond(1);
                 }
                 $values = array();
                 $values['period_1'] = $date->get(\Zend_Date::YEAR);
                 $values['period_2'] = $date->get(\Zend_Date::MONTH);
                 $values['range'] = $i;
                 $requiredRows[$i] = $values;
                 $date->addMonth(1);
             }
             $date->subSecond(1);
             $end = $date->getIso();
             break;
         case 'Y':
             $keyCount = 1;
             $date->setDay(1);
             $date->setMonth(1);
             $date->setTime(0);
             $date->addYear($this->dateFactor - $this->dateRange);
             $start = $date->getIso();
             for ($i = -$this->dateRange; $i <= $this->dateRange; $i++) {
                 if (0 == $i) {
                     $this->dateCurrentStart = clone $date;
                     $this->dateCurrentEnd = clone $date;
                     $this->dateCurrentEnd->addYear(1)->subSecond(1);
                 }
                 $values = array();
                 $values['period_1'] = $date->get(\Zend_Date::YEAR);
                 $values['range'] = $i;
                 $requiredRows[$i] = $values;
                 $date->addYear(1);
             }
             $date->subSecond(1);
             $end = $date->getIso();
             break;
         default:
             throw new \Gems_Exception_Coding('Incorrect date_type value: ' . $this->dateType);
     }
     $where = "{$this->dateFrom} BETWEEN '{$start}' AND '{$end}'";
     for ($i = -$this->dateRange; $i <= $this->dateRange; $i++) {
         $requiredRows[$i]['date_factor'] = $this->dateFactor + $i;
         $requiredRows[$i]['df_link'] = null;
         $requiredRows[$i]['df_label'] = null;
     }
     if ($this->dateRange > 0) {
         $requiredRows[-$this->dateRange]['df_link'] = $this->dateFactor - $this->dateRange * 2;
         $requiredRows[-$this->dateRange]['df_label'] = $this->_('<<');
         $requiredRows[$this->dateRange]['df_link'] = $this->dateFactor + $this->dateRange * 2;
         $requiredRows[$this->dateRange]['df_label'] = $this->_('>>');
         if ($this->dateRange > 1) {
             $i = intval($this->dateRange / 2);
             $requiredRows[-$i]['df_link'] = $this->dateFactor - 1;
             $requiredRows[-$i]['df_label'] = $this->_('<');
             $requiredRows[$i]['df_link'] = $this->dateFactor + 1;
             $requiredRows[$i]['df_label'] = $this->_('>');
         }
         $requiredRows[0]['df_link'] = $this->dateFactor ? '0' : null;
         $requiredRows[0]['df_label'] = $this->_('Now!');
     }
     if ($this->dateFactor) {
         $today = new \MUtil_Date();
         $this->dateFactorChanges['D'] = $this->dateCurrentStart->diffDays($today);
         $this->dateFactorChanges['W'] = $this->dateCurrentStart->diffWeeks($today);
         $this->dateFactorChanges['M'] = $this->dateCurrentStart->diffMonths($today);
         $this->dateFactorChanges['Y'] = $this->dateCurrentStart->diffYears($today);
     } else {
         $this->dateFactorChanges = array_fill_keys(array('D', 'W', 'M', 'Y'), 0);
     }
     // \MUtil_Echo::track($requiredRows);
     // \MUtil_Echo::rs($start, $end, $where);
     $select = new \Zend_Db_Select($this->db);
     $select->from($this->dataTableName, $groupby + $this->getDbFields());
     $select->where($where);
     $select->group($groupby);
     $this->processSelect($select);
     // \MUtil_Echo::r((string) $select);
     $model = new \MUtil_Model_SelectModel($select, $this->dataTableName);
     // Display by column cannot use formatFunction as it is a simple repeater
     // $model->set('duration_avg', 'formatFunction', $this->util->getLocalized()->formatNumber);
     $transformer = new \MUtil_Model_Transform_RequiredRowsTransformer();
     $transformer->setDefaultRow($this->getDefaultRow());
     $transformer->setRequiredRows($requiredRows);
     $transformer->setKeyItemCount($keyCount);
     $model->addTransformer($transformer);
     return $model;
 }