想到偶尔有同学需要做问卷调查或者在线考试的情况,所以做的该模块,下面将逻辑图和部分代码奉上。要体验的话登录http://www.5atl.com 体验
微信扫描,回复答题即可体验,同时也有电脑版,登录http://www.5atl.com
/* * post检查答案正确与否,先检查是否登录,答案正确写入记录表中 本方法是大规模阉割了数据结构的,业务逻辑如此,看到看不懂的地方肯定是我删掉了,你自己填写你的逻辑和数据源即可 */ public function check() { // 1. 传入答题和答案的序列 $result = json_decode(input('data'), true); // 2. 从数据库中加载正确的答题列表和答案 $exam = ???? if (time() > $exam['endtime']) { return json([ 'code' => - 1, 'msg' => '本次答题活动已结束' ]); } $items = ????此处查询考题出来 if ($items == null) { return json([ 'code' => - 1, 'msg' => '考题都还没出呢' ]); } // 3. 对比两个序列,把不匹配的项目列出来 $list = []; foreach ($items as $item) { foreach ($result as $child) { if ($item['id'] == $child['itemid']) { if ($item['rightanswer'] == $child['selected']) { $item['selected'] = $child['selected']; $list[] = $item; } } } } // 4. 计算分值 正确数量占比 $score = count($list) * 100 / count($items); // 5. 分值和错误答案的正确选择,并返回奖励序列号,写入result $msg = ''; if ($exam['examor'] == 0 && $exam['mark'] <= $score) { $msg = '真厉害,共您答对' . count($list) . '道题,得分' . $score . '分!'; if ($exam['havegit'] === 1) { //此处发送短信 } } if ($exam['examor'] == 0 && $exam['mark'] > $score) { $msg = '真抱歉,共您答对' . count($list) . '道题,得分' . $score . '分!'; } if ($exam['examor'] == 1) { $msg = '感謝您参与本次调查活动'; if ($exam['havegit'] === 1) { //此处发短信 } } }