考试系统随机出题处理逻辑

 /*
     * 随机处理逻辑
     * */
    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;
    }

 

posted @ 2018-11-22 19:08  ノGHJ  阅读(565)  评论(0编辑  收藏  举报