<?php
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* QuizModel.php
|
|
* Created by PhpStorm.
|
|
* Date: 2026/10/17
|
|
* Time: 09:40
|
|
* Author: qian.guo
|
|
* E-mail: yiyezhiqiutel@gmail.com
|
|
*/
|
|
|
|
namespace app\model;
|
|
|
|
use think\facade\Log;
|
|
|
|
/**
|
|
* 题库模型
|
|
* 设计说明:
|
|
* - 数据源当前是单一 mock 文件(config('quiz.mock_path')),数据库设计与建表已延后
|
|
* - 建库后本类方法体换成 Db::name('question') 等,方法名与返回结构不变,调用方无感知
|
|
* - 只做数据读取与结构整理,不做业务判断、不拼 json
|
|
* - 题目数量/轴数/极点数/类型数/功能数全部由数据决定,本类不写死任何数量
|
|
*/
|
|
class QuizModel
|
|
{
|
|
// 单次请求内缓存,避免同一请求反复读盘
|
|
private static $quiz_data = null;
|
|
|
|
/**
|
|
* 获取试卷信息
|
|
* getPaperInfo
|
|
* @return array
|
|
*/
|
|
public static function getPaperInfo()
|
|
{
|
|
$data = self::getQuizData();
|
|
|
|
return $data['paper'];
|
|
}
|
|
|
|
/**
|
|
* 获取轴列表
|
|
* getAxisList
|
|
* @return array 每条含 code / name / a / b / a_name / b_name
|
|
*/
|
|
public static function getAxisList()
|
|
{
|
|
$paper = self::getPaperInfo();
|
|
|
|
return array_values($paper['axes']);
|
|
}
|
|
|
|
/**
|
|
* 获取极点列表
|
|
* getPoleList
|
|
* @return array 键为极点码,值为 name / axis / desc
|
|
*/
|
|
public static function getPoleList()
|
|
{
|
|
$data = self::getQuizData();
|
|
|
|
return $data['poles'];
|
|
}
|
|
|
|
/**
|
|
* 获取题目列表
|
|
* getQuestionList
|
|
* @return array 二维数组,按文件顺序
|
|
*/
|
|
public static function getQuestionList()
|
|
{
|
|
$data = self::getQuizData();
|
|
|
|
return $data['questions'];
|
|
}
|
|
|
|
/**
|
|
* 获取题目总数
|
|
* getQuestionTotal
|
|
* @return int
|
|
*/
|
|
public static function getQuestionTotal()
|
|
{
|
|
return count(self::getQuestionList());
|
|
}
|
|
|
|
/**
|
|
* 获取按题号索引的题目表
|
|
* getQuestionMap
|
|
* @return array 键为题号,选项按选项 key 索引
|
|
*
|
|
* 评分引擎需要「按题号取题、按选项 key 取选项」的结构
|
|
* 原始 JSON 里 options 是列表,直接拿下标取会错位,所以在这里统一一次
|
|
*/
|
|
public static function getQuestionMap()
|
|
{
|
|
$question_list = self::getQuestionList();
|
|
|
|
$data = [];
|
|
foreach ($question_list as $question) {
|
|
$option_map = [];
|
|
foreach ($question['options'] as $option) {
|
|
$effect = null;
|
|
if (!empty($option['effect'])) {
|
|
$effect = $option['effect'];
|
|
}
|
|
|
|
$item = [];
|
|
$item['score'] = (float) $option['score'];
|
|
$item['dimension_code'] = (string) $option['dimension_code'];
|
|
$item['effect'] = $effect;
|
|
$option_map[$option['key']] = $item;
|
|
}
|
|
|
|
$item = [];
|
|
$item['q_no'] = intval($question['q_no']);
|
|
$item['dimension_code'] = (string) $question['dimension_code'];
|
|
$item['polarity'] = intval($question['polarity'] ?? 1);
|
|
$item['options'] = $option_map;
|
|
$data[$item['q_no']] = $item;
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 获取对外题目列表(白名单裁剪)
|
|
* getPublicQuestionList
|
|
* @return array 只含 q_no / title / options[key,text]
|
|
*
|
|
* 必须用白名单而不是「删掉 score 字段」:后者在题库新增字段时会默默泄漏
|
|
* score / dimension_code / effect 一旦泄漏,答案方向可直接反推,测评失去意义
|
|
*/
|
|
public static function getPublicQuestionList()
|
|
{
|
|
$question_list = self::getQuestionList();
|
|
|
|
$data = [];
|
|
foreach ($question_list as $question) {
|
|
$option_list = [];
|
|
foreach ($question['options'] as $option) {
|
|
$item = [];
|
|
$item['key'] = $option['key'];
|
|
$item['text'] = $option['text'];
|
|
$option_list[] = $item;
|
|
}
|
|
|
|
$item = [];
|
|
$item['q_no'] = $question['q_no'];
|
|
$item['title'] = $question['title'];
|
|
$item['options'] = $option_list;
|
|
$data[] = $item;
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 获取类型列表
|
|
* getTypeList
|
|
* @return array 键为类型码
|
|
*/
|
|
public static function getTypeList()
|
|
{
|
|
$data = self::getQuizData();
|
|
|
|
return $data['types'];
|
|
}
|
|
|
|
/**
|
|
* 获取指定类型信息
|
|
* getTypeInfo
|
|
* @param string $type_code 类型码,如 INFP
|
|
* @return array 不存在返回空数组
|
|
*/
|
|
public static function getTypeInfo($type_code)
|
|
{
|
|
$type_list = self::getTypeList();
|
|
|
|
if (empty($type_list[$type_code])) {
|
|
return [];
|
|
}
|
|
|
|
return $type_list[$type_code];
|
|
}
|
|
|
|
/**
|
|
* 获取认知功能定义列表
|
|
* getFunctionList
|
|
* @return array 未配置时返回空数组
|
|
*/
|
|
public static function getFunctionList()
|
|
{
|
|
$paper = self::getPaperInfo();
|
|
|
|
if (empty($paper['functions'])) {
|
|
return [];
|
|
}
|
|
|
|
return array_values($paper['functions']);
|
|
}
|
|
|
|
/**
|
|
* 读取题库数据
|
|
* getQuizData
|
|
* @return array
|
|
*/
|
|
private static function getQuizData()
|
|
{
|
|
if (self::$quiz_data !== null) {
|
|
return self::$quiz_data;
|
|
}
|
|
|
|
$mock_path = (string) config('quiz.mock_path');
|
|
|
|
if (!is_file($mock_path)) {
|
|
Log::error('[QuizModel] 题库数据文件缺失: ' . $mock_path);
|
|
|
|
return [];
|
|
}
|
|
|
|
$raw = file_get_contents($mock_path);
|
|
if ($raw === false) {
|
|
Log::error('[QuizModel] 题库数据文件不可读: ' . $mock_path);
|
|
|
|
return [];
|
|
}
|
|
|
|
$data = json_decode($raw, true);
|
|
if (empty($data['paper']) || empty($data['questions']) || empty($data['types'])) {
|
|
Log::error('[QuizModel] 题库数据格式不符(需含 paper / questions / types)');
|
|
|
|
return [];
|
|
}
|
|
|
|
self::$quiz_data = $data;
|
|
|
|
return $data;
|
|
}
|
|
}
|