Question.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 答题
  7. */
  8. class Question extends Api
  9. {
  10. protected $noNeedLogin = [];
  11. protected $noNeedRight = ['*'];
  12. public function get_question(){
  13. //上次绑定选手的时间不是今天
  14. if($this->auth->bind_player_date != strtotime(date('Y-m-d'))){
  15. $this->error('先绑定单位再答题',null,2);
  16. }
  17. //获取10个随机题目
  18. $count = config('site.exam_times_user_eday');
  19. $lists = Db::name('exam_question')
  20. ->field('id,kind,title,options_json')
  21. ->where('is_material_child', 0)// 材料题子题不显示
  22. ->where('status', 'NORMAL')// 正常
  23. ->where('deletetime', NULL)
  24. ->orderRaw('rand()')->limit($count)->select();
  25. foreach($lists as $key => $val){
  26. $val['options_json'] = $this->getOptionsJsonAttr($val['options_json']);
  27. $lists[$key] = $val;
  28. }
  29. $this->success(1,$lists);
  30. }
  31. public function getOptionsJsonAttr($value)
  32. {
  33. if ($value = json_decode($value, true)) {
  34. $data = [];
  35. foreach ($value as $key => $row) {
  36. $arr['key'] = $key;
  37. $arr['value'] = $row;
  38. array_push($data, $arr);
  39. }
  40. return $data;
  41. }
  42. return [];
  43. }
  44. public function getAnswerAttr($value, $data)
  45. {
  46. if (is_array($value)) {
  47. return $value;
  48. }
  49. if (in_array($data['kind'], ['FILL', 'SHORT'])) {
  50. return json_decode($value, true);
  51. }
  52. return $value;
  53. }
  54. /**
  55. * 交卷
  56. */
  57. public function submit()
  58. {
  59. $request = Request::instance();
  60. $user_id = $this->auth->id;
  61. $paper_id = $request->post('paper_id/d', 0);
  62. $questions = $request->post('questions/a', []);
  63. $start_time = $request->post('start_time/d', time());
  64. $room_id = 0;
  65. $room_grade_id = $request->post('room_grade_id/d', 0);
  66. if (!$user_id || !$paper_id || !$questions) {
  67. $this->error('提交数据有误');
  68. }
  69. $check = Db::name('exam_grade')->where('status',1)->where('user_id',$user_id)->where('paper_id',$paper_id)->find();
  70. if(!$check){
  71. $this->error('交卷有误,或者您已交卷');
  72. }
  73. $grade_id = $check['id'];
  74. $start_time = $check['start_time'];
  75. // 考场考试
  76. if ($room_id) {
  77. if (!$room_grade_id) {
  78. $this->error('提交数据不合法');
  79. }
  80. // 考场考试
  81. $result = ExamService::roomExam($user_id, $room_id, $room_grade_id, $questions, $start_time, $paper, $room, $is_makeup, $room_grade_log);
  82. // 记录考场考试成绩
  83. $room_grade_log->allowField(true)->save(
  84. array_merge(
  85. $result,
  86. [
  87. // 'cate_id' => $paper['cate_id'],
  88. 'user_id' => $user_id,
  89. 'paper_id' => $paper_id,
  90. 'is_makeup' => $is_makeup,
  91. 'is_pre' => 0, // 提交成绩后不再为预创建标记
  92. ],
  93. [
  94. 'exam_mode' => ExamMode::ROOM,
  95. ]
  96. )
  97. );
  98. } else {
  99. $result = ExamService::paperExam($user_id, $paper_id, $questions, $start_time, $paper);
  100. // 记录考试成绩
  101. /*GradeModel::create(array_merge(
  102. $result,
  103. [
  104. 'cate_id' => $paper['cate_id'],
  105. 'user_id' => $user_id,
  106. 'paper_id' => $paper_id,
  107. ],
  108. [
  109. // 'exam_mode' => ExamMode::PAPER,
  110. 'date' => date('Y-m-d'),
  111. ]), true);*/
  112. $update = array_merge(
  113. $result,
  114. [
  115. 'cate_id' => $paper['cate_id'],
  116. 'updatetime' => time(),
  117. 'date' => date('Y-m-d'),
  118. 'status' => 2,
  119. 'finish_time' => time(),
  120. ]);
  121. unset($update['pass_score']);
  122. unset($update['start_time']);
  123. $rs = Db::name('exam_grade')->where('id',$grade_id)->update($update);
  124. if($rs === false){
  125. $this->error('交卷失败');
  126. }
  127. }
  128. $result['nickname'] = $this->auth->nickname;
  129. //删除本试卷分数最低的试卷
  130. $old_grade = Db::name('exam_grade')->where('user_id',$user_id)->where('paper_id',$paper_id)->where('id','NEQ',$grade_id)->find();
  131. if(!empty($old_grade)){
  132. if($old_grade['score'] <= $update['score']){
  133. $delete_id = $old_grade['id'];
  134. }else{
  135. $delete_id = $grade_id;
  136. }
  137. Db::name('exam_grade')->where('id',$delete_id)->delete();
  138. }
  139. //删除本试卷分数最低的试卷
  140. $this->success('',$result);
  141. // return json($result);
  142. }
  143. }