QuestionModel.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. namespace app\admin\model\exam;
  3. use addons\exam\model\BaseModel;
  4. use think\Collection;
  5. use think\Db;
  6. use think\db\exception\DataNotFoundException;
  7. use think\db\exception\ModelNotFoundException;
  8. use think\exception\DbException;
  9. use think\Model;
  10. use traits\model\SoftDelete;
  11. class QuestionModel extends BaseModel
  12. {
  13. use SoftDelete;
  14. // 表名
  15. protected $name = 'exam_question';
  16. // 自动写入时间戳字段
  17. protected $autoWriteTimestamp = 'int';
  18. // 定义时间戳字段名
  19. protected $createTime = 'createtime';
  20. protected $updateTime = 'updatetime';
  21. protected $deleteTime = 'deletetime';
  22. // 追加属性
  23. protected $append
  24. = [
  25. 'kind_text',
  26. 'difficulty_text',
  27. 'status_text',
  28. 'title_video_url',
  29. 'explain_video_url',
  30. ];
  31. const kindList = ['JUDGE', 'SINGLE', 'MULTI', /*'FILL',*/ 'SHORT', /*'MATERIAL'*/];
  32. const difficultyList = ['EASY', 'GENERAL', 'HARD'];
  33. const statusList = ['NORMAL', 'HIDDEN'];
  34. public function getKindList()
  35. {
  36. return [
  37. 'JUDGE' => '判断题',
  38. 'SINGLE' => '单选题',
  39. 'MULTI' => '多选题',
  40. // 'FILL' => '填空题',
  41. 'SHORT' => '简答题',
  42. // 'MATERIAL' => '材料题',
  43. ];
  44. }
  45. public function getDifficultyList()
  46. {
  47. return ['EASY' => '简单', 'GENERAL' => '普通', 'HARD' => '困难'];
  48. }
  49. public function getStatusList()
  50. {
  51. return ['NORMAL' => '正常', 'HIDDEN' => '隐藏'];
  52. }
  53. public function getKindTextAttr($value, $data)
  54. {
  55. $value = $value ? $value : ($data['kind'] ?? '');
  56. $list = $this->getKindList();
  57. return $list[$value] ?? '';
  58. }
  59. public function getDifficultyTextAttr($value, $data)
  60. {
  61. $value = $value ? $value : ($data['difficulty'] ?? '');
  62. $list = $this->getDifficultyList();
  63. return $list[$value] ?? '';
  64. }
  65. public function getStatusTextAttr($value, $data)
  66. {
  67. $value = $value ? $value : ($data['status'] ?? '');
  68. $list = $this->getStatusList();
  69. return $list[$value] ?? '';
  70. }
  71. public function getTitleVideoUrlAttr($value, $data)
  72. {
  73. $value = $data['title_video'] ?? '';
  74. return cdnurl($value, true);
  75. }
  76. public function getExplainVideoUrlAttr($value, $data)
  77. {
  78. $value = $data['explain_video'] ?? '';
  79. return cdnurl($value, true);
  80. }
  81. public function cate()
  82. {
  83. return $this->belongsTo(CateModel::class, 'cate_id', 'id', [], 'LEFT')->setEagerlyType(0);
  84. }
  85. public function cates()
  86. {
  87. return $this->belongsTo(CateModel::class, 'cate_id');
  88. }
  89. /**
  90. * 材料题子题目
  91. * @return \think\model\relation\HasMany
  92. */
  93. public function materialQuestions()
  94. {
  95. return $this->hasMany(MaterialQuestionModel::class, 'parent_question_id', 'id')->order('weigh');
  96. }
  97. public function materialParent()
  98. {
  99. return $this->belongsTo(self::class, 'material_question_id', 'id');
  100. }
  101. protected function scopeCate($query, $cate_ids)
  102. {
  103. $query->whereIn('cate_id', $cate_ids);
  104. }
  105. protected function scopeKind($query, $kind, $limit = 0)
  106. {
  107. $query->where('kind', $kind);
  108. if ($limit) {
  109. $query->limit($limit);
  110. }
  111. }
  112. protected function scopeDifficulty($query, $difficulty, $limit = 0)
  113. {
  114. $query->where('difficulty', $difficulty);
  115. }
  116. /**
  117. * 获取多题库下各类题型的不同难度题数
  118. * @param $cate_ids
  119. * @return array|bool|\PDOStatement|string|Model|null
  120. */
  121. public function getCount($cate_ids)
  122. {
  123. return Db::name($this->name)
  124. ->whereIn('cate_id', $cate_ids)
  125. ->where('is_material_child', 0) // 材料题子题不计入总数
  126. ->whereNull('deletetime')
  127. ->field("
  128. COUNT(id) as 'total',
  129. COUNT(CASE WHEN kind = 'JUDGE' then 1 END) as 'judge',
  130. COUNT(CASE WHEN kind = 'JUDGE' and difficulty = 'EASY' then 1 END) as 'judge_easy',
  131. COUNT(CASE WHEN kind = 'JUDGE' and difficulty = 'GENERAL' then 1 END) as 'judge_general',
  132. COUNT(CASE WHEN kind = 'JUDGE' and difficulty = 'HARD' then 1 END) as 'judge_hard',
  133. COUNT(CASE WHEN kind = 'SINGLE' then 1 END) as 'single',
  134. COUNT(CASE WHEN kind = 'SINGLE' and difficulty = 'EASY' then 1 END) as 'single_easy',
  135. COUNT(CASE WHEN kind = 'SINGLE' and difficulty = 'GENERAL' then 1 END) as 'single_general',
  136. COUNT(CASE WHEN kind = 'SINGLE' and difficulty = 'HARD' then 1 END) as 'single_hard',
  137. COUNT(CASE WHEN kind = 'MULTI' then 1 END) as 'multi',
  138. COUNT(CASE WHEN kind = 'MULTI' and difficulty = 'EASY' then 1 END) as 'multi_easy',
  139. COUNT(CASE WHEN kind = 'MULTI' and difficulty = 'GENERAL' then 1 END) as 'multi_general',
  140. COUNT(CASE WHEN kind = 'MULTI' and difficulty = 'HARD' then 1 END) as 'multi_hard',
  141. COUNT(CASE WHEN kind = 'FILL' then 1 END) as 'fill',
  142. COUNT(CASE WHEN kind = 'FILL' and difficulty = 'EASY' then 1 END) as 'fill_easy',
  143. COUNT(CASE WHEN kind = 'FILL' and difficulty = 'GENERAL' then 1 END) as 'fill_general',
  144. COUNT(CASE WHEN kind = 'FILL' and difficulty = 'HARD' then 1 END) as 'fill_hard',
  145. COUNT(CASE WHEN kind = 'SHORT' then 1 END) as 'short',
  146. COUNT(CASE WHEN kind = 'SHORT' and difficulty = 'EASY' then 1 END) as 'short_easy',
  147. COUNT(CASE WHEN kind = 'SHORT' and difficulty = 'GENERAL' then 1 END) as 'short_general',
  148. COUNT(CASE WHEN kind = 'SHORT' and difficulty = 'HARD' then 1 END) as 'short_hard',
  149. COUNT(CASE WHEN kind = 'MATERIAL' then 1 END) as 'material',
  150. COUNT(CASE WHEN kind = 'MATERIAL' and difficulty = 'EASY' then 1 END) as 'material_easy',
  151. COUNT(CASE WHEN kind = 'MATERIAL' and difficulty = 'GENERAL' then 1 END) as 'material_general',
  152. COUNT(CASE WHEN kind = 'MATERIAL' and difficulty = 'HARD' then 1 END) as 'material_hard'
  153. ")->find();
  154. }
  155. /*
  156. * 根据关键词模糊查询10条题目
  157. * Robin
  158. * */
  159. // public function getList($params)
  160. // {
  161. // $param = array_merge([
  162. // 'keyword' => '', // 搜索关键词
  163. // 'sortType' => '', // 排序类型
  164. // 'listRows' => 20, // 每页数量
  165. // 'sortRand' => 0 //是否随机查询,0 or 1
  166. // ], $params);
  167. //
  168. // if ($param['keyword'])
  169. // $this->where('title', 'like', '%' . $param['keyword'] . '%');
  170. //
  171. // if ($param['sortType'])
  172. // $this->order($param['sortType']);
  173. //
  174. // if ($param['sortRand'])
  175. // $this->orderRaw('rand()');
  176. //
  177. // return $this->with('collected')->where('cate_id', intval($param['cate_id']))
  178. // ->where('status', 'NORMAL')
  179. // ->paginate($param['listRows'], true);
  180. // }
  181. /**
  182. * 获取题目列表
  183. * @param string $cates 分类ID,多个逗号隔开
  184. * @param string $kind 题型
  185. * @param array $with 关联模型
  186. * @return QuestionModel
  187. */
  188. public static function getListByCateAndKind($cates, $kind, $with = [])
  189. {
  190. return self::with($with)
  191. ->whereIn('cate_id', $cates)
  192. ->where('kind', $kind)
  193. ->where('is_material_child', 0)// 材料题子题不显示
  194. ->orderRaw('rand()');
  195. }
  196. /**
  197. * 获取试卷固定题目
  198. * @param int $paper_id 试卷ID
  199. * @param array $with 关联模型
  200. * @return bool|\PDOStatement|string|Collection
  201. * @throws DataNotFoundException
  202. * @throws ModelNotFoundException
  203. * @throws DbException
  204. */
  205. public static function getFixListByPaper($paper_id, $with = [])
  206. {
  207. $questions = self::with($with)
  208. ->alias('question_model')
  209. ->join('exam_paper_question pq', 'question_model.id = pq.question_id')
  210. ->where('pq.paper_id', $paper_id)
  211. ->where('is_material_child', 0)
  212. ->field('question_model.*, pq.score, pq.sort, pq.answer_config')
  213. ->order('pq.sort', 'asc')
  214. ->select();
  215. foreach ($questions as &$question) {
  216. if (!empty($question['answer_config'])) {
  217. // 简答题 - 替换答案分数配置
  218. if ($question['kind'] == 'SHORT') {
  219. $question['answer'] = $question['answer_config'];
  220. }
  221. }
  222. }
  223. return $questions;
  224. }
  225. /**
  226. * 记录错题。
  227. * 没用到,改用下面的方法了
  228. */
  229. public function logWrong($user_id, $user_answer = null)
  230. {
  231. // if ($item = QuestionWrongModel::where('user_id', $user_id)
  232. // ->where('question_id', $this->id)
  233. // ->find()) {
  234. // $item->user_answer = $user_answer;
  235. // $item->save();
  236. //
  237. // return $item;
  238. // } else {
  239. // return QuestionWrongModel::create([
  240. // 'user_id' => $user_id,
  241. // 'question_id' => $this->id,
  242. // 'user_answer' => $user_answer,
  243. // ]);
  244. // }
  245. if (is_array($user_answer)) {
  246. $user_answer = json_encode($user_answer, JSON_UNESCAPED_UNICODE);
  247. } else if (is_string($user_answer)) {
  248. $user_answer = trim($user_answer);
  249. if (strpos($user_answer, ',')) {
  250. $user_answer = json_encode(explode(',', $user_answer), JSON_UNESCAPED_UNICODE);
  251. }
  252. } else {
  253. $user_answer = null;
  254. }
  255. return QuestionWrongModel::updateOrCreate(
  256. [
  257. 'user_id' => $user_id,
  258. 'question_id' => $this->id,
  259. ],
  260. [
  261. 'user_id' => $user_id,
  262. 'question_id' => $this->id,
  263. 'user_answer' => $user_answer, //is_array($user_answer) ? json_encode($user_answer, JSON_UNESCAPED_UNICODE) : $user_answer,
  264. ]
  265. );
  266. }
  267. /**
  268. * 记录错题
  269. */
  270. public static function recordWrong($question_id, $user_id, $user_answer = null)
  271. {
  272. if (is_array($user_answer)) {
  273. $user_answer = json_encode($user_answer, JSON_UNESCAPED_UNICODE);
  274. } else if (is_string($user_answer)) {
  275. $user_answer = trim($user_answer);
  276. if (strpos($user_answer, ',')) {
  277. $user_answer = json_encode(explode(',', $user_answer), JSON_UNESCAPED_UNICODE);
  278. }
  279. } else {
  280. $user_answer = null;
  281. }
  282. return QuestionWrongModel::updateOrCreate(
  283. [
  284. 'user_id' => $user_id,
  285. 'question_id' => $question_id,
  286. ],
  287. [
  288. 'user_id' => $user_id,
  289. 'question_id' => $question_id,
  290. 'user_answer' => $user_answer, //is_array($user_answer) ? json_encode($user_answer, JSON_UNESCAPED_UNICODE) : $user_answer,
  291. ]
  292. );
  293. }
  294. }