Skip to content

Commit

Permalink
exam add max user count
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaomlove committed Aug 7, 2024
1 parent eae609e commit 6894508
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 38 deletions.
14 changes: 12 additions & 2 deletions app/Filament/Resources/System/ExamResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,32 @@ public static function form(Form $form): Form
Forms\Components\Section::make(__('label.exam.section_base_info'))->schema([
Forms\Components\TextInput::make('name')->required()->columnSpan(['sm' => 2])->label(__('label.name')),
Forms\Components\Select::make('type')
->required()->columnSpan(['sm' => 2])
->required()
->columnSpanFull()
->label(__('exam.type'))
->options(Exam::listTypeOptions())
->helperText(__('exam.type_help'))
->reactive()
,
Forms\Components\TextInput::make('success_reward_bonus')
->columnSpanFull()
->required()
->label(__('exam.success_reward_bonus'))
->hidden(fn (\Closure $get) => $get('type') != Exam::TYPE_TASK)
,
Forms\Components\TextInput::make('fail_deduct_bonus')
->columnSpanFull()
->required()
->label(__('exam.fail_deduct_bonus'))
->hidden(fn (\Closure $get) => $get('type') != Exam::TYPE_TASK)
,
Forms\Components\TextInput::make('max_user_count')
->columnSpanFull()
->required()
->numeric()
->label(__('exam.max_user_count'))
->hidden(fn (\Closure $get) => $get('type') != Exam::TYPE_TASK)
,

Forms\Components\Repeater::make('indexes')->schema([
Forms\Components\Select::make('index')
Expand Down Expand Up @@ -148,7 +158,7 @@ public static function table(Table $table): Table
Tables\Columns\TextColumn::make('end')->label(__('label.end')),
Tables\Columns\TextColumn::make('durationText')->label(__('label.duration')),
Tables\Columns\TextColumn::make('recurringText')->label(__('exam.recurring')),
Tables\Columns\TextColumn::make('filterFormatted')->label(__('label.exam.filter_formatted'))->html(),
Tables\Columns\TextColumn::make('filterFormatted')->label(__('label.exam.filter_formatted'))->html()->extraAttributes([]),
Tables\Columns\BooleanColumn::make('is_discovered')->label(__('label.exam.is_discovered')),
Tables\Columns\TextColumn::make('priority')->label(__('label.priority')),
Tables\Columns\TextColumn::make('statusText')->label(__('label.status')),
Expand Down
12 changes: 11 additions & 1 deletion app/Models/Exam.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Exam extends NexusModel
{
protected $fillable = [
'name', 'description', 'begin', 'end', 'duration', 'status', 'is_discovered', 'filters', 'indexes', 'priority',
'recurring', 'type', 'success_reward_bonus', 'fail_deduct_bonus'
'recurring', 'type', 'success_reward_bonus', 'fail_deduct_bonus', 'max_user_count'
];

public $timestamps = true;
Expand Down Expand Up @@ -294,4 +294,14 @@ public function isTypeTask(): bool
return $this->type == self::TYPE_TASK;
}

public function users()
{
return $this->belongsToMany(User::class, "exam_users", "exam_id", "uid");
}

public function OnGoingUsers()
{
return $this->users()->wherePivot("status", ExamUser::STATUS_NORMAL);
}

}
19 changes: 19 additions & 0 deletions app/Repositories/ExamRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,19 @@ public function assignToUser(int $uid, int $examId, $begin = null, $end = null)
$locale = $user->locale;
$authUserClass = get_user_class();
$authUserId = get_user_id();
$now = Carbon::now();
if (!empty($exam->begin)) {
$specificBegin = Carbon::parse($exam->begin);
if ($specificBegin->isAfter($now)) {
throw new NexusException(nexus_trans("exam.not_between_begin_end_time", [], $locale));
}
}
if (!empty($exam->end)) {
$specificEnd = Carbon::parse($exam->end);
if ($specificEnd->isBefore($now)) {
throw new NexusException(nexus_trans("exam.not_between_begin_end_time", [], $locale));
}
}
if ($exam->isTypeExam()) {
if ($authUserClass <= $user->class) {
//exam only can assign by upper class admin
Expand All @@ -378,6 +391,12 @@ public function assignToUser(int $uid, int $examId, $begin = null, $end = null)
//task only can be claimed by self
throw new NexusException(nexus_trans('exam.claim_by_yourself_only', [], $locale));
}
if ($exam->max_user_count > 0) {
$claimUserCount = ExamUser::query()->where("exam_id", $examId)->count();
if ($claimUserCount >= $exam->max_user_count) {
throw new NexusException(nexus_trans('exam.reach_max_user_count', [], $locale));
}
}
}

if (!$this->isExamMatchUser($exam, $user)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('exams', function (Blueprint $table) {
$table->integer("max_user_count")->default(0);
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('exams', function (Blueprint $table) {
$table->dropColumn("max_user_count");
});
}
};
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.13');
defined('RELEASE_DATE') || define('RELEASE_DATE', '2024-08-03');
defined('RELEASE_DATE') || define('RELEASE_DATE', '2024-08-08');
defined('IN_TRACKER') || define('IN_TRACKER', false);
defined('PROJECTNAME') || define("PROJECTNAME","NexusPHP");
defined('NEXUSPHPURL') || define("NEXUSPHPURL","https://nexusphp.org");
Expand Down
45 changes: 17 additions & 28 deletions public/task.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
require "../include/bittorrent.php";
dbconn();
loggedinorreturn();
$query = \App\Models\Exam::query()->where('type', \App\Models\Exam::TYPE_TASK)->where("status", \App\Models\Exam::STATUS_ENABLED);
$query = \App\Models\Exam::query()
->where('type', \App\Models\Exam::TYPE_TASK)
->where("status", \App\Models\Exam::STATUS_ENABLED)
;
$total = (clone $query)->count();
$perPage = 20;
list($paginationTop, $paginationBottom, $limit, $offset) = pager($perPage, $total, "?");
$rows = (clone $query)->offset($offset)->take($perPage)->orderBy('id', 'desc')->get();
$rows = (clone $query)->offset($offset)->take($perPage)->orderBy('id', 'desc')->withCount("users")->get();
$title = nexus_trans('exam.type_task');
$columnNameLabel = nexus_trans('label.name');
$columnIndexLabel = nexus_trans('exam.index');
Expand All @@ -20,6 +23,7 @@
$columnFailDeductLabel = nexus_trans('exam.fail_deduct_bonus');
$columnDescriptionDeductLabel = nexus_trans('label.description');
$columnClaimLabel = nexus_trans('exam.action_claim_task');
$columnClaimedUserCountLabel = nexus_trans('exam.claimed_user_count');

$header = '<h1 style="text-align: center">'.$title.'</h1>';
stdhead($title);
Expand All @@ -35,6 +39,7 @@
<td class="colhead">$columnTargetUserLabel</td>
<td class="colhead">$columnSuccessRewardLabel</td>
<td class="colhead">$columnFailDeductLabel</td>
<td class="colhead">$columnClaimedUserCountLabel</td>
<td class="colhead">$columnDescriptionDeductLabel</td>
<td class="colhead">$columnClaimLabel</td>
</tr>
Expand All @@ -51,10 +56,10 @@
//dd(last_query());
foreach ($rows as $row) {
$claimDisabled = $claimClass = '';
$claimBtnText = "认领";
$claimBtnText = nexus_trans("exam.action_claim_task");
if ($userTasks->has($row->id)) {
$claimDisabled = " disabled";
$claimBtnText = "已认领";
$claimBtnText = nexus_trans("exam.claimed_already");
} else {
$claimClass = "claim";
}
Expand All @@ -64,12 +69,13 @@
);
$columns = [];
$columns[] = sprintf('<td class="nowrap"><strong>%s</strong></td>', $row->name);
$columns[] = sprintf('<td>%s</td>', $row->indexFormatted);
$columns[] = sprintf('<td class="nowrap">%s</td>', $row->indexFormatted);
$columns[] = sprintf('<td>%s</td>', $row->getBeginForUser());
$columns[] = sprintf('<td>%s</td>', $row->getEndForUser());
$columns[] = sprintf('<td>%s</td>', $row->filterFormatted);
$columns[] = sprintf('<td>%s</td>', number_format($row->success_reward_bonus));
$columns[] = sprintf('<td>%s</td>', number_format($row->fail_deduct_bonus));
$columns[] = sprintf('<td>%s</td>', sprintf("%s/%s",$row->users_count, $row->max_user_count ?: nexus_trans("label.infinite")));
$columns[] = sprintf('<td>%s</td>', $row->description);
$columns[] = sprintf('<td>%s</td>', $claimAction);
$table .= sprintf('<tr>%s</tr>', implode("", $columns));
Expand All @@ -83,35 +89,17 @@
jQuery('.claim').on('click', function (e) {
let id = jQuery(this).attr('data-id')
layer.confirm("{$confirmBuyMsg}", function (index) {
layer.close(index)
let params = {
action: "claimTask",
params: {exam_id: id}
}
console.log(params)
jQuery('body').loading({
stoppable: false
});
jQuery.post('ajax.php', params, function(response) {
console.log(response)
if (response.ret != 0) {
layer.alert(response.msg)
return
}
window.location.reload()
}, 'json')
})
})
jQuery('.gift').on('click', function (e) {
let medalId = jQuery(this).attr('data-id')
let uid = jQuery(this).prev().val()
if (!uid) {
layer.alert('Require UID')
return
}
layer.confirm("{$confirmGiftMsg}" + uid + " ?", function (index) {
let params = {
action: "giftMedal",
params: {medal_id: medalId, uid: uid}
}
console.log(params)
jQuery.post('ajax.php', params, function(response) {
jQuery('body').loading('stop');
console.log(response)
if (response.ret != 0) {
layer.alert(response.msg)
Expand All @@ -122,6 +110,7 @@
})
})
JS;
\Nexus\Nexus::js('vendor/jquery-loading/jquery.loading.min.js', 'footer', true);
\Nexus\Nexus::js($js, 'footer', false);
stdfoot();

8 changes: 6 additions & 2 deletions resources/lang/en/exam.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,17 @@
'type' => 'Type',
'type_help' => 'Exam are regular exam and failing them will result in account banning. Tasks can be set to reward bonus or deduct bonus depending on whether they are completed or not',

'fail_deduct_bonus' => 'Deduct bonus for task failure',
'success_reward_bonus' => 'Reward bonus for task completion',
'fail_deduct_bonus' => 'Deduct bonus for failure',
'success_reward_bonus' => 'Reward bonus for 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',
'not_between_begin_end_time' => 'Not between begin & end time',
'reach_max_user_count' => 'The number of claimed users has reached its maximum',
'claimed_user_count' => 'Claimed',
'max_user_count' => 'Max claim user count(0 means unlimited)',
];
8 changes: 6 additions & 2 deletions resources/lang/zh_CN/exam.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,17 @@
'type' => '类型',
'type_help' => '考核是常规的考核,不通过会被封禁账号。任务可根据完成与否设置奖励魔力或扣除魔力',

'fail_deduct_bonus' => '任务失败扣除魔力',
'success_reward_bonus' => '任务完成奖励魔力',
'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' => '已经认领',
'not_between_begin_end_time' => '不在开始结束时间范围内',
'reach_max_user_count' => '认领人数已达上限',
'claimed_user_count' => '认领人数',
'max_user_count' => '最多认领人数(0表示无限制)',
];
8 changes: 6 additions & 2 deletions resources/lang/zh_TW/exam.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,17 @@
'type' => '类型',
'type_help' => '考核是常规的考核,不通过会被封禁账号。任务可根据完成与否设置奖励魔力或扣除魔力',

'fail_deduct_bonus' => '任务失败扣除魔力',
'success_reward_bonus' => '任务完成奖励魔力',
'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' => '已經認領',
'not_between_begin_end_time' => '不在開始結束時間範圍內',
'reach_max_user_count' => '認領人數已達上限',
'claimed_user_count' => '認領人數',
'max_user_count' => '最多認領人數(0表示無限製)',
];

0 comments on commit 6894508

Please sign in to comment.