TestPaperQuestions.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\merchant\model\questions;
  12. use traits\ModelTrait;
  13. use basic\ModelBasic;
  14. use service\UtilService as Util;
  15. use app\merchant\model\questions\Questions as QuestionsModel;
  16. use app\merchant\model\questions\QuestionsCategpry;
  17. /**
  18. * 试卷试题 Model
  19. * Class TestPaperQuestions
  20. * @package app\admin\model\questions
  21. */
  22. class TestPaperQuestions extends ModelBasic
  23. {
  24. use ModelTrait;
  25. /**手动组题
  26. * @param $id
  27. * @param $number
  28. * @param $data
  29. */
  30. public static function addTestPaperQuestions($id, $type, $number, $data, $score)
  31. {
  32. if (!count($data) && $number <= 0) return true;
  33. foreach ($data as $key => $value) {
  34. $item['type'] = $type;
  35. $item['question_type'] = $value->question_type;
  36. $item['test_id'] = $id;
  37. $item['questions_id'] = $value->id;
  38. if (self::be($item)) continue;
  39. $item['sort'] = $value->sort;
  40. $number = $number - 1;
  41. $item['score'] = $score;
  42. if ($number < 0) continue;
  43. self::set($item);
  44. }
  45. return true;
  46. }
  47. /**试题列表
  48. * @param $id
  49. */
  50. public static function getTestPaperList($where, $id, $type)
  51. {
  52. $data = self::where(['type' => $type, 'test_id' => $id])->order('sort desc,id desc')->page($where['page'], $where['limit'])->select();
  53. foreach ($data as $key => &$item) {
  54. $item['title'] = QuestionsModel::where('id', $item['questions_id'])->value('stem');
  55. switch ($item['question_type']) {
  56. case 1:
  57. $item['types'] = '单选题';
  58. break;
  59. case 2:
  60. $item['types'] = '多选题';
  61. break;
  62. case 3:
  63. $item['types'] = '判断题';
  64. break;
  65. }
  66. }
  67. $count = self::where(['type' => $type, 'test_id' => $id])->count();
  68. return compact('data', 'count');
  69. }
  70. /**条件处理
  71. * @param $test_id
  72. * @param $record_id
  73. * @param $question_type
  74. * @return TestPaperQuestions
  75. */
  76. public static function setRecordWhere($test_id,$record_id,$question_type)
  77. {
  78. $model =self::alias('tq')->where('tq.test_id',$test_id)->where('q.question_type',$question_type)
  79. ->join('ExaminationTestRecord e','e.questions_id=tq.questions_id and e.e_id='.$record_id,'LEFT')
  80. ->join('Questions q','q.id=tq.questions_id')
  81. ->join('TestPaper t','t.id=tq.test_id')
  82. ->field('t.single_number,t.single_score,t.many_number,t.many_score,t.judge_number,t.judge_score,q.stem,q.image,q.is_img,q.option,q.answer,q.difficulty,q.question_type,e.user_answer,e.is_correct,e.score');
  83. return $model;
  84. }
  85. /**用户答题结果
  86. * @param $where
  87. */
  88. public static function getExaminationRecordAnswers($test_id,$record_id,$question_type)
  89. {
  90. $model=self::setRecordWhere($test_id,$record_id,$question_type);
  91. $data = ($data = $model->select()) && count($data) ? $data->toArray() : [];
  92. foreach ($data as $key=>&$item){
  93. if($item['is_correct']!=2) $item['score']=0;
  94. }
  95. return $data;
  96. }
  97. /** 随机组题
  98. * @param int $id
  99. * @param int $type 1=练习 2=考试
  100. * @param $questions 试卷
  101. * @param int $cate_id 试题分类ID
  102. */
  103. public static function addRandomGroupQuestions($id, $type, $question_type, $cate_id, $number, $score)
  104. {
  105. if (!$cate_id) return false;
  106. $cateIds = QuestionsCategpry::categoryId($cate_id);
  107. $data = QuestionsModel::getCateIds($question_type, $cateIds);
  108. $res = self::setTestPaper($id, $type, $number, $data, $score);
  109. if ($res) return true;
  110. else return false;
  111. }
  112. public static function setTestPaper($id, $type, $number, $data, $score)
  113. {
  114. if (!count($data) || $number <= 0) return true;
  115. if (count($data) < $number) $number = count($data);
  116. if (count($data) >= $number && $number == 1) {
  117. $ids = ['0' => 0];
  118. } else {
  119. $ids = array_rand($data, $number);
  120. }
  121. foreach ($ids as $key => $value) {
  122. $item['type'] = $type;
  123. $item['question_type'] = $data[$value]['question_type'];
  124. $item['test_id'] = $id;
  125. $item['questions_id'] = $data[$value]['id'];
  126. $item['score'] = $score;
  127. if (self::be($item)) continue;
  128. self::set($item);
  129. }
  130. return true;
  131. }
  132. public static function testPaperQuestions($id)
  133. {
  134. if (!$id) return [];
  135. return self::where(['test_id' => $id])->order('sort desc,id desc')->field('id,questions_id')->select();
  136. }
  137. /**试卷中各类试题
  138. * @param $id
  139. * @param $question_type
  140. */
  141. public static function gettestPaperQuestions($id, $question_type)
  142. {
  143. if (!$id) return [];
  144. return self::alias('t')->where(['t.test_id' => $id, 't.question_type' => $question_type, 'q.is_del' => 0])->order('sort desc,id desc')
  145. ->join('Questions q', 't.questions_id=q.id')
  146. ->field('t.sort,t.test_id,q.id,q.question_type,q.pid,q.stem,q.is_del,q.is_img')
  147. ->select();
  148. }
  149. /**
  150. * 试卷中各类试题数量
  151. */
  152. public static function testPaperQuestionsNumber($id, $question_type)
  153. {
  154. return self::where(['test_id' => $id, 'question_type' => $question_type])->count();
  155. }
  156. }