exam user set begin and end when cronjobAssign()

This commit is contained in:
lgb
2024-04-10 11:54:03 +08:00
parent 68a0bd96c3
commit b36fd64885
4 changed files with 35 additions and 9 deletions

View File

@@ -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("<br/>", $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");
}
);
}
}

View File

@@ -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;
}

View File

@@ -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,
];

View File

@@ -1,6 +1,6 @@
<?php
defined('VERSION_NUMBER') || define('VERSION_NUMBER', '1.8.12');
defined('RELEASE_DATE') || define('RELEASE_DATE', '2024-04-09');
defined('RELEASE_DATE') || define('RELEASE_DATE', '2024-04-10');
defined('IN_TRACKER') || define('IN_TRACKER', false);
defined('PROJECTNAME') || define("PROJECTNAME","NexusPHP");
defined('NEXUSPHPURL') || define("NEXUSPHPURL","https://nexusphp.org");