Skip to content

Commit

Permalink
Fixed issue 19255: PHP8.1 + debug : survey with only stardate broke s…
Browse files Browse the repository at this point in the history
…urvey listing (#3629)
  • Loading branch information
Shnoulle committed Nov 27, 2023
1 parent ad88ae8 commit 45bc113
Showing 1 changed file with 35 additions and 36 deletions.
71 changes: 35 additions & 36 deletions application/models/Survey.php
Expand Up @@ -1096,19 +1096,13 @@ public function getState()
if ($this->active == 'N') {
return 'inactive';
}
if ($this->expires != '' || $this->startdate != '') {
// Time adjust
$sNow = date("Y-m-d H:i:s", strtotime((string) Yii::app()->getConfig('timeadjust'), strtotime(date("Y-m-d H:i:s"))));
$sStop = ($this->expires != '') ? date("Y-m-d H:i:s", strtotime((string) Yii::app()->getConfig('timeadjust'), strtotime($this->expires))) : null;
$sStart = ($this->startdate != '') ? date("Y-m-d H:i:s", strtotime((string) Yii::app()->getConfig('timeadjust'), strtotime($this->startdate))) : null;

// Time comparison
$oNow = new DateTime($sNow);
$oStop = empty($sStop) ? null : new DateTime($sStop);
$oStart = empty($sStart) ? null : new DateTime($sStart);

$bExpired = (!is_null($sStop) && $oStop < $oNow);
$bWillRun = (!is_null($sStart) && $oStart > $oNow);
if (!empty($this->expires) || !empty($this->startdate)) {
// Create DateTime for now, stop and start for date comparison
$oNow = self::shiftedDateTime("now");
$oStop = self::shiftedDateTime($this->expires);
$oStart = self::shiftedDateTime($this->startdate);
$bExpired = (!is_null($oStop) && $oStop < $oNow);
$bWillRun = (!is_null($oStart) && $oStart > $oNow);

if ($bExpired) {
return 'expired';
Expand All @@ -1117,7 +1111,7 @@ public function getState()
// And what happen if $sStop < $sStart : must return something other ?
return 'willRun';
}
if (!is_null($sStop)) {
if (!is_null($oStop)) {
return 'willExpire';
}
}
Expand All @@ -1132,12 +1126,9 @@ public function getState()
public function getIsDateExpired()
{
if (!empty($this->expires)) {
$sNow = date("Y-m-d H:i:s", strtotime((string) Yii::app()->getConfig('timeadjust'), strtotime(date("Y-m-d H:i:s"))));
$sStop = ($this->expires != '') ? date("Y-m-d H:i:s", strtotime((string) Yii::app()->getConfig('timeadjust'), strtotime($this->expires))) : $sNow;

$oNow = new DateTime($sNow);
$oStop = new DateTime($sStop);
return $oStop < $oNow;
$oNow = self::shiftedDateTime("now");
$oStop = self::shiftedDateTime($this->expires);
return !empty($oStop) && $oStop < $oNow;
}
return false;
}
Expand All @@ -1154,23 +1145,17 @@ public function getRunning()
// If the survey is not active, no date test is needed
if ($this->active === 'N') {
$running = '<a href="' . App()->createUrl('/surveyAdministration/view/surveyid/' . $this->sid) . '" class="survey-state" data-bs-toggle="tooltip" title="' . gT('Inactive') . '"><i class="ri-stop-fill text-secondary me-1"></i>' . gT('Inactive') . '</a>';
} elseif ($this->expires != '' || $this->startdate != '') {
// If it's active, then we check if not expired
// Time adjust
$sNow = date("Y-m-d H:i:s", strtotime((string) Yii::app()->getConfig('timeadjust'), strtotime(date("Y-m-d H:i:s"))));
$sStop = ($this->expires != '') ? date("Y-m-d H:i:s", strtotime((string) Yii::app()->getConfig('timeadjust'), strtotime($this->expires))) : null;
$sStart = ($this->startdate != '') ? date("Y-m-d H:i:s", strtotime((string) Yii::app()->getConfig('timeadjust'), strtotime($this->startdate))) : null;
} elseif (!empty($this->expires) || !empty($this->startdate)) {
// Create DateTime for now, stop and start for date comparison
$oNow = self::shiftedDateTime("now");
$oStop = self::shiftedDateTime($this->expires);
$oStart = self::shiftedDateTime($this->startdate);

// Time comparaison
$oNow = new DateTime($sNow);
$oStop = new DateTime($sStop);
$oStart = new DateTime($sStart);
$bExpired = (!is_null($oStop) && $oStop < $oNow);
$bWillRun = (!is_null($oStart) && $oStart > $oNow);

$bExpired = (!is_null($sStop) && $oStop < $oNow);
$bWillRun = (!is_null($sStart) && $oStart > $oNow);

$sStop = $sStop != null ? convertToGlobalSettingFormat($sStop) : null;
$sStart = convertToGlobalSettingFormat($sStart);
$sStop = !is_null($oStop) ? convertToGlobalSettingFormat($oStop->format('Y-m-d H:i:s')) : "";
$sStart = !is_null($oStart) ? convertToGlobalSettingFormat($oStart->format('Y-m-d H:i:s')) : "";

// Icon generaton (for CGridView)
$sIconRunNoEx = '<a href="' . App()->createUrl('/surveyAdministration/view/surveyid/' . $this->sid) . '" class="survey-state" data-bs-toggle="tooltip" title="' . gT('End: Never') . '"><i class="ri-play-fill text-primary me-1"></i>' . gT('End: Never') . '</a>';
Expand All @@ -1183,7 +1168,7 @@ public function getRunning()
// Expire prior to will start
$running = ($bExpired) ? $sIconExpired : $sIconFuture;
} else {
if (is_null($sStop)) {
if ($sStop === "") {
$running = $sIconRunNoEx;
} else {
$running = $sIconRunning;
Expand Down Expand Up @@ -2392,4 +2377,18 @@ public function checkExpireAfterStart($attributes, $params)
$this->addError('expires', gT("Expiration date can't be lower than the start date", 'unescaped'));
}
}

/**
* Get a dateime DB and return DateTime or null adjusted
* @var string|null $datetime in PHP datetime formats
* @return \DateTime|null
*/
private static function shiftedDateTime($datetime)
{
if (is_string($datetime) && strtotime($datetime)) {
$datetime = dateShift($datetime, "Y-m-d H:i:s", strval(Yii::app()->getConfig('timeadjust')));
return new DateTime($datetime);
}
return null;
}
}

0 comments on commit 45bc113

Please sign in to comment.