Files
nexusphp/app/Models/ExamUser.php

130 lines
3.6 KiB
PHP
Raw Normal View History

2021-04-25 02:12:14 +08:00
<?php
namespace App\Models;
use App\Repositories\ExamRepository;
2021-04-25 02:12:14 +08:00
class ExamUser extends NexusModel
{
protected $fillable = ['exam_id', 'uid', 'status', 'progress', 'begin', 'end', 'is_done'];
2021-04-25 02:12:14 +08:00
public $timestamps = true;
const STATUS_NORMAL = 0;
const STATUS_FINISHED = 1;
2021-06-10 02:16:08 +08:00
const STATUS_AVOIDED = -1;
2021-04-25 02:12:14 +08:00
2022-05-13 03:12:38 +08:00
public static array $status = [
2021-04-25 02:12:14 +08:00
self::STATUS_NORMAL => ['text' => 'Normal'],
self::STATUS_FINISHED => ['text' => 'Finished'],
2021-06-10 02:16:08 +08:00
self::STATUS_AVOIDED => ['text' => 'Avoided'],
2021-04-25 02:12:14 +08:00
];
const IS_DONE_YES = 1;
const IS_DONE_NO = 0;
2022-05-13 03:12:38 +08:00
public static array $isDoneInfo = [
2021-05-15 02:13:33 +08:00
self::IS_DONE_YES => ['text' => 'Yes'],
self::IS_DONE_NO => ['text' => 'No'],
];
2021-04-25 02:12:14 +08:00
protected $casts = [
'progress' => 'json'
2021-04-25 02:12:14 +08:00
];
2021-05-06 01:49:05 +08:00
public function getStatusTextAttribute(): string
2021-04-25 02:12:14 +08:00
{
return nexus_trans('exam-user.status.' . $this->status);
2021-04-25 02:12:14 +08:00
}
2021-05-15 02:13:33 +08:00
public function getIsDoneTextAttribute(): string
{
return self::$isDoneInfo[$this->is_done]['text'] ?? '';
}
2022-07-02 15:08:23 +08:00
public function getProgressFormattedAttribute(): array
{
$examRep = new ExamRepository();
return $examRep->getProgressFormatted($this->exam, $this->progress);
2022-07-02 15:08:23 +08:00
}
2022-06-27 01:39:01 +08:00
public static function listStatus($onlyKeyValue = false): array
{
$result = self::$status;
$keyValues = [];
foreach ($result as $key => &$value) {
$text = nexus_trans('exam-user.status.' . $key);
$value['text'] = $text;
$keyValues[$key] = $text;
}
if ($onlyKeyValue) {
return $keyValues;
}
return $result;
}
2021-04-29 19:18:13 +08:00
public function getBeginAttribute()
{
2021-05-06 01:49:05 +08:00
$begin = $this->getRawOriginal('begin');
if ($begin) {
2021-05-06 11:57:23 +08:00
do_log(sprintf('examUser: %s, begin from self: %s', $this->id, $begin));
2021-05-06 01:49:05 +08:00
return $begin;
}
$exam = $this->exam;
$begin = $exam->getRawOriginal('begin');
if ($begin) {
2021-05-06 11:57:23 +08:00
do_log(sprintf('examUser: %s, begin from exam(%s): %s', $this->id, $exam->id, $begin));
2021-05-06 01:49:05 +08:00
return $begin;
}
if ($exam->duration > 0) {
2021-05-06 11:57:23 +08:00
do_log(sprintf('examUser: %s, begin from self created_at(%s)', $this->id, $this->getRawOriginal('created_at')));
2021-05-06 01:49:05 +08:00
return $this->created_at->toDateTimeString();
}
return null;
2021-04-29 19:18:13 +08:00
}
public function getEndAttribute()
{
2021-05-06 01:49:05 +08:00
$end = $this->getRawOriginal('end');
if ($end) {
2021-05-06 11:57:23 +08:00
do_log(sprintf('examUser: %s, end from self: %s', $this->id, $end));
2021-05-06 01:49:05 +08:00
return $end;
}
$exam = $this->exam;
$end = $exam->getRawOriginal('end');
if ($end) {
2021-05-06 11:57:23 +08:00
do_log(sprintf('examUser: %s, end from exam(%s): %s', $this->id, $exam->id, $end));
2021-05-06 01:49:05 +08:00
return $end;
}
$duration = $exam->duration;
if ($duration > 0) {
2021-05-06 11:57:23 +08:00
do_log(sprintf('examUser: %s, end from self created_at + exam(%s) created_at: %s + %s days', $this->id, $exam->id, $this->getRawOriginal('created_at'), $duration));
2025-05-02 21:21:37 +07:00
return $this->created_at->addDays((int)$duration)->toDateTimeString();
2021-05-06 01:49:05 +08:00
}
return null;
2021-04-29 19:18:13 +08:00
}
2021-04-25 02:12:14 +08:00
public function exam(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Exam::class, 'exam_id');
}
public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(User::class, 'uid');
}
2023-11-14 02:13:21 +08:00
public function progresses(): \Illuminate\Database\Eloquent\Relations\HasMany
2021-04-25 21:28:58 +08:00
{
return $this->hasMany(ExamProgress::class, 'exam_user_id');
}
2021-04-25 02:12:14 +08:00
}