model = new \app\admin\model\teacher\Collection; $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 add() { if (false === $this->request->isPost()) { 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['question_ids'])){ $this->error('出题方式为任务自选,请选择任务'); } } $params = $this->preExcludeFields($params); if ($this->dataLimit && $this->dataLimitFieldAutoFill) { $params[$this->dataLimitField] = $this->auth->id; } $result = false; Db::startTrans(); try { $depart_ids = $params['depart_ids']; //是否采用模型验证 if ($this->modelValidate) { $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate; $this->model->validateFailException()->validate($validate); } $result = $this->model->allowField(true)->save($params); foreach(explode(',',$depart_ids) as $item){ if(!empty($item)){ $arr = [ 'exam_collection_id' => $this->model->exam_collection_id, 'dept_id' => $item, 'update_time' => date('Y-m-d H:i:s'), ]; Db::name('real_exam_collection_dept')->insert($arr); } } 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 edit($ids = null) { $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } if (false === $this->request->isPost()) { $this->view->assign('row', $row); $this->assignConfig('row_info', $row); return $this->view->fetch(); } $params = $this->request->post('row/a'); if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } 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['question_ids'])){ $this->error('出题方式为任务自选,请选择任务'); } } $params = $this->preExcludeFields($params); $result = false; Db::startTrans(); try { //是否采用模型验证 if ($this->modelValidate) { $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate; $row->validateFailException()->validate($validate); } $result = $row->allowField(true)->save($params); 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 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); } if(!empty($values['exam_collection_state'])){ if($values['exam_collection_state']==2){ $state_count = $this->model->where(['exam_collection_state'=>$values['exam_collection_state'],'exam_collection_type'=>3])->count(); if($state_count>0){ $this->error('已有启用的考试,请先关闭原来的考试'); } } } $count = 0; 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); } }