show-exam

This commit is contained in:
xiaomlove
2021-04-25 21:28:58 +08:00
parent 512af7511d
commit 7cfde3f95b
13 changed files with 206 additions and 17 deletions

View File

@@ -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);
}
}

View File

@@ -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;
}

View File

@@ -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');
}
}

34
app/Models/Torrent.php Normal file
View File

@@ -0,0 +1,34 @@
<?php
namespace App\Models;
class Torrent extends NexusModel
{
protected $fillable = [
'name', 'filename', 'save_as', 'descr', 'small_descr', 'ori_descr',
'category', 'source', 'medium', 'codec', 'standard', 'processing', 'team', 'audiocodec',
'size', 'added', 'type', 'numfiles', 'owner', 'nfo', 'sp_state', 'promotion_time_type',
'promotion_until', 'anonymous', 'url', 'pos_state', 'cache_stamp', 'picktype', 'picktime',
'last_reseed', 'pt_gen', 'tags', 'technical_info'
];
const VISIBLE_YES = 'yes';
const VISIBLE_NO = 'no';
const BANNED_YES = 'yes';
const BANNED_NO = 'no';
public $timestamps = true;
public function checkIsNormal(array $fields = ['visible', 'banned'])
{
if (in_array('visible', $fields) && $this->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;
}
}

View File

@@ -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');
}

View File

@@ -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;
}

View File

@@ -1,5 +1,11 @@
<?php
if (!empty($_SERVER['HTTP_X_REQUEST_ID'])) {
define('REQUEST_ID', $_SERVER['HTTP_X_REQUEST_ID']);
} else {
define('REQUEST_ID', intval(LARAVEL_START * 10000));
}
/*
|--------------------------------------------------------------------------
| Create The Application

View File

@@ -15,9 +15,11 @@ class CreateExamProgressTable extends Migration
{
Schema::create('exam_progress', function (Blueprint $table) {
$table->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');

View File

@@ -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'));

15
include/eloquent.php Normal file
View File

@@ -0,0 +1,15 @@
<?php
use \Illuminate\Database\Capsule\Manager as Capsule;
$config = require ROOT_PATH . 'config/nexus.php';
$connectionMysql = $config['mysql'];
$connectionMysql['driver'] = 'mysql';
$connectionMysql['charset'] = 'utf8mb4';
$connectionMysql['collation'] = 'utf8mb4_unicode_ci';
$capsule = new Capsule;
$capsule->addConnection($connectionMysql, 'default');
$capsule->setAsGlobal();
$capsule->bootEloquent();

View File

@@ -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)
{

View File

@@ -315,7 +315,10 @@ $lang_functions = array
'text_required' => '不能为空',
'text_invalid' => '非法',
'text_technical_info' => '技术信息',
'text_technical_info_help_text' => '文件技术信息来自软件 <b><a href="https://mediaarea.net/en/MediaInfo" target=\'_blank\'>MediaInfo</a></b>,用该软件打开文件,点击菜单视图(View)->文件(Text),在框中右键->全选,再右键->复制,粘贴到这里来。'
'text_technical_info_help_text' => '文件技术信息来自软件 <b><a href="https://mediaarea.net/en/MediaInfo" target=\'_blank\'>MediaInfo</a></b>,用该软件打开文件,点击菜单视图(View)->文件(Text),在框中右键->全选,再右键->复制,粘贴到这里来。',
'exam_name' => '考核名称',
'exam_time_range' => '考核时间',
'exam_index' => '考核指标',
);
?>

43
nexus/Exam/Exam.php Normal file
View File

@@ -0,0 +1,43 @@
<?php
namespace Nexus\Exam;
use App\Repositories\ExamRepository;
use App\Models\Exam as ExamModel;
class Exam
{
public function render($uid)
{
global $lang_functions;
$examRep = new ExamRepository();
$userExams = $examRep->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));
}
}