2021-04-19 20:13:21 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
2022-06-27 01:39:01 +08:00
|
|
|
use Carbon\Carbon;
|
2021-05-08 16:25:55 +08:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
|
2021-04-19 20:13:21 +08:00
|
|
|
class Exam extends NexusModel
|
|
|
|
|
{
|
2022-04-18 19:07:35 +08:00
|
|
|
protected $fillable = ['name', 'description', 'begin', 'end', 'duration', 'status', 'is_discovered', 'filters', 'indexes', 'priority'];
|
2021-04-23 20:05:39 +08:00
|
|
|
|
2021-04-25 02:12:14 +08:00
|
|
|
public $timestamps = true;
|
|
|
|
|
|
2021-04-23 20:05:39 +08:00
|
|
|
protected $casts = [
|
2023-04-29 03:46:14 +08:00
|
|
|
'filters' => 'array',
|
2021-04-23 20:05:39 +08:00
|
|
|
'indexes' => 'array',
|
|
|
|
|
];
|
2021-04-20 20:18:02 +08:00
|
|
|
|
|
|
|
|
const STATUS_ENABLED = 0;
|
|
|
|
|
const STATUS_DISABLED = 1;
|
|
|
|
|
|
|
|
|
|
public static $status = [
|
2021-04-23 20:05:39 +08:00
|
|
|
self::STATUS_ENABLED => ['text' => 'Enabled'],
|
|
|
|
|
self::STATUS_DISABLED => ['text' => 'Disabled'],
|
2021-04-20 20:18:02 +08:00
|
|
|
];
|
|
|
|
|
|
2021-05-02 17:24:05 +08:00
|
|
|
const DISCOVERED_YES = 1;
|
|
|
|
|
const DISCOVERED_NO = 0;
|
|
|
|
|
|
|
|
|
|
public static $discovers = [
|
|
|
|
|
self::DISCOVERED_NO => ['text' => 'No'],
|
|
|
|
|
self::DISCOVERED_YES => ['text' => 'Yes'],
|
|
|
|
|
];
|
|
|
|
|
|
2021-04-20 20:18:02 +08:00
|
|
|
const INDEX_UPLOADED = 1;
|
2021-04-27 02:44:44 +08:00
|
|
|
const INDEX_SEED_TIME_AVERAGE = 2;
|
2021-04-20 20:18:02 +08:00
|
|
|
const INDEX_DOWNLOADED = 3;
|
2021-05-05 22:28:19 +08:00
|
|
|
const INDEX_SEED_BONUS = 4;
|
2023-11-08 03:17:23 +08:00
|
|
|
const INDEX_SEED_POINTS = 5;
|
|
|
|
|
const INDEX_UPLOAD_TORRENT_COUNT = 6;
|
2021-04-20 20:18:02 +08:00
|
|
|
|
2023-11-08 03:17:23 +08:00
|
|
|
public static array $indexes = [
|
2021-06-12 23:21:40 +08:00
|
|
|
self::INDEX_UPLOADED => ['name' => 'Uploaded', 'unit' => 'GB', 'source_user_field' => 'uploaded'],
|
|
|
|
|
self::INDEX_DOWNLOADED => ['name' => 'Downloaded', 'unit' => 'GB', 'source_user_field' => 'downloaded'],
|
2022-07-02 15:08:23 +08:00
|
|
|
self::INDEX_SEED_TIME_AVERAGE => ['name' => 'Seed time average', 'unit' => 'Hour', 'source_user_field' => 'seedtime'],
|
2022-02-22 13:34:50 +08:00
|
|
|
self::INDEX_SEED_BONUS => ['name' => 'Bonus', 'unit' => '', 'source_user_field' => 'seedbonus'],
|
2023-11-08 03:17:23 +08:00
|
|
|
self::INDEX_SEED_POINTS => ['name' => 'Seed points', 'unit' => '', 'source_user_field' => ''],
|
|
|
|
|
self::INDEX_UPLOAD_TORRENT_COUNT => ['name' => 'Upload torrent', 'unit' => '', 'source_user_field' => ''],
|
2021-04-23 20:05:39 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const FILTER_USER_CLASS = 'classes';
|
|
|
|
|
const FILTER_USER_REGISTER_TIME_RANGE = 'register_time_range';
|
2021-06-11 20:32:57 +08:00
|
|
|
const FILTER_USER_DONATE = 'donate_status';
|
2023-11-15 02:19:03 +08:00
|
|
|
const FILTER_USER_REGISTER_DAYS_RANGE = 'register_days_range';
|
2021-04-23 20:05:39 +08:00
|
|
|
|
|
|
|
|
public static $filters = [
|
2021-05-02 17:24:05 +08:00
|
|
|
self::FILTER_USER_CLASS => ['name' => 'User class'],
|
|
|
|
|
self::FILTER_USER_REGISTER_TIME_RANGE => ['name' => 'User register time range'],
|
2021-06-12 23:21:40 +08:00
|
|
|
self::FILTER_USER_DONATE => ['name' => 'User donated'],
|
2023-11-15 02:19:03 +08:00
|
|
|
self::FILTER_USER_REGISTER_DAYS_RANGE => ['name' => 'User register days range'],
|
2021-04-20 20:18:02 +08:00
|
|
|
];
|
|
|
|
|
|
2021-05-08 16:25:55 +08:00
|
|
|
protected static function booted()
|
|
|
|
|
{
|
|
|
|
|
static::saving(function (Model $model) {
|
|
|
|
|
$model->duration = (int)$model->duration;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-02 15:08:23 +08:00
|
|
|
public static function listIndex($onlyKeyValue = false): array
|
|
|
|
|
{
|
|
|
|
|
$result = self::$indexes;
|
|
|
|
|
$keyValues = [];
|
|
|
|
|
foreach ($result as $key => &$value) {
|
|
|
|
|
$text = nexus_trans("exam.index_text_$key");
|
|
|
|
|
$value['text'] = $text;
|
|
|
|
|
$keyValues[$key] = $text;
|
|
|
|
|
}
|
|
|
|
|
if ($onlyKeyValue) {
|
|
|
|
|
return $keyValues;
|
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-27 02:44:44 +08:00
|
|
|
public function getStatusTextAttribute(): string
|
2021-04-20 20:18:02 +08:00
|
|
|
{
|
2022-07-02 15:08:23 +08:00
|
|
|
return $this->status == self::STATUS_ENABLED ? nexus_trans('label.enabled') : nexus_trans('label.disabled');
|
2021-04-20 20:18:02 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-02 17:24:05 +08:00
|
|
|
public function getIsDiscoveredTextAttribute(): string
|
|
|
|
|
{
|
|
|
|
|
return self::$discovers[$this->is_discovered]['text'] ?? '';
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-08 16:31:19 +08:00
|
|
|
public function getDurationTextAttribute(): string
|
2021-05-06 01:49:05 +08:00
|
|
|
{
|
2021-05-08 16:31:19 +08:00
|
|
|
if ($this->duration > 0) {
|
|
|
|
|
return $this->duration . ' Days';
|
2021-05-08 16:25:55 +08:00
|
|
|
}
|
|
|
|
|
return '';
|
2021-05-06 01:49:05 +08:00
|
|
|
}
|
|
|
|
|
|
2022-06-27 01:39:01 +08:00
|
|
|
public function getIndexFormattedAttribute(): string
|
|
|
|
|
{
|
|
|
|
|
$indexes = $this->indexes;
|
|
|
|
|
$arr = [];
|
|
|
|
|
foreach ($indexes as $index) {
|
|
|
|
|
if (isset($index['checked']) && $index['checked']) {
|
|
|
|
|
$arr[] = sprintf(
|
|
|
|
|
'%s: %s %s',
|
2022-07-02 15:08:23 +08:00
|
|
|
nexus_trans("exam.index_text_{$index['index']}"),
|
2022-06-27 01:39:01 +08:00
|
|
|
$index['require_value'],
|
|
|
|
|
self::$indexes[$index['index']]['unit'] ?? ''
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return implode("<br/>", $arr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getFilterFormattedAttribute(): string
|
|
|
|
|
{
|
|
|
|
|
$currentFilters = $this->filters;
|
|
|
|
|
$arr = [];
|
|
|
|
|
$filter = self::FILTER_USER_CLASS;
|
2023-04-29 03:46:14 +08:00
|
|
|
if (!empty($currentFilters[$filter])) {
|
|
|
|
|
$classes = collect(User::$classes)->only($currentFilters[$filter]);
|
2022-07-02 15:08:23 +08:00
|
|
|
$arr[] = sprintf('%s: %s', nexus_trans("exam.filters.$filter"), $classes->pluck('text')->implode(', '));
|
2022-06-27 01:39:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$filter = self::FILTER_USER_REGISTER_TIME_RANGE;
|
2023-04-29 03:46:14 +08:00
|
|
|
if (!empty($currentFilters[$filter])) {
|
|
|
|
|
$range = $currentFilters[$filter];
|
|
|
|
|
if (!empty($range[0]) || !empty($range[1])) {
|
|
|
|
|
$arr[] = sprintf(
|
|
|
|
|
"%s: <br/>%s ~ %s",
|
|
|
|
|
nexus_trans("exam.filters.$filter"),
|
|
|
|
|
$range[0] ? Carbon::parse($range[0])->toDateTimeString() : '--',
|
|
|
|
|
$range[1] ? Carbon::parse($range[1])->toDateTimeString() : '--'
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-06-27 01:39:01 +08:00
|
|
|
}
|
|
|
|
|
|
2023-11-15 02:19:03 +08:00
|
|
|
$filter = self::FILTER_USER_REGISTER_DAYS_RANGE;
|
|
|
|
|
if (!empty($currentFilters[$filter])) {
|
|
|
|
|
$range = $currentFilters[$filter];
|
|
|
|
|
if (!empty($range[0]) || !empty($range[1])) {
|
|
|
|
|
$arr[] = sprintf(
|
|
|
|
|
"%s: %s ~ %s",
|
|
|
|
|
nexus_trans("exam.filters.$filter"),
|
|
|
|
|
$range[0] ?? "--",
|
|
|
|
|
$range[1] ?? '--'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-27 01:39:01 +08:00
|
|
|
$filter = self::FILTER_USER_DONATE;
|
2023-04-29 03:46:14 +08:00
|
|
|
if (!empty($currentFilters[$filter])) {
|
|
|
|
|
$donateStatus = collect(User::$donateStatus)->only($currentFilters[$filter]);
|
2022-07-02 15:08:23 +08:00
|
|
|
$arr[] = sprintf('%s: %s', nexus_trans("exam.filters.$filter"), $donateStatus->pluck('text')->implode(', '));
|
2022-06-27 01:39:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return implode("<br/>", $arr);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-19 20:13:21 +08:00
|
|
|
}
|