From 67d68e73f75c76386e80ab27ff13a6173ca39345 Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Mon, 14 Jun 2021 01:54:19 +0800 Subject: [PATCH] exam progress update when get --- app/Repositories/ExamRepository.php | 25 +++++++++++++++++++------ classes/class_attendance.php | 6 +++--- include/cleanup.php | 2 -- nexus/Exam/Exam.php | 2 +- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/app/Repositories/ExamRepository.php b/app/Repositories/ExamRepository.php index 8b13683f..fd496368 100644 --- a/app/Repositories/ExamRepository.php +++ b/app/Repositories/ExamRepository.php @@ -440,6 +440,10 @@ class ExamRepository extends BaseRepository } $examUser = $examUser->first(); } + if ($examUser->status != ExamUser::STATUS_NORMAL) { + do_log("examUser: {$examUser->id} status not normal, won't update progress."); + return false; + } $exam = $examUser->exam; if (!$user instanceof User) { $user = $examUser->user()->select(['id', 'uploaded', 'downloaded', 'seedtime', 'leechtime', 'seedbonus'])->first(); @@ -548,7 +552,8 @@ class ExamRepository extends BaseRepository "[UPDATE_PROGRESS] %s, result: %s, cost time: %s sec", json_encode($update), var_export($result, true), sprintf('%.3f', microtime(true) - $beginTimestamp) )); - return true; + $examUser->progress_formatted = $examProgressFormatted; + return $examUser; } @@ -557,19 +562,15 @@ class ExamRepository extends BaseRepository * * @param $uid * @param null $status - * @param string[] $with * @return mixed|null */ - public function getUserExamProgress($uid, $status = null, $with = ['exam', 'user']) + public function getUserExamProgress($uid, $status = null) { $logPrefix = "uid: $uid"; $query = ExamUser::query()->where('uid', $uid)->orderBy('exam_id', 'desc'); if (!is_null($status)) { $query->where('status', $status); } - if (!empty($with)) { - $query->with($with); - } $examUsers = $query->get(); if ($examUsers->isEmpty()) { do_log("$logPrefix, no examUser, query: " . last_query()); @@ -579,6 +580,18 @@ class ExamRepository extends BaseRepository do_log("$logPrefix, user exam more than 1.", 'warning'); } $examUser = $examUsers->first(); + $logPrefix .= ", examUser: " . $examUser->id; + try { + $updateResult = $this->updateProgress($examUser); + if ($updateResult) { + do_log("$logPrefix, [UPDATE_SUCCESS_RETURN_DIRECTLY]"); + return $updateResult; + } else { + do_log("$logPrefix, [UPDATE_SUCCESS_FAIL]"); + } + } catch (\Exception $exception) { + do_log("$logPrefix, [UPDATE_SUCCESS_FAIL]: " . $exception->getMessage(), 'error'); + } $exam = $examUser->exam; $progress = $examUser->progress; do_log("$logPrefix, progress: " . nexus_json_encode($progress)); diff --git a/classes/class_attendance.php b/classes/class_attendance.php index 47abb885..b04e1d35 100644 --- a/classes/class_attendance.php +++ b/classes/class_attendance.php @@ -23,16 +23,16 @@ class Attendance public function attend($initial = 10, $step = 5, $maximum = 2000, $continous = array()) { if($this->check(true)) return false; - $res = sql_query(sprintf('SELECT DATEDIFF(%s, `added`) AS diff, `days`, `total_days`, `total_points` FROM `attendance` WHERE `uid` = %u ORDER BY `id` DESC LIMIT 1', sqlesc($this->curdate), $this->userid)) or sqlerr(__FILE__,__LINE__); + $res = sql_query(sprintf('SELECT DATEDIFF(%s, `added`) AS diff, `days`, `total_days` FROM `attendance` WHERE `uid` = %u ORDER BY `id` DESC LIMIT 1', sqlesc($this->curdate), $this->userid)) or sqlerr(__FILE__,__LINE__); $doUpdate = mysql_num_rows($res); if ($doUpdate) { $row = mysql_fetch_row($res); do_log("uid: {$this->userid}, row: " . json_encode($row)); } else { - $row = [0, 0, 0, 0]; + $row = [0, 0, 0]; } $points = min($initial + $step * $row['total_attend_times'], $maximum); - list($datediff, $days, $totalDays, $totalPoints) = $row; + list($datediff, $days, $totalDays) = $row; $cdays = $datediff == 1 ? ++$days : 1; if($cdays > 1){ krsort($continous); diff --git a/include/cleanup.php b/include/cleanup.php index 58af31aa..8d2d471e 100644 --- a/include/cleanup.php +++ b/include/cleanup.php @@ -286,8 +286,6 @@ function docleanup($forceAll = 0, $printProgress = false) { if ($is_donor == 'yes' && $donortimes_bonus > 0) $all_bonus = $all_bonus * $donortimes_bonus; KPS("+",$all_bonus,$arr["userid"]); - //update exam progress - $examRep->updateProgress($arr['userid']); } } $log = 'calculate seeding bonus'; diff --git a/nexus/Exam/Exam.php b/nexus/Exam/Exam.php index 76a3d4df..fc0cb6a4 100644 --- a/nexus/Exam/Exam.php +++ b/nexus/Exam/Exam.php @@ -10,7 +10,7 @@ class Exam public function render($uid) { $examRep = new ExamRepository(); - $userExam = $examRep->getUserExamProgress($uid, ExamUser::STATUS_NORMAL, ['exam']); + $userExam = $examRep->getUserExamProgress($uid, ExamUser::STATUS_NORMAL); if (empty($userExam)) { return ''; }