123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- <?php
- namespace app\admin\model\exam;
- use addons\exam\model\BaseModel;
- use think\Collection;
- use think\Db;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\ModelNotFoundException;
- use think\exception\DbException;
- use think\Model;
- use traits\model\SoftDelete;
- class QuestionModel extends BaseModel
- {
- use SoftDelete;
-
- protected $name = 'exam_question';
-
- protected $autoWriteTimestamp = 'int';
-
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- protected $deleteTime = 'deletetime';
-
- protected $append
- = [
- 'kind_text',
- 'difficulty_text',
- 'status_text',
- 'title_video_url',
- 'explain_video_url',
- ];
- const kindList = ['JUDGE', 'SINGLE', 'MULTI', 'SHORT', ];
- const difficultyList = ['EASY', 'GENERAL', 'HARD'];
- const statusList = ['NORMAL', 'HIDDEN'];
- public function getKindList()
- {
- return [
- 'JUDGE' => '判断题',
- 'SINGLE' => '单选题',
- 'MULTI' => '多选题',
- 'SHORT' => '简答题',
- ];
- }
- public function getDifficultyList()
- {
- return ['EASY' => '简单', 'GENERAL' => '普通', 'HARD' => '困难'];
- }
- public function getStatusList()
- {
- return ['NORMAL' => '正常', 'HIDDEN' => '隐藏'];
- }
- public function getKindTextAttr($value, $data)
- {
- $value = $value ? $value : ($data['kind'] ?? '');
- $list = $this->getKindList();
- return $list[$value] ?? '';
- }
- public function getDifficultyTextAttr($value, $data)
- {
- $value = $value ? $value : ($data['difficulty'] ?? '');
- $list = $this->getDifficultyList();
- return $list[$value] ?? '';
- }
- public function getStatusTextAttr($value, $data)
- {
- $value = $value ? $value : ($data['status'] ?? '');
- $list = $this->getStatusList();
- return $list[$value] ?? '';
- }
- public function getTitleVideoUrlAttr($value, $data)
- {
- $value = $data['title_video'] ?? '';
- return cdnurl($value, true);
- }
- public function getExplainVideoUrlAttr($value, $data)
- {
- $value = $data['explain_video'] ?? '';
- return cdnurl($value, true);
- }
- public function cate()
- {
- return $this->belongsTo(CateModel::class, 'cate_id', 'id', [], 'LEFT')->setEagerlyType(0);
- }
- public function cates()
- {
- return $this->belongsTo(CateModel::class, 'cate_id');
- }
-
- public function materialQuestions()
- {
- return $this->hasMany(MaterialQuestionModel::class, 'parent_question_id', 'id')->order('weigh');
- }
- public function materialParent()
- {
- return $this->belongsTo(self::class, 'material_question_id', 'id');
- }
- protected function scopeCate($query, $cate_ids)
- {
- $query->whereIn('cate_id', $cate_ids);
- }
- protected function scopeKind($query, $kind, $limit = 0)
- {
- $query->where('kind', $kind);
- if ($limit) {
- $query->limit($limit);
- }
- }
- protected function scopeDifficulty($query, $difficulty, $limit = 0)
- {
- $query->where('difficulty', $difficulty);
- }
-
- public function getCount($cate_ids)
- {
- return Db::name($this->name)
- ->whereIn('cate_id', $cate_ids)
- ->where('is_material_child', 0)
- ->whereNull('deletetime')
- ->field("
- COUNT(id) as 'total',
-
- COUNT(CASE WHEN kind = 'JUDGE' then 1 END) as 'judge',
- COUNT(CASE WHEN kind = 'JUDGE' and difficulty = 'EASY' then 1 END) as 'judge_easy',
- COUNT(CASE WHEN kind = 'JUDGE' and difficulty = 'GENERAL' then 1 END) as 'judge_general',
- COUNT(CASE WHEN kind = 'JUDGE' and difficulty = 'HARD' then 1 END) as 'judge_hard',
-
- COUNT(CASE WHEN kind = 'SINGLE' then 1 END) as 'single',
- COUNT(CASE WHEN kind = 'SINGLE' and difficulty = 'EASY' then 1 END) as 'single_easy',
- COUNT(CASE WHEN kind = 'SINGLE' and difficulty = 'GENERAL' then 1 END) as 'single_general',
- COUNT(CASE WHEN kind = 'SINGLE' and difficulty = 'HARD' then 1 END) as 'single_hard',
-
- COUNT(CASE WHEN kind = 'MULTI' then 1 END) as 'multi',
- COUNT(CASE WHEN kind = 'MULTI' and difficulty = 'EASY' then 1 END) as 'multi_easy',
- COUNT(CASE WHEN kind = 'MULTI' and difficulty = 'GENERAL' then 1 END) as 'multi_general',
- COUNT(CASE WHEN kind = 'MULTI' and difficulty = 'HARD' then 1 END) as 'multi_hard',
-
- COUNT(CASE WHEN kind = 'FILL' then 1 END) as 'fill',
- COUNT(CASE WHEN kind = 'FILL' and difficulty = 'EASY' then 1 END) as 'fill_easy',
- COUNT(CASE WHEN kind = 'FILL' and difficulty = 'GENERAL' then 1 END) as 'fill_general',
- COUNT(CASE WHEN kind = 'FILL' and difficulty = 'HARD' then 1 END) as 'fill_hard',
-
- COUNT(CASE WHEN kind = 'SHORT' then 1 END) as 'short',
- COUNT(CASE WHEN kind = 'SHORT' and difficulty = 'EASY' then 1 END) as 'short_easy',
- COUNT(CASE WHEN kind = 'SHORT' and difficulty = 'GENERAL' then 1 END) as 'short_general',
- COUNT(CASE WHEN kind = 'SHORT' and difficulty = 'HARD' then 1 END) as 'short_hard',
-
- COUNT(CASE WHEN kind = 'MATERIAL' then 1 END) as 'material',
- COUNT(CASE WHEN kind = 'MATERIAL' and difficulty = 'EASY' then 1 END) as 'material_easy',
- COUNT(CASE WHEN kind = 'MATERIAL' and difficulty = 'GENERAL' then 1 END) as 'material_general',
- COUNT(CASE WHEN kind = 'MATERIAL' and difficulty = 'HARD' then 1 END) as 'material_hard'
-
- ")->find();
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static function getListByCateAndKind($cates, $kind, $with = [])
- {
- return self::with($with)
- ->whereIn('cate_id', $cates)
- ->where('kind', $kind)
- ->where('is_material_child', 0)
- ->orderRaw('rand()');
- }
-
- public static function getFixListByPaper($paper_id, $with = [])
- {
- $questions = self::with($with)
- ->alias('question_model')
- ->join('exam_paper_question pq', 'question_model.id = pq.question_id')
- ->where('pq.paper_id', $paper_id)
- ->where('is_material_child', 0)
- ->field('question_model.*, pq.score, pq.sort, pq.answer_config')
- ->order('pq.sort', 'asc')
- ->select();
- foreach ($questions as &$question) {
- if (!empty($question['answer_config'])) {
-
- if ($question['kind'] == 'SHORT') {
- $question['answer'] = $question['answer_config'];
- }
- }
- }
- return $questions;
- }
-
- public function logWrong($user_id, $user_answer = null)
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if (is_array($user_answer)) {
- $user_answer = json_encode($user_answer, JSON_UNESCAPED_UNICODE);
- } else if (is_string($user_answer)) {
- $user_answer = trim($user_answer);
- if (strpos($user_answer, ',')) {
- $user_answer = json_encode(explode(',', $user_answer), JSON_UNESCAPED_UNICODE);
- }
- } else {
- $user_answer = null;
- }
- return QuestionWrongModel::updateOrCreate(
- [
- 'user_id' => $user_id,
- 'question_id' => $this->id,
- ],
- [
- 'user_id' => $user_id,
- 'question_id' => $this->id,
- 'user_answer' => $user_answer,
- ]
- );
- }
-
- public static function recordWrong($question_id, $user_id, $user_answer = null)
- {
- if (is_array($user_answer)) {
- $user_answer = json_encode($user_answer, JSON_UNESCAPED_UNICODE);
- } else if (is_string($user_answer)) {
- $user_answer = trim($user_answer);
- if (strpos($user_answer, ',')) {
- $user_answer = json_encode(explode(',', $user_answer), JSON_UNESCAPED_UNICODE);
- }
- } else {
- $user_answer = null;
- }
- return QuestionWrongModel::updateOrCreate(
- [
- 'user_id' => $user_id,
- 'question_id' => $question_id,
- ],
- [
- 'user_id' => $user_id,
- 'question_id' => $question_id,
- 'user_answer' => $user_answer,
- ]
- );
- }
- }
|