diff --git a/app/Filament/Resources/System/ExamResource.php b/app/Filament/Resources/System/ExamResource.php index cb806c00..31d551e4 100644 --- a/app/Filament/Resources/System/ExamResource.php +++ b/app/Filament/Resources/System/ExamResource.php @@ -56,10 +56,12 @@ class ExamResource extends Resource ->reactive() , Forms\Components\TextInput::make('success_reward_bonus') + ->required() ->label(__('exam.success_reward_bonus')) ->hidden(fn (\Closure $get) => $get('type') != Exam::TYPE_TASK) , Forms\Components\TextInput::make('fail_deduct_bonus') + ->required() ->label(__('exam.fail_deduct_bonus')) ->hidden(fn (\Closure $get) => $get('type') != Exam::TYPE_TASK) , diff --git a/app/Filament/Resources/System/ExamResource/Pages/CreateExam.php b/app/Filament/Resources/System/ExamResource/Pages/CreateExam.php index 7fe9e14e..581c3347 100644 --- a/app/Filament/Resources/System/ExamResource/Pages/CreateExam.php +++ b/app/Filament/Resources/System/ExamResource/Pages/CreateExam.php @@ -29,6 +29,7 @@ class CreateExam extends CreateRecord } $this->redirect($this->getResource()::getUrl('index')); } catch (\Exception $exception) { + do_log($exception->getMessage() . "\n" . $exception->getTraceAsString(), "error"); $this->notify('danger', $exception->getMessage()); } } diff --git a/app/Models/Exam.php b/app/Models/Exam.php index 0333467a..efe820cf 100644 --- a/app/Models/Exam.php +++ b/app/Models/Exam.php @@ -165,7 +165,10 @@ class Exam extends NexusModel $filter = self::FILTER_USER_CLASS; if (!empty($currentFilters[$filter])) { $classes = collect(User::$classes)->only($currentFilters[$filter]); - $arr[] = sprintf('%s: %s', nexus_trans("exam.filters.$filter"), $classes->pluck('text')->implode(', ')); + $arr[] = sprintf( + '%s: %s', + nexus_trans("exam.filters.$filter"), $classes->map(fn ($value, $key) => User::getClassText($key))->implode(', ') + ); } $filter = self::FILTER_USER_REGISTER_TIME_RANGE; diff --git a/app/Models/User.php b/app/Models/User.php index eccddb2a..c064f664 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -519,6 +519,11 @@ class User extends Authenticatable implements FilamentUser, HasName return $this->belongsToMany(Exam::class, "exam_users", "uid", "exam_id"); } + public function onGoingExamAndTasks(): \Illuminate\Database\Eloquent\Relations\BelongsToMany + { + return $this->examAndTasks()->wherePivot("status", ExamUser::STATUS_NORMAL); + } + public function getAvatarAttribute($value) { if ($value) { diff --git a/app/Repositories/ExamRepository.php b/app/Repositories/ExamRepository.php index 27f342f3..037294ba 100644 --- a/app/Repositories/ExamRepository.php +++ b/app/Repositories/ExamRepository.php @@ -364,18 +364,30 @@ class ExamRepository extends BaseRepository /** @var Exam $exam */ $exam = Exam::query()->find($examId); $user = User::query()->findOrFail($uid); - if (Auth::user()->class <= $user->class) { - throw new NexusException("No permission !"); + $locale = $user->locale; + $authUserClass = get_user_class(); + $authUserId = get_user_id(); + if ($exam->isTypeExam()) { + if ($authUserClass <= $user->class) { + //exam only can assign by upper class admin + throw new NexusException(nexus_trans("nexus.no_permission", [], $locale)); + } + } elseif ($exam->isTypeTask()) { + if ($user->id != $authUserId) { + //task only can be claimed by self + throw new NexusException(nexus_trans('exam.claim_by_yourself_only', [], $locale)); + } } + if (!$this->isExamMatchUser($exam, $user)) { - throw new NexusException("Exam: {$exam->id} no match this user."); + throw new NexusException(nexus_trans('exam.not_match_target_user', [], $locale)); } if ($user->exams()->where('status', ExamUser::STATUS_NORMAL)->exists()) { - throw new NexusException("User: $uid already has exam on the way."); + throw new NexusException(nexus_trans('exam.has_other_on_the_way', ['type_text' => $exam->typeText], $locale)); } $exists = $user->exams()->where('exam_id', $exam->id)->exists(); if ($exists) { - throw new NexusException("Exam: {$exam->id} already assign to user: {$user->id}."); + throw new NexusException(nexus_trans('exam.claimed_already', [], $locale)); } $data = [ 'exam_id' => $exam->id, diff --git a/include/constants.php b/include/constants.php index d7bbfd10..9b6a7a7b 100644 --- a/include/constants.php +++ b/include/constants.php @@ -1,6 +1,6 @@ saveUserMedal($CURUSER['id'], $data); } + + public static function claimTask($params) + { + global $CURUSER; + $rep = new \App\Repositories\ExamRepository(); + return $rep->assignToUser($CURUSER['id'], $params['exam_id']); + } } $class = 'AjaxInterface'; diff --git a/public/task.php b/public/task.php index 319197f1..aebf5b4e 100644 --- a/public/task.php +++ b/public/task.php @@ -43,18 +43,20 @@ TABLE; $now = now(); $table .= ''; $userInfo = \App\Models\User::query()->findOrFail($CURUSER['id'], \App\Models\User::$commonFields); -$userTasks = $userInfo->examAndTasks()->where("type", \App\Models\Exam::TYPE_TASK) +$userTasks = $userInfo->onGoingExamAndTasks()->where("type", \App\Models\Exam::TYPE_TASK) ->orderBy('id', 'desc') ->get() ->keyBy('id') ; -//dd($userTasks); +//dd(last_query()); foreach ($rows as $row) { $claimDisabled = $claimClass = ''; $claimBtnText = "认领"; if ($userTasks->has($row->id)) { $claimDisabled = " disabled"; $claimBtnText = "已认领"; + } else { + $claimClass = "claim"; } $claimAction = sprintf( '', @@ -75,15 +77,15 @@ foreach ($rows as $row) { $table .= ''; echo $header . $table . $paginationBottom; end_main_frame(); -$confirmBuyMsg = nexus_trans('medal.confirm_to_buy'); +$confirmBuyMsg = nexus_trans('exam.confirm_to_claim'); $confirmGiftMsg = nexus_trans('medal.confirm_to_gift'); $js = << 'Deduct bonus for task failure', 'success_reward_bonus' => 'Reward bonus for task completion', + + 'action_claim_task' => 'Claim', + 'confirm_to_claim' => 'Sure you want to claim?' , + 'claim_by_yourself_only' => 'Claim only by yourself!' , + 'not_match_target_user' => 'You are not a matching target user!' , + 'has_other_on_the_way' => 'There is an other :type_text in progress!' , + 'claimed_already' => 'Already claimed', ]; diff --git a/resources/lang/en/nexus.php b/resources/lang/en/nexus.php index f9fac3a7..22f359cc 100644 --- a/resources/lang/en/nexus.php +++ b/resources/lang/en/nexus.php @@ -16,4 +16,5 @@ return [ 'no_limit' => 'No limit', 'sum' => 'Sum', 'do_not_repeat' => 'Please do not repeat the operation!', + 'no_permission' => 'No permission!', ]; diff --git a/resources/lang/zh_CN/exam.php b/resources/lang/zh_CN/exam.php index 488b3f54..12fe4cbd 100644 --- a/resources/lang/zh_CN/exam.php +++ b/resources/lang/zh_CN/exam.php @@ -61,5 +61,9 @@ return [ 'success_reward_bonus' => '任务完成奖励魔力', 'action_claim_task' => '领取', - + 'confirm_to_claim' => '确定要认领吗?', + 'claim_by_yourself_only' => '只能自己认领!', + 'not_match_target_user' => '你不是匹配的目标用户!', + 'has_other_on_the_way' => '有其他进行中的:type_text', + 'claimed_already' => '已经认领', ]; diff --git a/resources/lang/zh_CN/nexus.php b/resources/lang/zh_CN/nexus.php index d92d4e00..44050c35 100644 --- a/resources/lang/zh_CN/nexus.php +++ b/resources/lang/zh_CN/nexus.php @@ -16,4 +16,5 @@ return [ 'no_limit' => '不限', 'sum' => '累计', 'do_not_repeat' => '请不要重复操作!', + 'no_permission' => '无权限!', ]; diff --git a/resources/lang/zh_TW/exam.php b/resources/lang/zh_TW/exam.php index c8ad4ec3..b43fb763 100644 --- a/resources/lang/zh_TW/exam.php +++ b/resources/lang/zh_TW/exam.php @@ -48,4 +48,11 @@ return [ 'fail_deduct_bonus' => '任务失败扣除魔力', 'success_reward_bonus' => '任务完成奖励魔力', + + 'action_claim_task' => '領取', + 'confirm_to_claim' => '確定要認領嗎?', + 'claim_by_yourself_only' => '只能自己認領!', + 'not_match_target_user' => '你不是匹配的目標用戶!', + 'has_other_on_the_way' => '有其他進行中的:type_text', + 'claimed_already' => '已經認領', ]; diff --git a/resources/lang/zh_TW/nexus.php b/resources/lang/zh_TW/nexus.php index 921268bd..fe0f7821 100644 --- a/resources/lang/zh_TW/nexus.php +++ b/resources/lang/zh_TW/nexus.php @@ -16,4 +16,5 @@ return [ 'no_limit' => '不限', 'sum' => '累計', 'do_not_repeat' => '請不要重復操作!', + 'no_permission' => '無權限!', ];