255], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'employee' => 'Employee ID', 'start' => 'Date of start activity', 'end' => 'Date of end activity', ]; } /** * Если ближайшие 10 минут пользователь был активен, продлеваем конец сессии на 10 минут. * Иначе создаём новую сессию. * * @param int $userId * @return string * @throws \Exception */ public static function ping(int $userId) { if (!$userId) return ''; $lastRecord = self::find() ->where(['employee' => $userId]) ->orderBy('end DESC') ->limit(1) ->one() ; $now = new DateTime(); if ($lastRecord) { $end = strtotime($lastRecord->end); if ($end > $now->getTimestamp()) { $now->add(new DateInterval('PT10M')); $lastRecord->end = $now->format(self::FORMAT); $lastRecord->save(); return 'Время продлено'; } } $userActivity = new self(); $userActivity->employee = $userId; $userActivity->start = $now->format(self::FORMAT); $now->add(new DateInterval('PT10M')); $userActivity->end = $now->format(self::FORMAT); $userActivity->save(); return 'Создана новая активность'; } }