Skip to content

Commit

Permalink
exam add user filter: register_days_range
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaomlove committed Nov 14, 2023
1 parent 4907b2f commit 8e649ff
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 3 deletions.
5 changes: 3 additions & 2 deletions app/Console/Commands/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ public function __construct()
*/
public function handle()
{
CleanupRepository::recordBatch(NexusDB::redis(), "1", "5");
CleanupRepository::runBatchJobCalculateUserSeedBonus("bbbbb");
$str = "2023-11-14 02:59:00";
$diff = Carbon::parse($str)->diffInDays(now());
dd($diff);
}

}
2 changes: 2 additions & 0 deletions app/Filament/Resources/System/ExamResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ public static function form(Form $form): Form
->label(__('label.user.class')),
Forms\Components\DateTimePicker::make('filters.register_time_range.0')->label(__("label.exam.register_time_range.begin")),
Forms\Components\DateTimePicker::make('filters.register_time_range.1')->label(__("label.exam.register_time_range.end")),
Forms\Components\TextInput::make('filters.register_days_range.0')->numeric()->label(__("label.exam.register_days_range.begin")),
Forms\Components\TextInput::make('filters.register_days_range.1')->numeric()->label(__("label.exam.register_days_range.end")),
Forms\Components\CheckboxList::make('filters.donate_status')
->options(self::$yesOrNo)
->label(__('label.exam.donated')),
Expand Down
15 changes: 15 additions & 0 deletions app/Models/Exam.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ class Exam extends NexusModel
const FILTER_USER_CLASS = 'classes';
const FILTER_USER_REGISTER_TIME_RANGE = 'register_time_range';
const FILTER_USER_DONATE = 'donate_status';
const FILTER_USER_REGISTER_DAYS_RANGE = 'register_days_range';

public static $filters = [
self::FILTER_USER_CLASS => ['name' => 'User class'],
self::FILTER_USER_REGISTER_TIME_RANGE => ['name' => 'User register time range'],
self::FILTER_USER_DONATE => ['name' => 'User donated'],
self::FILTER_USER_REGISTER_DAYS_RANGE => ['name' => 'User register days range'],
];

protected static function booted()
Expand Down Expand Up @@ -138,6 +140,19 @@ public function getFilterFormattedAttribute(): string
}
}

$filter = self::FILTER_USER_REGISTER_DAYS_RANGE;
if (!empty($currentFilters[$filter])) {
$range = $currentFilters[$filter];
if (!empty($range[0]) || !empty($range[1])) {
$arr[] = sprintf(
"%s: %s ~ %s",
nexus_trans("exam.filters.$filter"),
$range[0] ?? "--",
$range[1] ?? '--'
);
}
}

$filter = self::FILTER_USER_DONATE;
if (!empty($currentFilters[$filter])) {
$donateStatus = collect(User::$donateStatus)->only($currentFilters[$filter]);
Expand Down
50 changes: 50 additions & 0 deletions app/Repositories/ExamRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,30 @@ private function checkFilters(array $params)
throw new \InvalidArgumentException("Invalid user register time end: $end");
}
}
if ($begin && $end && $begin > $end) {
throw new \InvalidArgumentException("user register time begin must less than end");
}

$filter = Exam::FILTER_USER_REGISTER_DAYS_RANGE;
$begin = $filters[$filter][0] ?? null;
$end = $filters[$filter][1] ?? null;
if ($begin) {
if (is_numeric($begin) && $begin >= 0) {
$hasValid = true;
} else {
throw new \InvalidArgumentException("Invalid user register days begin: $begin" );
}
}
if ($end) {
if (is_numeric($end) && $end >= 0) {
$hasValid = true;
} else {
throw new \InvalidArgumentException("Invalid user register days end: $end");
}
}
if ($begin && $end && $begin > $end) {
throw new \InvalidArgumentException("user register days begin must less than end");
}

if (!$hasValid) {
throw new \InvalidArgumentException("No valid filters");
Expand Down Expand Up @@ -276,6 +300,20 @@ private function isExamMatchUser(Exam $exam, $user): bool
return false;
}

