ApiController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Clocked;
  4. use App\Exam;
  5. use App\Examarg;
  6. use App\Grade;
  7. use App\Question;
  8. use App\School;
  9. use App\Sclass;
  10. use App\Score;
  11. use App\Test;
  12. use App\User;
  13. use function GuzzleHttp\Psr7\get_message_body_summary;
  14. use function GuzzleHttp\Psr7\str;
  15. use Illuminate\Http\Request;
  16. use Illuminate\Support\Arr;
  17. use Illuminate\Support\Facades\Auth;
  18. use Illuminate\Support\Facades\DB;
  19. use Illuminate\Support\Facades\Session;
  20. class ApiController extends Controller
  21. {
  22. //
  23. public function getAllSchool()
  24. {
  25. return School::select('id', 'name')->get();
  26. }
  27. public function getSchoolClass(Request $request)
  28. {
  29. $q = $request->get('id');
  30. return Sclass::where('school_id', $q)->select('id', 'name')->get();
  31. }
  32. public function getAllGrade()
  33. {
  34. return Grade::select('id', 'name')->get();
  35. }
  36. public function getUserinfo(Request $request)
  37. {
  38. $user_id = Auth::guard('wechat')->user()->id;
  39. return User::where('id', $user_id)->select('id', 'name', 'avatar')->first();
  40. }
  41. public function getQuestion()
  42. {
  43. $user_id = Auth::guard('wechat')->user()->id;
  44. $score = Clocked::where(DB::raw("date_format(created_at,'%Y-%m-%d')"), date('Y-m-d'))->where('user_id', $user_id)->first();
  45. if ($score) {
  46. $question = Question::find($score->question_id)->select('id', 'name', 'options', 'answers', 'radio')->first();
  47. } else {
  48. $question = Question::inRandomOrder()->first();
  49. $clocked = new Clocked();
  50. $clocked->question_id = $question->id;
  51. $clocked->user_id = $user_id;
  52. $clocked->save();
  53. }
  54. return $question;
  55. }
  56. public function cardsubmit(Request $request)
  57. {
  58. $model = Clocked::create([
  59. 'user_id' => $request->get('user_id'),
  60. 'question_id' => $request->get('question_id'),
  61. 'answer' => $request->get('answer'),
  62. 'finish' => 1
  63. ]);
  64. if ($model) {
  65. return json_encode(['status' => 1, 'msg' => '打卡成功']);
  66. }
  67. return json_encode(['status' => 0, 'msg' => '打卡失败']);
  68. }
  69. public function startExam($exam_args_id)
  70. {
  71. $user_id = Auth::guard('wechat')->user()->id;
  72. $user_sclass = Auth::guard('wechat')->user()->class_id;
  73. // start 考试试题 选项排序
  74. // 获取考试名称和考试参数
  75. $exarm_args = Examarg::where('id', $exam_args_id)->select('id', 'name', 'sclass_id', 'exam_id', 'exam_start', 'exam_end', 'exam_time')->first();
  76. $score = Score::where('user_id', $user_id)
  77. ->where("exam_id", $exarm_args['exam_id'])
  78. ->where('examarg_id', $exam_args_id)->first();
  79. if ($score) {
  80. if ($score->finish == 1) {
  81. return Array("msg" => "考试已经结束");
  82. }
  83. $examjson = $score->exam;
  84. } else {
  85. // 检查用户 id 和 班级是否匹配,不匹配返回空
  86. if (!in_array($user_sclass, $exarm_args['sclass_id'])) {
  87. return [];
  88. }
  89. // 检查 score 和 exam 里面的数据是否一致。如果一致。发送数据
  90. if (!$exarm_args->exam) {
  91. return [];
  92. }
  93. if (!$exarm_args->exam['question']) {
  94. return [];
  95. }
  96. $qlist = $exarm_args->exam['question'];
  97. $user_question = Question::find($qlist);
  98. $socre = [];
  99. $user_q_save = array(); // 随机试卷后的排序格式
  100. $user_q_save_real = array();
  101. $user_score = Score::where('user_id', $user_id)
  102. ->where("exam_id", $exarm_args['exam_id'])
  103. ->where('examarg_id', $exam_args_id)->select("user_id", 'exam_id', 'exam', 'grade')->first();
  104. //判断是否已经有数据
  105. if (!$user_score) {
  106. foreach ($user_question as $qsts => $qvalue) {
  107. $options = preg_split('/\r\n/', $qvalue->options);
  108. $user_op = array();
  109. foreach ($options as $opt => $val) {
  110. array_push($user_op, array(
  111. "num" => $opt + 1,
  112. "val" => $val
  113. ));
  114. }
  115. $answer = preg_split('/,/', $qvalue->answers);
  116. shuffle($user_op);
  117. array_push($user_q_save, array(
  118. 'q' => $qvalue->name, //问题
  119. 'qid' => $qvalue->id, // 问题id
  120. "o" => $user_op, //问题选项
  121. 'a' => $answer, // 问题答案
  122. 'im' => $qvalue->radio, //是否多选
  123. 'ya' => [], //你的答案
  124. "ir" => 2, //是否正确 2. 没有答过 1 是正确 0 是错误
  125. ));
  126. }
  127. // return $user_q_save;
  128. // end 考试试题 选项排序
  129. shuffle($user_q_save);
  130. //
  131. foreach ($user_q_save as $qkey => $val) {
  132. $user_q_save_real[$qkey + 1] = $val;
  133. }
  134. $examjson = array(
  135. "exam" => $user_q_save_real,
  136. "start_time" => $exarm_args->exam_start,
  137. "end_time" => $exarm_args->exam_end,
  138. "totaltile" => $exarm_args->exam_time,
  139. "examname" => $exarm_args->name
  140. );
  141. $score = new Score();
  142. $score->exam = $examjson;
  143. $score->user_id = $user_id;
  144. $score->exam_id = $exarm_args->exam_id;
  145. $score->examarg_id = $exarm_args->id;
  146. $score->start_time = date("Y-m-d H:i:s");
  147. $score->total_time = $exarm_args->exam_time;
  148. $score->save();
  149. // Score::insertGetId()
  150. //如果为空
  151. }
  152. // end 考试试题 选项排序
  153. // //前端处理考试数据
  154. }
  155. $fexam_datas = array();//前台返回试卷题目
  156. foreach ($examjson['exam'] as $exam_data => $value) {
  157. $fexam_datas[$exam_data] = array(
  158. 'q' => $value['q'],
  159. 'qid' => $value['qid'],
  160. 'o' => $value['o'],
  161. 'ya' => $value['ya'],
  162. 'im' => $value['im']
  163. );
  164. }
  165. $examjson['exam'] = $fexam_datas;
  166. $examjson['scid'] = $score->id;
  167. $total = strtotime($score->total_time) - strtotime('00:00:00');
  168. if ($score->start_time) {
  169. $usetime = strtotime(date('Y-m-d H:i:s')) - strtotime($score->start_time);
  170. }
  171. $remin = $total - $usetime;
  172. if ($remin < 0) {
  173. $remin = 0;
  174. }
  175. $examjson['remin'] = $remin;
  176. return $examjson;
  177. }
  178. public function answer(Request $request, Test $test)
  179. {
  180. $time = array_get($request->get('time'), 'text', '00:00:00');
  181. $answer = $request->get('answer', []);
  182. $questions = $request->get('questions', []);
  183. $paper = $request->get('paper', 0);
  184. $uid = $request->get('uid', 0);
  185. $is_switch = Test::where('paper_id', $paper)->where('uid', $uid)->count();
  186. if ($is_switch) {
  187. return response()->json(['status' => 0, 'msg' => '您已经提交过了', 'url' => '/examList']);
  188. }
  189. $result = $this->result($questions, $answer);
  190. $user = User::where('id', $uid)->first();
  191. $re = $test->fill([
  192. 'time' => $time,
  193. 'uid' => $uid,
  194. 'answer' => $answer,
  195. 'question' => $questions,
  196. 'paper_id' => $paper,
  197. 'result' => $result,
  198. 'school' => $user->school_text,
  199. 'class' => $user->class_text,
  200. 'grade' => $user->grade_text,
  201. 'mark' => $result['mark']
  202. ]);
  203. $re->save();
  204. if ($re) {
  205. return response()->json(['status' => 1, 'data' => $re]);
  206. }
  207. return response()->json(['status' => 0, 'msg' => '提交失败', 'data' => []]);
  208. }
  209. protected function result($questions, $answer)
  210. {
  211. $list = [];
  212. $mark = 0;
  213. foreach ($questions as $key => $question) {
  214. $daan = array_get($question, 'answers', []);
  215. if (count($daan) > 1) {
  216. $da = array_get($answer, $key, []);
  217. if (count($da) > 0) {
  218. $f = false;
  219. $rmark = 0;
  220. foreach ($da as $d) {
  221. if (in_array($d, $daan)) {
  222. $rmark = round($question['mark'] / 2);
  223. $list[$key] = 2;
  224. $f = true;
  225. } else {
  226. $f = false;
  227. $list[$key] = 0;
  228. break;
  229. }
  230. }
  231. if ($f && count($da) === count($daan)) {
  232. $rmark = $question['mark'];
  233. $list[$key] = 1;
  234. }
  235. $mark += $rmark;
  236. }
  237. } else {
  238. if ($daan[0] === array_get($answer, $key, '')) {
  239. $list[$key] = 1;
  240. $mark += array_get($question, 'mark', 1);
  241. } else {
  242. $list[$key] = 0;
  243. }
  244. }
  245. }
  246. $result['list'] = $list;
  247. $result['mark'] = $mark;
  248. $res = array_count_values($list);
  249. $result['right'] = array_get($res, 1, 0);
  250. $result['error'] = array_get($res, 0, 0);
  251. return $result;
  252. }
  253. public
  254. function answerSubmit(Request $request)
  255. {
  256. $answer = $request['answer'];
  257. $currQuest = $request['currQuest'];
  258. $qid = $request['questionId'];
  259. $scid = $request['scid'];
  260. if (is_integer($scid)) {
  261. $score = Score::where('id', $scid)->first();
  262. } else {
  263. return Array("msg" => "error");
  264. }
  265. if (!is_integer($currQuest) || !is_integer($qid)) {
  266. return Array("msg" => "error");
  267. }
  268. $examjson = $score->exam;
  269. $examdata = $examjson['exam'];
  270. $question = $examdata[$currQuest];
  271. if (is_array($answer)) {
  272. //多选
  273. if ($question['im'] == 1) {
  274. $examjson['exam'][$currQuest]['ya'] = $answer;
  275. if (array_diff($answer, $question['a'])) {
  276. $examjson['exam'][$currQuest]['ir'] = 1;
  277. } else {
  278. $examjson['exam'][$currQuest]['ir'] = 0;
  279. }
  280. $update_score = Score::find($scid);
  281. $update_score->exam = $examjson;
  282. $update_score->save();
  283. return Array("msg" => "提交成功");
  284. } else {
  285. return Array("msg" => "error");
  286. }
  287. } else if (is_numeric($answer)) {
  288. //单选
  289. if ($question['im'] == 0 && $question['qid'] == $qid) {
  290. // return $examjson;
  291. $examjson['exam'][$currQuest]['ya'] = ["$answer"];
  292. if (in_array($answer, $question['a'])) {
  293. $examjson['exam'][$currQuest]['ir'] = 1;
  294. } else {
  295. $examjson['exam'][$currQuest]['ir'] = 0;
  296. }
  297. $update_score = Score::find($scid);
  298. $update_score->exam = $examjson;
  299. $update_score->save();
  300. return Array("msg" => "提交成功");
  301. }
  302. return Array("msg" => "提交失败");
  303. }
  304. }
  305. function test()
  306. {
  307. $a = [1, 2, 3];
  308. $b = ['2', 1, '3'];
  309. // dd(is_numeric("2"));
  310. if (array_diff($a, $b)) {
  311. return 'a';
  312. } else {
  313. return 'b';
  314. }
  315. }
  316. //
  317. // function finishExam(Request $request){
  318. // $scid = $request['scid'];
  319. // if(is_integer($scid)){
  320. // $score = Score::find($scid);
  321. // $examdata = $score->exam['exam'];
  322. // $sum = 0;
  323. // foreach ($examdata as $qid=>$question){
  324. // if ($question['ir'] == 1){
  325. // $sum += 1;
  326. // }
  327. // }
  328. // $score->grade = $sum;
  329. // $score->finish = 1;
  330. // $score->save();
  331. //
  332. // }
  333. // return array("success"=>'ok');
  334. //
  335. // }
  336. function finishExam(Request $request)
  337. {
  338. $adata = $request['adata'];
  339. $scid = $request['scid'];
  340. if (is_integer($scid)) {
  341. $score = Score::where('id', $scid)->first();
  342. if ($score->finish == 1) {
  343. return Array("msg" => "已经提交");
  344. }
  345. } else {
  346. return Array("msg" => "error");
  347. }
  348. $exam = $score->exam;
  349. foreach ($adata as $key => $qdata) {
  350. $answer = $qdata['answer'];
  351. $currQuest = $qdata['currQuest'];
  352. $qid = $qdata['questionId'];
  353. $scid = $qdata['scid'];
  354. $examjson = $score->exam;
  355. $examdata = $examjson['exam'];
  356. $question = $examdata[$currQuest];
  357. if (is_array($answer)) {
  358. //多选
  359. if ($question['im'] == 1) {
  360. $examjson['exam'][$currQuest]['ya'] = $answer;
  361. // dd ( array_diff($answer,$question['a']));
  362. if (!array_diff($answer, $question['a'])) {
  363. $examjson['exam'][$currQuest]['ir'] = 1;
  364. } else {
  365. $examjson['exam'][$currQuest]['ir'] = 0;
  366. }
  367. // $update_score = Score::find($scid);
  368. // $update_score->exam = $examjson;
  369. // $update_score->save();
  370. } else {
  371. return Array("msg" => "error");
  372. }
  373. } else if (is_numeric($answer)) {
  374. //单选
  375. if ($question['im'] == 0 && $question['qid'] == $qid) {
  376. // return $examjson;
  377. $examjson['exam'][$currQuest]['ya'] = ["$answer"];
  378. if (in_array($answer, $question['a'])) {
  379. $examjson['exam'][$currQuest]['ir'] = 1;
  380. } else {
  381. $examjson['exam'][$currQuest]['ir'] = 0;
  382. }
  383. }
  384. }
  385. // return $examjson;
  386. $score->exam = $examjson;
  387. $score->save();
  388. }
  389. $score = Score::where('id', $scid)->first();
  390. $examdata = $score['exam']['exam'];
  391. $sum = 0;
  392. foreach ($examdata as $qid => $question) {
  393. if ($question['ir'] == 1) {
  394. $sum += 1;
  395. }
  396. }
  397. $score->grade = $sum;
  398. $score->finish = 1;
  399. // $score->save();
  400. return array("success" => 'ok');
  401. }
  402. }