176 lines
4.4 KiB
PHP
176 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace app\common\model\app\exam;
|
|
use think\Model;
|
|
use app\common\model\app\exam\Exercises;
|
|
use app\common\model\app\exam\WarehouseQuestion;
|
|
use app\common\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([
|
|
'exercises_id'=>$exercisesId,
|
|
'stutus'=>1
|
|
])->find();
|
|
|
|
if(!$exercisesInfo){
|
|
return false;
|
|
}
|
|
|
|
$questionIds = $this->getQuestionIds($userId,$exercisesId,$exercisesInfo['question_mode'],$exercisesInfo['question_count']);
|
|
|
|
if(!$questionIds){
|
|
return false;
|
|
}
|
|
|
|
$logId = self::insertGetId([
|
|
'user_id'=>$userId,
|
|
'uniacid'=>UNIACID,
|
|
'exercises_id'=>$exercisesId,
|
|
'question_ids'=>implode(",",$questionIds),
|
|
'data_index'=>'',
|
|
'question_count'=>count($questionIds),
|
|
'result_true_count'=>0,
|
|
'createtime'=>time()
|
|
]);
|
|
|
|
if(!$logId){
|
|
return false;
|
|
}
|
|
|
|
return $logId;
|
|
}
|
|
|
|
public function getQuestionList($userId,$exercisesId){
|
|
|
|
//获取出题配置
|
|
$exercisesInfo = Exercises::where([
|
|
'id'=>$exercisesId,
|
|
'status'=>1
|
|
])->find();
|
|
|
|
if(!$exercisesInfo){
|
|
return false;
|
|
}
|
|
|
|
$questionIds = $this->getQuestionIds($userId,$exercisesId,$exercisesInfo['question_mode'],$exercisesInfo['question_count']);
|
|
|
|
if(!$questionIds){
|
|
return false;
|
|
}
|
|
|
|
$questionList = WarehouseQuestion::where([
|
|
'id'=>['in',$questionIds]
|
|
])->select();
|
|
|
|
if(!$questionList){
|
|
return false;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
public function getQuestionIds($userId,$exercisesId,$mode,$count){
|
|
|
|
if($mode == 'random'){
|
|
$questionIds = $this->getRandomQuestion($exercisesId,$count);
|
|
}else{
|
|
|
|
// 判断是否已经过题了
|
|
$exercisesLog = self::where([
|
|
'exercises_id'=>$exercisesId,
|
|
'user_id'=>$userId
|
|
])->order('createtime','desc')->find();
|
|
|
|
$dataIndex = 0;
|
|
if($exercisesLog){
|
|
$dataIndex = $exercisesLog['index'];
|
|
}
|
|
$questionIds = $this->getSortQuestion($exercisesId,$dataIndex,$count);
|
|
}
|
|
|
|
if(!$questionIds){
|
|
return false;
|
|
}
|
|
|
|
shuffle($questionIds);
|
|
|
|
return $questionIds;
|
|
}
|
|
|
|
|
|
/**
|
|
* 随机出题
|
|
* @return void
|
|
*/
|
|
public function getRandomQuestion($exercisesId,$count){
|
|
|
|
$groupIds = ExercisesBindWarehouseGroup::getNotInExercisesGroupIds($exercisesId);
|
|
|
|
if(!$groupIds){
|
|
return false;
|
|
}
|
|
|
|
$questionIds = WarehouseQuestion::where(['group_id'=>['not in',$groupIds]])->orderRaw('rand()')->limit($count)->field('id')->column('id');
|
|
|
|
if(!$questionIds){
|
|
return false;
|
|
}
|
|
|
|
return $questionIds;
|
|
}
|
|
|
|
|
|
/**
|
|
* 顺序出题
|
|
* @param $exercisesId 练习
|
|
* @param $dataIndex 上次查询的下标
|
|
* @param $count 获取数量
|
|
* @return void
|
|
*/
|
|
public function getSortQuestion($exercisesId,$dataIndex,$count){
|
|
|
|
}
|
|
|
|
} |