123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- /**
- * 答题
- */
- class Question extends Api
- {
- protected $noNeedLogin = [];
- protected $noNeedRight = ['*'];
- public function get_question(){
- //上次绑定选手的时间不是今天
- if($this->auth->bind_player_date != strtotime(date('Y-m-d'))){
- $this->error('先绑定单位再答题',null,2);
- }
- //获取10个随机题目
- $count = config('site.exam_times_user_eday');
- $lists = Db::name('exam_question')
- ->field('id,kind,title,options_json')
- ->where('is_material_child', 0)// 材料题子题不显示
- ->where('status', 'NORMAL')// 正常
- ->where('deletetime', NULL)
- ->orderRaw('rand()')->limit($count)->select();
- foreach($lists as $key => $val){
- $val['options_json'] = $this->getOptionsJsonAttr($val['options_json']);
- $lists[$key] = $val;
- }
- $this->success(1,$lists);
- }
- public function getOptionsJsonAttr($value)
- {
- if ($value = json_decode($value, true)) {
- $data = [];
- foreach ($value as $key => $row) {
- $arr['key'] = $key;
- $arr['value'] = $row;
- array_push($data, $arr);
- }
- return $data;
- }
- return [];
- }
- public function getAnswerAttr($value, $data)
- {
- if (is_array($value)) {
- return $value;
- }
- if (in_array($data['kind'], ['FILL', 'SHORT'])) {
- return json_decode($value, true);
- }
- return $value;
- }
- /**
- * 交卷
- */
- public function submit()
- {
- $request = Request::instance();
- $user_id = $this->auth->id;
- $paper_id = $request->post('paper_id/d', 0);
- $questions = $request->post('questions/a', []);
- $start_time = $request->post('start_time/d', time());
- $room_id = 0;
- $room_grade_id = $request->post('room_grade_id/d', 0);
- if (!$user_id || !$paper_id || !$questions) {
- $this->error('提交数据有误');
- }
- $check = Db::name('exam_grade')->where('status',1)->where('user_id',$user_id)->where('paper_id',$paper_id)->find();
- if(!$check){
- $this->error('交卷有误,或者您已交卷');
- }
- $grade_id = $check['id'];
- $start_time = $check['start_time'];
- // 考场考试
- if ($room_id) {
- if (!$room_grade_id) {
- $this->error('提交数据不合法');
- }
- // 考场考试
- $result = ExamService::roomExam($user_id, $room_id, $room_grade_id, $questions, $start_time, $paper, $room, $is_makeup, $room_grade_log);
- // 记录考场考试成绩
- $room_grade_log->allowField(true)->save(
- array_merge(
- $result,
- [
- // 'cate_id' => $paper['cate_id'],
- 'user_id' => $user_id,
- 'paper_id' => $paper_id,
- 'is_makeup' => $is_makeup,
- 'is_pre' => 0, // 提交成绩后不再为预创建标记
- ],
- [
- 'exam_mode' => ExamMode::ROOM,
- ]
- )
- );
- } else {
- $result = ExamService::paperExam($user_id, $paper_id, $questions, $start_time, $paper);
- // 记录考试成绩
- /*GradeModel::create(array_merge(
- $result,
- [
- 'cate_id' => $paper['cate_id'],
- 'user_id' => $user_id,
- 'paper_id' => $paper_id,
- ],
- [
- // 'exam_mode' => ExamMode::PAPER,
- 'date' => date('Y-m-d'),
- ]), true);*/
- $update = array_merge(
- $result,
- [
- 'cate_id' => $paper['cate_id'],
- 'updatetime' => time(),
- 'date' => date('Y-m-d'),
- 'status' => 2,
- 'finish_time' => time(),
- ]);
- unset($update['pass_score']);
- unset($update['start_time']);
- $rs = Db::name('exam_grade')->where('id',$grade_id)->update($update);
- if($rs === false){
- $this->error('交卷失败');
- }
- }
- $result['nickname'] = $this->auth->nickname;
- //删除本试卷分数最低的试卷
- $old_grade = Db::name('exam_grade')->where('user_id',$user_id)->where('paper_id',$paper_id)->where('id','NEQ',$grade_id)->find();
- if(!empty($old_grade)){
- if($old_grade['score'] <= $update['score']){
- $delete_id = $old_grade['id'];
- }else{
- $delete_id = $grade_id;
- }
- Db::name('exam_grade')->where('id',$delete_id)->delete();
- }
- //删除本试卷分数最低的试卷
- $this->success('',$result);
- // return json($result);
- }
- }
|