You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
2.6 KiB

11 months ago
<?php
namespace App\Models;
class Study extends SoftDeletesModel
{
protected $table = 'studies';
protected $casts = [
'file' => 'array',
'expire_day' => 'integer',
'minute' => 'integer',
'rate' => 'decimal:2',
];
// 学习类型常量
const TYPE_VISITOR = 1; // 访客
const TYPE_VISITOR_CAR = 2; // 访客车辆
const TYPE_LOGISTICS_CAR = 3; // 物流车辆
/**
* 关联学习题目
*/
public function studyAsks()
{
return $this->hasMany(StudyAsk::class, 'study_id');
}
/**
* 获取学习类型文本
*/
public function getTypeTextAttribute()
{
$types = [
self::TYPE_VISITOR => '访客',
self::TYPE_VISITOR_CAR => '访客车辆',
self::TYPE_LOGISTICS_CAR => '物流车辆',
];
return $types[$this->type] ?? '未知';
}
/**
11 months ago
* 获取文件列表(多文件)
11 months ago
*/
public function getFilesAttribute()
{
if (!$this->file) {
return [];
}
return Upload::whereIn('id', $this->file)->get();
}
/**
* 检查学习是否有效
*/
public function isValid()
{
return $this->expire_day > 0;
}
/**
* 获取学习有效期(天数)
*/
public function getExpireDays()
{
return $this->expire_day ?? 0;
}
/**
* 获取最低学习时间(分钟)
*/
public function getMinute()
{
return $this->minute ?? 0;
}
/**
* 获取通过正确率
*/
public function getPassRate()
{
return $this->rate ?? 0;
}
/**
* 获取随机题目
*/
public function getRandomQuestions($limit = 10)
{
return $this->studyAsks()
->inRandomOrder()
->limit($limit)
->get();
}
/**
* 检查学习内容是否完整
*/
public function isComplete()
{
return !empty($this->name) &&
!empty($this->content) &&
$this->studyAsks()->count() > 0;
}
/**
* 获取学习统计信息
*/
public function getStatistics()
{
return [
'total_questions' => $this->studyAsks()->count(),
'single_choice' => $this->studyAsks()->where('type', StudyAsk::TYPE_SINGLE)->count(),
'multiple_choice' => $this->studyAsks()->where('type', StudyAsk::TYPE_MULTIPLE)->count(),
'expire_days' => $this->getExpireDays(),
'minute' => $this->getMinute(),
'pass_rate' => $this->getPassRate(),
];
}
}