$filter = Exam::FILTER_USER_REGISTER_DAYS_RANGE;
$filterValues = $filters[$filter] ?? [];
$value = $user->added->diffInDays(now());
$begin = $filterValues[0] ?? null;
$end = $filterValues[1] ?? null;
if ($begin !== null && $value < $begin) {
do_log("$logPrefix, user registerDays: $value not bigger than begin: " . $begin);
return false;
}
if ($end !== null && $value > $end) {
do_log("$logPrefix, user registerDays: $value not less than end: " . $end);
return false;
}

return true;
}

Expand Down Expand Up @@ -953,6 +991,18 @@ public function fetchUserAndDoAssign(Exam $exam): bool|int
$baseQuery->where("$userTable.added", '<=', Carbon::parse($range[1])->toDateTimeString());
}
}

$filter = Exam::FILTER_USER_REGISTER_DAYS_RANGE;
$range = $filters[$filter] ?? [];
if (!empty($range)) {
if (!empty($range[0])) {
$baseQuery->where("$userTable.added", "<=", now()->subDays($range[0])->toDateTimeString());
}
if (!empty($range[1])) {
$baseQuery->where("$userTable.added", '>=', now()->subDays($range[1])->toDateTimeString());
}
}

//Does not has this exam
$baseQuery->whereDoesntHave('exams', function (Builder $query) use ($exam) {
$query->where('exam_id', $exam->id);
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-14');
defined('RELEASE_DATE') || define('RELEASE_DATE', '2023-11-15');
defined('IN_TRACKER') || define('IN_TRACKER', false);
defined('PROJECTNAME') || define("PROJECTNAME","NexusPHP");
defined('NEXUSPHPURL') || define("NEXUSPHPURL","https://nexusphp.org");
Expand Down
1 change: 1 addition & 0 deletions resources/lang/en/exam.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
\App\Models\Exam::FILTER_USER_CLASS => 'User class',
\App\Models\Exam::FILTER_USER_REGISTER_TIME_RANGE => 'Register time range',
\App\Models\Exam::FILTER_USER_DONATE => 'Donated',
\App\Models\Exam::FILTER_USER_REGISTER_DAYS_RANGE => 'Range of days of registration',
],
'require_value' => 'Require',
'current_value' => 'Current',
Expand Down
4 changes: 4 additions & 0 deletions resources/lang/en/label.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@
'begin' => 'Register time begin',
'end' => 'Register time end',
],
'register_days_range' => [
'begin' => 'Minimum days registered',
'end' => 'Maximum days registered',
],
'donated' => 'Donated',
'index_formatted' => 'Exam indexes',
'filter_formatted' => 'Target users',
Expand Down
1 change: 1 addition & 0 deletions resources/lang/zh_CN/exam.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
\App\Models\Exam::FILTER_USER_CLASS => '用户等级',
\App\Models\Exam::FILTER_USER_REGISTER_TIME_RANGE => '注册时间范围',
\App\Models\Exam::FILTER_USER_DONATE => '是否捐赠',
\App\Models\Exam::FILTER_USER_REGISTER_DAYS_RANGE => '注册天数范围',
],
'require_value' => '要求',
'current_value' => '当前',
Expand Down
4 changes: 4 additions & 0 deletions resources/lang/zh_CN/label.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@
'begin' => '注册时间开始',
'end' => '注册时间结束',
],
'register_days_range' => [
'begin' => '注册天数最少',
'end' => '注册天数最多',
],
'donated' => '是否捐赠',
'index_formatted' => '考核指标',
'filter_formatted' => '目标用户',
Expand Down
1 change: 1 addition & 0 deletions resources/lang/zh_TW/exam.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
\App\Models\Exam::FILTER_USER_CLASS => '用戶等級',
\App\Models\Exam::FILTER_USER_REGISTER_TIME_RANGE => '註冊時間範圍',
\App\Models\Exam::FILTER_USER_DONATE => '是否捐贈',
\App\Models\Exam::FILTER_USER_REGISTER_DAYS_RANGE => '註冊天數範圍',
],
'require_value' => '要求',
'current_value' => '當前',
Expand Down
4 changes: 4 additions & 0 deletions resources/lang/zh_TW/label.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@
'begin' => '註冊時間開始',
'end' => '註冊時間結束',
],
'register_days_range' => [
'begin' => '註冊天數最少',
'end' => '註冊天數最多',
],
'donated' => '是否捐贈',
'index_formatted' => '考核指標',
'filter_formatted' => '目標用戶',
Expand Down

0 comments on commit 8e649ff

Please sign in to comment.