Collection.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. namespace app\admin\controller\teacher;
  3. use app\common\controller\Backend;
  4. use app\common\model\Config as ConfigModel;
  5. use think\Db;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. use app\admin\model\department\Department as DepartmentModel;
  9. use app\admin\model\teacher\Task;
  10. use app\admin\model\Fault;
  11. /**
  12. * sim-考试集合管理
  13. *
  14. * @icon fa fa-circle-o
  15. */
  16. class Collection extends Backend
  17. {
  18. /**
  19. * Collection模型对象
  20. * @var \app\admin\model\teacher\Collection
  21. */
  22. protected $model = null;
  23. protected $exam_model = null;
  24. protected $whereExtend = null;
  25. public function _initialize()
  26. {
  27. parent::_initialize();
  28. $this->model = new \app\admin\model\teacher\Collection;
  29. $this->exam_model = new \app\admin\model\teacher\Exams;
  30. $this->assignConfig('sim_sim_type', ConfigModel::getSimTypeList());
  31. $this->assign('sim_sim_type', ConfigModel::getSimTypeList());
  32. $this->assignConfig('sim_question_setting_method', ConfigModel::getSimQuestionList());
  33. $this->assign('sim_question_setting_method', ConfigModel::getSimQuestionList());
  34. $this->whereExtend['exam_collection_type'] = 3;
  35. }
  36. /**
  37. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  38. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  39. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  40. */
  41. public function add()
  42. {
  43. if (false === $this->request->isPost()) {
  44. return $this->view->fetch();
  45. }
  46. $params = $this->request->post('row/a');
  47. if (empty($params)) {
  48. $this->error(__('Parameter %s can not be empty', ''));
  49. }
  50. if(empty($params['depart_ids'])){
  51. $this->error('请选择区队名称');
  52. }
  53. if(!empty($params['question_setting_method'])){
  54. if($params['question_setting_method']==2 && empty($params['question_ids'])){
  55. $this->error('出题方式为考题自选,请选择考题');
  56. }
  57. if($params['question_setting_method']==3 && empty($params['question_ids'])){
  58. $this->error('出题方式为任务自选,请选择任务');
  59. }
  60. }
  61. $params = $this->preExcludeFields($params);
  62. $result = false;
  63. Db::startTrans();
  64. try {
  65. $depart_ids = $params['depart_ids'];
  66. $result = $this->model->allowField(true)->save($params);
  67. foreach(explode(',',$depart_ids) as $item){
  68. if(!empty($item)){
  69. $arr = [
  70. 'exam_collection_id' => $this->model->exam_collection_id,
  71. 'dept_id' => $item,
  72. 'update_time' => date('Y-m-d H:i:s'),
  73. ];
  74. Db::name('real_exam_collection_dept')->insert($arr);
  75. }
  76. }
  77. //匹配的学员自动加入 mx_real_exam 考试表 总分
  78. $admin_list = Db::name('admin')->where('depart_id','in',$depart_ids)->select();
  79. foreach ($admin_list as $it){
  80. $arr1 = [
  81. 'exam_collection_id' => $this->model->exam_collection_id,
  82. 'exam_collection_name' => $params['exam_collection_name'],
  83. 'exam_collection_type' => $params['exam_collection_type'],
  84. 'sim_type' => $params['sim_type'],
  85. 'user_username' => $it['username'],
  86. 'user_nickname' => $it['nickname'],
  87. 'user_id' => $it['id'],
  88. 'user_depart_id' => $it['depart_id'],
  89. 'seat_id'=>01,
  90. 'sim_id'=>intval($params['sim_type']),
  91. 'create_time'=>date('Y-m-d H:i:s'),
  92. 'update_time' => date('Y-m-d H:i:s'),
  93. ];
  94. $examid = Db::name('real_exam')->insertGetId($arr1);
  95. //匹配的学员自动加入 mx_real_exam_fault 考试故障关联表
  96. if($params['question_setting_method']==1){ //系统随机
  97. $fault_list = Fault::where(['sim_type' => $params['sim_type'], 'fault_state' => 0,'fault_type'=>3])->select();
  98. }else if($params['question_setting_method']==2){//教师自选
  99. $fault_list = Fault::where(['sim_type' => $params['sim_type'],'fault_id'=>['in',$params['question_ids']], 'fault_state' => 0,'fault_type'=>3])->select();
  100. }else if($params['question_setting_method']==3){ //任务自选
  101. $fault_list = Db::name('task_fault')->where(['task_id'=>['in',$params['question_ids']]])->select();
  102. }
  103. $fault_key = array_rand($fault_list, 3);
  104. foreach ($fault_list as $key=> $item1){
  105. $flag = 0;
  106. if(in_array($key,$fault_key)){
  107. $flag = 1;
  108. }
  109. $add= [
  110. 'exam_id'=>$examid,
  111. 'fault_id'=>$item1->fault_id,
  112. 'ref_type'=>2,
  113. 'flag'=>$flag,
  114. 'ref_state'=>3,
  115. 'sim_fault_question_value'=>'0000000'.rand(1,2),
  116. 'createtime'=>time(),
  117. 'updatetime'=>time(),
  118. 'create_time'=>date('Y-m-d H:i:s'),
  119. 'update_time'=>date('Y-m-d H:i:s'),
  120. ];
  121. Db::name('real_exam_fault')->insert($add);
  122. }
  123. unset($item1);
  124. }
  125. Db::commit();
  126. } catch (ValidateException|PDOException|Exception $e) {
  127. Db::rollback();
  128. $this->error($e->getMessage());
  129. }
  130. if ($result === false) {
  131. $this->error(__('No rows were inserted'));
  132. }
  133. $this->success();
  134. }
  135. public function edit($ids = null)
  136. {
  137. $row = $this->model->get($ids);
  138. if (!$row) {
  139. $this->error(__('No Results were found'));
  140. }
  141. if (false === $this->request->isPost()) {
  142. $examing_count = $this->exam_model->where(['exam_collection_id'=>$ids,'exam_status'=>4])->count();
  143. if($examing_count>0){
  144. $this->error('有学员考试中,无法修改');
  145. }
  146. $this->view->assign('row', $row);
  147. $this->assignConfig('row_info', $row);
  148. return $this->view->fetch();
  149. }
  150. $params = $this->request->post('row/a');
  151. if (empty($params)) {
  152. $this->error(__('Parameter %s can not be empty', ''));
  153. }
  154. if(!empty($params['question_setting_method'])){
  155. if($params['question_setting_method']==2 && empty($params['question_ids'])){
  156. $this->error('出题方式为考题自选,请选择考题');
  157. }
  158. if($params['question_setting_method']==3 && empty($params['question_ids'])){
  159. $this->error('出题方式为任务自选,请选择任务');
  160. }
  161. }
  162. $params = $this->preExcludeFields($params);
  163. $result = false;
  164. Db::startTrans();
  165. try {
  166. $depart_ids = $params['depart_ids'];
  167. $result = $row->allowField(true)->save($params);
  168. Db::name('real_exam_collection_dept')->where(['exam_collection_id'=>$row->exam_collection_id])->delete();
  169. foreach(explode(',',$depart_ids) as $item){
  170. if(!empty($item)){
  171. $arr = [
  172. 'exam_collection_id' => $row->exam_collection_id,
  173. 'dept_id' => $item,
  174. 'update_time' => date('Y-m-d H:i:s'),
  175. ];
  176. Db::name('real_exam_collection_dept')->insert($arr);
  177. }
  178. }
  179. Db::name('real_exam')->where(['exam_collection_id'=>$row->exam_collection_id])->delete();
  180. //匹配的学员自动加入 mx_real_exam 考试表 总分
  181. $admin_list = Db::name('admin')->where('depart_id','in',$depart_ids)->select();
  182. foreach ($admin_list as $it){
  183. $arr1 = [
  184. 'exam_collection_id' => $row->exam_collection_id,
  185. 'exam_collection_name' => $params['exam_collection_name'],
  186. 'exam_collection_type' => $row['exam_collection_type'],
  187. 'sim_type' => $params['sim_type'],
  188. 'user_username' => $it['username'],
  189. 'user_nickname' => $it['nickname'],
  190. 'user_id' => $it['id'],
  191. 'user_depart_id' => $it['depart_id'],
  192. 'seat_id'=>01,
  193. 'sim_id'=>intval($params['sim_type']),
  194. 'create_time'=>date('Y-m-d H:i:s'),
  195. 'update_time' => date('Y-m-d H:i:s'),
  196. ];
  197. $examid = Db::name('real_exam')->insertGetId($arr1);
  198. //匹配的学员自动加入 mx_real_exam_fault 考试故障关联表
  199. if($params['question_setting_method']==1){ //系统随机
  200. $fault_list = Fault::where(['sim_type' => $params['sim_type'], 'fault_state' => 0,'fault_type'=>3])->select();
  201. }else if($params['question_setting_method']==2){//教师自选
  202. $fault_list = Fault::where(['sim_type' => $params['sim_type'],'fault_id'=>['in',$params['question_ids']], 'fault_state' => 0,'fault_type'=>3])->select();
  203. }else if($params['question_setting_method']==3){ //任务自选
  204. $fault_list = Db::name('task_fault')->where(['task_id'=>['in',$params['question_ids']]])->select();
  205. }
  206. Db::name('real_exam_fault')->where(['exam_id'=>$examid])->delete();
  207. $fault_key = array_rand($fault_list, 3);
  208. foreach ($fault_list as $key=> $item1){
  209. $flag = 0;
  210. if(in_array($key,$fault_key)){
  211. $flag = 1;
  212. }
  213. $add= [
  214. 'exam_id'=>$examid,
  215. 'fault_id'=>$item1['fault_id'],
  216. 'ref_type'=>2,
  217. 'flag'=>$flag,
  218. 'ref_state'=>3,
  219. 'sim_fault_question_value'=>'0000000'.rand(1,2),
  220. 'createtime'=>time(),
  221. 'updatetime'=>time(),
  222. 'create_time'=>date('Y-m-d H:i:s'),
  223. 'update_time'=>date('Y-m-d H:i:s'),
  224. ];
  225. Db::name('real_exam_fault')->insert($add);
  226. }
  227. unset($item1);
  228. }
  229. Db::commit();
  230. } catch (ValidateException|PDOException|Exception $e) {
  231. Db::rollback();
  232. $this->error($e->getMessage());
  233. }
  234. if (false === $result) {
  235. $this->error(__('No rows were updated'));
  236. }
  237. $this->success();
  238. }
  239. public function multi($ids = null)
  240. {
  241. if (false === $this->request->isPost()) {
  242. $this->error(__('Invalid parameters'));
  243. }
  244. $ids = $ids ?: $this->request->post('ids');
  245. if (empty($ids)) {
  246. $this->error(__('Parameter %s can not be empty', 'ids'));
  247. }
  248. if (false === $this->request->has('params')) {
  249. $this->error(__('No rows were updated'));
  250. }
  251. parse_str($this->request->post('params'), $values);
  252. $values = true ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  253. // $values = $this->auth->isSuperAdmin() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  254. if (empty($values)) {
  255. $this->error(__('You have no permission'));
  256. }
  257. $adminIds = $this->getDataLimitAdminIds();
  258. if (is_array($adminIds)) {
  259. $this->model->where($this->dataLimitField, 'in', $adminIds);
  260. }
  261. if(!empty($values['exam_collection_state'])){
  262. if($values['exam_collection_state']==2){
  263. $state_count = $this->model->where(['exam_collection_state'=>$values['exam_collection_state'],'exam_collection_type'=>3])->count();
  264. if($state_count>0){
  265. $this->error('已有启用的考试,请先关闭原来的考试');
  266. }
  267. }
  268. }
  269. $count = 0;
  270. Db::startTrans();
  271. try {
  272. $list = $this->model->where($this->model->getPk(), 'in', $ids)->select();
  273. foreach ($list as $item) {
  274. $count += $item->allowField(true)->isUpdate(true)->save($values);
  275. }
  276. Db::commit();
  277. } catch (PDOException|Exception $e) {
  278. Db::rollback();
  279. $this->error($e->getMessage());
  280. }
  281. if ($count) {
  282. $this->success();
  283. }
  284. $this->error(__('No rows were updated'));
  285. }
  286. //选择考题
  287. public function faults($type = null)
  288. {
  289. //设置过滤方法
  290. $this->request->filter(['strip_tags', 'trim']);
  291. if (false === $this->request->isAjax()) {
  292. $this->assignConfig('sim_type', $type);
  293. return $this->view->fetch();
  294. }
  295. //如果发送的来源是 Selectpage,则转发到 Selectpage
  296. if ($this->request->request('keyField')) {
  297. return $this->selectpage();
  298. }
  299. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  300. $where_fault = [
  301. 'sim_type' => $type,
  302. 'fault_type' => 3,
  303. 'fault_state'=>0
  304. ];
  305. $list = Fault::where($where)->where($where_fault)
  306. ->order($sort, $order)
  307. ->paginate($limit);
  308. foreach ($list as $k => $v){
  309. $exam = Fault::where(['fault_id'=>$v['parent_fault_id'],'fault_type'=>1])->find();
  310. if(isset($exam)){
  311. $v->parent_name = $exam['name'];
  312. }else{
  313. $v->parent_name = '';
  314. }
  315. }
  316. unset($v);
  317. unset($v);
  318. $result = ['total' => $list->total(), 'rows' => $list->items()];
  319. return json($result);
  320. }
  321. //选择任务
  322. public function task($type= null)
  323. {
  324. //设置过滤方法
  325. $this->request->filter(['strip_tags', 'trim']);
  326. if (false === $this->request->isAjax()) {
  327. $this->assignConfig('sim_type', $type);
  328. return $this->view->fetch();
  329. }
  330. //如果发送的来源是 Selectpage,则转发到 Selectpage
  331. if ($this->request->request('keyField')) {
  332. return $this->selectpage();
  333. }
  334. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  335. $list = Task::where($where)->where('sim_type',$type)
  336. ->order($sort, $order)
  337. ->paginate($limit);
  338. unset($v);
  339. $result = ['total' => $list->total(), 'rows' => $list->items()];
  340. return json($result);
  341. }
  342. /**
  343. 老师开启考试
  344. */
  345. public function start($ids = null){
  346. // $row = $this->model->get($ids);
  347. // if (!$row) {
  348. // $this->error(__('No Results were found'));
  349. // }
  350. // $params['starttime'] = time();
  351. // $result = $row->allowField(true)->save($params);
  352. $this->success('开始成功');
  353. }
  354. }