diff --git a/app/Models/Exam.php b/app/Models/Exam.php index b2cd12ca..07fb30d4 100644 --- a/app/Models/Exam.php +++ b/app/Models/Exam.php @@ -3,6 +3,8 @@ namespace App\Models; use Carbon\Carbon; +use Google\Service\Dataproc\RegexValidation; +use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Model; class Exam extends NexusModel @@ -162,4 +164,28 @@ class Exam extends NexusModel return implode("
", $arr); } + public function begin(): Attribute + { + return new Attribute( + get: fn ($value) => $value ? Carbon::parse($value) : Carbon::now() + ); + } + + public function end(): Attribute + { + return new Attribute( + get: function ($value, $attributes) { + if ($value) { + return Carbon::parse($value); + } + if (!empty($attributes['duration'])) { + /** @var Carbon $begin */ + $begin = $this->begin; + return $begin->addDays($attributes['duration']); + } + throw new \RuntimeException("No specific end or duration"); + } + ); + } + } diff --git a/app/Models/ExamUser.php b/app/Models/ExamUser.php index 8ed336d5..fb129e26 100644 --- a/app/Models/ExamUser.php +++ b/app/Models/ExamUser.php @@ -67,16 +67,14 @@ class ExamUser extends NexusModel public function getBeginAttribute() { $begin = $this->getRawOriginal('begin'); - $end = $this->getRawOriginal('end'); - if ($begin && $end) { + if ($begin) { do_log(sprintf('examUser: %s, begin from self: %s', $this->id, $begin)); return $begin; } $exam = $this->exam; $begin = $exam->getRawOriginal('begin'); - $end = $exam->getRawOriginal('end'); - if ($begin && $end) { + if ($begin) { do_log(sprintf('examUser: %s, begin from exam(%s): %s', $this->id, $exam->id, $begin)); return $begin; } @@ -90,17 +88,15 @@ class ExamUser extends NexusModel public function getEndAttribute() { - $begin = $this->getRawOriginal('begin'); $end = $this->getRawOriginal('end'); - if ($begin && $end) { + if ($end) { do_log(sprintf('examUser: %s, end from self: %s', $this->id, $end)); return $end; } $exam = $this->exam; - $begin = $exam->getRawOriginal('begin'); $end = $exam->getRawOriginal('end'); - if ($begin && $end) { + if ($end) { do_log(sprintf('examUser: %s, end from exam(%s): %s', $this->id, $exam->id, $end)); return $end; } diff --git a/app/Repositories/ExamRepository.php b/app/Repositories/ExamRepository.php index 08dd1eec..de2b5bdb 100644 --- a/app/Repositories/ExamRepository.php +++ b/app/Repositories/ExamRepository.php @@ -1018,6 +1018,8 @@ class ExamRepository extends BaseRepository $size = 1000; $minId = 0; $result = 0; + $begin = $exam->begin; + $end = $exam->end; while (true) { $logPrefix = sprintf('[%s], exam: %s, size: %s', __FUNCTION__, $exam->id , $size); $users = (clone $baseQuery)->where("$userTable.id", ">", $minId)->limit($size)->get(); @@ -1033,6 +1035,8 @@ class ExamRepository extends BaseRepository $insert = [ 'uid' => $user->id, 'exam_id' => $exam->id, + 'begin' => $begin, + 'end' => $end, 'created_at' => $now, 'updated_at' => $now, ]; diff --git a/include/constants.php b/include/constants.php index 3be2e708..be847c06 100644 --- a/include/constants.php +++ b/include/constants.php @@ -1,6 +1,6 @@