checkLogin(); // 接收参数 $recordId = request('record_id',0); // 如果不是课中习题 if( !$recordId ) return json_send(['code'=>'error','msg'=>'请选择学习记录']); // 查询答题记录 $answerList = $Model->query() ->where([['record_id','=',$recordId]]) ->select(['id','record_id','course_id','question_id','answer_id','is_answer','get_score']) ->get()->toArray(); // 循环答题 foreach ($answerList as $key => $value) { // 题目标题 $value['question_title']= $VideoQuestion->getOne($value['question_id'],'title'); $value['answer_list'] = $VideoAnswer->getListByQuestion($value['question_id']); $value['answer_list'] = array_values($value['answer_list']); // 重组 $answerList[$key] = $value; } // 返回结果 return json_send(['code'=>'success','msg'=>'获取成功','data'=>$answerList]); } /** * 上报课中习题答案 /api/video_learn_answer/play_exam * */ public function play_exam(Model $Model,LearnQuestion $LearnQuestion,VideoQuestion $VideoQuestion,VideoAnswer $VideoAnswer,CustomScore $CustomScore,LearnRecord $LearnRecord){ // 用户登录 $uid = $this->checkLogin(); // 接收参数 $data['custom_uid'] = $uid; $data['record_id'] = request('record_id',0); $data['course_id'] = request('course_id',0); $data['question_id'] = request('question_id',0); $data['answer_id'] = request('answer_id',0); // if( ! $data['record_id'] ) return json_send(['code'=>'error','msg'=>'请选择学习记录']); if( ! $data['course_id'] ) return json_send(['code'=>'error','msg'=>'请选择学习课程']); if( ! $data['question_id'] )return json_send(['code'=>'error','msg'=>'请选择习题问题']); if( ! $data['answer_id'] ) return json_send(['code'=>'error','msg'=>'请选择习题答案']); // 判断是不是课中习题 $isLearnQuestion = $LearnQuestion->getOne($data['course_id'],$data['question_id']); // 如果不是课中习题 if( !$isLearnQuestion ) return json_send(['code'=>'error','msg'=>'题目有误或者题目已停用']); // 通过记录ID。查询问题是否已经回答 $havaAnswer = $Model->query()->where([['record_id','=',$data['record_id']],['custom_uid','=',$uid],['question_id','=',$data['question_id']]])->first(); // 如果答过题 if( $havaAnswer ) return json_send(['code'=>'error','msg'=>'已答题,请无重复作答']); // 查询该用户该课程该题答题记录 $answerRecord = $Model->where([['custom_uid','=',$uid],['course_id','=',$data['course_id']],['question_id','=',$data['question_id']]])->first(); // 获取题目分数 $data['score'] = (int) $VideoQuestion->getOne($data['question_id'],'score'); // 获取答案 $data['is_answer'] = (int) $VideoAnswer->getOne($data['question_id'],$data['answer_id'],'is_answer'); // 如果没有答题记录,并且题目答案正确,则获取分数 $data['get_score'] = (!$answerRecord && $data['is_answer']) ? $data['score'] : 0; // 获取题目数量 $learnData['question_total']= count($LearnQuestion->getList($data['course_id'])); // 查询答题数量 $learnData['answer_total'] = ($Model->query()->where([['record_id','=',$data['record_id']],['custom_uid','=',$uid]])->count()) + 1; // 查询答对数量 $learnData['isanswer_total']= ($Model->query()->where([['record_id','=',$data['record_id']],['custom_uid','=',$uid],['is_answer','=',1]])->count()) + ($data['is_answer'] ? 1 : 0); // 查询答题总积分 $learnData['get_score'] = intval($Model->query()->where([['record_id','=',$data['record_id']],['custom_uid','=',$uid]])->sum('get_score')) + $data['get_score']; // 开始事务 DB::beginTransaction(); // 保存答题记录 $data['id'] = $Model->add($data); // 答题记录保存失败 if ( !$data['id'] ) { // 回滚事务 DB::rollBack(); // 失败提醒 return json_send(['code'=>'error', 'msg'=>'答题记录失败,请重试']); } // 如果有获取积分 if ($data['get_score']) { // 积分奖励 $result = $CustomScore->trade($uid,$data['id'],$data['get_score'],10,1,'课中评测奖励'); // 积分奖励失败 if( isset($result['error']) ) { // 回滚事务 DB::rollBack(); // 失败提醒 return json_send(['code'=>'error', 'msg'=>'积分下发失败,请重试','data'=>['error'=>$result['error']]]); } } // 写入/修改学习记录 $result = $LearnRecord->edit($data['record_id'],$learnData); // 写入失败 if( !$result ) { // 回滚事务 DB::rollBack(); // 失败提醒 return json_send(['code'=>'error', 'msg'=>'学习记录失败,请重试','data'=>['error'=>'学习记录写入失败']]); } // 提交事务 DB::commit(); // 成功提醒 return json_send(['code'=>'success', 'msg'=>'提交成功','data'=>$data]); } }