From d1b7561aae18178c4a2aeac9c7f943b539e68f52 Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Tue, 27 Apr 2021 02:44:44 +0800 Subject: [PATCH] [exam] add progress do update the same time --- admin/src/App.vue | 6 +- admin/src/views/exam/user.vue | 2 +- app/Console/Commands/Test.php | 3 +- app/Models/Exam.php | 6 +- app/Models/ExamUser.php | 4 +- app/Models/NexusModel.php | 2 +- app/Models/User.php | 2 - app/Repositories/ExamRepository.php | 28 ++++++--- bootstrap/app.php | 3 +- ...1_04_24_084104_create_exam_users_table.php | 2 +- include/functions.php | 2 +- include/globalfunctions.php | 7 +++ lang/chs/lang_functions.php | 4 ++ lang/cht/lang_functions.php | 14 ++++- lang/en/lang_functions.php | 14 ++++- nexus/Exam/Exam.php | 18 +++++- resources/views/welcome.blade.php | 61 +++---------------- 17 files changed, 97 insertions(+), 81 deletions(-) diff --git a/admin/src/App.vue b/admin/src/App.vue index f983fa8d..8b6b1def 100644 --- a/admin/src/App.vue +++ b/admin/src/App.vue @@ -29,9 +29,9 @@ - - Agent allow - + + + Exam diff --git a/admin/src/views/exam/user.vue b/admin/src/views/exam/user.vue index b639109f..b7d65e51 100644 --- a/admin/src/views/exam/user.vue +++ b/admin/src/views/exam/user.vue @@ -6,7 +6,7 @@
- Add +
diff --git a/app/Console/Commands/Test.php b/app/Console/Commands/Test.php index bb38166a..a3ae66ba 100644 --- a/app/Console/Commands/Test.php +++ b/app/Console/Commands/Test.php @@ -45,8 +45,7 @@ class Test extends Command public function handle() { $rep = new ExamRepository(); -// $r = $rep->assignToUser(3); - echo new A; + $r = $rep->addProgress(1, 1, 250, 1); dd($r); } } diff --git a/app/Models/Exam.php b/app/Models/Exam.php index 349b6370..6e9e8614 100644 --- a/app/Models/Exam.php +++ b/app/Models/Exam.php @@ -22,13 +22,13 @@ class Exam extends NexusModel ]; const INDEX_UPLOADED = 1; - const INDEX_SEED_TIME_AVERGAGE = 2; + const INDEX_SEED_TIME_AVERAGE = 2; const INDEX_DOWNLOADED = 3; const INDEX_BONUS = 4; public static $indexes = [ self::INDEX_UPLOADED => ['name' => 'Uploaded', 'unit' => 'GB'], - self::INDEX_SEED_TIME_AVERGAGE => ['name' => 'Seed Time Average', 'unit' => 'Hour'], + self::INDEX_SEED_TIME_AVERAGE => ['name' => 'Seed Time Average', 'unit' => 'Hour'], self::INDEX_DOWNLOADED => ['name' => 'Downloaded', 'unit' => 'GB'], self::INDEX_BONUS => ['name' => 'Bonus', 'unit' => ''], ]; @@ -41,7 +41,7 @@ class Exam extends NexusModel self::FILTER_USER_REGISTER_TIME_RANGE => ['name' => 'User Register Time Range'], ]; - public function getStatusTextAttribute() + public function getStatusTextAttribute(): string { return self::$status[$this->status]['text'] ?? ''; } diff --git a/app/Models/ExamUser.php b/app/Models/ExamUser.php index 5347e5cf..c6d34a3a 100644 --- a/app/Models/ExamUser.php +++ b/app/Models/ExamUser.php @@ -4,7 +4,7 @@ namespace App\Models; class ExamUser extends NexusModel { - protected $fillable = ['exam_id', 'uid', 'status', 'result']; + protected $fillable = ['exam_id', 'uid', 'status', 'progress']; public $timestamps = true; @@ -17,7 +17,7 @@ class ExamUser extends NexusModel ]; protected $casts = [ - 'result' => 'json' + 'progress' => 'json' ]; public function getStatusTextAttribute() diff --git a/app/Models/NexusModel.php b/app/Models/NexusModel.php index e4f43e95..84cc2ab7 100644 --- a/app/Models/NexusModel.php +++ b/app/Models/NexusModel.php @@ -11,7 +11,7 @@ class NexusModel extends Model public $timestamps = false; - protected $perPage = 2; +// protected $perPage = 2; /** * 为数组 / JSON 序列化准备日期。 diff --git a/app/Models/User.php b/app/Models/User.php index e6489505..035128fd 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -57,8 +57,6 @@ class User extends Authenticatable self::CLASS_STAFF_LEADER => ['text' => 'Staff Leader'], ]; - protected $perPage = 2; - public function getClassTextAttribute(): string { return self::$classes[$this->class]['text'] ?? ''; diff --git a/app/Repositories/ExamRepository.php b/app/Repositories/ExamRepository.php index 3a1590a4..741a7a94 100644 --- a/app/Repositories/ExamRepository.php +++ b/app/Repositories/ExamRepository.php @@ -208,6 +208,7 @@ class ExamRepository extends BaseRepository public function addProgress(int $examUserId, int $indexId, int $value, int $torrentId) { + $logPrefix = "examUserId: $examUserId, indexId: $indexId, value: $value, torrentId: $torrentId"; $examUser = ExamUser::query()->with(['exam', 'user'])->findOrFail($examUserId); if ($examUser->status != ExamUser::STATUS_NORMAL) { throw new \InvalidArgumentException("ExamUser: $examUserId is not normal."); @@ -226,7 +227,7 @@ class ExamRepository extends BaseRepository } $torrentFields = ['id', 'visible', 'banned']; $torrent = Torrent::query()->findOrFail($torrentId, $torrentFields); - $torrent->checkIsNormal(true, $torrentFields); + $torrent->checkIsNormal($torrentFields); $user = $examUser->user; $user->checkIsNormal(); @@ -238,8 +239,12 @@ class ExamRepository extends BaseRepository 'index' => $indexId, 'value' => $value, ]; - do_log('[addProgress] ' . nexus_json_encode($data)); - return $examUser->progresses()->create($data); + do_log("$logPrefix [addProgress] " . nexus_json_encode($data)); + $newProgress = $examUser->progresses()->create($data); + $examProgress = $this->calculateProgress($examUser); + do_log("$logPrefix [updateProgress] " . nexus_json_encode($examProgress)); + $examUser->update(['progress' => $examProgress]); + return $newProgress; } public function getUserExamProgress($uid, $status = null) @@ -256,11 +261,17 @@ class ExamRepository extends BaseRepository if ($examUsers->count() > 1) { do_log("$logPrefix, user exam more than 1.", 'warning'); } - /** @var ExamUser $examUser */ $examUser = $examUsers->first(); - /** @var Exam $exam */ + $progress = $this->calculateProgress($examUser); + do_log("$logPrefix, progress: " . nexus_json_encode($progress)); + $examUser->progress = $progress; + return $examUser; + } + + private function calculateProgress(ExamUser $examUser) + { $exam = $examUser->exam; - $logPrefix .= ", exam: " . $exam->id; + $logPrefix = ", examUser: " . $examUser->id; if ($examUser->begin) { $logPrefix .= ", begin from examUser: " . $examUser->id; $begin = $examUser->begin; @@ -289,8 +300,9 @@ class ExamRepository extends BaseRepository ->get(); do_log("$logPrefix, query: " . last_query() . ", progressSum: " . $progressSum->toJson()); - $examUser->progress = $progressSum->pluck('sum', 'index')->toArray(); - return $examUser; + + return $progressSum->pluck('sum', 'index')->toArray(); + } diff --git a/bootstrap/app.php b/bootstrap/app.php index 48a3b45e..195fbfd7 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -3,7 +3,8 @@ if (!empty($_SERVER['HTTP_X_REQUEST_ID'])) { define('REQUEST_ID', $_SERVER['HTTP_X_REQUEST_ID']); } else { - define('REQUEST_ID', intval(LARAVEL_START * 10000)); + $requestId = str_replace('.', '', LARAVEL_START); + define('REQUEST_ID', str_pad($requestId, 14, "0", STR_PAD_RIGHT)); } define('IN_NEXUS', false); diff --git a/database/migrations/2021_04_24_084104_create_exam_users_table.php b/database/migrations/2021_04_24_084104_create_exam_users_table.php index 9dd0d672..2baa9e9e 100644 --- a/database/migrations/2021_04_24_084104_create_exam_users_table.php +++ b/database/migrations/2021_04_24_084104_create_exam_users_table.php @@ -20,7 +20,7 @@ class CreateExamUsersTable extends Migration $table->integer('status')->default(0); $table->dateTime('begin')->nullable(); $table->dateTime('end')->nullable(); - $table->text('result')->nullable(); + $table->text('progress')->nullable(); $table->timestamps(); }); } diff --git a/include/functions.php b/include/functions.php index cbac95e4..5b4b2f7a 100644 --- a/include/functions.php +++ b/include/functions.php @@ -2601,7 +2601,7 @@ if ($msgalert) $exam = new \Nexus\Exam\Exam(); $examHtml = $exam->render($CURUSER['id']); if (!empty($examHtml)) { - msgalert("messages.php", $examHtml, "green"); + msgalert("messages.php", $examHtml, "blue"); } } if ($offlinemsg) diff --git a/include/globalfunctions.php b/include/globalfunctions.php index 88b9b5a2..216ef75a 100644 --- a/include/globalfunctions.php +++ b/include/globalfunctions.php @@ -533,3 +533,10 @@ function formatDatetime($datetime, $format = 'Y-m-d H:i:s') } return $datetime; } + +function formatTtl($seconds) +{ + if ($seconds < 60) { + return $seconds . "seconds"; + } +} diff --git a/lang/chs/lang_functions.php b/lang/chs/lang_functions.php index 26741592..734fa81a 100644 --- a/lang/chs/lang_functions.php +++ b/lang/chs/lang_functions.php @@ -324,6 +324,10 @@ $lang_functions = array 'exam_progress_result' => '结果', 'exam_progress_result_pass_yes' => '通过!', 'exam_progress_result_pass_no' => '未通过!', + 'exam_index_' . \App\Models\Exam::INDEX_UPLOADED => '上传量', + 'exam_index_' . \App\Models\Exam::INDEX_SEED_TIME_AVERAGE => '平均做种时间', + 'exam_index_' . \App\Models\Exam::INDEX_DOWNLOADED => '下载量', + 'exam_index_' . \App\Models\Exam::INDEX_BONUS => '魔力值', ); ?> diff --git a/lang/cht/lang_functions.php b/lang/cht/lang_functions.php index 64606661..d9928b25 100644 --- a/lang/cht/lang_functions.php +++ b/lang/cht/lang_functions.php @@ -316,7 +316,19 @@ $lang_functions = array 'text_required' => '不能為空', 'text_invalid' => '非法', 'text_technical_info' => '技術信息', - 'text_technical_info_help_text' => '文件技術信息來自軟件 MediaInfo,用該軟件打開文件,點擊菜單視圖(View)->文件(Text),在框中右鍵->全選,再右鍵->復制,粘貼到這裏來。' + 'text_technical_info_help_text' => '文件技術信息來自軟件 MediaInfo,用該軟件打開文件,點擊菜單視圖(View)->文件(Text),在框中右鍵->全選,再右鍵->復制,粘貼到這裏來。', + 'exam_name' => '考核名稱', + 'exam_time_range' => '考核時間', + 'exam_index' => '考核指標', + 'exam_require' => '要求', + 'exam_progress_current' => '當前', + 'exam_progress_result' => '結果', + 'exam_progress_result_pass_yes' => '通過!', + 'exam_progress_result_pass_no' => '未通過!', + 'exam_index_' . \App\Models\Exam::INDEX_UPLOADED => '上傳量', + 'exam_index_' . \App\Models\Exam::INDEX_SEED_TIME_AVERAGE => '平均做種時間', + 'exam_index_' . \App\Models\Exam::INDEX_DOWNLOADED => '下載量', + 'exam_index_' . \App\Models\Exam::INDEX_BONUS => '魔力值', ); ?> diff --git a/lang/en/lang_functions.php b/lang/en/lang_functions.php index f88e5d06..c450842f 100644 --- a/lang/en/lang_functions.php +++ b/lang/en/lang_functions.php @@ -317,7 +317,19 @@ $lang_functions = array 'text_required' => 'Required', 'text_invalid' => 'Invalid', 'text_technical_info' => 'Technical Info', - 'text_technical_info_help_text' => 'Technical Information comes from software MediaInfo,open file, click the view menu > text > right click in the box > select all > copy > past into this box.' + 'text_technical_info_help_text' => 'Technical Information comes from software MediaInfo,open file, click the view menu > text > right click in the box > select all > copy > past into this box.', + 'exam_name' => 'Exam Name', + 'exam_time_range' => 'Exam Time', + 'exam_index' => 'Exam Index', + 'exam_require' => 'Require', + 'exam_progress_current' => 'Current', + 'exam_progress_result' => 'Result', + 'exam_progress_result_pass_yes' => 'Pass!', + 'exam_progress_result_pass_no' => 'Not Pass!', + 'exam_index_' . \App\Models\Exam::INDEX_UPLOADED => 'Uploaded', + 'exam_index_' . \App\Models\Exam::INDEX_SEED_TIME_AVERAGE => 'Seed Time Average', + 'exam_index_' . \App\Models\Exam::INDEX_DOWNLOADED => 'Downloaded', + 'exam_index_' . \App\Models\Exam::INDEX_BONUS => 'Bonus', ); ?> diff --git a/nexus/Exam/Exam.php b/nexus/Exam/Exam.php index 3992aff0..efc92ee8 100644 --- a/nexus/Exam/Exam.php +++ b/nexus/Exam/Exam.php @@ -25,10 +25,10 @@ class Exam $currentValue = $userExam->progress[$index['index']] ?? 0; $unit = ExamModel::$indexes[$index['index']]['unit'] ?? ''; $row[] = sprintf( - '%s:%s, %s:%s %s, %s:%s %s, %s:%s', - $lang_functions['exam_index'] . ($key + 1), ExamModel::$indexes[$index['index']]['name'] ?? '', + '%s:%s, %s:%s %s, %s:%s, %s:%s', + $lang_functions['exam_index'] . ($key + 1), $lang_functions['exam_index_' . $index['index']] ?? '', $lang_functions['exam_require'], $requireValue, $unit, - $lang_functions['exam_progress_current'], $currentValue, $unit, + $lang_functions['exam_progress_current'], $this->formatCurrentValue($index['index'], $currentValue), $lang_functions['exam_progress_result'], $currentValue >= $requireValue ? $lang_functions['exam_progress_result_pass_yes'] : $lang_functions['exam_progress_result_pass_no'] ); @@ -36,4 +36,16 @@ class Exam } return nl2br(implode("\n", $row)); } + + private function formatCurrentValue($indexId, $value) + { + if ($indexId == ExamModel::INDEX_DOWNLOADED || $indexId == ExamModel::INDEX_UPLOADED) { + return mksize($value); + } + if ($indexId == ExamModel::INDEX_SEED_TIME_AVERAGE) { + return mkprettytime($value); + } + return $value; + + } } diff --git a/resources/views/welcome.blade.php b/resources/views/welcome.blade.php index b1905c92..9c660a2e 100644 --- a/resources/views/welcome.blade.php +++ b/resources/views/welcome.blade.php @@ -4,7 +4,7 @@ - Laravel + NexusPHP @@ -22,40 +22,18 @@
- @if (Route::has('login')) - - @endif -
-
- - - - - -
-
- Laravel has wonderful, thorough documentation covering every aspect of the framework. Whether you are new to the framework or have previous experience with Laravel, we recommend reading all of the documentation from beginning to end. + 功能完善、生态丰富、久经市场检验的 PT 建站系统。
@@ -63,12 +41,12 @@
- Laracasts offers thousands of video tutorials on Laravel, PHP, and JavaScript development. Check them out, see for yourself, and massively level up your development skills in the process. + 当今最流行的 PHP 开发框架。
@@ -76,12 +54,12 @@
- Laravel News is a community driven portal and newsletter aggregating all of the latest and most important news in the Laravel ecosystem, including new package releases and tutorials. + 包含安装、配置、使用等知识。
@@ -89,12 +67,12 @@
-
Vibrant Ecosystem
+
- Laravel's robust library of first-party tools and libraries, such as Forge, Vapor, Nova, and Envoyer help you take your projects to the next level. Pair them with powerful open source libraries like Cashier, Dusk, Echo, Horizon, Sanctum, Telescope, and more. + 为之贡献代码、提出功能建议或反馈 bug。
@@ -102,28 +80,9 @@
-
-
- - - - - - Shop - - - - - - - - Sponsor - -
-
- Laravel v{{ Illuminate\Foundation\Application::VERSION }} (PHP v{{ PHP_VERSION }}) + Nexus PHP 1.6 + Laravel v{{ Illuminate\Foundation\Application::VERSION }} (PHP v{{ PHP_VERSION }})