Skip to content

Commit

Permalink
exam fixed after assign
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaomlove committed Nov 13, 2023
1 parent c28fd3b commit 4907b2f
Show file tree
Hide file tree
Showing 15 changed files with 153 additions and 11 deletions.
29 changes: 28 additions & 1 deletion app/Filament/Resources/User/ExamUserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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;
Expand Down Expand Up @@ -94,8 +95,34 @@ public static function table(Table $table): Table
$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'),
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

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
{
Expand All @@ -16,7 +18,7 @@ class ViewExamUser extends ViewRecord

private function getDetailCardData(): array
{
// dd($this->record->progressFormatted);
// dd($this->record);
$data = [];
$record = $this->record;
$data[] = [
Expand Down Expand Up @@ -82,6 +84,31 @@ protected function getActions(): array
})
->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(),
];
}
Expand Down
2 changes: 1 addition & 1 deletion app/Models/ExamUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
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');
}
Expand Down
72 changes: 65 additions & 7 deletions app/Repositories/ExamRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
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;

Expand Down Expand Up @@ -312,11 +310,33 @@ public function assignToUser(int $uid, int $examId, $begin = null, $end = null)
$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);
Expand Down Expand Up @@ -506,12 +526,19 @@ public function updateProgress($examUser, $user = null): ExamUser|bool
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');
Expand Down Expand Up @@ -721,6 +748,9 @@ public function getProgressFormatted(Exam $exam, array $progress, $locale = null
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'] ?? '';
Expand Down Expand Up @@ -769,6 +799,32 @@ public function avoidExamUser(int $examUserId)
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();
Expand Down Expand Up @@ -983,6 +1039,8 @@ public function cronjobCheckout($ignoreTimeRange = false): int
$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]");
Expand Down
2 changes: 1 addition & 1 deletion include/constants.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
defined('VERSION_NUMBER') || define('VERSION_NUMBER', '1.8.9');
defined('RELEASE_DATE') || define('RELEASE_DATE', '2023-11-08');
defined('RELEASE_DATE') || define('RELEASE_DATE', '2023-11-14');
defined('IN_TRACKER') || define('IN_TRACKER', false);
defined('PROJECTNAME') || define("PROJECTNAME","NexusPHP");
defined('NEXUSPHPURL') || define("NEXUSPHPURL","https://nexusphp.org");
Expand Down
9 changes: 9 additions & 0 deletions include/globalfunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,15 @@ function get_user_id()
return auth()->user()->id ?? 0;
}

function get_pure_username()
{
if (IN_NEXUS) {
global $CURUSER;
return $CURUSER["username"] ?? "";
}
return auth()->user()->username ?? "";
}

function nexus()
{
return \Nexus\Nexus::instance();
Expand Down
2 changes: 2 additions & 0 deletions resources/lang/en/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@
],
'exam_user' => [
'bulk_action_avoid_label' => 'Bulk avoid',
'bulk_action_update_end_label' => 'Bulk modify end time',
'action_avoid' => 'Avoid',
'action_update_end' => 'Modify end time',
'result_passed' => 'Passed!',
'result_not_passed' => 'Not passed!',
],
Expand Down
1 change: 1 addition & 0 deletions resources/lang/en/exam-user.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
\App\Models\ExamUser::STATUS_AVOIDED => 'Avoided',
\App\Models\ExamUser::STATUS_NORMAL => 'Normal',
],
'end_can_not_before_begin' => "End time: :end can't be before begin time: :begin",
];
4 changes: 4 additions & 0 deletions resources/lang/en/message.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@
'subject' => 'Successful torrent purchase reminder',
'body' => 'You spent :bonus bonus to successfully buy the torrent:[url=:url]:torrent_name[/url]',
],
'exam_user_end_time_updated' => [
'subject' => 'Exam :exam_name end time changed',
'body' => 'The end time of your in-progress exam :exam_name has changed from :old_end_time to :new_end_time. admin: :operator, reason: :reason.',
],
];
2 changes: 2 additions & 0 deletions resources/lang/zh_CN/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@
],
'exam_user' => [
'bulk_action_avoid_label' => '批量免除',
'bulk_action_update_end_label' => '批量修改结束时间',
'action_avoid' => '免除',
'action_update_end' => '修改结束时间',
'result_passed' => '通过!',
'result_not_passed' => '未通过!',
],
Expand Down
1 change: 1 addition & 0 deletions resources/lang/zh_CN/exam-user.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
\App\Models\ExamUser::STATUS_AVOIDED => '已免除',
\App\Models\ExamUser::STATUS_NORMAL => '考核中',
],
'end_can_not_before_begin' => '结束时间::end 不能在开始时间::begin 之前',
];
4 changes: 4 additions & 0 deletions resources/lang/zh_CN/message.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@
'subject' => '成功购买种子提醒',
'body' => '你花费 :bonus 魔力成功购买了种子:[url=:url]:torrent_name[/url]',
],
'exam_user_end_time_updated' => [
'subject' => '考核 :exam_name 结束时间变更',
'body' => '你进行中的考核::exam_name 的结束时间由 :old_end_time 变更为 :new_end_time。管理员::operator,原因::reason。',
],
];
2 changes: 2 additions & 0 deletions resources/lang/zh_TW/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@
],
'exam_user' => [
'bulk_action_avoid_label' => '批量免除',
'bulk_action_update_end_label' => '批量修改結束時間',
'action_avoid' => '免除',
'action_update_end' => '修改結束時間',
'result_passed' => '通過!',
'result_not_passed' => '未通過!',
],
Expand Down
1 change: 1 addition & 0 deletions resources/lang/zh_TW/exam-user.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
\App\Models\ExamUser::STATUS_AVOIDED => '已免除',
\App\Models\ExamUser::STATUS_NORMAL => '考核中',
],
'end_can_not_before_begin' => '結束時間::end 不能在開始時間::begin 之前',
];
4 changes: 4 additions & 0 deletions resources/lang/zh_TW/message.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@
'subject' => '成功購買種子提醒',
'body' => '你花費 :bonus 魔力成功購買了種子:[url=:url]:torrent_name[/url]',
],
'exam_user_end_time_updated' => [
'subject' => '考核 :exam_name 結束時間變更',
'body' => '你進行中的考核::exam_name 的結束時間由 :old_end_time 變更為 :new_end_time。管理員::operator,原因::reason。',
],
];

0 comments on commit 4907b2f

Please sign in to comment.