Ejemplo n.º 1
0
 /**
  * Calculate the until date for single survey insertion
  *
  * @param \MUtil_Date $from
  * @return \MUtil_Date
  */
 public function getInsertDateUntil(\MUtil_Date $from)
 {
     return Period::applyPeriod($from, $this->_gemsSurvey['gsu_valid_for_unit'], $this->_gemsSurvey['gsu_valid_for_length']);
 }
 /**
  * Calculate the field value using the current values
  *
  * @param array $currentValue The current value
  * @param array $fieldData The other known field values
  * @param array $trackData The currently available track data (track id may be empty)
  * @return mixed the new value
  */
 public function calculateFieldValue($currentValue, array $fieldData, array $trackData)
 {
     if ($currentValue || isset($this->_fieldDefinition['gtf_filter_id'])) {
         $agenda = $this->loader->getAgenda();
         if ($this->_lastActiveKey && isset($this->_fieldDefinition['gtf_filter_id'])) {
             $fromDate = false;
             $lastActive = self::$_lastActiveAppointment[$this->_lastActiveKey];
             if ($lastActive instanceof \Gems_Agenda_Appointment && $lastActive->isActive()) {
                 $fromDate = $lastActive->getAdmissionTime();
             }
             if (!$fromDate && isset($trackData['gr2t_start_date']) && $trackData['gr2t_start_date']) {
                 if ($trackData['gr2t_start_date'] instanceof \Zend_Date) {
                     $fromDate = $trackData['gr2t_start_date'];
                 } else {
                     $fromDate = new \MUtil_Date($trackData['gr2t_start_date'], \Gems_Tracker::DB_DATETIME_FORMAT);
                 }
                 // Always use start of the day for start date comparisons
                 $fromDate->setTime('00:00:00');
             }
             if ($fromDate instanceof \MUtil_Date) {
                 $select = $agenda->createAppointmentSelect(array('gap_id_appointment'));
                 $select->onlyActive()->forFilterId($this->_fieldDefinition['gtf_filter_id'])->forRespondent($trackData['gr2t_id_user'], $trackData['gr2t_id_organization']);
                 $minDate = Period::applyPeriod($fromDate, $this->_fieldDefinition['gtf_min_diff_unit'], $this->_fieldDefinition['gtf_min_diff_length']);
                 if ($this->_fieldDefinition['gtf_max_diff_exists']) {
                     $maxDate = Period::applyPeriod($fromDate, $this->_fieldDefinition['gtf_max_diff_unit'], $this->_fieldDefinition['gtf_max_diff_length']);
                 } else {
                     $maxDate = null;
                 }
                 if ($this->_fieldDefinition['gtf_min_diff_length'] > 0) {
                     $select->forPeriod($minDate, $maxDate, true);
                 } else {
                     $select->forPeriod($maxDate, $minDate, false);
                 }
                 if ($this->_fieldDefinition['gtf_uniqueness']) {
                     switch ($this->_fieldDefinition['gtf_uniqueness']) {
                         case 1:
                             // Track instances may link only once to an appointment
                             $select->uniqueInTrackInstance(self::$_lastActiveAppointmentIds[$this->_lastActiveKey]);
                             break;
                         case 2:
                             // Tracks of this type may link only once to an appointment
                             if (isset($trackData['gr2t_id_respondent_track'])) {
                                 $respTrackId = $trackData['gr2t_id_respondent_track'];
                             } else {
                                 $respTrackId = null;
                             }
                             $select->uniqueForTrackId($this->_trackId, $respTrackId, self::$_lastActiveAppointmentIds[$this->_lastActiveKey]);
                             break;
                             // default:
                     }
                 }
                 // Query ready
                 $newValue = $select->fetchOne();
                 if ($newValue) {
                     $currentValue = $newValue;
                 }
             }
         }
         if ($this->_lastActiveKey && $currentValue) {
             $appointment = $agenda->getAppointment($currentValue);
             if ($appointment->isActive()) {
                 self::$_lastActiveAppointment[$this->_lastActiveKey] = $appointment;
                 self::$_lastActiveAppointmentIds[$this->_lastActiveKey][$currentValue] = $currentValue;
             }
         }
     }
     return $currentValue;
 }
 /**
  *
  * @param \MUtil_Date $startDate
  * @param string $type
  * @param int $period
  * @return \MUtil_Date
  */
 protected function calculateFromDate($startDate, $type, $period)
 {
     return Period::applyPeriod($startDate, $type, $period);
 }