exam fixed after assign

This commit is contained in:
xiaomlove
2023-11-14 02:13:21 +08:00
parent c28fd3b086
commit 4907b2f7ac
15 changed files with 153 additions and 11 deletions
@@ -8,6 +8,7 @@ use App\Models\Exam;
use App\Models\ExamUser;
use App\Repositories\ExamRepository;
use App\Repositories\HitAndRunRepository;
use Carbon\Carbon;
use Filament\Forms;
use Filament\Resources\Form;
use Filament\Resources\Resource;
@@ -94,8 +95,34 @@ class ExamUserResource extends Resource
$rep->avoidExamUserBulk(['id' => $idArr], Auth::user());
})
->deselectRecordsAfterCompletion()
->requiresConfirmation()
->label(__('admin.resources.exam_user.bulk_action_avoid_label'))
->icon('heroicon-o-x')
->icon('heroicon-o-x'),
Tables\Actions\BulkAction::make('UpdateEnd')
->form([
Forms\Components\DateTimePicker::make('end')
->required()
->label(__('label.end'))
,
Forms\Components\Textarea::make('reason')
->label(__('label.reason'))
,
])
->action(function (Collection $records, array $data) {
$end = Carbon::parse($data['end']);
$rep = new ExamRepository();
foreach ($records as $record) {
if ($end->isAfter($record->begin)) {
$rep->updateExamUserEnd($record, $end, $data['reason'] ?? '');
} else {
do_log(sprintf("examUser: %d end: %s is before begin: %s, skip", $record->id, $end, $record->begin));
}
}
})
->deselectRecordsAfterCompletion()
->label(__('admin.resources.exam_user.bulk_action_update_end_label'))
->icon('heroicon-o-pencil'),
]);
}
@@ -4,9 +4,11 @@ namespace App\Filament\Resources\User\ExamUserResource\Pages;
use App\Filament\Resources\User\ExamUserResource;
use App\Repositories\ExamRepository;
use Carbon\Carbon;
use Filament\Pages\Actions;
use Filament\Resources\Pages\ViewRecord;
use Filament\Tables\Table;
use Filament\Forms;
class ViewExamUser extends ViewRecord
{
@@ -16,7 +18,7 @@ class ViewExamUser extends ViewRecord
private function getDetailCardData(): array
{
// dd($this->record->progressFormatted);
// dd($this->record);
$data = [];
$record = $this->record;
$data[] = [
@@ -82,6 +84,31 @@ class ViewExamUser extends ViewRecord
})
->label(__('admin.resources.exam_user.action_avoid')),
Actions\Action::make('UpdateEnd')
->mountUsing(fn (Forms\ComponentContainer $form) => $form->fill([
'end' => $this->record->end,
]))
->form([
Forms\Components\DateTimePicker::make('end')
->required()
->label(__('label.end'))
,
Forms\Components\Textarea::make('reason')
->label(__('label.reason'))
,
])
->action(function (array $data) {
$examRep = new ExamRepository();
try {
$examRep->updateExamUserEnd($this->record, Carbon::parse($data['end']), $data['reason'] ?? "");
$this->notify('success', 'Success !');
$this->record = $this->resolveRecord($this->record->id);
} catch (\Exception $exception) {
$this->notify('danger', $exception->getMessage());
}
})
->label(__('admin.resources.exam_user.action_update_end')),
Actions\DeleteAction::make(),
];
}
+1 -1
View File
@@ -124,7 +124,7 @@ class ExamUser extends NexusModel
return $this->belongsTo(User::class, 'uid');
}
public function progresses()
public function progresses(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(ExamProgress::class, 'exam_user_id');
}
+65 -7
View File
@@ -6,16 +6,14 @@ use App\Models\Exam;
use App\Models\ExamProgress;
use App\Models\ExamUser;
use App\Models\Message;
use App\Models\Setting;
use App\Models\Snatch;
use App\Models\Torrent;
use App\Models\User;
use App\Models\UserBanLog;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
@@ -312,11 +310,33 @@ class ExamRepository extends BaseRepository
$data = [
'exam_id' => $exam->id,
];
if ($begin && $end) {
$logPrefix .= ", specific begin and end";
$data['begin'] = $begin;
$data['end'] = $end;
if (empty($begin)) {
if (!empty($exam->begin)) {
$begin = $exam->begin;
$logPrefix .= ", begin from exam->begin: $begin";
} else {
$begin = now();
$logPrefix .= ", begin from now: $begin";
}
} else {
$begin = Carbon::parse($begin);
}
if (empty($end)) {
if (!empty($exam->end)) {
$end = $exam->end;
$logPrefix .= ", end from exam->end: $end";
} elseif ($exam->duration > 0) {
$duration = $exam->duration;
$end = $begin->clone()->addDays($duration)->toDateTimeString();
$logPrefix .= ", end from begin + duration($duration): $end";
} else {
throw new \RuntimeException("No specific end or duration");
}
} else {
$end = Carbon::parse($end);
}
$data['begin'] = $begin;
$data['end'] = $end;
do_log("$logPrefix, data: " . nexus_json_encode($data));
$examUser = $user->exams()->create($data);
$this->updateProgress($examUser, $user);
@@ -506,12 +526,19 @@ class ExamRepository extends BaseRepository
if (empty($end)) {
throw new \InvalidArgumentException("$logPrefix, exam: {$examUser->id} no end.");
}
/**
* @var $progressGrouped Collection
*/
$progressGrouped = $examUser->progresses->keyBy("index");
$examUserProgressFieldData = [];
$now = now();
foreach ($exam->indexes as $index) {
if (!isset($index['checked']) || !$index['checked']) {
continue;
}
if ($progressGrouped->isNotEmpty() && !$progressGrouped->has($index['index'])) {
continue;
}
if (!isset(Exam::$indexes[$index['index']])) {
$msg = "Unknown index: {$index['index']}";
do_log("$logPrefix, $msg", 'error');
@@ -721,6 +748,9 @@ class ExamRepository extends BaseRepository
if (!isset($index['checked']) || !$index['checked']) {
continue;
}
if (!isset($progress[$index['index']])) {
continue;
}
$currentValue = $progress[$index['index']] ?? 0;
$requireValue = $index['require_value'];
$unit = Exam::$indexes[$index['index']]['unit'] ?? '';
@@ -769,6 +799,32 @@ class ExamRepository extends BaseRepository
return $result;
}
public function updateExamUserEnd(ExamUser $examUser, Carbon $end, string $reason = "")
{
if ($end->isBefore($examUser->begin)) {
throw new \InvalidArgumentException(nexus_trans("exam-user.end_can_not_before_begin", ['begin' => $examUser->begin, 'end' => $end]));
}
$oldEndTime = $examUser->end;
$locale = $examUser->user->locale;
$examName = $examUser->exam->name;
Message::add([
'sender' => 0,
'receiver' => $examUser->uid,
'added' => now(),
'subject' => nexus_trans('message.exam_user_end_time_updated.subject', [
'exam_name' => $examName
], $locale),
'msg' => nexus_trans('message.exam_user_end_time_updated.body', [
'exam_name' => $examName,
'old_end_time' => $oldEndTime,
'new_end_time' => $end,
'operator' => get_pure_username(),
'reason' => $reason,
], $locale),
]);
$examUser->update(['end' => $end]);
}
public function removeExamUserBulk(array $params, User $user)
{
$result = $this->getExamUserBulkQuery($params)->delete();
@@ -983,6 +1039,8 @@ class ExamRepository extends BaseRepository
$examUser->delete();
continue;
}
//update to the newest progress
$examUser = $this->updateProgress($examUser, $examUser->user);
$locale = $examUser->user->locale;
if ($examUser->is_done) {
do_log("$currentLogPrefix, [is_done]");