初始化项目:添加后端代码、ThinkPHP框架、前端资源
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model\app\exam;
|
||||
use think\Model;
|
||||
|
||||
/**
|
||||
* 题库练习
|
||||
*/
|
||||
class Exercises extends Model
|
||||
{
|
||||
// 表名,不含前缀
|
||||
protected $name = 'app_exam_exercises';
|
||||
// 自动写入时间戳字段
|
||||
protected $autoWriteTimestamp = false;
|
||||
// 定义时间戳字段名
|
||||
protected $createTime = false;
|
||||
protected $updateTime = false;
|
||||
// 追加属性
|
||||
protected $append = [
|
||||
];
|
||||
|
||||
public static function subscribe($userId,$exercisesId){
|
||||
}
|
||||
|
||||
public static function checkSubscribe($userId,$exercisesId){
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model\app\exam;
|
||||
|
||||
use think\Model;
|
||||
use app\common\model\app\exam\WarehouseGroup;
|
||||
class ExercisesBindWarehouseGroup extends Model
|
||||
{
|
||||
|
||||
|
||||
// 表名
|
||||
protected $name = 'app_exam_exercises_bind_warehouse_group';
|
||||
|
||||
// 自动写入时间戳字段
|
||||
protected $autoWriteTimestamp = 'integer';
|
||||
|
||||
// 定义时间戳字段名
|
||||
protected $createTime = 'createtime';
|
||||
protected $updateTime = false;
|
||||
protected $deleteTime = false;
|
||||
|
||||
// 追加属性
|
||||
protected $append = [
|
||||
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* 获取练习绑定的题库分组
|
||||
* @param $exercisesId
|
||||
* @return mixed
|
||||
*/
|
||||
public static function getExercisesBindWarehouseGroupIds($exercisesId){
|
||||
return self::where([
|
||||
'exercises_id'=>$exercisesId
|
||||
])->field('warehouse_group_id')->column('warehouse_group_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取没有被练习绑定的分组ID
|
||||
* 为什么要查这个?
|
||||
* 当分组被删除时,绑定被删除的分组的题目的分组ID未变更,
|
||||
* 当练习绑定了未分组的题目集合是,仅搜索分组为0的数据查不到上面情景的题目,故获取该类型的分组ID进行not in 反查询
|
||||
* @return void
|
||||
*/
|
||||
public static function getNotInExercisesGroupIds($exercisesId){
|
||||
$bindIds = self::getExercisesBindWarehouseGroupIds($exercisesId);
|
||||
|
||||
return WarehouseGroup::where([
|
||||
'id'=>['not in',$bindIds]
|
||||
])->field('id')->column('id');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
<?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){
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model\app\exam;
|
||||
use think\Model;
|
||||
|
||||
/**
|
||||
* 题库练习订阅
|
||||
*/
|
||||
class ExercisesSubscribe extends Model
|
||||
{
|
||||
// 表名,不含前缀
|
||||
protected $name = 'app_exam_exercises_subscribe';
|
||||
// 自动写入时间戳字段
|
||||
protected $autoWriteTimestamp = false;
|
||||
// 定义时间戳字段名
|
||||
protected $createTime = false;
|
||||
protected $updateTime = false;
|
||||
// 追加属性
|
||||
protected $append = [
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* 增加订阅
|
||||
* @param $userId 用户
|
||||
* @param $exercisesId 练习ID
|
||||
* @param $type 订阅方式
|
||||
* @return void
|
||||
*/
|
||||
public static function subscribe($userId,$exercisesId,$type = ''){
|
||||
self::insert([
|
||||
'user_id'=>$userId,
|
||||
'exercises_id'=>$exercisesId,
|
||||
'type'=>$type,
|
||||
'createtime'=>time(),
|
||||
'uniacid'=>UNIACID
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否订阅
|
||||
* @param $userId
|
||||
* @param $exercisesId
|
||||
* @return void
|
||||
*/
|
||||
public static function checkSubscribe($userId,$exercisesId){
|
||||
$data = self::where([
|
||||
'user_id'=>$userId,
|
||||
'exercises_id'=>$exercisesId
|
||||
])->find();
|
||||
|
||||
if($data){
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否有使用权限
|
||||
* @param $userId
|
||||
* @param $exercisesId
|
||||
* @return void
|
||||
*/
|
||||
public static function checkAuth($userId,$exercisesId){
|
||||
|
||||
$exercises = \app\common\model\app\exam\Exercises::where([
|
||||
'id'=>$exercisesId,
|
||||
'status'=>1
|
||||
])->find();
|
||||
|
||||
if(!$exercises){
|
||||
return false;
|
||||
}
|
||||
|
||||
switch ($exercises['sales_type']){
|
||||
case 1:
|
||||
return true;
|
||||
break;
|
||||
case 2:
|
||||
$isSubscribe = self::checkSubscribe($userId,$exercisesId);
|
||||
if($isSubscribe){
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
//关联课程 判断课程订阅权限
|
||||
if(\app\common\model\user\Subscription::getSubscriptionAuth($userId,$exercises['bind_course'])){
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model\app\exam;
|
||||
|
||||
use think\Model;
|
||||
|
||||
|
||||
class WarehouseGroup extends Model
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// 表名
|
||||
protected $name = 'app_exam_warehouse_group';
|
||||
|
||||
// 自动写入时间戳字段
|
||||
protected $autoWriteTimestamp = 'integer';
|
||||
|
||||
// 定义时间戳字段名
|
||||
protected $createTime = 'createtime';
|
||||
protected $updateTime = false;
|
||||
protected $deleteTime = false;
|
||||
|
||||
|
||||
/**
|
||||
* 获取所有的分组ID
|
||||
* @return mixed
|
||||
*/
|
||||
public static function getAllGroupIds(){
|
||||
return self::field('id')->column('id');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model\app\exam;
|
||||
|
||||
use think\Model;
|
||||
|
||||
|
||||
class WarehouseQuestion extends Model
|
||||
{
|
||||
|
||||
|
||||
// 表名
|
||||
protected $name = 'app_exam_warehouse_question';
|
||||
|
||||
// 自动写入时间戳字段
|
||||
protected $autoWriteTimestamp = 'integer';
|
||||
|
||||
// 定义时间戳字段名
|
||||
protected $createTime = 'createtime';
|
||||
protected $updateTime = false;
|
||||
protected $deleteTime = false;
|
||||
|
||||
// 追加属性
|
||||
protected $append = [
|
||||
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user