/**
  * Checks whether the survey for this token was completed and processes the result
  *
  * @param int $userId The id of the gems user
  * @return int self::COMPLETION_NOCHANGE || (self::COMPLETION_DATACHANGE | self::COMPLETION_EVENTCHANGE)
  */
 public function checkTokenCompletion($userId)
 {
     $result = self::COMPLETION_NOCHANGE;
     // Some defaults
     $values['gto_completion_time'] = null;
     $values['gto_duration_in_sec'] = null;
     $values['gto_result'] = null;
     if ($this->inSource()) {
         $survey = $this->getSurvey();
         $values['gto_in_source'] = 1;
         $startTime = $survey->getStartTime($this);
         if ($startTime instanceof \MUtil_Date) {
             // Value from source overrules any set date time
             $values['gto_start_time'] = $startTime->toString(\Gems_Tracker::DB_DATETIME_FORMAT);
         } else {
             // Otherwise use the time kept by Gems.
             $startTime = $this->getDateTime('gto_start_time');
             //What if we have older tokens... where no gto_start_time was set??
             if (is_null($startTime)) {
                 $startTime = new \MUtil_Date();
             }
             // No need to set $values['gto_start_time'], it does not change
         }
         // If there is a start date there can be a completion date
         if ($startTime instanceof \MUtil_Date) {
             if ($survey->isCompleted($this)) {
                 $complTime = $survey->getCompletionTime($this);
                 $setCompletionTime = true;
                 if (!$complTime instanceof \MUtil_Date) {
                     // Token is completed but the source cannot tell the time
                     //
                     // Try to see it was stored already
                     $complTime = $this->getDateTime('gto_completion_time');
                     if ($complTime instanceof \MUtil_Date) {
                         // Again no need to change a time that did not change
                         unset($values['gto_completion_time']);
                         $setCompletionTime = false;
                     } else {
                         // Well anyhow it was completed now or earlier. Get the current moment.
                         $complTime = new \MUtil_Date();
                     }
                 }
                 //Set completion time for completion event
                 if ($setCompletionTime) {
                     $values['gto_completion_time'] = $complTime->toString(\Gems_Tracker::DB_DATETIME_FORMAT);
                     //Save the old value
                     $originalCompletionTime = $this->_gemsData['gto_completion_time'];
                     $this->_gemsData['gto_completion_time'] = $values['gto_completion_time'];
                 }
                 // Process any Gems side survey dependent changes
                 if ($changed = $this->handleAfterCompletion()) {
                     // Communicate change
                     $result += self::COMPLETION_EVENTCHANGE;
                     if (\Gems_Tracker::$verbose) {
                         \MUtil_Echo::r($changed, 'Source values for ' . $this->_tokenId . ' changed by event.');
                     }
                 }
                 if ($setCompletionTime) {
                     //Reset to old value, so changes will be picked up
                     $this->_gemsData['gto_completion_time'] = $originalCompletionTime;
                 }
                 $values['gto_duration_in_sec'] = max($complTime->diffSeconds($startTime), 0);
                 //If the survey has a resultfield, store it
                 if ($resultField = $survey->getResultField()) {
                     $rawAnswers = $this->getRawAnswers();
                     if (isset($rawAnswers[$resultField])) {
                         // Cast to string, because that is the way the result is stored in the db
                         // not casting to strings means e.g. float results always result in
                         // an update, even when they did not change.
                         $values['gto_result'] = (string) $rawAnswers[$resultField];
                         // Chunk of text that is too long
                         if ($len = $this->_getResultFieldLength()) {
                             $values['gto_result'] = substr($values['gto_result'], 0, $len);
                         }
                     }
                 }
                 if ($this->project->hasResponseDatabase()) {
                     $this->toResponseDatabase($userId);
                 }
             }
         }
     } else {
         $values['gto_in_source'] = 0;
         $values['gto_start_time'] = null;
     }
     if ($this->_updateToken($values, $userId)) {
         // Communicate change
         $result += self::COMPLETION_DATACHANGE;
     }
     return $result;
 }
Example #2
0
 /**
  * @param string $firstDate
  * @param string $firstFormat
  * @param string $secondDate
  * @param string $secondFormat
  * @param int $expectedDiffDays
  * @param int $expectedDiffSeconds
  *
  * @dataProvider providerTestTimeZones
  */
 public function testDiffTimeZones($firstDate, $firstFormat, $secondDate, $secondFormat, $expectedDiffDays, $expectedDiffSeconds)
 {
     $date = new \MUtil_Date($firstDate, $firstFormat);
     $otherDate = new \MUtil_Date($secondDate, $secondFormat);
     $diff = $date->diffDays($otherDate);
     // echo $secondFormat . ' ' . $date->getTimestamp() . ' - ' . $date->getGmtOffset() . ' vs  ' . $otherDate->getTimestamp() . ' - ' . $otherDate->getGmtOffset() . "\n";
     $this->assertEquals($expectedDiffDays, $diff);
     $this->assertEquals($expectedDiffSeconds, $date->diffSeconds($otherDate));
 }