get(); } public function getSchoolClass(Request $request) { $q = $request->get('id'); return Sclass::where('school_id', $q)->select('id', 'name')->get(); } public function getAllGrade() { return Grade::select('id', 'name')->get(); } public function getUserinfo(Request $request) { $user_id = Auth::guard('wechat')->user()->id; return User::where('id', $user_id)->select('id', 'name', 'avatar')->first(); } public function getQuestion() { $user_id = Auth::guard('wechat')->user()->id; $score = Clocked::where(DB::raw("date_format(created_at,'%Y-%m-%d')"), date('Y-m-d'))->where('user_id', $user_id)->first(); if ($score) { $question = Question::find($score->question_id)->select('id', 'name', 'options', 'answers', 'radio')->first(); } else { $question = Question::inRandomOrder()->first(); $clocked = new Clocked(); $clocked->question_id = $question->id; $clocked->user_id = $user_id; $clocked->save(); } return $question; } public function cardsubmit(Request $request) { $model = Clocked::create([ 'user_id' => $request->get('user_id'), 'question_id' => $request->get('question_id'), 'answer' => $request->get('answer'), 'finish' => 1 ]); if ($model) { return json_encode(['status' => 1, 'msg' => '打卡成功']); } return json_encode(['status' => 0, 'msg' => '打卡失败']); } public function startExam($exam_args_id) { $user_id = Auth::guard('wechat')->user()->id; $user_sclass = Auth::guard('wechat')->user()->class_id; // start 考试试题 选项排序 // 获取考试名称和考试参数 $exarm_args = Examarg::where('id', $exam_args_id)->select('id', 'name', 'sclass_id', 'exam_id', 'exam_start', 'exam_end', 'exam_time')->first(); $score = Score::where('user_id', $user_id) ->where("exam_id", $exarm_args['exam_id']) ->where('examarg_id', $exam_args_id)->first(); if ($score) { if ($score->finish == 1) { return Array("msg" => "考试已经结束"); } $examjson = $score->exam; } else { // 检查用户 id 和 班级是否匹配,不匹配返回空 if (!in_array($user_sclass, $exarm_args['sclass_id'])) { return []; } // 检查 score 和 exam 里面的数据是否一致。如果一致。发送数据 if (!$exarm_args->exam) { return []; } if (!$exarm_args->exam['question']) { return []; } $qlist = $exarm_args->exam['question']; $user_question = Question::find($qlist); $socre = []; $user_q_save = array(); // 随机试卷后的排序格式 $user_q_save_real = array(); $user_score = Score::where('user_id', $user_id) ->where("exam_id", $exarm_args['exam_id']) ->where('examarg_id', $exam_args_id)->select("user_id", 'exam_id', 'exam', 'grade')->first(); //判断是否已经有数据 if (!$user_score) { foreach ($user_question as $qsts => $qvalue) { $options = preg_split('/\r\n/', $qvalue->options); $user_op = array(); foreach ($options as $opt => $val) { array_push($user_op, array( "num" => $opt + 1, "val" => $val )); } $answer = preg_split('/,/', $qvalue->answers); shuffle($user_op); array_push($user_q_save, array( 'q' => $qvalue->name, //问题 'qid' => $qvalue->id, // 问题id "o" => $user_op, //问题选项 'a' => $answer, // 问题答案 'im' => $qvalue->radio, //是否多选 'ya' => [], //你的答案 "ir" => 2, //是否正确 2. 没有答过 1 是正确 0 是错误 )); } // return $user_q_save; // end 考试试题 选项排序 shuffle($user_q_save); // foreach ($user_q_save as $qkey => $val) { $user_q_save_real[$qkey + 1] = $val; } $examjson = array( "exam" => $user_q_save_real, "start_time" => $exarm_args->exam_start, "end_time" => $exarm_args->exam_end, "totaltile" => $exarm_args->exam_time, "examname" => $exarm_args->name ); $score = new Score(); $score->exam = $examjson; $score->user_id = $user_id; $score->exam_id = $exarm_args->exam_id; $score->examarg_id = $exarm_args->id; $score->start_time = date("Y-m-d H:i:s"); $score->total_time = $exarm_args->exam_time; $score->save(); // Score::insertGetId() //如果为空 } // end 考试试题 选项排序 // //前端处理考试数据 } $fexam_datas = array();//前台返回试卷题目 foreach ($examjson['exam'] as $exam_data => $value) { $fexam_datas[$exam_data] = array( 'q' => $value['q'], 'qid' => $value['qid'], 'o' => $value['o'], 'ya' => $value['ya'], 'im' => $value['im'] ); } $examjson['exam'] = $fexam_datas; $examjson['scid'] = $score->id; $total = strtotime($score->total_time) - strtotime('00:00:00'); if ($score->start_time) { $usetime = strtotime(date('Y-m-d H:i:s')) - strtotime($score->start_time); } $remin = $total - $usetime; if ($remin < 0) { $remin = 0; } $examjson['remin'] = $remin; return $examjson; } public function answer(Request $request, Test $test) { $time = array_get($request->get('time'), 'text', '00:00:00'); $answer = $request->get('answer', []); $questions = $request->get('questions', []); $paper = $request->get('paper', 0); $uid = $request->get('uid', 0); $is_switch = Test::where('paper_id', $paper)->where('uid', $uid)->count(); if ($is_switch) { return response()->json(['status' => 0, 'msg' => '您已经提交过了', 'url' => '/examList']); } $result = $this->result($questions, $answer); $user = User::where('id', $uid)->first(); $re = $test->fill([ 'time' => $time, 'uid' => $uid, 'answer' => $answer, 'question' => $questions, 'paper_id' => $paper, 'result' => $result, 'school' => $user->school_text, 'class' => $user->class_text, 'grade' => $user->grade_text, 'mark' => $result['mark'] ]); $re->save(); if ($re) { return response()->json(['status' => 1, 'data' => $re]); } return response()->json(['status' => 0, 'msg' => '提交失败', 'data' => []]); } protected function result($questions, $answer) { $list = []; $mark = 0; foreach ($questions as $key => $question) { $daan = array_get($question, 'answers', []); if (count($daan) > 1) { $da = array_get($answer, $key, []); if (count($da) > 0) { $f = false; $rmark = 0; foreach ($da as $d) { if (in_array($d, $daan)) { $rmark = round($question['mark'] / 2); $list[$key] = 2; $f = true; } else { $f = false; $list[$key] = 0; break; } } if ($f && count($da) === count($daan)) { $rmark = $question['mark']; $list[$key] = 1; } $mark += $rmark; } } else { if ($daan[0] === array_get($answer, $key, '')) { $list[$key] = 1; $mark += array_get($question, 'mark', 1); } else { $list[$key] = 0; } } } $result['list'] = $list; $result['mark'] = $mark; $res = array_count_values($list); $result['right'] = array_get($res, 1, 0); $result['error'] = array_get($res, 0, 0); return $result; } public function answerSubmit(Request $request) { $answer = $request['answer']; $currQuest = $request['currQuest']; $qid = $request['questionId']; $scid = $request['scid']; if (is_integer($scid)) { $score = Score::where('id', $scid)->first(); } else { return Array("msg" => "error"); } if (!is_integer($currQuest) || !is_integer($qid)) { return Array("msg" => "error"); } $examjson = $score->exam; $examdata = $examjson['exam']; $question = $examdata[$currQuest]; if (is_array($answer)) { //多选 if ($question['im'] == 1) { $examjson['exam'][$currQuest]['ya'] = $answer; if (array_diff($answer, $question['a'])) { $examjson['exam'][$currQuest]['ir'] = 1; } else { $examjson['exam'][$currQuest]['ir'] = 0; } $update_score = Score::find($scid); $update_score->exam = $examjson; $update_score->save(); return Array("msg" => "提交成功"); } else { return Array("msg" => "error"); } } else if (is_numeric($answer)) { //单选 if ($question['im'] == 0 && $question['qid'] == $qid) { // return $examjson; $examjson['exam'][$currQuest]['ya'] = ["$answer"]; if (in_array($answer, $question['a'])) { $examjson['exam'][$currQuest]['ir'] = 1; } else { $examjson['exam'][$currQuest]['ir'] = 0; } $update_score = Score::find($scid); $update_score->exam = $examjson; $update_score->save(); return Array("msg" => "提交成功"); } return Array("msg" => "提交失败"); } } function test() { $a = [1, 2, 3]; $b = ['2', 1, '3']; // dd(is_numeric("2")); if (array_diff($a, $b)) { return 'a'; } else { return 'b'; } } // // function finishExam(Request $request){ // $scid = $request['scid']; // if(is_integer($scid)){ // $score = Score::find($scid); // $examdata = $score->exam['exam']; // $sum = 0; // foreach ($examdata as $qid=>$question){ // if ($question['ir'] == 1){ // $sum += 1; // } // } // $score->grade = $sum; // $score->finish = 1; // $score->save(); // // } // return array("success"=>'ok'); // // } function finishExam(Request $request) { $adata = $request['adata']; $scid = $request['scid']; if (is_integer($scid)) { $score = Score::where('id', $scid)->first(); if ($score->finish == 1) { return Array("msg" => "已经提交"); } } else { return Array("msg" => "error"); } $exam = $score->exam; foreach ($adata as $key => $qdata) { $answer = $qdata['answer']; $currQuest = $qdata['currQuest']; $qid = $qdata['questionId']; $scid = $qdata['scid']; $examjson = $score->exam; $examdata = $examjson['exam']; $question = $examdata[$currQuest]; if (is_array($answer)) { //多选 if ($question['im'] == 1) { $examjson['exam'][$currQuest]['ya'] = $answer; // dd ( array_diff($answer,$question['a'])); if (!array_diff($answer, $question['a'])) { $examjson['exam'][$currQuest]['ir'] = 1; } else { $examjson['exam'][$currQuest]['ir'] = 0; } // $update_score = Score::find($scid); // $update_score->exam = $examjson; // $update_score->save(); } else { return Array("msg" => "error"); } } else if (is_numeric($answer)) { //单选 if ($question['im'] == 0 && $question['qid'] == $qid) { // return $examjson; $examjson['exam'][$currQuest]['ya'] = ["$answer"]; if (in_array($answer, $question['a'])) { $examjson['exam'][$currQuest]['ir'] = 1; } else { $examjson['exam'][$currQuest]['ir'] = 0; } } } // return $examjson; $score->exam = $examjson; $score->save(); } $score = Score::where('id', $scid)->first(); $examdata = $score['exam']['exam']; $sum = 0; foreach ($examdata as $qid => $question) { if ($question['ir'] == 1) { $sum += 1; } } $score->grade = $sum; $score->finish = 1; // $score->save(); return array("success" => 'ok'); } }