考試系統隨機出題處理邏輯
阿新 • • 發佈:2018-11-22
/* * 隨機處理邏輯 * */ private function _Rand($type, $num){ if (intval($num) <= 0){ return false;} $problemArr = ProblemModel::getBaseArr(['state' => 1, 'type' => $type], ['id'], [], ['order' => SORT_DESC]); $problemArr = array_column($problemArr, 'id');$PaperArr = PaperProblemModel::getBaseArr(['type' => $type], ['problem_id']); $PaperArr = array_column($PaperArr, 'problem_id'); $d_count = count($problemArr); if ($d_count < $num) { $this->_error('資料表選題數量不夠', []);} $Arr = array_diff($problemArr, $PaperArr);$count = count($Arr); if ($count < $num){ $return = array_rand($problemArr, $num); } else { $problemArr = array_unique($Arr); $return = array_rand($problemArr, $num); } $res = []; foreach ($return as $k => $v){$res[$k] = $problemArr[$return[$k]]; } return $res; }