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.
169 lines
3.9 KiB
169 lines
3.9 KiB
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
class StudyAsk extends SoftDeletesModel
|
|
{
|
|
protected $table = 'study_asks';
|
|
|
|
protected $casts = [
|
|
'answer' => 'array',
|
|
'type' => 'boolean',
|
|
];
|
|
|
|
// 题目类型常量
|
|
const TYPE_SINGLE = 1; // 单选
|
|
const TYPE_MULTIPLE = 2; // 多选
|
|
|
|
/**
|
|
* 关联学习内容
|
|
*/
|
|
public function study()
|
|
{
|
|
return $this->belongsTo(Study::class, 'study_id');
|
|
}
|
|
|
|
/**
|
|
* 获取题目类型文本
|
|
*/
|
|
public function getTypeTextAttribute()
|
|
{
|
|
$types = [
|
|
self::TYPE_SINGLE => '单选题',
|
|
self::TYPE_MULTIPLE => '多选题',
|
|
];
|
|
return $types[$this->type] ?? '未知';
|
|
}
|
|
|
|
/**
|
|
* 获取正确答案
|
|
*/
|
|
public function getCorrectAnswers()
|
|
{
|
|
if (!$this->answer) {
|
|
return [];
|
|
}
|
|
|
|
$correctAnswers = [];
|
|
foreach ($this->answer as $option) {
|
|
if (isset($option['result']) && $option['result'] == 1) {
|
|
$correctAnswers[] = $option['content'] ?? '';
|
|
}
|
|
}
|
|
return $correctAnswers;
|
|
}
|
|
|
|
/**
|
|
* 获取所有选项
|
|
*/
|
|
public function getOptions()
|
|
{
|
|
if (!$this->answer) {
|
|
return [];
|
|
}
|
|
|
|
$options = [];
|
|
foreach ($this->answer as $index => $option) {
|
|
$options[] = [
|
|
'index' => $index,
|
|
'content' => $option['content'] ?? '',
|
|
'is_correct' => isset($option['result']) && $option['result'] == 1,
|
|
];
|
|
}
|
|
return $options;
|
|
}
|
|
|
|
/**
|
|
* 检查答案是否正确
|
|
*/
|
|
public function checkAnswer($userAnswers)
|
|
{
|
|
$correctAnswers = $this->getCorrectAnswers();
|
|
|
|
if ($this->type == self::TYPE_SINGLE) {
|
|
// 单选题:用户答案必须是单个且与正确答案匹配
|
|
if (is_array($userAnswers)) {
|
|
$userAnswers = $userAnswers[0] ?? '';
|
|
}
|
|
return in_array($userAnswers, $correctAnswers);
|
|
} else {
|
|
// 多选题:用户答案必须与正确答案完全匹配
|
|
if (!is_array($userAnswers)) {
|
|
return false;
|
|
}
|
|
|
|
sort($userAnswers);
|
|
sort($correctAnswers);
|
|
return $userAnswers === $correctAnswers;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取题目难度(基于选项数量)
|
|
*/
|
|
public function getDifficulty()
|
|
{
|
|
$optionCount = count($this->answer ?? []);
|
|
|
|
if ($optionCount <= 2) {
|
|
return '简单';
|
|
} elseif ($optionCount <= 4) {
|
|
return '中等';
|
|
} else {
|
|
return '困难';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 验证题目数据完整性
|
|
*/
|
|
public function isValid()
|
|
{
|
|
// 检查基本信息
|
|
if (empty($this->title) || empty($this->answer)) {
|
|
return false;
|
|
}
|
|
|
|
// 检查答案格式
|
|
if (!is_array($this->answer)) {
|
|
return false;
|
|
}
|
|
|
|
// 检查是否有正确答案
|
|
$hasCorrectAnswer = false;
|
|
foreach ($this->answer as $option) {
|
|
if (isset($option['result']) && $option['result'] == 1) {
|
|
$hasCorrectAnswer = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!$hasCorrectAnswer) {
|
|
return false;
|
|
}
|
|
|
|
// 检查选项内容
|
|
foreach ($this->answer as $option) {
|
|
if (empty($option['content'])) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 获取题目统计信息
|
|
*/
|
|
public function getStatistics()
|
|
{
|
|
return [
|
|
'type' => $this->getTypeTextAttribute(),
|
|
'difficulty' => $this->getDifficulty(),
|
|
'option_count' => count($this->answer ?? []),
|
|
'correct_answer_count' => count($this->getCorrectAnswers()),
|
|
'is_valid' => $this->isValid(),
|
|
];
|
|
}
|
|
}
|