Files
nexusphp/app/Repositories/ExamRepository.php

1349 lines
54 KiB
PHP
Raw Normal View History

2021-04-19 20:13:21 +08:00
<?php
namespace App\Repositories;
2021-04-28 01:22:25 +08:00
use App\Exceptions\NexusException;
2024-05-24 02:27:44 +08:00
use App\Models\BonusLogs;
2021-04-19 20:13:21 +08:00
use App\Models\Exam;
2021-04-25 21:28:58 +08:00
use App\Models\ExamProgress;
2021-04-25 02:12:14 +08:00
use App\Models\ExamUser;
use App\Models\Message;
use App\Models\Snatch;
2021-04-25 21:28:58 +08:00
use App\Models\Torrent;
2021-04-19 20:13:21 +08:00
use App\Models\User;
2021-05-12 13:45:00 +08:00
use App\Models\UserBanLog;
2025-01-27 14:07:44 +08:00
use App\Models\UserModifyLog;
2021-04-20 20:18:02 +08:00
use Carbon\Carbon;
2021-06-12 23:21:40 +08:00
use Illuminate\Database\Eloquent\Builder;
2021-04-23 20:05:39 +08:00
use Illuminate\Support\Arr;
2023-11-14 02:13:21 +08:00
use Illuminate\Support\Collection;
2022-07-18 01:37:50 +08:00
use Illuminate\Support\Facades\Auth;
2021-04-27 19:13:32 +08:00
use Illuminate\Support\Facades\DB;
use Nexus\Database\NexusDB;
2021-04-19 20:13:21 +08:00
class ExamRepository extends BaseRepository
{
public function getList(array $params)
{
$query = Exam::query();
$query->orderBy('priority', 'desc')->orderBy('id', 'asc');
2021-04-19 20:13:21 +08:00
return $query->paginate();
}
public function store(array $params)
{
$diffInHours = $this->checkBeginEnd($params);
$this->checkIndexes($params, $diffInHours);
2021-06-12 23:21:40 +08:00
$this->checkFilters($params);
2022-04-17 16:38:44 +08:00
/**
* does not limit this
* @since 1.7.4
*/
// $valid = $this->listValid(null, Exam::DISCOVERED_YES);
// if ($valid->isNotEmpty() && $params['status'] == Exam::STATUS_ENABLED) {
// throw new NexusException("Enabled and discovered exam already exists.");
// }
$exam = Exam::query()->create($this->formatParams($params));
2021-04-19 20:13:21 +08:00
return $exam;
}
public function update(array $params, $id)
{
$diffInHours = $this->checkBeginEnd($params);
$this->checkIndexes($params, $diffInHours);
2021-06-12 23:21:40 +08:00
$this->checkFilters($params);
2022-04-17 16:38:44 +08:00
/**
* does not limit this
* @since 1.7.4
*/
// $valid = $this->listValid($id, Exam::DISCOVERED_YES);
// if ($valid->isNotEmpty() && $params['status'] == Exam::STATUS_ENABLED) {
// throw new NexusException("Enabled and discovered exam already exists.");
// }
2021-04-19 20:13:21 +08:00
$exam = Exam::query()->findOrFail($id);
2022-04-17 16:38:44 +08:00
$exam->update($this->formatParams($params));
2021-04-19 20:13:21 +08:00
return $exam;
}
2022-04-17 16:38:44 +08:00
private function formatParams(array $params): array
{
if (isset($params['begin']) && $params['begin'] == '') {
$params['begin'] = null;
}
if (isset($params['end']) && $params['end'] == '') {
$params['end'] = null;
}
2022-07-02 15:08:23 +08:00
$params['priority'] = intval($params['priority'] ?? 0);
2022-04-17 16:38:44 +08:00
return $params;
}
private function checkIndexes(array $params, float $examDuration): bool
2021-04-25 02:12:14 +08:00
{
if (empty($params['indexes'])) {
throw new \InvalidArgumentException("Require index.");
}
2021-05-02 17:24:05 +08:00
$validIndex = [];
foreach ($params['indexes'] as $index) {
2021-05-07 18:30:58 +08:00
if (isset($index['checked']) && !$index['checked']) {
2021-05-02 17:24:05 +08:00
continue;
}
2022-07-02 15:08:23 +08:00
if (isset($validIndex[$index['index']])) {
throw new \InvalidArgumentException(nexus_trans('admin.resources.exam.index_duplicate', ['index' => nexus_trans("exam.index_text_{$index['index']}")]));
}
2021-05-08 16:25:55 +08:00
if (isset($index['require_value']) && !ctype_digit((string)$index['require_value'])) {
2021-05-02 17:24:05 +08:00
throw new \InvalidArgumentException(sprintf(
2021-05-07 18:30:58 +08:00
'Invalid require value for index: %s.', $index['index']
2021-05-02 17:24:05 +08:00
));
}
if ($index['index'] == Exam::INDEX_SEED_TIME_AVERAGE) {
if ($index['require_value'] > $examDuration) {
throw new \InvalidArgumentException(nexus_trans(
'admin.resources.exam.index_seed_time_average_require_value_invalid',
['index_seed_time_average_require_value' => $index['require_value'], 'duration' => $examDuration]
));
}
}
2022-07-02 15:08:23 +08:00
$validIndex[$index['index']] = $index;
2021-05-02 17:24:05 +08:00
}
2021-04-25 02:12:14 +08:00
if (empty($validIndex)) {
throw new \InvalidArgumentException("Require valid index.");
}
return true;
}
/**
* check if begin/end valid, if yes, return diff in hours, else throw InvalidArgumentException
* @param array $params
* @return float
*/
private function checkBeginEnd(array $params): float
2021-05-06 13:01:17 +08:00
{
2024-04-13 13:55:32 +08:00
if (
!empty($params['begin']) && !empty($params['end'])
&& empty($params['duration'])
&& empty($params['recurring'])
) {
$begin = Carbon::parse($params['begin']);
$end = Carbon::parse($params['end']);
return round($begin->diffInHours($end, true));
2021-05-06 13:01:17 +08:00
}
2024-04-13 13:55:32 +08:00
if (
empty($params['begin']) && empty($params['end'])
&& isset($params['duration']) && ctype_digit((string)$params['duration']) && $params['duration'] > 0
&& empty($params['recurring'])
) {
//unit: day
return round(floatval($params['duration']) * 24);
2024-04-13 13:55:32 +08:00
}
if (
empty($params['begin']) && empty($params['end'])
&& empty($params['duration'])
&& !empty($params['recurring'])
) {
$exam = new Exam(['recurring' => $params['recurring']]);
$now = Carbon::now();
$begin = $exam->getRecurringBegin($now);
$end = $exam->getRecurringEnd($now);
return round($begin->diffInHours($end, true));
2021-05-06 13:01:17 +08:00
}
2024-04-13 13:55:32 +08:00
throw new \InvalidArgumentException(nexus_trans("exam.time_condition_invalid"));
2021-05-06 13:01:17 +08:00
}
2021-06-12 23:21:40 +08:00
private function checkFilters(array $params)
{
$filters = $params['filters'];
$hasValid = false;
$filter = Exam::FILTER_USER_CLASS;
if (!empty($filters[$filter])) {
$hasValid = true;
$diff = array_diff($filters[$filter], array_keys(User::$classes));
if (!empty($diff)) {
throw new \InvalidArgumentException(sprintf('Invalid user class: %s', json_encode($diff)));
}
}
$filter = Exam::FILTER_USER_DONATE;
if (!empty($filters[$filter])) {
$hasValid = true;
$diff = array_diff($filters[$filter], array_keys(User::$donateStatus));
if (!empty($diff)) {
throw new \InvalidArgumentException(sprintf('Invalid user donate status: %s', json_encode($diff)));
}
}
$filter = Exam::FILTER_USER_REGISTER_TIME_RANGE;
$begin = $filters[$filter][0] ?? null;
$end = $filters[$filter][1] ?? null;
if ($begin) {
if (strtotime($begin)) {
$hasValid = true;
} else {
throw new \InvalidArgumentException("Invalid user register time begin: $begin" );
}
}
if ($end) {
if (strtotime($end)) {
$hasValid = true;
} else {
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");
}
2021-06-12 23:21:40 +08:00
if (!$hasValid) {
throw new \InvalidArgumentException("No valid filters");
}
return true;
}
2021-04-23 20:05:39 +08:00
public function getDetail($id)
{
$exam = Exam::query()->findOrFail($id);
return $exam;
}
2021-06-11 02:21:42 +08:00
/**
* delete an exam task, also will delete all exam user and progress.
*
* @param $id
* @return bool
*/
2021-04-25 02:12:14 +08:00
public function delete($id)
2021-04-20 20:18:02 +08:00
{
2021-04-25 02:12:14 +08:00
$exam = Exam::query()->findOrFail($id);
DB::transaction(function () use ($exam) {
2021-06-11 02:21:42 +08:00
do {
$deleted = ExamUser::query()->where('exam_id', $exam->id)->limit(10000)->delete();
} while ($deleted > 0);
do {
$deleted = ExamProgress::query()->where('exam_id', $exam->id)->limit(10000)->delete();
} while ($deleted > 0);
$exam->delete();
});
return true;
2021-04-23 20:05:39 +08:00
}
2021-04-25 02:12:14 +08:00
public function listIndexes()
2021-04-23 20:05:39 +08:00
{
$out = [];
2021-04-25 02:12:14 +08:00
foreach(Exam::$indexes as $key => $value) {
$value['index'] = $key;
2021-04-23 20:05:39 +08:00
$out[] = $value;
2021-04-20 20:18:02 +08:00
}
return $out;
}
2021-04-21 01:13:24 +08:00
/**
2021-04-25 02:12:14 +08:00
* list valid exams
2021-04-21 01:13:24 +08:00
*
* @param null $excludeId
2021-04-25 02:12:14 +08:00
* @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
2021-04-21 01:13:24 +08:00
*/
2024-05-18 14:53:30 +08:00
public function listValid($excludeId = null, $isDiscovered = null, $type = null)
2021-04-20 20:18:02 +08:00
{
$now = Carbon::now();
$query = Exam::query()
2021-04-20 20:18:02 +08:00
->where('status', Exam::STATUS_ENABLED)
2024-04-13 13:55:32 +08:00
->whereRaw("if(begin is not null and end is not null, begin <= '$now' and end >= '$now', duration > 0 or recurring is not null)")
2022-04-17 16:38:44 +08:00
;
2021-05-02 17:24:05 +08:00
if (!is_null($excludeId)) {
$query->whereNotIn('id', Arr::wrap($excludeId));
}
2021-05-02 17:24:05 +08:00
if (!is_null($isDiscovered)) {
$query->where('is_discovered', $isDiscovered);
}
2024-05-18 14:53:30 +08:00
if (!is_null($type)) {
$query->where("type", $type);
}
2022-04-18 20:31:01 +08:00
return $query->orderBy('priority', 'desc')->orderBy('id', 'asc')->get();
2021-04-25 02:12:14 +08:00
}
/**
* list user match exams
*
* @param $uid
* @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
*/
public function listMatchExam($uid)
{
2024-05-18 14:53:30 +08:00
$exams = $this->listValid(null, null, Exam::TYPE_EXAM);
return $this->filterForUser($exams, $uid);
}
2021-04-25 02:12:14 +08:00
2024-05-18 14:53:30 +08:00
public function listMatchTask($uid)
{
$exams = $this->listValid(null, null, Exam::TYPE_TASK);
return $this->filterForUser($exams, $uid);
}
private function filterForUser(Collection $exams, $uid): Collection
{
$userInfo = User::query()->findOrFail($uid, User::$commonFields);
return $exams->filter(function (Exam $exam) use ($userInfo) {
return $this->isExamMatchUser($exam, $userInfo);
});
}
2021-04-25 02:12:14 +08:00
2024-05-18 14:53:30 +08:00
2021-06-12 23:21:40 +08:00
private function isExamMatchUser(Exam $exam, $user): bool
{
if (!$user instanceof User) {
$user = User::query()->findOrFail(intval($user), ['id', 'username', 'added', 'class']);
}
$logPrefix = sprintf('exam: %s, user: %s', $exam->id, $user->id);
$filters = $exam->filters;
$filter = Exam::FILTER_USER_CLASS;
2023-05-10 02:25:26 +08:00
$filterValues = $filters[$filter] ?? [];
2023-04-29 03:46:14 +08:00
if (!empty($filterValues) && !in_array($user->class, $filterValues)) {
do_log("$logPrefix, user class: {$user->class} not in: " . json_encode($filterValues));
return false;
}
$filter = Exam::FILTER_USER_DONATE;
2023-05-10 02:25:26 +08:00
$filterValues = $filters[$filter] ?? [];
2023-04-29 03:46:14 +08:00
if (!empty($filterValues) && !in_array($user->donate_status, $filterValues)) {
do_log("$logPrefix, user donate status: {$user->donate_status} not in: " . json_encode($filterValues));
return false;
}
$filter = Exam::FILTER_USER_REGISTER_TIME_RANGE;
2023-05-10 02:25:26 +08:00
$filterValues = $filters[$filter] ?? [];
$added = $user->added->toDateTimeString();
2023-04-29 03:46:14 +08:00
$registerTimeBegin = isset($filterValues[0]) ? Carbon::parse($filterValues[0])->toDateTimeString() : '';
$registerTimeEnd = isset($filterValues[1]) ? Carbon::parse($filterValues[1])->toDateTimeString() : '';
2021-06-12 23:21:40 +08:00
if (!empty($registerTimeBegin) && $added < $registerTimeBegin) {
do_log("$logPrefix, user added: $added not bigger than begin: " . $registerTimeBegin);
return false;
}
2021-06-12 23:21:40 +08:00
if (!empty($registerTimeEnd) && $added > $registerTimeEnd) {
do_log("$logPrefix, user added: $added not less than end: " . $registerTimeEnd);
return false;
}
2021-04-25 02:12:14 +08:00
$filter = Exam::FILTER_USER_REGISTER_DAYS_RANGE;
$filterValues = $filters[$filter] ?? [];
2025-01-19 23:41:50 +08:00
$value = $user->added->diffInDays(now(), true);
$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;
}
2024-05-09 10:26:20 +08:00
try {
$user->checkIsNormal();
return true;
} catch (\Throwable $throwable) {
do_log("$logPrefix, user is not normal: " . $throwable->getMessage());
return false;
}
2021-04-25 02:12:14 +08:00
}
2021-04-25 21:28:58 +08:00
2021-04-25 02:12:14 +08:00
/**
* assign exam to user
*
2021-04-27 19:13:32 +08:00
* @param int $uid
2021-05-02 17:24:05 +08:00
* @param int $examId
2021-04-26 20:37:17 +08:00
* @param null $begin
* @param null $end
2021-04-25 02:12:14 +08:00
* @return mixed
*/
2021-05-02 17:24:05 +08:00
public function assignToUser(int $uid, int $examId, $begin = null, $end = null)
2021-04-25 02:12:14 +08:00
{
2021-04-26 20:37:17 +08:00
$logPrefix = "uid: $uid, examId: $examId, begin: $begin, end: $end";
2024-04-13 13:55:32 +08:00
/** @var Exam $exam */
2021-05-02 17:24:05 +08:00
$exam = Exam::query()->find($examId);
2021-04-25 02:12:14 +08:00
$user = User::query()->findOrFail($uid);
2024-06-07 02:51:05 +08:00
$locale = $user->locale;
$authUserClass = get_user_class();
$authUserId = get_user_id();
2024-08-08 03:14:34 +08:00
$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));
}
}
2024-06-07 02:51:05 +08:00
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));
}
2024-08-08 03:14:34 +08:00
if ($exam->max_user_count > 0) {
2024-08-09 21:40:00 +08:00
$claimUserCount = $exam->onGoingUsers()->count();
2024-08-08 03:14:34 +08:00
if ($claimUserCount >= $exam->max_user_count) {
throw new NexusException(nexus_trans('exam.reach_max_user_count', [], $locale));
}
}
2022-07-18 01:37:50 +08:00
}
2024-06-07 02:51:05 +08:00
2021-04-29 19:18:13 +08:00
if (!$this->isExamMatchUser($exam, $user)) {
2024-06-07 02:51:05 +08:00
throw new NexusException(nexus_trans('exam.not_match_target_user', [], $locale));
2021-04-29 19:18:13 +08:00
}
2021-04-29 02:52:22 +08:00
if ($user->exams()->where('status', ExamUser::STATUS_NORMAL)->exists()) {
2024-06-07 02:51:05 +08:00
throw new NexusException(nexus_trans('exam.has_other_on_the_way', ['type_text' => $exam->typeText], $locale));
2021-04-29 02:52:22 +08:00
}
2024-08-03 18:36:59 +08:00
$exists = ExamUser::query()
->where("uid", $uid)
->where('exam_id', $exam->id)
->where("status", ExamUser::STATUS_NORMAL)
->exists()
;
2021-04-25 02:12:14 +08:00
if ($exists) {
2024-06-07 02:51:05 +08:00
throw new NexusException(nexus_trans('exam.claimed_already', [], $locale));
2021-04-20 20:18:02 +08:00
}
2021-04-26 20:37:17 +08:00
$data = [
'exam_id' => $exam->id,
];
2023-11-14 02:13:21 +08:00
if (empty($begin)) {
2024-04-13 13:55:32 +08:00
$begin = $exam->getBeginForUser();
2023-11-14 02:13:21 +08:00
} else {
$begin = Carbon::parse($begin);
}
if (empty($end)) {
2024-04-13 13:55:32 +08:00
$end = $exam->getEndForUser();
2023-11-14 02:13:21 +08:00
} else {
$end = Carbon::parse($end);
2021-04-26 20:37:17 +08:00
}
2023-11-14 02:13:21 +08:00
$data['begin'] = $begin;
$data['end'] = $end;
2021-04-26 20:37:17 +08:00
do_log("$logPrefix, data: " . nexus_json_encode($data));
2021-06-11 02:21:42 +08:00
$examUser = $user->exams()->create($data);
$this->updateProgress($examUser, $user);
2021-06-11 02:21:42 +08:00
return $examUser;
2021-04-20 20:18:02 +08:00
}
2021-04-25 02:12:14 +08:00
public function listUser(array $params)
{
$query = ExamUser::query();
if (!empty($params['uid'])) {
$query->where('uid', $params['uid']);
}
if (!empty($params['exam_id'])) {
$query->where('exam_id', $params['exam_id']);
}
2022-03-04 16:16:56 +08:00
if (isset($params['is_done']) && is_numeric($params['is_done'])) {
$query->where('is_done', $params['is_done']);
}
if (isset($params['status']) && is_numeric($params['status'])) {
$query->where('status', $params['status']);
}
2021-04-25 02:12:14 +08:00
list($sortField, $sortType) = $this->getSortFieldAndType($params);
$query->orderBy($sortField, $sortType);
$result = $query->with(['user', 'exam'])->paginate();
return $result;
}
2021-06-13 20:53:14 +08:00
/**
* @deprecated old version used
* @param int $uid
* @param int $torrentId
* @param array $indexAndValue
* @return bool
* @throws NexusException
*/
2021-04-29 02:52:22 +08:00
public function addProgress(int $uid, int $torrentId, array $indexAndValue)
2021-04-25 21:28:58 +08:00
{
2021-04-29 02:52:22 +08:00
$logPrefix = "uid: $uid, torrentId: $torrentId, indexAndValue: " . json_encode($indexAndValue);
do_log($logPrefix);
$user = User::query()->findOrFail($uid);
$user->checkIsNormal();
$now = Carbon::now()->toDateTimeString();
$examUser = $user->exams()->where('status', ExamUser::STATUS_NORMAL)->orderBy('id', 'desc')->first();
if (!$examUser) {
do_log("no exam is on the way, " . last_query());
return false;
2021-04-25 21:28:58 +08:00
}
$exam = $examUser->exam;
2021-04-29 02:52:22 +08:00
if (!$exam) {
throw new NexusException("exam: {$examUser->exam_id} not exists.");
2021-04-25 21:28:58 +08:00
}
2021-05-06 01:49:05 +08:00
$begin = $examUser->begin;
$end = $examUser->end;
if (!$begin || !$end) {
do_log(sprintf("no begin or end, examUser: %s", $examUser->toJson()));
return false;
}
2021-04-29 02:52:22 +08:00
if ($now < $begin || $now > $end) {
do_log(sprintf("now: %s, not in exam time range: %s ~ %s", $now, $begin, $end));
return false;
2021-04-25 21:28:58 +08:00
}
2021-04-29 02:52:22 +08:00
$indexes = collect($exam->indexes)->keyBy('index');
do_log("examUser: " . $examUser->toJson() . ", indexes: " . $indexes->toJson());
2021-05-05 22:28:19 +08:00
if (!isset($indexAndValue[Exam::INDEX_SEED_BONUS])) {
//seed bonus is relative to user all torrents, not single one, torrentId = 0
$torrentFields = ['id', 'visible', 'banned'];
$torrent = Torrent::query()->findOrFail($torrentId, $torrentFields);
$torrent->checkIsNormal($torrentFields);
}
2021-04-25 21:28:58 +08:00
2021-04-29 02:52:22 +08:00
$insert = [];
foreach ($indexAndValue as $indexId => $value) {
if (!$indexes->has($indexId)) {
do_log(sprintf('Exam: %s does not has index: %s.', $exam->id, $indexId));
continue;
}
$indexInfo = $indexes->get($indexId);
if (!isset($indexInfo['checked']) || !$indexInfo['checked']) {
do_log(sprintf('Exam: %s index: %s is not checked.', $exam->id, $indexId));
continue;
}
$insert[] = [
'exam_user_id' => $examUser->id,
'uid' => $user->id,
'exam_id' => $exam->id,
'torrent_id' => $torrentId,
'index' => $indexId,
'value' => $value,
'created_at' => $now,
'updated_at' => $now,
];
}
if (empty($insert)) {
do_log("no progress to insert.");
return false;
}
ExamProgress::query()->insert($insert);
do_log("[addProgress] " . nexus_json_encode($insert));
2021-04-25 21:28:58 +08:00
2021-06-09 15:11:02 +08:00
/**
* Updating progress is more performance intensive and will only be done with a certain probability
*/
$probability = (int)nexus_env('EXAM_PROGRESS_UPDATE_PROBABILITY', 60);
$random = mt_rand(1, 100);
do_log("probability: $probability, random: $random");
if ($random > $probability) {
do_log("[SKIP_UPDATE_PROGRESS], random: $random > probability: $probability", 'warning');
return true;
}
$examProgress = $this->calculateProgress($examUser);
$examProgressFormatted = $this->getProgressFormatted($exam, $examProgress);
$examNotPassed = array_filter($examProgressFormatted, function ($item) {
return !$item['passed'];
});
$update = [
'progress' => $examProgress,
'is_done' => count($examNotPassed) ? ExamUser::IS_DONE_NO : ExamUser::IS_DONE_YES,
];
2021-04-29 02:52:22 +08:00
do_log("[updateProgress] " . nexus_json_encode($update));
$examUser->update($update);
2021-04-29 02:52:22 +08:00
return true;
2021-04-25 21:28:58 +08:00
}
2021-06-12 23:21:40 +08:00
/**
* in exam_progress table
* old version: value is an increment
* new version: both value and init_value are cumulative, increment = value - init_value
*
* in exam_users table, progress field always is increment
* old version: progress = sum(exam_progress.value)
* new versionprogress = exam_progress.value - exam_progress.init_value
*
* @param $examUser
* @param null $user
* @return ExamUser|bool
2021-06-12 23:21:40 +08:00
*/
public function updateProgress($examUser, $user = null): ExamUser|bool
2021-06-11 02:21:42 +08:00
{
2021-06-13 20:53:14 +08:00
$beginTimestamp = microtime(true);
2021-06-11 02:21:42 +08:00
if (!$examUser instanceof ExamUser) {
2021-06-12 23:21:40 +08:00
$uid = intval($examUser);
$examUser = ExamUser::query()
->where('uid', $uid)
->where('status', ExamUser::STATUS_NORMAL)
->get();
if ($examUser->isEmpty()) {
do_log("user: $uid no exam.");
return false;
}
if ($examUser->count() > 1) {
do_log("user: $uid more than one active exam.");
return false;
}
$examUser = $examUser->first();
2021-06-11 02:21:42 +08:00
}
2021-06-14 01:54:19 +08:00
if ($examUser->status != ExamUser::STATUS_NORMAL) {
do_log("examUser: {$examUser->id} status not normal, won't update progress.");
return false;
}
2021-06-14 12:49:16 +08:00
if ($examUser->is_done == ExamUser::IS_DONE_YES) {
2022-04-04 17:26:26 +08:00
/**
* continue update
* @since v1.7.0
*/
// do_log("examUser: {$examUser->id} is done, won't update progress.");
// return false;
2021-06-14 12:49:16 +08:00
}
2021-06-11 02:21:42 +08:00
$exam = $examUser->exam;
if (!$user instanceof User) {
$user = $examUser->user()->select(['id', 'uploaded', 'downloaded', 'seedtime', 'leechtime', 'seedbonus', 'seed_points'])->first();
2021-06-11 02:21:42 +08:00
}
$attributes = [
'exam_user_id' => $examUser->id,
2021-06-11 02:21:42 +08:00
'uid' => $user->id,
'exam_id' => $exam->id,
];
$logPrefix = json_encode($attributes);
$begin = $examUser->begin;
if (empty($begin)) {
throw new \InvalidArgumentException("$logPrefix, exam: {$examUser->id} no begin.");
}
$end = $examUser->end;
if (empty($end)) {
throw new \InvalidArgumentException("$logPrefix, exam: {$examUser->id} no end.");
}
2023-11-14 02:13:21 +08:00
/**
* @var $progressGrouped Collection
*/
$progressGrouped = $examUser->progresses->keyBy("index");
2021-06-12 23:21:40 +08:00
$examUserProgressFieldData = [];
2021-06-13 20:53:14 +08:00
$now = now();
2021-06-12 23:21:40 +08:00
foreach ($exam->indexes as $index) {
2021-06-11 02:21:42 +08:00
if (!isset($index['checked']) || !$index['checked']) {
continue;
}
2023-11-14 02:13:21 +08:00
if ($progressGrouped->isNotEmpty() && !$progressGrouped->has($index['index'])) {
continue;
}
2021-06-12 23:21:40 +08:00
if (!isset(Exam::$indexes[$index['index']])) {
$msg = "Unknown index: {$index['index']}";
do_log("$logPrefix, $msg", 'error');
throw new \RuntimeException($msg);
2021-06-11 02:21:42 +08:00
}
2021-06-13 20:53:14 +08:00
do_log("$logPrefix, [HANDLING INDEX {$index['index']}]: " . json_encode($index));
2021-06-12 23:21:40 +08:00
//First, collect data to store/update in table: exam_progress
$attributes['index'] = $index['index'];
2021-06-13 20:53:14 +08:00
$attributes['created_at'] = $now;
$attributes['updated_at'] = $now;
$attributes['value'] = $this->getProgressValue($user, $index['index'], $examUser);
2021-06-13 20:53:14 +08:00
do_log("[GET_TOTAL_VALUE]: " . $attributes['value']);
2021-06-12 23:21:40 +08:00
$newVersionProgress = ExamProgress::query()
->where('exam_user_id', $examUser->id)
->where('torrent_id', -1)
->where('index', $index['index'])
->orderBy('id', 'desc')
->first();
2021-06-12 23:21:40 +08:00
do_log("check newVersionProgress: " . last_query() . ", exists: " . json_encode($newVersionProgress));
if ($newVersionProgress) {
//just need to do update the value
if ($attributes['value'] != $newVersionProgress->value) {
$newVersionProgress->update(['value' => $attributes['value']]);
2021-06-13 20:53:14 +08:00
do_log("newVersionProgress [EXISTS], doUpdate: " . last_query());
2021-06-12 23:21:40 +08:00
} else {
2021-06-13 20:53:14 +08:00
do_log("newVersionProgress [EXISTS], no change....");
2021-06-12 23:21:40 +08:00
}
$attributes['init_value'] = $newVersionProgress->init_value;
} else {
//do insert.
$attributes['init_value'] = $attributes['value'];
$attributes['torrent_id'] = -1;
ExamProgress::query()->insert($attributes);
2021-06-13 20:53:14 +08:00
do_log("newVersionProgress [NOT EXISTS], doInsert with: " . json_encode($attributes));
2021-06-12 23:21:40 +08:00
}
//Second, update exam_user.progress
if ($index['index'] == Exam::INDEX_SEED_TIME_AVERAGE) {
$torrentCountsRes = Snatch::query()
->where('userid', $user->id)
->where('last_action', '>=', $begin)
->where('last_action', '<=', $end)
2021-06-12 23:21:40 +08:00
->selectRaw("count(distinct(torrentid)) as counts")
->first();
2021-06-13 20:53:14 +08:00
do_log("special index: {$index['index']}, get torrent count by: " . last_query());
//if just seeding, no download torrent, counts = 1
if ($torrentCountsRes && $torrentCountsRes->counts > 0) {
$torrentCounts = $torrentCountsRes->counts;
do_log("torrent count: $torrentCounts");
2021-06-12 23:21:40 +08:00
} else {
2021-06-13 20:53:14 +08:00
$torrentCounts = 1;
do_log("torrent count is 0, use 1");
2021-06-12 23:21:40 +08:00
}
2021-06-13 20:53:14 +08:00
$examUserProgressFieldData[$index['index']] = bcdiv(bcsub($attributes['value'], $attributes['init_value']), $torrentCounts);
do_log(sprintf(
"torrentCounts > 0, examUserProgress: (total(%s) - init_value(%s)) / %s = %s",
$attributes['value'], $attributes['init_value'], $torrentCounts, $examUserProgressFieldData[$index['index']]
));
2021-06-12 23:21:40 +08:00
} else {
2021-06-13 20:53:14 +08:00
$examUserProgressFieldData[$index['index']] = bcsub($attributes['value'], $attributes['init_value']);
2021-06-12 23:21:40 +08:00
do_log(sprintf(
"normal index: {$index['index']}, examUserProgress: total(%s) - init_value(%s) = %s",
$attributes['value'], $attributes['init_value'], $examUserProgressFieldData[$index['index']]
));
}
2021-06-11 02:21:42 +08:00
}
2021-06-12 23:21:40 +08:00
$examProgressFormatted = $this->getProgressFormatted($exam, $examUserProgressFieldData);
$examNotPassed = array_filter($examProgressFormatted, function ($item) {
return !$item['passed'];
});
2021-06-12 23:21:40 +08:00
$update = [
2021-06-12 23:21:40 +08:00
'progress' => $examUserProgressFieldData,
'is_done' => count($examNotPassed) ? ExamUser::IS_DONE_NO : ExamUser::IS_DONE_YES,
];
2021-06-12 23:21:40 +08:00
$result = $examUser->update($update);
2021-06-13 20:53:14 +08:00
do_log(sprintf(
"[UPDATE_PROGRESS] %s, result: %s, cost time: %s sec",
json_encode($update), var_export($result, true), sprintf('%.3f', microtime(true) - $beginTimestamp)
));
2021-06-14 01:54:19 +08:00
$examUser->progress_formatted = $examProgressFormatted;
return $examUser;
2021-06-11 02:21:42 +08:00
}
private function getProgressValue(User $user, int $index, ExamUser $examUser)
{
if ($index == Exam::INDEX_UPLOADED) {
return $user->uploaded;
}
if ($index == Exam::INDEX_DOWNLOADED) {
return $user->downloaded;
}
if ($index == Exam::INDEX_SEED_BONUS) {
return $user->seedbonus;
}
if ($index == Exam::INDEX_SEED_TIME_AVERAGE) {
return $user->seedtime;
}
if ($index == Exam::INDEX_SEED_POINTS) {
return $user->seed_points;
}
if ($index == Exam::INDEX_UPLOAD_TORRENT_COUNT) {
return Torrent::query()->where("owner", $user->id)->where("added", ">=", $examUser->created_at)->normal()->count();
}
throw new \InvalidArgumentException("Invalid index: $index");
}
2021-06-12 23:21:40 +08:00
/**
* get user exam status
*
* @param $uid
* @param null $status
* @return mixed|null
*/
2021-06-14 01:54:19 +08:00
public function getUserExamProgress($uid, $status = null)
2021-04-25 21:28:58 +08:00
{
$logPrefix = "uid: $uid";
2021-04-27 19:13:32 +08:00
$query = ExamUser::query()->where('uid', $uid)->orderBy('exam_id', 'desc');
if (!is_null($status)) {
2021-04-25 21:28:58 +08:00
$query->where('status', $status);
}
2021-04-26 20:37:17 +08:00
$examUsers = $query->get();
if ($examUsers->isEmpty()) {
2021-06-10 02:16:08 +08:00
do_log("$logPrefix, no examUser, query: " . last_query());
2021-04-26 20:37:17 +08:00
return null;
2021-04-25 21:28:58 +08:00
}
2021-04-26 20:37:17 +08:00
if ($examUsers->count() > 1) {
do_log("$logPrefix, user exam more than 1.", 'warning');
2021-04-25 21:28:58 +08:00
}
2021-04-26 20:37:17 +08:00
$examUser = $examUsers->first();
2021-06-14 01:54:19 +08:00
$logPrefix .= ", examUser: " . $examUser->id;
try {
$updateResult = $this->updateProgress($examUser);
if ($updateResult) {
2021-06-14 02:12:21 +08:00
do_log("$logPrefix, [UPDATE_PROGRESS_SUCCESS_RETURN_DIRECTLY]");
2021-06-14 01:54:19 +08:00
return $updateResult;
} else {
2021-06-14 02:12:21 +08:00
do_log("$logPrefix, [UPDATE_PROGRESS_FAIL]");
2021-06-14 01:54:19 +08:00
}
} catch (\Exception $exception) {
2021-06-14 02:12:21 +08:00
do_log("$logPrefix, [UPDATE_PROGRESS_FAIL]: " . $exception->getMessage(), 'error');
2021-06-14 01:54:19 +08:00
}
2021-04-27 19:13:32 +08:00
$exam = $examUser->exam;
$progress = $examUser->progress;
do_log("$logPrefix, progress: " . nexus_json_encode($progress));
$examUser->progress = $progress;
$examUser->progress_formatted = $this->getProgressFormatted($exam, (array)$progress);
return $examUser;
}
2021-06-12 23:21:40 +08:00
/**
* @deprecated
2021-06-12 23:21:40 +08:00
* @param ExamUser $examUser
* @param bool $allSum
2021-06-12 23:21:40 +08:00
* @return array|null
*/
public function calculateProgress(ExamUser $examUser, bool $allSum = false)
{
2021-04-29 02:52:22 +08:00
$logPrefix = "examUser: " . $examUser->id;
2021-05-06 01:49:05 +08:00
$begin = $examUser->begin;
$end = $examUser->end;
if (!$begin) {
2021-04-26 20:37:17 +08:00
do_log("$logPrefix, no begin");
return null;
}
2021-05-06 01:49:05 +08:00
if (!$end) {
2021-04-26 20:37:17 +08:00
do_log("$logPrefix, no end");
return null;
}
$progressSum = $examUser->progresses()
->where('created_at', '>=', $begin)
->where('created_at', '<=', $end)
->selectRaw("`index`, sum(`value`) as sum")
->groupBy(['index'])
2021-04-29 02:52:22 +08:00
->get()
->pluck('sum', 'index')
->toArray();
$logPrefix .= ", progressSum raw: " . json_encode($progressSum) . ", query: " . last_query();
2021-06-12 23:21:40 +08:00
if ($allSum) {
2021-06-15 01:46:36 +08:00
do_log($logPrefix);
2021-06-12 23:21:40 +08:00
return $progressSum;
}
2021-04-29 02:52:22 +08:00
$index = Exam::INDEX_SEED_TIME_AVERAGE;
if (isset($progressSum[$index])) {
$torrentCount = $examUser->progresses()
->where('index', $index)
->where('torrent_id', '>=', 0)
2021-04-29 02:52:22 +08:00
->selectRaw('count(distinct(torrent_id)) as torrent_count')
->first()
->torrent_count;
$progressSum[$index] = intval($progressSum[$index] / $torrentCount);
$logPrefix .= ", index: INDEX_SEED_TIME_AVERAGE, get torrent count: $torrentCount, from query: " . last_query();
2021-04-29 02:52:22 +08:00
}
2021-04-25 21:28:58 +08:00
2021-04-29 02:52:22 +08:00
do_log("$logPrefix, final progressSum: " . json_encode($progressSum));
2021-04-29 02:52:22 +08:00
return $progressSum;
2021-04-25 21:28:58 +08:00
}
public function getProgressFormatted(Exam $exam, array $progress, $locale = null)
2021-04-27 19:13:32 +08:00
{
$result = [];
foreach ($exam->indexes as $key => $index) {
if (!isset($index['checked']) || !$index['checked']) {
continue;
}
2023-11-14 02:13:21 +08:00
if (!isset($progress[$index['index']])) {
continue;
}
2021-04-27 19:13:32 +08:00
$currentValue = $progress[$index['index']] ?? 0;
$requireValue = $index['require_value'];
$unit = Exam::$indexes[$index['index']]['unit'] ?? '';
2021-04-27 19:13:32 +08:00
switch ($index['index']) {
case Exam::INDEX_UPLOADED:
case Exam::INDEX_DOWNLOADED:
$currentValueFormatted = mksize($currentValue);
$requireValueAtomic = $requireValue * 1024 * 1024 * 1024;
break;
case Exam::INDEX_SEED_TIME_AVERAGE:
$currentValueFormatted = number_format($currentValue / 3600, 2) . " $unit";
2021-04-27 19:13:32 +08:00
$requireValueAtomic = $requireValue * 3600;
break;
default:
$currentValueFormatted = $currentValue;
$requireValueAtomic = $requireValue;
}
2021-05-02 17:24:05 +08:00
$index['name'] = Exam::$indexes[$index['index']]['name'] ?? '';
$index['index_formatted'] = nexus_trans('exam.index_text_' . $index['index']);
$index['require_value_formatted'] = "$requireValue $unit";
2021-04-27 19:13:32 +08:00
$index['current_value'] = $currentValue;
$index['current_value_formatted'] = $currentValueFormatted;
$index['passed'] = $currentValue >= $requireValueAtomic;
2025-02-25 02:05:49 +08:00
$index['index_result'] = $index['passed'] ? nexus_trans($exam->getPassResultTransKey('pass')) : nexus_trans($exam->getPassResultTransKey('not_pass'));
2021-04-27 19:13:32 +08:00
$result[] = $index;
}
return $result;
}
public function removeExamUser(int $examUserId)
{
$examUser = ExamUser::query()->findOrFail($examUserId);
$result = DB::transaction(function () use ($examUser) {
2021-06-11 02:21:42 +08:00
do {
$deleted = $examUser->progresses()->limit(10000)->delete();
} while ($deleted > 0);
2021-04-27 19:13:32 +08:00
return $examUser->delete();
});
return $result;
}
2021-06-10 02:16:08 +08:00
public function avoidExamUser(int $examUserId)
{
2021-06-11 02:21:42 +08:00
$examUser = ExamUser::query()->where('status',ExamUser::STATUS_NORMAL)->findOrFail($examUserId);
2021-06-10 02:16:08 +08:00
$result = $examUser->update(['status' => ExamUser::STATUS_AVOIDED]);
return $result;
}
2023-11-14 02:13:21 +08:00
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]));
}
if ($examUser->status != ExamUser::STATUS_NORMAL) {
throw new \LogicException(nexus_trans("exam-user.status_not_allow_update_end", ['status_text' => nexus_trans('exam-user.status.' . ExamUser::STATUS_NORMAL)]));
}
2023-11-14 02:13:21 +08:00
$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]);
}
2022-05-13 03:12:38 +08:00
public function removeExamUserBulk(array $params, User $user)
{
$result = $this->getExamUserBulkQuery($params)->delete();
do_log(sprintf(
'user: %s bulk delete by filter: %s, result: %s',
$user->id, json_encode($params), json_encode($result)
), 'alert');
return $result;
}
public function avoidExamUserBulk(array $params, User $user): int
{
$query = $this->getExamUserBulkQuery($params)->where('status', ExamUser::STATUS_NORMAL);
$update = [
'status' => ExamUser::STATUS_AVOIDED,
];
$affected = $query->update($update);
do_log(sprintf(
'user: %s bulk avoid by filter: %s, affected: %s',
$user->id, json_encode($params), $affected
), 'alert');
return $affected;
}
private function getExamUserBulkQuery(array $params): Builder
{
$query = ExamUser::query();
$hasWhere = false;
$validFilter = ['uid', 'id', 'exam_id'];
foreach ($validFilter as $item) {
if (!empty($params[$item])) {
$hasWhere = true;
$query->whereIn($item, Arr::wrap($params[$item]));
}
}
if (!$hasWhere) {
throw new \InvalidArgumentException("No filter.");
}
return $query;
}
2021-06-11 02:21:42 +08:00
public function recoverExamUser(int $examUserId)
{
$examUser = ExamUser::query()->where('status',ExamUser::STATUS_AVOIDED)->findOrFail($examUserId);
$result = $examUser->update(['status' => ExamUser::STATUS_NORMAL]);
return $result;
}
public function cronjonAssign()
{
2024-05-18 14:53:30 +08:00
$exams = $this->listValid(null, Exam::DISCOVERED_YES, Exam::TYPE_EXAM);
if ($exams->isEmpty()) {
2021-05-02 17:24:05 +08:00
do_log("No valid and discovered exam.");
2021-04-29 19:18:13 +08:00
return false;
}
2022-04-17 16:38:44 +08:00
/**
* valid exam can has multiple
*
* @since 1.7.4
*/
// if ($exams->count() > 1) {
// do_log("Valid and discovered exam more than 1.", "error");
// return false;
// }
$result = 0;
foreach ($exams as $exam) {
$start = microtime(true);
$count = $this->fetchUserAndDoAssign($exam);
do_log(sprintf(
'exam: %s assign to user count: %s -> %s, cost time: %s',
$exam->id, gettype($count), $count, number_format(microtime(true) - $start, 3)
));
$result += $count;
}
2022-04-17 16:38:44 +08:00
return $result;
}
2022-04-20 00:52:12 +08:00
public function fetchUserAndDoAssign(Exam $exam): bool|int
2022-04-17 16:38:44 +08:00
{
2021-06-09 15:11:02 +08:00
$filters = $exam->filters;
do_log("exam: {$exam->id}, filters: " . nexus_json_encode($filters));
$userTable = (new User())->getTable();
$examUserTable = (new ExamUser())->getTable();
2022-04-17 16:38:44 +08:00
//Fetch user doesn't has this exam and doesn't has any other unfinished exam
2021-04-29 19:18:13 +08:00
$baseQuery = User::query()
2021-06-09 15:11:02 +08:00
->where("$userTable.enabled", User::ENABLED_YES)
->where("$userTable.status", User::STATUS_CONFIRMED)
->selectRaw("$userTable.*")
2021-04-29 19:18:13 +08:00
->orderBy("$userTable.id", "asc");
2021-06-09 15:11:02 +08:00
2021-06-12 23:21:40 +08:00
$filter = Exam::FILTER_USER_CLASS;
2023-04-29 03:46:14 +08:00
if (!empty($filters[$filter])) {
$baseQuery->whereIn("$userTable.class", $filters[$filter]);
2021-06-09 15:11:02 +08:00
}
2021-06-12 23:21:40 +08:00
$filter = Exam::FILTER_USER_DONATE;
2023-04-29 03:46:14 +08:00
if (!empty($filters[$filter]) && count($filters[$filter]) == 1) {
$donateStatus = $filters[$filter][0];
2021-06-12 23:21:40 +08:00
if ($donateStatus == User::DONATE_YES) {
2022-04-20 00:52:12 +08:00
$baseQuery->where(function (Builder $query) {
$query->where('donor', 'yes')->where(function (Builder $query) {
$query->where('donoruntil', '0000-00-00 00:00:00')->orWhereNull('donoruntil')->orWhere('donoruntil', '>=', Carbon::now());
});
});
2021-06-12 23:21:40 +08:00
} elseif ($donateStatus == User::DONATE_NO) {
2022-04-20 00:52:12 +08:00
$baseQuery->where(function (Builder $query) {
$query->where('donor', 'no')->orWhere(function (Builder $query) {
$query->where('donoruntil', '!=','0000-00-00 00:00:00')->whereNotNull('donoruntil')->where('donoruntil', '<', Carbon::now());
});
2021-06-13 20:53:14 +08:00
});
2021-06-12 23:21:40 +08:00
} else {
do_log("{$exam->id} filter $filter: $donateStatus invalid.", "error");
return false;
}
}
$filter = Exam::FILTER_USER_REGISTER_TIME_RANGE;
2023-05-10 02:25:26 +08:00
$range = $filters[$filter] ?? [];
2021-06-12 23:21:40 +08:00
if (!empty($range)) {
2022-07-02 15:08:23 +08:00
if (!empty($range[0])) {
$baseQuery->where("$userTable.added", ">=", Carbon::parse($range[0])->toDateTimeString());
}
if (!empty($range[1])) {
$baseQuery->where("$userTable.added", '<=', Carbon::parse($range[1])->toDateTimeString());
}
2021-06-09 15:11:02 +08:00
}
$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());
}
}
2022-04-17 16:38:44 +08:00
//Does not has this exam
$baseQuery->whereDoesntHave('exams', function (Builder $query) use ($exam) {
$query->where('exam_id', $exam->id);
});
//Does not has any other normal exam
$baseQuery->whereDoesntHave('exams', function (Builder $query) use ($exam) {
$query->where('status',ExamUser::STATUS_NORMAL);
});
2021-06-09 15:11:02 +08:00
$size = 1000;
2021-04-29 19:18:13 +08:00
$minId = 0;
$result = 0;
2024-04-13 13:55:32 +08:00
$begin = $exam->getBeginForUser();
$end = $exam->getEndForUser();
2021-04-29 19:18:13 +08:00
while (true) {
$logPrefix = sprintf('[%s], exam: %s, size: %s', __FUNCTION__, $exam->id , $size);
$users = (clone $baseQuery)->where("$userTable.id", ">", $minId)->limit($size)->get();
2021-06-09 15:11:02 +08:00
do_log("$logPrefix, query: " . last_query() . ", counts: " . $users->count());
2021-04-29 19:18:13 +08:00
if ($users->isEmpty()) {
2021-06-09 15:11:02 +08:00
do_log("no more data...");
2021-04-29 19:18:13 +08:00
break;
}
$now = Carbon::now()->toDateTimeString();
foreach ($users as $user) {
$minId = $user->id;
$currentLogPrefix = sprintf("$logPrefix, user: %s", $user->id);
2021-06-11 02:21:42 +08:00
$insert = [
2021-04-29 19:18:13 +08:00
'uid' => $user->id,
'exam_id' => $exam->id,
'begin' => $begin,
'end' => $end,
2021-04-29 19:18:13 +08:00
'created_at' => $now,
'updated_at' => $now,
];
do_log("$currentLogPrefix, exam will be assigned to this user.");
2021-06-11 02:21:42 +08:00
$examUser = ExamUser::query()->create($insert);
$this->updateProgress($examUser, $user);
2021-06-11 02:21:42 +08:00
$result++;
2021-04-29 19:18:13 +08:00
}
}
return $result;
}
2021-06-14 12:49:16 +08:00
public function cronjobCheckout($ignoreTimeRange = false): int
{
$now = Carbon::now()->toDateTimeString();
$examUserTable = (new ExamUser())->getTable();
$examTable = (new Exam())->getTable();
2021-05-12 13:45:00 +08:00
$userTable = (new User())->getTable();
2021-04-29 19:18:13 +08:00
$baseQuery = ExamUser::query()
->join($examTable, "$examUserTable.exam_id", "=", "$examTable.id")
->where("$examUserTable.status", ExamUser::STATUS_NORMAL)
->selectRaw("$examUserTable.*")
->with(['exam', 'user', 'user.language'])
->orderBy("$examUserTable.id", "asc");
2021-04-29 19:18:13 +08:00
if (!$ignoreTimeRange) {
2021-05-06 11:57:23 +08:00
$whenThens = [];
$whenThens[] = "when $examUserTable.`end` is not null then $examUserTable.`end` < '$now'";
$whenThens[] = "when $examTable.`end` is not null then $examTable.`end` < '$now'";
$whenThens[] = "when $examTable.duration > 0 then date_add($examUserTable.created_at, interval $examTable.duration day) < '$now'";
$baseQuery->whereRaw(sprintf("case %s else false end", implode(" ", $whenThens)));
2021-04-29 19:18:13 +08:00
}
$size = 1000;
2021-04-29 19:18:13 +08:00
$minId = 0;
$result = 0;
2021-05-12 13:45:00 +08:00
while (true) {
2021-04-29 19:18:13 +08:00
$logPrefix = sprintf('[%s], size: %s', __FUNCTION__, $size);
$examUsers = (clone $baseQuery)->where("$examUserTable.id", ">", $minId)->limit($size)->get();
do_log("$logPrefix, fetch exam users: {$examUsers->count()} by: " . last_query());
if ($examUsers->isEmpty()) {
do_log("$logPrefix, no more data...");
break;
}
2021-04-29 19:18:13 +08:00
$result += $examUsers->count();
$now = Carbon::now()->toDateTimeString();
2025-01-27 14:07:44 +08:00
$examUserIdArr = $uidToDisable = $messageToSend = $userBanLog = [];
2025-01-20 13:11:19 +08:00
$bonusLog = $userBonusUpdate = $uidToUpdateBonus = [];
2024-04-13 13:55:32 +08:00
$examUserToInsert = [];
2025-01-27 14:07:44 +08:00
$userModifyLogs = [];
foreach ($examUsers as $examUser) {
2021-04-29 19:18:13 +08:00
$minId = $examUser->id;
$examUserIdArr[] = $examUser->id;
$uid = $examUser->uid;
2024-05-24 02:27:44 +08:00
clear_inbox_count_cache($uid);
2024-04-13 13:55:32 +08:00
/** @var Exam $exam */
2021-05-12 13:45:00 +08:00
$exam = $examUser->exam;
$currentLogPrefix = sprintf("$logPrefix, user: %s, exam: %s, examUser: %s", $uid, $examUser->exam_id, $examUser->id);
if (!$examUser->user) {
do_log("$currentLogPrefix, user not exists, remove it!", 'error');
$examUser->progresses()->delete();
$examUser->delete();
continue;
}
2023-11-14 02:13:21 +08:00
//update to the newest progress
$examUser = $this->updateProgress($examUser, $examUser->user);
2021-04-29 19:18:13 +08:00
$locale = $examUser->user->locale;
if ($examUser->is_done) {
do_log("$currentLogPrefix, [is_done]");
2024-05-24 02:27:44 +08:00
$subjectTransKey = $exam->getMessageSubjectTransKey("pass");
$msgTransKey = $exam->getMessageContentTransKey("pass");
if ($exam->isTypeExam()) {
if (!empty($exam->recurring) && $this->isExamMatchUser($exam, $examUser->user)) {
$examUserToInsert[] = [
'uid' => $examUser->user->id,
'exam_id' => $exam->id,
'begin' => $exam->getBeginForUser(),
'end' => $exam->getEndForUser(),
'created_at' => $now,
'updated_at' => $now,
];
}
} elseif ($exam->isTypeTask()) {
//reward bonus
if ($exam->success_reward_bonus > 0) {
$uidToUpdateBonus[] = $uid;
$bonusLog[] = [
"uid" => $uid,
"old_total_value" => $examUser->user->seedbonus,
"value" => $exam->success_reward_bonus,
"new_total_value" => $examUser->user->seedbonus + $exam->success_reward_bonus,
"business_type" => BonusLogs::BUSINESS_TYPE_TASK_PASS_REWARD,
];
$userBonusUpdate[] = sprintf("when `id` = %s then seedbonus + %d", $uid, $exam->success_reward_bonus);
}
2024-05-10 10:24:50 +08:00
}
} else {
2024-05-24 02:27:44 +08:00
do_log("$currentLogPrefix, [not_done]");
$subjectTransKey = $exam->getMessageSubjectTransKey("not_pass");
$msgTransKey = $exam->getMessageContentTransKey("not_pass");
if ($exam->isTypeExam()) {
//ban user
do_log("$currentLogPrefix, [will be banned]");
clear_user_cache($examUser->user->id, $examUser->user->passkey);
$uidToDisable[] = $uid;
$userModcomment = nexus_trans('exam.ban_user_modcomment', [
'exam_name' => $exam->name,
'begin' => $examUser->begin,
'end' => $examUser->end
], $locale);
2025-01-27 14:07:44 +08:00
// $userModcomment = sprintf('%s - %s', date('Y-m-d'), $userModcomment);
// $userModcommentUpdate[] = sprintf("when `id` = %s then concat_ws('\n', '%s', modcomment)", $uid, $userModcomment);
$userModifyLogs[] = [
'user_id' => $uid,
'content' => $userModcomment,
'created_at' => $now,
'updated_at' => $now,
];
2024-05-24 02:27:44 +08:00
$banLogReason = nexus_trans('exam.ban_log_reason', [
'exam_name' => $exam->name,
'begin' => $examUser->begin,
'end' => $examUser->end,
], $locale);
$userBanLog[] = [
'uid' => $uid,
'username' => $examUser->user->username,
'reason' => $banLogReason,
];
} elseif ($exam->isTypeTask()) {
//deduct bonus
if ($exam->fail_deduct_bonus > 0) {
$uidToUpdateBonus[] = $uid;
$bonusLog[] = [
"uid" => $uid,
"old_total_value" => $examUser->user->seedbonus,
"value" => -1*$exam->fail_deduct_bonus,
"new_total_value" => $examUser->user->seedbonus - $exam->fail_deduct_bonus,
"business_type" => BonusLogs::BUSINESS_TYPE_TASK_NOT_PASS_DEDUCT,
];
$userBonusUpdate[] = sprintf("when `id` = %s then seedbonus - %d", $uid, $exam->fail_deduct_bonus);
}
}
}
2021-04-29 19:18:13 +08:00
$subject = nexus_trans($subjectTransKey, [], $locale);
$msg = nexus_trans($msgTransKey, [
2021-05-12 13:45:00 +08:00
'exam_name' => $exam->name,
2021-04-29 19:18:13 +08:00
'begin' => $examUser->begin,
2024-05-24 02:27:44 +08:00
'end' => $examUser->end,
'success_reward_bonus' => $exam->success_reward_bonus,
'fail_deduct_bonus' => $exam->fail_deduct_bonus,
2021-04-29 19:18:13 +08:00
], $locale);
$messageToSend[] = [
'receiver' => $uid,
'added' => $now,
'subject' => $subject,
'msg' => $msg
];
}
2025-01-27 14:07:44 +08:00
DB::transaction(function () use ($uidToDisable, $messageToSend, $examUserIdArr, $examUserToInsert, $userBanLog, $userModifyLogs, $userBonusUpdate, $bonusLog, $uidToUpdateBonus, $userTable, $logPrefix) {
2021-04-29 19:18:13 +08:00
ExamUser::query()->whereIn('id', $examUserIdArr)->update(['status' => ExamUser::STATUS_FINISHED]);
2021-06-13 20:53:14 +08:00
do {
$deleted = ExamProgress::query()->whereIn('exam_user_id', $examUserIdArr)->limit(10000)->delete();
do_log("$logPrefix, [DELETE_EXAM_PROGRESS], deleted: $deleted");
} while($deleted > 0);
Message::query()->insert($messageToSend);
if (!empty($uidToDisable)) {
2021-05-12 13:45:00 +08:00
$uidStr = implode(', ', $uidToDisable);
$sql = sprintf(
2025-01-27 14:07:44 +08:00
"update %s set enabled = '%s' where id in (%s)",
$userTable, User::ENABLED_NO, $uidStr
2021-05-12 13:45:00 +08:00
);
$updateResult = DB::update($sql);
do_log(sprintf("$logPrefix, disable %s users: %s, sql: %s, updateResult: %s", count($uidToDisable), $uidStr, $sql, $updateResult));
}
if (!empty($userBanLog)) {
UserBanLog::query()->insert($userBanLog);
}
2024-04-13 13:55:32 +08:00
if (!empty($examUserToInsert)) {
ExamUser::query()->insert($examUserToInsert);
}
2024-05-24 02:27:44 +08:00
if (!empty($userBonusUpdate)) {
$uidStr = implode(', ', $uidToUpdateBonus);
$sql = sprintf(
2025-01-20 13:11:19 +08:00
"update %s set seedbonus = case %s end where id in (%s)",
$userTable, implode(' ', $userBonusUpdate), $uidStr
2024-05-24 02:27:44 +08:00
);
$updateResult = DB::update($sql);
do_log(sprintf("$logPrefix, update %s users: %s seedbonus, sql: %s, updateResult: %s", count($uidToUpdateBonus), $uidStr, $sql, $updateResult));
}
if (!empty($bonusLog)) {
BonusLogs::query()->insert($bonusLog);
}
2025-01-27 14:07:44 +08:00
if (!empty($userModifyLogs)) {
UserModifyLog::query()->insert($userModifyLogs);
}
});
}
2021-04-29 19:18:13 +08:00
return $result;
}
2021-06-14 12:49:16 +08:00
public function updateProgressBulk(): array
{
$query = ExamUser::query()
->where('status', ExamUser::STATUS_NORMAL)
->where('is_done', ExamUser::IS_DONE_NO);
$page = 1;
$size = 1000;
$total = $success = 0;
while (true) {
$logPrefix = "[UPDATE_EXAM_PROGRESS], page: $page, size: $size";
$rows = $query->forPage($page, $size)->get();
$count = $rows->count();
$total += $count;
do_log("$logPrefix, " . last_query() . ", count: $count");
if ($rows->isEmpty()) {
do_log("$logPrefix, no more data...");
break;
}
foreach ($rows as $row) {
$result = $this->updateProgress($row);
do_log("$logPrefix, examUser: " . $row->toJson() . ", result type: " . gettype($result));
if ($result) {
2021-06-14 12:49:16 +08:00
$success += 1;
}
}
$page++;
}
2021-06-14 13:36:31 +08:00
$result = compact('total', 'success');
do_log("$logPrefix, result: " . json_encode($result));
return $result;
2021-06-14 12:49:16 +08:00
}
2021-04-20 20:18:02 +08:00
2021-04-19 20:13:21 +08:00
}