From 7cfde3f95bd30606c980db61b10af5ae08561c25 Mon Sep 17 00:00:00 2001
From: xiaomlove <353856593@qq.com>
Date: Sun, 25 Apr 2021 21:28:58 +0800
Subject: [PATCH] show-exam
---
app/Console/Commands/Test.php | 9 ++-
app/Models/ExamProgress.php | 2 +-
app/Models/ExamUser.php | 5 ++
app/Models/Torrent.php | 34 ++++++++++
app/Models/User.php | 21 ++++--
app/Repositories/ExamRepository.php | 67 +++++++++++++++++++
bootstrap/app.php | 6 ++
...4_19_062743_create_exam_progress_table.php | 4 +-
include/core.php | 1 +
include/eloquent.php | 15 +++++
include/functions.php | 11 ++-
lang/chs/lang_functions.php | 5 +-
nexus/Exam/Exam.php | 43 ++++++++++++
13 files changed, 206 insertions(+), 17 deletions(-)
create mode 100644 app/Models/Torrent.php
create mode 100644 include/eloquent.php
create mode 100644 nexus/Exam/Exam.php
diff --git a/app/Console/Commands/Test.php b/app/Console/Commands/Test.php
index 5fa69f15..72c9f849 100644
--- a/app/Console/Commands/Test.php
+++ b/app/Console/Commands/Test.php
@@ -2,9 +2,13 @@
namespace App\Console\Commands;
+use App\Models\Exam;
+use App\Models\ExamProgress;
+use App\Models\ExamUser;
use App\Models\User;
use App\Repositories\ExamRepository;
use Illuminate\Console\Command;
+use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
class Test extends Command
@@ -40,9 +44,8 @@ class Test extends Command
*/
public function handle()
{
- $examRep = new ExamRepository();
- $user = User::query()->find(1);
- $r = $examRep->assignToUser($user->id);
+ $rep = new ExamRepository();
+ $r = $rep->listUserExamProgress(1);
dd($r);
}
}
diff --git a/app/Models/ExamProgress.php b/app/Models/ExamProgress.php
index e4050ca7..f1f0565f 100644
--- a/app/Models/ExamProgress.php
+++ b/app/Models/ExamProgress.php
@@ -4,7 +4,7 @@ namespace App\Models;
class ExamProgress extends NexusModel
{
- protected $fillable = ['exam_id', 'uid', 'type_id', 'value'];
+ protected $fillable = ['exam_user_id', 'exam_id', 'uid', 'index', 'value', 'torrent_id'];
public $timestamps = true;
}
diff --git a/app/Models/ExamUser.php b/app/Models/ExamUser.php
index 6ee855af..5347e5cf 100644
--- a/app/Models/ExamUser.php
+++ b/app/Models/ExamUser.php
@@ -35,5 +35,10 @@ class ExamUser extends NexusModel
return $this->belongsTo(User::class, 'uid');
}
+ public function progresses()
+ {
+ return $this->hasMany(ExamProgress::class, 'exam_user_id');
+ }
+
}
diff --git a/app/Models/Torrent.php b/app/Models/Torrent.php
new file mode 100644
index 00000000..b920c32e
--- /dev/null
+++ b/app/Models/Torrent.php
@@ -0,0 +1,34 @@
+getAttribute('visible') != self::VISIBLE_YES) {
+ throw new \InvalidArgumentException(sprintf('Torrent: %s is not visible.', $this->id));
+ }
+ if (in_array('banned', $fields) && $this->getAttribute('banned') == self::BANNED_YES) {
+ throw new \InvalidArgumentException(sprintf('Torrent: %s is banned.', $this->id));
+ }
+
+ return true;
+ }
+}
diff --git a/app/Models/User.php b/app/Models/User.php
index 73a755a2..bb4ef304 100644
--- a/app/Models/User.php
+++ b/app/Models/User.php
@@ -16,6 +16,9 @@ class User extends Authenticatable
const STATUS_CONFIRMED = 'confirmed';
const STATUS_PENDING = 'pending';
+ const ENABLED_YES = 'yes';
+ const ENABLED_NO = 'no';
+
const CLASS_PEASANT = "0";
const CLASS_USER = "1";
const CLASS_POWER_USER = "2";
@@ -96,12 +99,20 @@ class User extends Authenticatable
* @var array
*/
protected $casts = [
-
+ 'added' => 'datetime',
];
- protected $dates = [
- 'added'
- ];
+ public function checkIsNormal(array $fields = ['status', 'enabled'])
+ {
+ if (in_array('visible', $fields) && $this->getAttribute('status') != self::STATUS_CONFIRMED) {
+ throw new \InvalidArgumentException(sprintf('User: %s is not confirmed.', $this->id));
+ }
+ if (in_array('enabled', $fields) && $this->getAttribute('enabled') != self::ENABLED_YES) {
+ throw new \InvalidArgumentException(sprintf('User: %s is not enabled.', $this->id));
+ }
+
+ return true;
+ }
public function exams(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
@@ -111,7 +122,7 @@ class User extends Authenticatable
public function examDetails(): \Illuminate\Database\Eloquent\Relations\HasMany
{
- return $this->hasMany(ExamUser::class. 'uid');
+ return $this->hasMany(ExamUser::class, 'uid');
}
diff --git a/app/Repositories/ExamRepository.php b/app/Repositories/ExamRepository.php
index f944f39f..15b6a122 100644
--- a/app/Repositories/ExamRepository.php
+++ b/app/Repositories/ExamRepository.php
@@ -2,8 +2,10 @@
namespace App\Repositories;
use App\Models\Exam;
+use App\Models\ExamProgress;
use App\Models\ExamUser;
use App\Models\Setting;
+use App\Models\Torrent;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Support\Arr;
@@ -141,6 +143,7 @@ class ExamRepository extends BaseRepository
return $filtered;
}
+
/**
* assign exam to user
*
@@ -187,6 +190,70 @@ class ExamRepository extends BaseRepository
}
+ public function addProgress(int $examUserId, int $indexId, int $value, int $torrentId)
+ {
+ $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);
+ $torrent->checkIsNormal(true, $torrentFields);
+
+ $user = $examUser->user;
+ $user->checkIsNormal();
+
+ $data = [
+ 'uid' => $user->id,
+ 'exam_id' => $exam->id,
+ 'torrent_id' => $torrentId,
+ 'index' => $indexId,
+ 'value' => $value,
+ ];
+ Log::info('[addProgress]', $data);
+ return $examUser->progresses()->create($data);
+ }
+
+ public function listUserExamProgress($uid, $status = null)
+ {
+ $logPrefix = "uid: $uid";
+ $query = ExamUser::query()->with(['exam', 'user'])->where('uid', $uid)->orderBy('exam_id', 'desc');
+ if ($status) {
+ $query->where('status', $status);
+ }
+ $result = $query->paginate();
+ $idArr = array_column($result->items(), 'id');
+ $progressSum = ExamProgress::query()
+ ->whereIn('exam_user_id', $idArr)
+ ->groupBy(['exam_user_id', 'index'])
+ ->selectRaw('`exam_user_id`, `index`, sum(`value`) as `index_sum`')
+ ->get();
+
+ $map = [];
+ foreach ($progressSum as $value) {
+ $map[$value->exam_user_id][$value->index] = $value->index_sum;
+ }
+
+ foreach ($result as &$item) {
+ $item->progress_value = $map[$item->id] ?? [];
+ }
+
+ return $result;
+
+ }
+
diff --git a/bootstrap/app.php b/bootstrap/app.php
index 037e17df..6daf55fc 100644
--- a/bootstrap/app.php
+++ b/bootstrap/app.php
@@ -1,5 +1,11 @@
id();
+ $table->integer('exam_user_id')->index();
$table->integer('exam_id')->index();
$table->integer('uid')->index();
- $table->integer('type_id');
+ $table->integer('torrent_id');
+ $table->integer('index');
$table->integer('value');
$table->timestamps();
$table->index('created_at');
diff --git a/include/core.php b/include/core.php
index c9355d0e..ba7e32d7 100644
--- a/include/core.php
+++ b/include/core.php
@@ -17,6 +17,7 @@ if (!file_exists($rootpath . '.env')) {
require $rootpath . 'vendor/autoload.php';
require $rootpath . 'nexus/Database/helpers.php';
require $rootpath . 'classes/class_cache_redis.php';
+require $rootpath . 'include/eloquent.php';
require $rootpath . 'include/config.php';
ini_set('date.timezone', nexus_config('nexus.timezone'));
diff --git a/include/eloquent.php b/include/eloquent.php
new file mode 100644
index 00000000..9439d4b1
--- /dev/null
+++ b/include/eloquent.php
@@ -0,0 +1,15 @@
+addConnection($connectionMysql, 'default');
+$capsule->setAsGlobal();
+$capsule->bootEloquent();
+
+
diff --git a/include/functions.php b/include/functions.php
index 0eb2493e..cbac95e4 100644
--- a/include/functions.php
+++ b/include/functions.php
@@ -2598,12 +2598,11 @@ if ($msgalert)
}
//show the exam info
- $examRepository = new \App\Repositories\ExamRepository();
- $matchExams = $examRepository->listMatchExam($CURUSER['id']);
-// if ($matchExams->isNotEmpty()) {
-// dd($matchExams);
-// }
-
+ $exam = new \Nexus\Exam\Exam();
+ $examHtml = $exam->render($CURUSER['id']);
+ if (!empty($examHtml)) {
+ msgalert("messages.php", $examHtml, "green");
+ }
}
if ($offlinemsg)
{
diff --git a/lang/chs/lang_functions.php b/lang/chs/lang_functions.php
index ffd2a2a6..4c050910 100644
--- a/lang/chs/lang_functions.php
+++ b/lang/chs/lang_functions.php
@@ -315,7 +315,10 @@ $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' => '考核指标',
);
?>
diff --git a/nexus/Exam/Exam.php b/nexus/Exam/Exam.php
new file mode 100644
index 00000000..3806fe8b
--- /dev/null
+++ b/nexus/Exam/Exam.php
@@ -0,0 +1,43 @@
+listUserExamProgress($uid);
+ if ($userExams->isEmpty()) {
+ return '';
+ }
+ $htmlArr = [];
+ foreach ($userExams as $userExam) {
+ $exam = $userExam->exam;
+ $row = [];
+ $row[] = sprintf('%s:%s', $lang_functions['exam_name'], $exam->name);
+ $row[] = sprintf('%s:%s ~ %s', $lang_functions['exam_time_range'], $exam->begin, $exam->end);
+ foreach ($exam->indexes as $key => $index) {
+ if (isset($index['checked']) && $index['checked']) {
+ $requireValue = $index['require_value'];
+ $currentValue = $userExam->progress_value[$index['index']] ?? 0;
+ $unit = ExamModel::$indexes[$index['index']]['unit'] ?? '';
+ $row[] = sprintf(
+ '%s:%s, Require:%s %s, Current:%s %s, Result:%s',
+ $lang_functions['exam_index'] . ($key + 1),
+ ExamModel::$indexes[$index['index']]['name'] ?? '',
+ $requireValue, $unit,
+ $currentValue, $unit,
+ $currentValue >= $requireValue ? 'Done!' : 'Not done!'
+ );
+ }
+ }
+ $htmlArr[] = implode("\n", $row);
+ }
+ return nl2br(implode("\n\n", $htmlArr));
+ }
+}