想到偶爾有同學需要做問卷調查或者在線考試的情況,所以做的該模塊,下面將邏輯圖和部分代碼奉上。要體驗的話登錄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) { //此處發短信 } } }