初始化项目:添加后端代码、ThinkPHP框架、前端资源
This commit is contained in:
@@ -0,0 +1,248 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\model\app\exam;
|
||||
use think\Model;
|
||||
use app\api\model\app\exam\Exercises;
|
||||
use app\api\model\app\exam\WarehouseQuestion;
|
||||
use app\api\model\app\exam\ExercisesBindWarehouseGroup;
|
||||
/**
|
||||
* 题库练习记录
|
||||
*/
|
||||
class ExercisesLog extends Model
|
||||
{
|
||||
// 表名,不含前缀
|
||||
protected $name = 'app_exam_exercises_log';
|
||||
// 自动写入时间戳字段
|
||||
protected $autoWriteTimestamp = false;
|
||||
// 定义时间戳字段名
|
||||
protected $createTime = false;
|
||||
protected $updateTime = false;
|
||||
// 追加属性
|
||||
protected $append = [
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* 出题
|
||||
* @return void
|
||||
*/
|
||||
public function buildLog($userId,$exercisesId)
|
||||
{
|
||||
//获取出题配置
|
||||
$exercisesInfo = Exercises::where([
|
||||
'id'=>$exercisesId,
|
||||
'status'=>1
|
||||
])->find();
|
||||
|
||||
if(!$exercisesInfo){
|
||||
return false;
|
||||
}
|
||||
|
||||
$questionData = $this->getQuestionIds($userId,$exercisesId,$exercisesInfo['question_mode'],$exercisesInfo['question_count']);
|
||||
|
||||
if(!$questionData['ids']){
|
||||
return false;
|
||||
}
|
||||
|
||||
$logId = self::insertGetId([
|
||||
'user_id'=>$userId,
|
||||
'uniacid'=>UNIACID,
|
||||
'exercises_id'=>$exercisesId,
|
||||
'question_ids'=>implode(",",$questionData['ids']),
|
||||
'data_index'=>$questionData['data_index'],
|
||||
'question_count'=>count($questionData['ids']),
|
||||
'result_true_count'=>0,
|
||||
'createtime'=>time()
|
||||
]);
|
||||
|
||||
if(!$logId){
|
||||
return false;
|
||||
}
|
||||
|
||||
return $logId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取练习记录的问题列表
|
||||
* @param $logId
|
||||
* @return false
|
||||
*/
|
||||
public function getQuestionList($logId){
|
||||
//获取出题配置
|
||||
$logData = self::where([
|
||||
'id'=>$logId
|
||||
])->find();
|
||||
|
||||
if(!$logData){
|
||||
return false;
|
||||
}
|
||||
|
||||
$logData['question_ids'] = explode(",",$logData['question_ids']);
|
||||
|
||||
$questionList = $this->idsGetQestion($logData['question_ids']);
|
||||
|
||||
return $questionList;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 通过问题ID集合获取问题列表
|
||||
* @param $questionIds
|
||||
* @return false
|
||||
*/
|
||||
public function idsGetQestion($questionIds){
|
||||
|
||||
$questionList = WarehouseQuestion::where([
|
||||
'id'=>['in',$questionIds]
|
||||
])->orderRaw("find_in_set(id,'".implode(",",$questionIds)."')")->select();
|
||||
|
||||
if(!$questionList){
|
||||
return [];
|
||||
}
|
||||
|
||||
foreach ($questionList as &$item){
|
||||
$item->option = json_decode($item->option,true);
|
||||
|
||||
switch ($item['type']){
|
||||
case 'multiple':
|
||||
case 'indefinite':
|
||||
$item->answer = explode(",",$item->answer);
|
||||
break;
|
||||
case 'fillblank':
|
||||
$item->answer = json_decode($item->answer,true);
|
||||
break;
|
||||
case 'essay':
|
||||
// 问答题答案为富文本字符串,保持原样
|
||||
break;
|
||||
default:
|
||||
$item->answer = intval($item->answer);
|
||||
}
|
||||
}
|
||||
|
||||
return $questionList;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取问题集合ID
|
||||
* @param $userId
|
||||
* @param $exercisesId
|
||||
* @param $mode
|
||||
* @param $count
|
||||
* @return false
|
||||
*/
|
||||
public function getQuestionIds($userId,$exercisesId,$mode,$count){
|
||||
|
||||
// 判断是否已经过题了
|
||||
$exercisesLog = self::where([
|
||||
'exercises_id'=>$exercisesId,
|
||||
'user_id'=>$userId
|
||||
])->order('createtime','desc')->find();
|
||||
|
||||
|
||||
$dataIndex = 0;
|
||||
if($exercisesLog){
|
||||
$dataIndex = $exercisesLog['data_index'];
|
||||
}
|
||||
|
||||
if($mode == 'random'){
|
||||
$questionData = $this->getRandomQuestion($exercisesId,$dataIndex,$count);
|
||||
}else{
|
||||
$questionData = $this->getSortQuestion($exercisesId,$dataIndex,$count);
|
||||
}
|
||||
|
||||
if(!$questionData['ids']){
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
return $questionData;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 随机出题
|
||||
* @return void
|
||||
*/
|
||||
public function getRandomQuestion($exercisesId,$dataIndex,$count){
|
||||
$groupIds = ExercisesBindWarehouseGroup::getExercisesBindWarehouseGroupIds($exercisesId);
|
||||
$where = [];
|
||||
if($groupIds){
|
||||
$where = ['group_id'=>['in',$groupIds]];
|
||||
}
|
||||
|
||||
$questionIds = WarehouseQuestion::where($where)->orderRaw('rand()')->limit($count)->field('id')->column('id');
|
||||
|
||||
if($questionIds){
|
||||
shuffle($questionIds);
|
||||
}
|
||||
|
||||
return ['ids'=>$questionIds,'data_index'=>$dataIndex];
|
||||
}
|
||||
|
||||
/**
|
||||
* 顺序出题
|
||||
* @param $exercisesId 练习
|
||||
* @param $dataIndex 上次查询的下标
|
||||
* @param $count 获取数量
|
||||
* @return void
|
||||
*/
|
||||
public function getSortQuestion($exercisesId,$dataIndex,$count){
|
||||
$groupIds = ExercisesBindWarehouseGroup::getExercisesBindWarehouseGroupIds($exercisesId);
|
||||
|
||||
$where = [];
|
||||
if($groupIds){
|
||||
$where = ['group_id'=>['in',$groupIds]];
|
||||
}
|
||||
|
||||
$questionIds = WarehouseQuestion::where($where)->limit("{$dataIndex},$count")->field('id')->column('id');
|
||||
|
||||
if(count($questionIds) < $count){
|
||||
//说明问题不够了 需要重头再来
|
||||
$patchCount = $count - count($questionIds);
|
||||
|
||||
$dataIndex = $patchCount;
|
||||
|
||||
$patchQuestionIds = WarehouseQuestion::where($where)->limit("0,$patchCount")->field('id')->column('id');
|
||||
|
||||
if($patchQuestionIds){
|
||||
$questionIds = array_merge($questionIds,$patchQuestionIds);
|
||||
}
|
||||
|
||||
$questionIds = array_unique($questionIds);
|
||||
}else{
|
||||
$dataIndex += $count;
|
||||
}
|
||||
|
||||
return ['ids'=>$questionIds,'data_index'=>$dataIndex];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取做过的题目数量
|
||||
* @return void
|
||||
*/
|
||||
public function getCompleteQuestionCount($exercisesId){
|
||||
$questioIds = self::where([
|
||||
'exercises_id'=>$exercisesId
|
||||
])->field('question_ids')->column('question_ids');
|
||||
|
||||
if(!$questioIds){
|
||||
return 0;
|
||||
}
|
||||
|
||||
$ids = [];
|
||||
|
||||
foreach ($questioIds as $questioId){
|
||||
$ids = array_merge($ids,explode(",",$questioId));
|
||||
}
|
||||
|
||||
$ids = array_unique($ids);
|
||||
|
||||
return count($ids);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user