| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480 | <?phpnamespace app\admin\controller\teacher;use app\common\controller\Backend;use app\common\model\Config as ConfigModel;use think\Db;use think\exception\PDOException;use think\exception\ValidateException;use app\admin\model\department\Department as DepartmentModel;use app\admin\model\teacher\Task;use app\admin\model\Fault;use think\Env;/** * sim-考试集合管理 * * @icon fa fa-circle-o */class Collection extends Backend{    /**     * Collection模型对象     * @var \app\admin\model\teacher\Collection     */    protected $model = null;    protected $exam_model = null;    protected $whereExtend = null;    protected $noNeedRight = ['adduser','edituser', 'viewuser'];    public function _initialize()    {        parent::_initialize();        $this->model = new \app\admin\model\teacher\Collection;        $this->exam_model = new \app\admin\model\teacher\Exams;        $this->assignConfig('sim_sim_type', ConfigModel::getSimTypeList());        $this->assign('sim_sim_type', ConfigModel::getSimTypeList());        $this->assignConfig('sim_question_setting_method', ConfigModel::getSimQuestionList());        $this->assign('sim_question_setting_method', ConfigModel::getSimQuestionList());        $this->whereExtend['exam_collection_type'] = 3;    }    /**     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改     */    public function adduser($ids = null)    {        if (false === $this->request->isPost()) {            $this->assignConfig('user_ids',[]);            return $this->view->fetch();        }        $params = $this->request->post('row/a');        if (empty($params)) {            $this->error(__('Parameter %s can not be empty', ''));        }        if(empty($params['depart_ids'])){            $this->error('请选择学员名称');        }        if(!empty($params['question_setting_method'])){            if($params['question_setting_method']==2 && empty($params['question_ids'])){                $this->error('出题方式为考题自选,请选择考题');            }            if($params['question_setting_method']==3 && empty($params['task_id'])){                $this->error('出题方式为任务自选,请选择任务');            }        }        $depart_ids = $params['depart_ids'];        //先判断是否已经存在        $isset_exam = $this->exam_model->where(['user_id'=>['in',$depart_ids],'exam_collection_id'=>$ids])->find();        if(!empty($isset_exam)){            $this->error('添加的人员已存在该考试中,请重新选择');        }        $row = $this->model->get($ids);        $params = $this->preExcludeFields($params);        $result = false;        Db::startTrans();        try {                        if($params['question_setting_method']==3 && !empty($params['fault_name'])){                $params['question_name'] = $params['fault_name'];                unset($params['fault_name']);            }            // $result = $this->model->allowField(true)->save($params);                        $row->xueyuan_count = $row->xueyuan_count+$params['xueyuan_count'];            $row->save();            foreach(explode(',',$depart_ids) as $item){                if(!empty($item)){                    $admin = Db::name('admin')->where('id',$item)->find();                    $arr = [                        'exam_collection_id' => $ids,                        'exam_collection_name' => $row->exam_collection_name,                        'question_setting_method' => $params['question_setting_method'],                        'exam_collection_type' => $row['exam_collection_type'],                        'sim_type' => $params['sim_type'],                        'user_username' => $admin['username'],                        'user_nickname' => $admin['nickname'],                        'user_id' => $item,                        'seat_id'=>0,                        'task_id'=>$params['task_id'],                        'question_ids'=>$params['question_ids'],                        'question_name'=>$params['question_name'],                        'user_depart_id' => $admin['depart_id'],                        'createtime'=>time(),                        'updatetime'=>time(),                        'create_time' => date('Y-m-d H:i:s'),                        'update_time' => date('Y-m-d H:i:s'),                    ];                    $examid = $this->exam_model->insertGetId($arr);                                        //匹配的学员自动加入  mx_real_exam_fault 考试故障关联表                    $fault_list = Fault::where(['sim_type' => $params['sim_type'], 'fault_state' => 0,'fault_type'=>3])->select();                    if($params['question_setting_method']==1){ //系统随机                        if($params['sim_type']=='0002'){                            $fault_key1 = rand(0, 1);                            $fault_key2 = 4;                            $fault_key3 = rand(10,11);                        }else{                            $fault_key1 = rand(0, 1);                            $fault_key2 = rand(2, 3);                            $fault_key3 = rand(4, 5);                        }                        $fault_key = [$fault_key1,$fault_key2,$fault_key3];                    }else if($params['question_setting_method']==2){//教师自选                        $question_ids = $params['question_ids'];                    }else if($params['question_setting_method']==3){ //任务自选                        $task_ids = Db::name('task_fault')->where(['task_id'=>['in',$params['task_id']]])->column('fault_id');                    }                    foreach ($fault_list as $key=> $item1){                        $flag = 0;                        if(!empty($fault_key) && in_array($key,$fault_key)){                            $flag = 1;                        }                        if(!empty($question_ids) && in_array($item1['fault_id'],explode(',',$question_ids))){                            $flag = 1;                        }                        if(!empty($task_ids) && in_array($item1['fault_id'],$task_ids)){                            $flag = 1;                        }                        $add= [                            'exam_id'=>$examid,                            'fault_id'=>$item1['fault_id'],                            'ref_type'=>2,                            'flag'=>$flag,                            'ref_state'=>0,                            'createtime'=>time(),                            'updatetime'=>time(),                            'create_time'=>date('Y-m-d H:i:s'),                            'update_time'=>date('Y-m-d H:i:s'),                        ];                        Db::name('real_exam_fault')->insert($add);                    }                    unset($item1);                }            }            $result = true;            Db::commit();        } catch (ValidateException|PDOException|Exception $e) {            Db::rollback();            $this->error($e->getMessage());        }        if ($result === false) {            $this->error(__('No rows were inserted'));        }        $this->success();    }    public function edituser($ids = null)    {        $row = $this->model->get($ids);        if (!$row) {            $this->error(__('No Results were found'));        }        //获取当前考试的全部用户id        $user_ids = $this->exam_model->where(['exam_collection_id'=>$ids])->column('user_id');        if (false === $this->request->isPost()) {            $this->view->assign('row', $row);            $this->view->assign('user_ids',implode(',', $user_ids));            $this->assignConfig('user_ids', $user_ids);            return $this->view->fetch();        }        $params = $this->request->post('row/a');        if (empty($params)) {            $this->error(__('Parameter %s can not be empty', ''));        }        $params = $this->preExcludeFields($params);        $depart_id = $params['depart_id'];        //比较两者的不同,然后在删除        $array_diff = array_diff($user_ids,explode(',',$depart_id));        if(!empty($depart_id)){             //判断是否有已经开始考试的人员            $isset_exam = $this->exam_model->where(['user_id'=>['in',$array_diff],'exam_collection_id'=>$ids,'exam_status'=>4])->find();            if(!empty($isset_exam)){                $this->error('删除的人员已再考试中,请重新选择');            }        }        $result = false;        Db::startTrans();        try {            if(!empty($depart_id)){                 $depart_count = count($array_diff);                $params['xueyuan_count'] = $row->xueyuan_count-$depart_count??0;                $result = $row->allowField(true)->save($params);                foreach($array_diff as $item){                    if(!empty($item)){                        $exam =  $this->exam_model->where(['exam_collection_id'=>$ids,'user_id'=>$item])->find();                        //删除real_exam 的人员                        $this->exam_model->where(['exam_collection_id'=>$ids,'user_id'=>$item])->delete();                        //删除real_exam_fault 的故障内容                        Db::name('real_exam_fault')->where(['exam_id'=>$exam['exam_id']])->delete();                    }                }            }            $result = true;             Db::commit();        } catch (ValidateException|PDOException|Exception $e) {            Db::rollback();            $this->error($e->getMessage());        }        if (false === $result) {            $this->error(__('No rows were updated'));        }        $this->success();    }    //查看学员    public function viewuser($ids= null)    {        //设置过滤方法        $this->request->filter(['strip_tags', 'trim']);        if (false === $this->request->isAjax()) {            $this->assignConfig('ids', $ids);            return $this->view->fetch();        }        //如果发送的来源是 Selectpage,则转发到 Selectpage        if ($this->request->request('keyField')) {            return $this->selectpage();        }        [$where, $sort, $order, $offset, $limit] = $this->buildparams();        $list = $this->exam_model->where($where)->where('exam_collection_id',$ids)            ->order($sort, $order)            ->paginate($limit);        unset($v);        $result = ['total' => $list->total(), 'rows' => $list->items()];        return json($result);    }    public function multi($ids = null)    {        if (false === $this->request->isPost()) {            $this->error(__('Invalid parameters'));        }        $ids = $ids ?: $this->request->post('ids');        if (empty($ids)) {            $this->error(__('Parameter %s can not be empty', 'ids'));        }        if (false === $this->request->has('params')) {            $this->error(__('No rows were updated'));        }        parse_str($this->request->post('params'), $values);        $values = true ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));//        $values = $this->auth->isSuperAdmin() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));        if (empty($values)) {            $this->error(__('You have no permission'));        }        $adminIds = $this->getDataLimitAdminIds();        if (is_array($adminIds)) {            $this->model->where($this->dataLimitField, 'in', $adminIds);        }        $count = 0;        if(!empty($values['exam_collection_state'])){            //开始考试集合            if($values['exam_collection_state']==2){                //触发Java端后台的接口数据,                //dev-api/sim/real-exam-collection/teacher/exam/open/{examcollectionId}  $ids  put                if(Env::get('app.is_fault')){                    $url = config('site.url_type').'/sim/real-exam-collection/teacher/exam/open/'.$ids;                    $ret = json_decode(send_post($url),true);                    if($ret['code']!=200){                        $this->error($ret['msg']);                    }                }else{                    $state_count = $this->model->where(['exam_collection_state'=>$values['exam_collection_state'],'exam_collection_type'=>3])->count();                    if($state_count>0){                        $this->error('已有启用的考试,请先关闭原来的考试');                    }                }                $count = 1;            }            //关闭            if($values['exam_collection_state']==3){                //触发Java端后台的接口数据,                //dev-api/sim/real-exam-collection/teacher/exam/open/{examcollectionId}  $ids  put                if(Env::get('app.is_fault')){                    $url = config('site.url_type').'/sim/real-exam-collection/teacher/exam/close/'.$ids;                    $ret = json_decode(send_post($url),true);                    if($ret['code']!=200){                        $this->error($ret['msg']);                    }                }                $count = 1;            }        }        Db::startTrans();        try {            $list = $this->model->where($this->model->getPk(), 'in', $ids)->select();            foreach ($list as $item) {                $count += $item->allowField(true)->isUpdate(true)->save($values);            }            Db::commit();        } catch (PDOException|Exception $e) {            Db::rollback();            $this->error($e->getMessage());        }        if ($count) {            $this->success();        }        $this->error(__('No rows were updated'));    }    //选择考题    public function faults($type = null)    {        //设置过滤方法        $this->request->filter(['strip_tags', 'trim']);        if (false === $this->request->isAjax()) {            $this->assignConfig('sim_type', $type);            return $this->view->fetch();        }        //如果发送的来源是 Selectpage,则转发到 Selectpage        if ($this->request->request('keyField')) {            return $this->selectpage();        }        [$where, $sort, $order, $offset, $limit] = $this->buildparams();        $where_fault = [            'sim_type' => $type,            'fault_type' => 3,            'fault_state'=>0        ];        $list = Fault::where($where)->where($where_fault)            ->order($sort, $order)            ->paginate($limit);        foreach ($list as $k => $v){            $exam =  Fault::where(['fault_id'=>$v['parent_fault_id'],'fault_type'=>1])->find();            if(isset($exam)){                $v->parent_name = $exam['name'];            }else{                $v->parent_name = '';            }        }        unset($v);        unset($v);        $result = ['total' => $list->total(), 'rows' => $list->items()];        return json($result);    }    //选择任务    public function task($type= null)    {        //设置过滤方法        $this->request->filter(['strip_tags', 'trim']);        if (false === $this->request->isAjax()) {            $this->assignConfig('sim_type', $type);            return $this->view->fetch();        }        //如果发送的来源是 Selectpage,则转发到 Selectpage        if ($this->request->request('keyField')) {            return $this->selectpage();        }        [$where, $sort, $order, $offset, $limit] = $this->buildparams();        $list = Task::where($where)->where('sim_type',$type)            ->order($sort, $order)            ->paginate($limit);        unset($v);        $result = ['total' => $list->total(), 'rows' => $list->items()];        return json($result);    }    /**    老师开启考试     */    public function start($ids = null){        $row = $this->model->get($ids);        if (!$row) {            $this->error(__('No Results were found'));        }        $params['start_time'] = date('Y-m-d H:i:s');        $params['starttime'] = time();        $result = $row->allowField(true)->save($params);        $this->success('开始成功');    }    public function del($ids = null)    {        if (false === $this->request->isPost()) {            $this->error(__("Invalid parameters"));        }        $ids = $ids ?: $this->request->post("ids");        if (empty($ids)) {            $this->error(__('Parameter %s can not be empty', 'ids'));        }        $pk = $this->model->getPk();        $adminIds = $this->getDataLimitAdminIds();        if (is_array($adminIds)) {            $this->model->where($this->dataLimitField, 'in', $adminIds);        }        $list = $this->model->where($pk, 'in', $ids)->select();        $count = 0;        Db::startTrans();        try {            //删除考试集合  mx_real_exam_collection            foreach ($list as $item) {                if($item['yikao_count']>0){                    $this->error('已有学生参加考试,无法删除');                }                $count += $item->delete();            }            //删除学员考试 mx_real_exam            $exam_list = $this->exam_model->where('exam_collection_id',$ids)->select();            if(!empty($exam_list)){                foreach ($exam_list as $k=>$it)                {                    //删除学员考试故障表 mx_real_exam_fault                    Db::name('real_exam_fault')->where('exam_id',$it['exam_id'])->delete();                    $it->delete();                }            }            //删除考试关联区队表 mx_real_exam_collection_dept            // Db::name('real_exam_collection_dept')->where('exam_collection_id',$ids)->delete();            Db::commit();        } catch (PDOException|Exception $e) {            Db::rollback();            $this->error($e->getMessage());        }        if ($count) {            $this->success();        }        $this->error(__('No rows were deleted'));    }    public function rand_fault($fault_list = null){        // $fault_key = array_rand($fault_list,3);        // $fault_key1 = array_rand($fault_list);        // $fault_key2 = array_rand($fault_list);        // $fault_key3 = array_rand($fault_list);        // if($fault_list[$fault_key1]['parent_fault_id']==$fault_list[$fault_key2]['parent_fault_id']){        //     $fault_key2 = array_rand($fault_list);        // }        // if($fault_list[$fault_key2]['parent_fault_id']==$fault_list[$fault_key3]['parent_fault_id']){        //     $fault_key3 = array_rand($fault_list);        // }        // $arr = [$fault_key1,$fault_key2,$fault_key3];        $arr = [1,5,7];        return $arr;    }}
 |