task claim

This commit is contained in:
xiaomlove
2024-06-07 02:51:05 +08:00
parent 08067b9d01
commit bc81b158be
14 changed files with 68 additions and 15 deletions
@@ -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)
,
@@ -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());
}
}
+4 -1
View File
@@ -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;
+5
View File
@@ -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) {
+17 -5
View File
@@ -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,
+1 -1
View File
@@ -1,6 +1,6 @@
<?php
defined('VERSION_NUMBER') || define('VERSION_NUMBER', '1.8.12');
defined('RELEASE_DATE') || define('RELEASE_DATE', '2024-05-20');
defined('RELEASE_DATE') || define('RELEASE_DATE', '2024-06-07');
defined('IN_TRACKER') || define('IN_TRACKER', false);
defined('PROJECTNAME') || define("PROJECTNAME","NexusPHP");
defined('NEXUSPHPURL') || define("NEXUSPHPURL","https://nexusphp.org");
+7
View File
@@ -150,6 +150,13 @@ class AjaxInterface{
$rep = new \App\Repositories\MedalRepository();
return $rep->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';
+9 -7
View File
@@ -43,18 +43,20 @@ TABLE;
$now = now();
$table .= '<tbody>';
$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(
'<input type="button" class="%s" data-id="%s" value="%s"%s>',
@@ -75,15 +77,15 @@ foreach ($rows as $row) {
$table .= '</tbody></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 = <<<JS
jQuery('.buy').on('click', function (e) {
let medalId = jQuery(this).attr('data-id')
jQuery('.claim').on('click', function (e) {
let id = jQuery(this).attr('data-id')
layer.confirm("{$confirmBuyMsg}", function (index) {
let params = {
action: "buyMedal",
params: {medal_id: medalId}
action: "claimTask",
params: {exam_id: id}
}
console.log(params)
jQuery.post('ajax.php', params, function(response) {
+7
View File
@@ -48,4 +48,11 @@ return [
'fail_deduct_bonus' => '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',
];
+1
View File
@@ -16,4 +16,5 @@ return [
'no_limit' => 'No limit',
'sum' => 'Sum',
'do_not_repeat' => 'Please do not repeat the operation!',
'no_permission' => 'No permission!',
];
+5 -1
View File
@@ -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' => '已经认领',
];
+1
View File
@@ -16,4 +16,5 @@ return [
'no_limit' => '不限',
'sum' => '累计',
'do_not_repeat' => '请不要重复操作!',
'no_permission' => '无权限!',
];
+7
View File
@@ -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' => '已經認領',
];
+1
View File
@@ -16,4 +16,5 @@ return [
'no_limit' => '不限',
'sum' => '累計',
'do_not_repeat' => '請不要重復操作!',
'no_permission' => '無權限!',
];