QuestionModel.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. namespace addons\exam\model;
  3. use addons\exam\enum\CommonStatus;
  4. use app\admin\model\exam\QuestionCollectModel;
  5. class QuestionModel extends \app\admin\model\exam\QuestionModel
  6. {
  7. // 字段类型
  8. protected $type
  9. = [
  10. 'options_img' => 'array',
  11. // 'options_extend' => 'array',
  12. // 'options_json' => 'array'
  13. ];
  14. // 隐藏字段
  15. // protected $hidden = [
  16. // 'answer', 'explain'
  17. // ];
  18. public function getKindList()
  19. {
  20. return [
  21. 'JUDGE' => '判断题',
  22. 'SINGLE' => '单选题',
  23. 'MULTI' => '多选题',
  24. 'FILL' => '填空题',
  25. 'SHORT' => '简答题',
  26. 'MATERIAL' => '材料题',
  27. ];
  28. }
  29. public function getDifficultyList()
  30. {
  31. return ['EASY' => '简单', 'GENERAL' => '普通', 'HARD' => '困难'];
  32. }
  33. public function getStatusList()
  34. {
  35. return ['NORMAL' => '正常', 'HIDDEN' => '隐藏'];
  36. }
  37. public function getOptionsJsonAttr($value)
  38. {
  39. if ($value = json_decode($value, true)) {
  40. $data = [];
  41. foreach ($value as $key => $row) {
  42. $arr['key'] = $key;
  43. $arr['value'] = $row;
  44. $arr['click_index'] = false;
  45. array_push($data, $arr);
  46. }
  47. return $data;
  48. }
  49. return [];
  50. }
  51. public function getAnswerAttr($value, $data)
  52. {
  53. if (is_array($value)) {
  54. return $value;
  55. }
  56. if (in_array($data['kind'], ['FILL', 'SHORT'])) {
  57. return json_decode($value, true);
  58. }
  59. return $value;
  60. }
  61. // public function getOptionsExtendAttr($value)
  62. // {
  63. // return json_decode($value, true);
  64. // }
  65. /**
  66. * 试题是否已收藏过
  67. * @param $user_id
  68. * @param $questions
  69. * @return mixed
  70. */
  71. public static function isCollected($user_id, $questions)
  72. {
  73. $ids = array_column($questions, 'id');
  74. $collects = QuestionCollectModel::where('user_id', $user_id)->whereIn('question_id', $ids)->select();
  75. $collect_ids = array_column(collection($collects)->toArray(), 'question_id');
  76. foreach ($questions as &$question) {
  77. $question['collected'] = in_array($question['id'], $collect_ids);
  78. }
  79. return $questions;
  80. }
  81. /**
  82. * 获取试题列表
  83. * @param $params
  84. * @return array|int
  85. */
  86. public static function getList($params)
  87. {
  88. $param = array_merge([
  89. 'cate_id' => 0, // 按分类查询
  90. 'kind' => '', // 试题类型查询
  91. 'status' => CommonStatus::NORMAL, // 状态查询
  92. 'keyword' => '', // 搜索关键词
  93. 'user_id' => 0, // 传入用户ID时,查询收藏状态
  94. 'page_count' => 20, // 每页数量
  95. 'sort' => '', // 排序类型
  96. 'just_get_count' => 0, // 仅获取题数
  97. 'mode' => 'normal', // normal=普通模式,memory=记忆模式,random=随机查询
  98. 'memory_index' => 0, // 记忆模式 - 上次做题题标
  99. ], $params);
  100. $model = new static();
  101. $model->with(['materialQuestions.question'])->where('is_material_child', 0);
  102. // 查询条件
  103. if ($param['cate_id']) {
  104. $model->where('cate_id', $param['cate_id']);
  105. }
  106. if ($param['kind']) {
  107. $model->where('kind', $param['kind']);
  108. }
  109. if ($param['status']) {
  110. $model->where('status', $param['status']);
  111. }
  112. if ($param['keyword']) {
  113. $model->where('title', 'like', '%' . $param['keyword'] . '%');
  114. }
  115. // 仅获取题数
  116. if ($param['just_get_count']) {
  117. return ['total' => $model->count()];
  118. }
  119. switch ($param['mode']) {
  120. // 记忆模式
  121. case 'memory':
  122. // 排序
  123. if ($param['sort']) {
  124. $model->order($param['sort']);
  125. } else {
  126. $model->order('id asc');
  127. }
  128. $list['data'] = $model->select();
  129. break;
  130. // 随机模式
  131. case 'random':
  132. // 限制最多500条
  133. $list['data'] = $model->orderRaw('rand()')->limit(500)->select();
  134. break;
  135. // 普通模式
  136. default:
  137. // 排序
  138. if ($param['sort']) {
  139. $model->order($param['sort']);
  140. } else {
  141. $model->order('id asc');
  142. }
  143. $list = $model->paginate($param['page_count'])->toArray();
  144. break;
  145. }
  146. if ($list['data']) {
  147. // 题目是否已收藏
  148. if ($param['user_id']) {
  149. $list['data'] = self::isCollected($param['user_id'], $list['data']);
  150. }
  151. // 合并材料题子题目
  152. $list['data'] = self::mergeMaterialQuestions($list['data']);
  153. }
  154. return $list;
  155. }
  156. /**
  157. * 合并材料题子题目
  158. * @param $questions
  159. * @return mixed
  160. */
  161. public static function mergeMaterialQuestions($questions)
  162. {
  163. $questions = collection($questions)->toArray();
  164. // dd($questions);
  165. $material_questions = [];
  166. foreach ($questions as $key => $question) {
  167. if ($question['kind'] == 'MATERIAL') {
  168. foreach ($question['material_questions'] as $material_question) {
  169. // dd([$question]);
  170. $new_question = $material_question['question'];
  171. $new_question['material_id'] = $question['id'];
  172. $new_question['material_title'] = $question['title'];
  173. $new_question['material_score'] = $question['score'] ?? 1;
  174. $new_question['origin_answer'] = $material_question['question']['answer'];
  175. $new_question['score'] = $material_question['score'];
  176. $new_question['answer'] = $material_question['answer'];
  177. // with查询导致的数据格式问题,需要特殊处理
  178. if (in_array($new_question['kind'], ['FILL', 'SHORT']) && $new_question['answer'] && is_string($new_question['answer'])) {
  179. $new_question['answer'] = json_decode($new_question['answer'], true);
  180. }
  181. if ($new_question['options_img'] && is_string($new_question['options_img'])) {
  182. $new_question['options_img'] = json_decode($new_question['options_img'], true);
  183. }
  184. if ($new_question['options_json'] && is_string($new_question['options_json'])) {
  185. $new_question['options_json'] = json_decode($new_question['options_json'], true);
  186. // 特殊格式处理
  187. $keys = array_keys($new_question['options_json']);
  188. if (isset($keys[0]) && $keys[0] && !is_numeric($keys[0])) {
  189. $options_json = [];
  190. foreach ($new_question['options_json'] as $option_key => $option_val) {
  191. $options_json[] = [
  192. 'key' => $option_key,
  193. 'value' => $option_val,
  194. 'click_index' => false,
  195. ];
  196. }
  197. $new_question['options_json'] = $options_json;
  198. }
  199. }
  200. $new_question['show_full'] = false;
  201. $material_questions[] = $new_question;
  202. // $material_question['question']['material_id'] = $question['id'];
  203. // $material_question['question']['material_title'] = $question['title'];
  204. // $material_question['question']['material_score'] = $question['score'] ?? 1;
  205. // $material_question['question']['origin_answer'] = $material_question['question']['answer'];
  206. // $material_question['question']['answer'] = $material_question['answer'];
  207. // $material_question['question']['score'] = $material_question['score'];
  208. // $material_question['question']['show_full'] = false;
  209. // if ($material_question['question']['options_img'] && is_string($material_question['question']['options_img'])) {
  210. // $material_question['question']['options_img'] = json_decode($material_question['question']['options_img'], true);
  211. // }
  212. // if ($material_question['question']['options_json'] && is_string($material_question['question']['options_json'])) {
  213. // $material_question['question']['options_json'] = json_decode($material_question['question']['options_json'], true);
  214. //
  215. // // 特殊格式处理
  216. // $keys = array_keys($material_question['question']['options_json']);
  217. // if (isset($keys[0]) && $keys[0] && !is_numeric($keys[0])) {
  218. // $options_json = [];
  219. // foreach ($material_question['question']['options_json'] as $option_key => $option_val) {
  220. // $options_json[] = [
  221. // 'key' => $option_key,
  222. // 'value' => $option_val,
  223. // 'click_index' => false,
  224. // ];
  225. // }
  226. // $material_question['question']['options_json'] = $options_json;
  227. // }
  228. // }
  229. //
  230. // $material_questions[] = $material_question['question'];
  231. }
  232. // 删除材料题
  233. unset($questions[$key]);
  234. // array_splice($questions, $key, 1);
  235. // dd($questions);
  236. // array_slice($questions, $key, 1);
  237. }
  238. // dd(collection($material_questions)->toArray());
  239. }
  240. // dd(collection($questions)->toArray());
  241. // dd(collection($material_questions)->toArray());
  242. if ($material_questions) {
  243. $questions = array_merge(array_values($questions), $material_questions);
  244. }
  245. return $questions;
  246. }
  247. }