Files
amb_wechatapp/application/admin/model/app/exam/Exercises.php
T

105 lines
2.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace app\admin\model\app\exam;
use think\Model;
class Exercises extends Model
{
// 表名
protected $name = 'app_exam_exercises';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'integer';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = false;
// 追加属性
protected $append = [
'status_text'
];
public function getStatusList()
{
return ['1' => __('Status 1'), '0' => __('Status 0')];
}
public function getHideList()
{
return ['1' => __('Hide 1'), '0' => __('Hide 0')];
}
public function getSalesTypeList()
{
return ['1' => __('Sales_type 1'), '3' => __('Sales_type 3'), '2' => __('Sales_type 2')];
}
public function getQuestionModeList()
{
return ['sort' => __('Question_mode sort'), 'random' => __('Question_mode random')];
}
public function getStatusTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
$list = $this->getStatusList();
return isset($list[$value]) ? $list[$value] : '';
}
public function course()
{
return $this->belongsTo('app\admin\model\course\Course', 'bind_course', 'id', [], 'LEFT')->setEagerlyType(0);
}
/**
* 获取绑定的题库分组
* @param $exercisesId
* @return mixed
*/
public function getBindWarehouseGroup($exercisesId){
$data = \app\admin\model\app\exam\ExercisesBindWarehouseGroup::with(['group'])->where([
'exercises_id'=>$exercisesId,
])->order('id','asc')->select();
// 转换为数组处理
$result = collection($data)->toArray();
// 处理未分组的情况(warehouse_group_id = 0 或 group.id 为 null
$allGroupIds = null;
foreach ($result as &$item) {
// 检查 group.id 是否为 null(表示关联的分组不存在)
if (empty($item['group']['id'])) {
// 计算未分组的题目数量(只查询一次)
if ($allGroupIds === null) {
$allGroupIds = \app\admin\model\app\exam\WarehouseGroup::field('id')->column('id');
}
$questionCount = \app\admin\model\app\exam\WarehouseQuestion::where(['group_id' => ['NOT IN', $allGroupIds]])->count();
// 设置未分组信息
$item['group'] = [
'id' => 0,
'name' => '未分组',
'question_count' => $questionCount
];
}
}
return $result;
}
}