2021-04-19 20:13:21 +08:00
|
|
|
<?php
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
|
2021-04-28 01:22:25 +08:00
|
|
|
use App\Exceptions\NexusException;
|
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;
|
2021-04-28 19:44:48 +08:00
|
|
|
use App\Models\Message;
|
2021-04-19 20:13:21 +08:00
|
|
|
use App\Models\Setting;
|
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-04-20 20:18:02 +08:00
|
|
|
use Carbon\Carbon;
|
2021-04-28 19:44:48 +08:00
|
|
|
use Illuminate\Database\Query\JoinClause;
|
2021-04-23 20:05:39 +08:00
|
|
|
use Illuminate\Support\Arr;
|
2021-04-27 19:13:32 +08:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2021-04-19 20:13:21 +08:00
|
|
|
|
|
|
|
|
class ExamRepository extends BaseRepository
|
|
|
|
|
{
|
|
|
|
|
public function getList(array $params)
|
|
|
|
|
{
|
|
|
|
|
$query = Exam::query();
|
|
|
|
|
list($sortField, $sortType) = $this->getSortFieldAndType($params);
|
|
|
|
|
$query->orderBy($sortField, $sortType);
|
|
|
|
|
return $query->paginate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function store(array $params)
|
|
|
|
|
{
|
2021-04-25 02:12:14 +08:00
|
|
|
$this->checkIndexes($params);
|
2021-04-28 19:44:48 +08:00
|
|
|
$valid = $this->listValid();
|
|
|
|
|
if ($valid->isNotEmpty()) {
|
|
|
|
|
throw new NexusException("Valid exam already exists.");
|
|
|
|
|
}
|
2021-04-19 20:13:21 +08:00
|
|
|
$exam = Exam::query()->create($params);
|
|
|
|
|
return $exam;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function update(array $params, $id)
|
|
|
|
|
{
|
2021-04-25 02:12:14 +08:00
|
|
|
$this->checkIndexes($params);
|
2021-04-28 19:44:48 +08:00
|
|
|
$valid = $this->listValid($id);
|
|
|
|
|
if ($valid->isNotEmpty()) {
|
|
|
|
|
throw new NexusException("Valid exam already exists.");
|
|
|
|
|
}
|
2021-04-19 20:13:21 +08:00
|
|
|
$exam = Exam::query()->findOrFail($id);
|
|
|
|
|
$exam->update($params);
|
|
|
|
|
return $exam;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-25 02:12:14 +08:00
|
|
|
private function checkIndexes(array $params)
|
|
|
|
|
{
|
|
|
|
|
if (empty($params['indexes'])) {
|
|
|
|
|
throw new \InvalidArgumentException("Require index.");
|
|
|
|
|
}
|
|
|
|
|
$validIndex = array_filter($params['indexes'], function ($value) {
|
|
|
|
|
return isset($value['checked']) && $value['checked']
|
|
|
|
|
&& isset($value['require_value']) && $value['require_value'] > 0;
|
|
|
|
|
});
|
|
|
|
|
if (empty($validIndex)) {
|
|
|
|
|
throw new \InvalidArgumentException("Require valid index.");
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-23 20:05:39 +08:00
|
|
|
public function getDetail($id)
|
|
|
|
|
{
|
|
|
|
|
$exam = Exam::query()->findOrFail($id);
|
|
|
|
|
return $exam;
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
$result = $exam->delete();
|
|
|
|
|
return $result;
|
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
|
|
|
*
|
2021-04-28 19:44:48 +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
|
|
|
*/
|
2021-04-28 19:44:48 +08:00
|
|
|
public function listValid($excludeId = null)
|
2021-04-20 20:18:02 +08:00
|
|
|
{
|
|
|
|
|
$now = Carbon::now();
|
2021-04-28 19:44:48 +08:00
|
|
|
$query = Exam::query()
|
2021-04-20 20:18:02 +08:00
|
|
|
->where('begin', '<=', $now)
|
|
|
|
|
->where('end', '>=', $now)
|
|
|
|
|
->where('status', Exam::STATUS_ENABLED)
|
2021-04-28 19:44:48 +08:00
|
|
|
->orderBy('id', 'desc');
|
|
|
|
|
if ($excludeId) {
|
|
|
|
|
$query->whereNotIn('id', Arr::wrap($excludeId));
|
|
|
|
|
}
|
|
|
|
|
return $query->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)
|
|
|
|
|
{
|
2021-04-20 20:18:02 +08:00
|
|
|
$logPrefix = "uid: $uid";
|
2021-04-25 02:12:14 +08:00
|
|
|
$exams = $this->listValid();
|
|
|
|
|
if ($exams->isEmpty()) {
|
2021-04-26 20:37:17 +08:00
|
|
|
do_log("$logPrefix, no valid exam.");
|
2021-04-25 02:12:14 +08:00
|
|
|
return $exams;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 19:44:48 +08:00
|
|
|
$matched = $exams->filter(function (Exam $exam) use ($uid, $logPrefix) {
|
|
|
|
|
return $this->isExamMatchUser($exam, $uid);
|
|
|
|
|
});
|
2021-04-25 02:12:14 +08:00
|
|
|
|
2021-04-28 19:44:48 +08:00
|
|
|
return $matched;
|
|
|
|
|
}
|
2021-04-25 02:12:14 +08:00
|
|
|
|
2021-04-28 19:44:48 +08:00
|
|
|
private function isExamMatchUser(Exam $exam, $user)
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
if (empty($filters->classes)) {
|
|
|
|
|
do_log("$logPrefix, exam: {$exam->id} no class");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (!in_array($user->class, $filters->classes)) {
|
|
|
|
|
do_log("$logPrefix, user class: {$user->class} not in: " . json_encode($filters));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (!$user->added) {
|
|
|
|
|
do_log("$logPrefix, user no added time", 'warning');
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-04-25 02:12:14 +08:00
|
|
|
|
2021-04-28 19:44:48 +08:00
|
|
|
$added = $user->added->toDateTimeString();
|
|
|
|
|
$registerTimeBegin = $filters->register_time_range[0] ? Carbon::parse($filters->register_time_range[0])->toDateString() : '';
|
|
|
|
|
$registerTimeEnd = $filters->register_time_range[1] ? Carbon::parse($filters->register_time_range[1])->toDateString() : '';
|
|
|
|
|
if (empty($registerTimeBegin)) {
|
|
|
|
|
do_log("$logPrefix, exam: {$exam->id} no register_time_begin");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if ($added < $registerTimeBegin) {
|
|
|
|
|
do_log("$logPrefix, added: $added not after: " . $registerTimeBegin);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-04-25 02:12:14 +08:00
|
|
|
|
2021-04-28 19:44:48 +08:00
|
|
|
if (empty($registerTimeEnd)) {
|
|
|
|
|
do_log("$logPrefix, exam: {$exam->id} no register_time_end");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if ($added > $registerTimeEnd) {
|
|
|
|
|
do_log("$logPrefix, added: $added not before: " . $registerTimeEnd);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
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
|
|
|
|
|
* @param null $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-04-27 19:13:32 +08:00
|
|
|
public function assignToUser(int $uid, $examId = null, $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";
|
2021-04-25 02:12:14 +08:00
|
|
|
if ($examId > 0) {
|
|
|
|
|
$exam = Exam::query()->find($examId);
|
|
|
|
|
} else {
|
|
|
|
|
$exams = $this->listMatchExam($uid);
|
|
|
|
|
if ($exams->count() > 1) {
|
2021-04-26 20:37:17 +08:00
|
|
|
do_log(last_query());
|
2021-04-28 01:22:25 +08:00
|
|
|
throw new NexusException("Match exam more than 1.");
|
2021-04-20 20:18:02 +08:00
|
|
|
}
|
2021-04-25 02:12:14 +08:00
|
|
|
$exam = $exams->first();
|
|
|
|
|
}
|
|
|
|
|
if (!$exam) {
|
2021-04-28 01:22:25 +08:00
|
|
|
throw new NexusException("No valid exam.");
|
2021-04-25 02:12:14 +08:00
|
|
|
}
|
|
|
|
|
$user = User::query()->findOrFail($uid);
|
|
|
|
|
$exists = $user->exams()->where('exam_id', $exam->id)->exists();
|
|
|
|
|
if ($exists) {
|
2021-04-28 01:22:25 +08:00
|
|
|
throw new NexusException("Exam: {$exam->id} already assign to user: {$user->id}");
|
2021-04-20 20:18:02 +08:00
|
|
|
}
|
2021-04-26 20:37:17 +08:00
|
|
|
$data = [
|
|
|
|
|
'exam_id' => $exam->id,
|
|
|
|
|
];
|
|
|
|
|
if ($begin && $end) {
|
|
|
|
|
$logPrefix .= ", specific begin and end";
|
|
|
|
|
$data['begin'] = $begin;
|
|
|
|
|
$data['end'] = $end;
|
|
|
|
|
}
|
|
|
|
|
do_log("$logPrefix, data: " . nexus_json_encode($data));
|
|
|
|
|
$result = $user->exams()->create($data);
|
2021-04-20 20:18:02 +08:00
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
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']);
|
|
|
|
|
}
|
|
|
|
|
list($sortField, $sortType) = $this->getSortFieldAndType($params);
|
|
|
|
|
$query->orderBy($sortField, $sortType);
|
|
|
|
|
$result = $query->with(['user', 'exam'])->paginate();
|
|
|
|
|
return $result;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-25 21:28:58 +08:00
|
|
|
public function addProgress(int $examUserId, int $indexId, int $value, int $torrentId)
|
|
|
|
|
{
|
2021-04-27 02:44:44 +08:00
|
|
|
$logPrefix = "examUserId: $examUserId, indexId: $indexId, value: $value, torrentId: $torrentId";
|
2021-04-25 21:28:58 +08:00
|
|
|
$examUser = ExamUser::query()->with(['exam', 'user'])->findOrFail($examUserId);
|
|
|
|
|
if ($examUser->status != ExamUser::STATUS_NORMAL) {
|
|
|
|
|
throw new \InvalidArgumentException("ExamUser: $examUserId is not normal.");
|
|
|
|
|
}
|
|
|
|
|
if (!isset(Exam::$indexes[$indexId])) {
|
|
|
|
|
throw new \InvalidArgumentException("Invalid index id: $indexId.");
|
|
|
|
|
}
|
|
|
|
|
$exam = $examUser->exam;
|
|
|
|
|
$indexes = collect($exam->indexes)->keyBy('index');
|
|
|
|
|
if (!$indexes->has($indexId)) {
|
|
|
|
|
throw new \InvalidArgumentException(sprintf('Exam: %s does not has index: %s', $exam->id, $indexId));
|
|
|
|
|
}
|
|
|
|
|
$index = $indexes->get($indexId);
|
|
|
|
|
if (!isset($index['checked']) || !$index['checked']) {
|
|
|
|
|
throw new \InvalidArgumentException(sprintf('Exam: %s index: %s is not checked', $exam->id, $indexId));
|
|
|
|
|
}
|
|
|
|
|
$torrentFields = ['id', 'visible', 'banned'];
|
|
|
|
|
$torrent = Torrent::query()->findOrFail($torrentId, $torrentFields);
|
2021-04-27 02:44:44 +08:00
|
|
|
$torrent->checkIsNormal($torrentFields);
|
2021-04-25 21:28:58 +08:00
|
|
|
|
|
|
|
|
$user = $examUser->user;
|
|
|
|
|
$user->checkIsNormal();
|
|
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
|
'uid' => $user->id,
|
|
|
|
|
'exam_id' => $exam->id,
|
|
|
|
|
'torrent_id' => $torrentId,
|
|
|
|
|
'index' => $indexId,
|
|
|
|
|
'value' => $value,
|
|
|
|
|
];
|
2021-04-27 02:44:44 +08:00
|
|
|
do_log("$logPrefix [addProgress] " . nexus_json_encode($data));
|
|
|
|
|
$newProgress = $examUser->progresses()->create($data);
|
|
|
|
|
$examProgress = $this->calculateProgress($examUser);
|
2021-04-28 19:44:48 +08:00
|
|
|
$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,
|
|
|
|
|
];
|
|
|
|
|
do_log("$logPrefix [updateProgress] " . nexus_json_encode($update));
|
|
|
|
|
$examUser->update($update);
|
2021-04-27 02:44:44 +08:00
|
|
|
return $newProgress;
|
2021-04-25 21:28:58 +08:00
|
|
|
}
|
|
|
|
|
|
2021-04-27 19:13:32 +08:00
|
|
|
public function getUserExamProgress($uid, $status = null, $with = ['exam', 'user'])
|
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');
|
2021-04-28 19:44:48 +08:00
|
|
|
if (!is_null($status)) {
|
2021-04-25 21:28:58 +08:00
|
|
|
$query->where('status', $status);
|
|
|
|
|
}
|
2021-04-27 19:13:32 +08:00
|
|
|
if (!empty($with)) {
|
|
|
|
|
$query->with($with);
|
|
|
|
|
}
|
2021-04-26 20:37:17 +08:00
|
|
|
$examUsers = $query->get();
|
|
|
|
|
if ($examUsers->isEmpty()) {
|
|
|
|
|
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-04-27 19:13:32 +08:00
|
|
|
$exam = $examUser->exam;
|
|
|
|
|
if (empty($examUser->begin) || empty($examUser->end)) {
|
|
|
|
|
$examUser->begin = $exam->begin;
|
|
|
|
|
$examUser->end = $exam->end;
|
|
|
|
|
}
|
2021-04-27 02:44:44 +08:00
|
|
|
$progress = $this->calculateProgress($examUser);
|
|
|
|
|
do_log("$logPrefix, progress: " . nexus_json_encode($progress));
|
|
|
|
|
$examUser->progress = $progress;
|
2021-04-27 19:13:32 +08:00
|
|
|
$examUser->progress_formatted = $this->getProgressFormatted($exam, $progress);
|
2021-04-27 02:44:44 +08:00
|
|
|
return $examUser;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function calculateProgress(ExamUser $examUser)
|
|
|
|
|
{
|
2021-04-26 20:37:17 +08:00
|
|
|
$exam = $examUser->exam;
|
2021-04-27 02:44:44 +08:00
|
|
|
$logPrefix = ", examUser: " . $examUser->id;
|
2021-04-26 20:37:17 +08:00
|
|
|
if ($examUser->begin) {
|
|
|
|
|
$logPrefix .= ", begin from examUser: " . $examUser->id;
|
|
|
|
|
$begin = $examUser->begin;
|
|
|
|
|
} elseif ($exam->begin) {
|
|
|
|
|
$logPrefix .= ", begin from exam: " . $exam->id;
|
|
|
|
|
$begin = $exam->begin;
|
|
|
|
|
} else {
|
|
|
|
|
do_log("$logPrefix, no begin");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
if ($examUser->end) {
|
|
|
|
|
$logPrefix .= ", end from examUser: " . $examUser->id;
|
|
|
|
|
$end = $examUser->end;
|
|
|
|
|
} elseif ($exam->end) {
|
|
|
|
|
$logPrefix .= ", end from exam: " . $exam->id;
|
|
|
|
|
$end = $exam->end;
|
|
|
|
|
} else {
|
|
|
|
|
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'])
|
|
|
|
|
->get();
|
2021-04-25 21:28:58 +08:00
|
|
|
|
2021-04-28 19:44:48 +08:00
|
|
|
do_log("$logPrefix, " . last_query() . ", progressSum: " . $progressSum->toJson());
|
2021-04-27 02:44:44 +08:00
|
|
|
|
|
|
|
|
return $progressSum->pluck('sum', 'index')->toArray();
|
|
|
|
|
|
2021-04-25 21:28:58 +08:00
|
|
|
}
|
|
|
|
|
|
2021-04-27 19:13:32 +08:00
|
|
|
private function getProgressFormatted(Exam $exam, array $progress, $locale = null)
|
|
|
|
|
{
|
|
|
|
|
$result = [];
|
|
|
|
|
foreach ($exam->indexes as $key => $index) {
|
|
|
|
|
if (!isset($index['checked']) || !$index['checked']) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$currentValue = $progress[$index['index']] ?? 0;
|
|
|
|
|
$requireValue = $index['require_value'];
|
|
|
|
|
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 = mkprettytime($currentValue);
|
|
|
|
|
$requireValueAtomic = $requireValue * 3600;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
$currentValueFormatted = $currentValue;
|
|
|
|
|
$requireValueAtomic = $requireValue;
|
|
|
|
|
}
|
2021-04-28 19:44:48 +08:00
|
|
|
$index['index_formatted'] = nexus_trans('exam.index_text_' . $index['index']);
|
|
|
|
|
$index['require_value_formatted'] = "$requireValue " . ($index['unit'] ?? '');
|
2021-04-27 19:13:32 +08:00
|
|
|
$index['current_value'] = $currentValue;
|
|
|
|
|
$index['current_value_formatted'] = $currentValueFormatted;
|
|
|
|
|
$index['passed'] = $currentValue >= $requireValueAtomic;
|
|
|
|
|
$result[] = $index;
|
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function removeExamUser(int $examUserId)
|
|
|
|
|
{
|
|
|
|
|
$examUser = ExamUser::query()->findOrFail($examUserId);
|
|
|
|
|
$result = DB::transaction(function () use ($examUser) {
|
|
|
|
|
$examUser->progresses()->delete();
|
|
|
|
|
return $examUser->delete();
|
|
|
|
|
});
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 19:44:48 +08:00
|
|
|
public function cronjonAssign()
|
|
|
|
|
{
|
|
|
|
|
$exams = $this->listValid();
|
|
|
|
|
if ($exams->isEmpty()) {
|
|
|
|
|
do_log("No valid exam.");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if ($exams->count() > 1) {
|
|
|
|
|
do_log("Valid exam more than 1.", "warning");
|
|
|
|
|
}
|
|
|
|
|
/** @var Exam $exam */
|
|
|
|
|
$exam = $exams->first();
|
|
|
|
|
$userTable = (new User())->getTable();
|
|
|
|
|
$examUserTable = (new ExamUser())->getTable();
|
|
|
|
|
User::query()
|
|
|
|
|
->leftJoin($examUserTable, function (JoinClause $join) use ($examUserTable, $userTable) {
|
|
|
|
|
$join->on("$userTable.id", "=", "$examUserTable.uid")
|
|
|
|
|
->on("$examUserTable.status", "=", DB::raw(ExamUser::STATUS_NORMAL));
|
|
|
|
|
})
|
|
|
|
|
->whereRaw("$examUserTable.id is null")
|
|
|
|
|
->selectRaw("$userTable.*")
|
|
|
|
|
->chunk(100, function ($users) use ($exam) {
|
|
|
|
|
do_log("user count: " . $users->count() . last_query());
|
|
|
|
|
$insert = [];
|
|
|
|
|
$now = Carbon::now()->toDateTimeString();
|
|
|
|
|
foreach ($users as $user) {
|
|
|
|
|
$logPrefix = sprintf('[assignCronjob] user: %s, exam: %s', $user->id, $exam->id);
|
|
|
|
|
if (!$this->isExamMatchUser($exam, $user)) {
|
|
|
|
|
do_log("$logPrefix, exam not match user.");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$insert[] = [
|
|
|
|
|
'uid' => $user->id,
|
|
|
|
|
'exam_id' => $exam->id,
|
|
|
|
|
'created_at' => $now,
|
|
|
|
|
'updated_at' => $now,
|
|
|
|
|
];
|
|
|
|
|
do_log("$logPrefix, exam assign to user.");
|
|
|
|
|
}
|
|
|
|
|
ExamUser::query()->insert($insert);
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function cronjobCheckout()
|
|
|
|
|
{
|
|
|
|
|
$now = Carbon::now()->toDateTimeString();
|
|
|
|
|
$examUserTable = (new ExamUser())->getTable();
|
|
|
|
|
$examTable = (new Exam())->getTable();
|
|
|
|
|
$perPage = 100;
|
|
|
|
|
$page = 1;
|
|
|
|
|
$query = ExamUser::query()
|
|
|
|
|
->join($examTable, "$examUserTable.exam_id", "=", "$examTable.id")
|
|
|
|
|
->where("$examUserTable.status", ExamUser::STATUS_NORMAL)
|
|
|
|
|
->whereRaw("if($examUserTable.begin is not null, $examUserTable.begin <= '$now', $examTable.begin <= '$now')")
|
|
|
|
|
->whereRaw("if($examUserTable.end is not null, $examUserTable.end >= '$now', $examTable.end >= '$now')")
|
|
|
|
|
->selectRaw("$examUserTable.*")
|
|
|
|
|
->with(['exam', 'user', 'user.language'])
|
|
|
|
|
->orderBy("$examUserTable.id", "asc");
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
$logPrefix = sprintf('[%s], page: %s', __FUNCTION__, $page);
|
|
|
|
|
$examUsers = $query->forPage($page, $perPage)->get("$examUserTable.*");
|
|
|
|
|
if ($examUsers->isEmpty()) {
|
|
|
|
|
do_log("$logPrefix, no more data..." . last_query());
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
do_log("$logPrefix, fetch exam users: {$examUsers->count()}, " . last_query());
|
|
|
|
|
}
|
|
|
|
|
$now = Carbon::now()->toDateTimeString();
|
|
|
|
|
$idArr = $uidToDisable = $messageToSend = [];
|
|
|
|
|
foreach ($examUsers as $examUser) {
|
|
|
|
|
$idArr[] = $examUser->id;
|
|
|
|
|
$uid = $examUser->uid;
|
|
|
|
|
$currentLogPrefix = sprintf("$logPrefix, user: %s, exam: %s, examUser: %s", $uid, $examUser->exam_id, $examUser->id);
|
|
|
|
|
if ($examUser->is_done) {
|
|
|
|
|
do_log("$currentLogPrefix, [is_done]");
|
|
|
|
|
$messageToSend[] = [
|
|
|
|
|
'receiver' => $uid,
|
|
|
|
|
'added' => $now,
|
|
|
|
|
'subject' => 'Exam passed!',
|
|
|
|
|
'msg' => sprintf(
|
|
|
|
|
'Congratulations! You have complete the exam: %s in time(%s ~ %s)!',
|
|
|
|
|
$examUser->exam->name, $examUser->begin ?? $examUser->exam->begin, $examUser->end ?? $examUser->exam->end
|
|
|
|
|
),
|
|
|
|
|
];
|
|
|
|
|
} else {
|
|
|
|
|
do_log("$currentLogPrefix, [will be banned]");
|
|
|
|
|
//ban user
|
|
|
|
|
$uidToDisable[] = $uid;
|
|
|
|
|
$messageToSend[] = [
|
|
|
|
|
'receiver' => $uid,
|
|
|
|
|
'added' => $now,
|
|
|
|
|
'subject' => 'Exam not passed! And your account is banned!',
|
|
|
|
|
'msg' => sprintf(
|
|
|
|
|
'You did not complete the exam: %s in time(%s ~ %s), so your account has been banned!',
|
|
|
|
|
$examUser->exam->name, $examUser->begin ?? $examUser->exam->begin, $examUser->end ?? $examUser->exam->end
|
|
|
|
|
),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
DB::transaction(function () use ($uidToDisable, $messageToSend, $idArr) {
|
|
|
|
|
ExamUser::query()->whereIn('id', $idArr)->update(['status' => ExamUser::STATUS_FINISHED]);
|
|
|
|
|
Message::query()->insert($messageToSend);
|
|
|
|
|
if (!empty($uidToDisable)) {
|
|
|
|
|
User::query()->whereIn('id', $uidToDisable)->update(['enabled' => User::ENABLED_NO]);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
$page++;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-25 02:12:14 +08:00
|
|
|
|
|
|
|
|
|
2021-04-20 20:18:02 +08:00
|
|
|
|
2021-04-19 20:13:21 +08:00
|
|
|
}
|