Collection.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. use think\Env;
  12. /**
  13. * sim-考试集合管理
  14. *
  15. * @icon fa fa-circle-o
  16. */
  17. class Collection extends Backend
  18. {
  19. /**
  20. * Collection模型对象
  21. * @var \app\admin\model\teacher\Collection
  22. */
  23. protected $model = null;
  24. protected $exam_model = null;
  25. protected $whereExtend = null;
  26. public function _initialize()
  27. {
  28. parent::_initialize();
  29. $this->model = new \app\admin\model\teacher\Collection;
  30. $this->exam_model = new \app\admin\model\teacher\Exams;
  31. $this->assignConfig('sim_sim_type', ConfigModel::getSimTypeList());
  32. $this->assign('sim_sim_type', ConfigModel::getSimTypeList());
  33. $this->assignConfig('sim_question_setting_method', ConfigModel::getSimQuestionList());
  34. $this->assign('sim_question_setting_method', ConfigModel::getSimQuestionList());
  35. $this->whereExtend['exam_collection_type'] = 3;
  36. }
  37. /**
  38. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  39. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  40. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  41. */
  42. public function add()
  43. {
  44. if (false === $this->request->isPost()) {
  45. return $this->view->fetch();
  46. }
  47. $params = $this->request->post('row/a');
  48. if (empty($params)) {
  49. $this->error(__('Parameter %s can not be empty', ''));
  50. }
  51. if(empty($params['depart_ids'])){
  52. $this->error('请选择区队名称');
  53. }
  54. if(!empty($params['question_setting_method'])){
  55. if($params['question_setting_method']==2 && empty($params['question_ids'])){
  56. $this->error('出题方式为考题自选,请选择考题');
  57. }
  58. if($params['question_setting_method']==3 && empty($params['task_id'])){
  59. $this->error('出题方式为任务自选,请选择任务');
  60. }
  61. }
  62. $params = $this->preExcludeFields($params);
  63. $result = false;
  64. Db::startTrans();
  65. try {
  66. $depart_ids = $params['depart_ids'];
  67. if($params['question_setting_method']==3 && !empty($params['fault_name'])){
  68. $params['question_name'] = $params['fault_name'];
  69. unset($params['fault_name']);
  70. }
  71. $result = $this->model->allowField(true)->save($params);
  72. foreach(explode(',',$depart_ids) as $item){
  73. if(!empty($item)){
  74. $arr = [
  75. 'exam_collection_id' => $this->model->exam_collection_id,
  76. 'dept_id' => $item,
  77. 'update_time' => date('Y-m-d H:i:s'),
  78. ];
  79. Db::name('real_exam_collection_dept')->insert($arr);
  80. }
  81. }
  82. //匹配的学员自动加入 mx_real_exam 考试表 总分
  83. $admin_list = Db::name('admin')->where('depart_id','in',$depart_ids)->select();
  84. foreach ($admin_list as $it){
  85. $arr1 = [
  86. 'exam_collection_id' => $this->model->exam_collection_id,
  87. 'exam_collection_name' => $params['exam_collection_name'],
  88. 'exam_collection_type' => $params['exam_collection_type'],
  89. 'sim_type' => $params['sim_type'],
  90. 'user_username' => $it['username'],
  91. 'user_nickname' => $it['nickname'],
  92. 'user_id' => $it['id'],
  93. 'user_depart_id' => $it['depart_id'],
  94. 'create_time'=>date('Y-m-d H:i:s'),
  95. 'update_time' => date('Y-m-d H:i:s'),
  96. ];
  97. $examid = Db::name('real_exam')->insertGetId($arr1);
  98. //匹配的学员自动加入 mx_real_exam_fault 考试故障关联表
  99. $fault_list = Fault::where(['sim_type' => $params['sim_type'], 'fault_state' => 0,'fault_type'=>3])->select();
  100. if($params['question_setting_method']==1){ //系统随机
  101. // $fault_key = $this->rand_fault($fault_list);
  102. $fault_key1 = rand(0, 1);
  103. $fault_key2 = rand(2, 3);
  104. $fault_key3 = rand(4, 5);
  105. $fault_key = [$fault_key1,$fault_key2,$fault_key3];
  106. }else if($params['question_setting_method']==2){//教师自选
  107. $question_ids = $params['question_ids'];
  108. }else if($params['question_setting_method']==3){ //任务自选
  109. $task_ids = Db::name('task_fault')->where(['task_id'=>['in',$params['task_id']]])->column('fault_id');
  110. }
  111. foreach ($fault_list as $key=> $item1){
  112. $flag = 0;
  113. if(!empty($fault_key) && in_array($key,$fault_key)){
  114. $flag = 1;
  115. }
  116. if(!empty($question_ids) && in_array($item1['fault_id'],explode(',',$question_ids))){
  117. $flag = 1;
  118. }
  119. if(!empty($task_ids) && in_array($item1['fault_id'],$task_ids)){
  120. $flag = 1;
  121. }
  122. $add= [
  123. 'exam_id'=>$examid,
  124. 'fault_id'=>$item1['fault_id'],
  125. 'ref_type'=>2,
  126. 'flag'=>$flag,
  127. 'ref_state'=>0,
  128. 'createtime'=>time(),
  129. 'updatetime'=>time(),
  130. 'create_time'=>date('Y-m-d H:i:s'),
  131. 'update_time'=>date('Y-m-d H:i:s'),
  132. ];
  133. Db::name('real_exam_fault')->insert($add);
  134. }
  135. unset($item1);
  136. }
  137. Db::commit();
  138. } catch (ValidateException|PDOException|Exception $e) {
  139. Db::rollback();
  140. $this->error($e->getMessage());
  141. }
  142. if ($result === false) {
  143. $this->error(__('No rows were inserted'));
  144. }
  145. $this->success();
  146. }
  147. public function edit($ids = null)
  148. {
  149. $row = $this->model->get($ids);
  150. if (!$row) {
  151. $this->error(__('No Results were found'));
  152. }
  153. if (false === $this->request->isPost()) {
  154. // $examing_count = $this->exam_model->where(['exam_collection_id'=>$ids,'exam_status'=>4])->count();
  155. // if($examing_count>0){
  156. // $this->error('有学员考试中,无法修改');
  157. // }
  158. $this->view->assign('row', $row);
  159. $this->assignConfig('row_info', $row);
  160. return $this->view->fetch();
  161. }
  162. $params = $this->request->post('row/a');
  163. if (empty($params)) {
  164. $this->error(__('Parameter %s can not be empty', ''));
  165. }
  166. if(!empty($params['question_setting_method'])){
  167. if($params['question_setting_method']==2 && empty($params['question_ids'])){
  168. $this->error('出题方式为考题自选,请选择考题');
  169. }
  170. if($params['question_setting_method']==3 && empty($params['task_id'])){
  171. $this->error('出题方式为任务自选,请选择任务');
  172. }
  173. }
  174. $params = $this->preExcludeFields($params);
  175. $result = false;
  176. Db::startTrans();
  177. try {
  178. $depart_ids = $params['depart_ids'];
  179. if($params['question_setting_method']==3 && !empty($params['fault_name'])){
  180. $params['question_name'] = $params['fault_name'];
  181. unset($params['fault_name']);
  182. }
  183. $result = $row->allowField(true)->save($params);
  184. Db::name('real_exam_collection_dept')->where(['exam_collection_id'=>$row->exam_collection_id])->delete();
  185. foreach(explode(',',$depart_ids) as $item){
  186. if(!empty($item)){
  187. $arr = [
  188. 'exam_collection_id' => $row->exam_collection_id,
  189. 'dept_id' => $item,
  190. 'update_time' => date('Y-m-d H:i:s'),
  191. ];
  192. Db::name('real_exam_collection_dept')->insert($arr);
  193. }
  194. }
  195. Db::name('real_exam')->where(['exam_collection_id'=>$row->exam_collection_id,'starttime'=>0])->delete();
  196. //匹配的学员自动加入 mx_real_exam 考试表 总分
  197. $admin_list = Db::name('admin')->where('depart_id','in',$depart_ids)->select();
  198. foreach ($admin_list as $it){
  199. $real_exam = Db::name('real_exam')->where(['exam_collection_id'=>$row->exam_collection_id,'user_id'=>$it['id']])->find();
  200. if(empty($real_exam)){
  201. $arr1 = [
  202. 'exam_collection_id' => $row->exam_collection_id,
  203. 'exam_collection_name' => $params['exam_collection_name'],
  204. 'exam_collection_type' => $row['exam_collection_type'],
  205. 'sim_type' => $params['sim_type'],
  206. 'user_username' => $it['username'],
  207. 'user_nickname' => $it['nickname'],
  208. 'user_id' => $it['id'],
  209. 'user_depart_id' => $it['depart_id'],
  210. 'create_time'=>date('Y-m-d H:i:s'),
  211. 'update_time' => date('Y-m-d H:i:s'),
  212. ];
  213. $examid = Db::name('real_exam')->insertGetId($arr1);
  214. //匹配的学员自动加入 mx_real_exam_fault 考试故障关联表
  215. $fault_list = Fault::where(['sim_type' => $params['sim_type'], 'fault_state' => 0,'fault_type'=>3])->select();
  216. if($params['question_setting_method']==1){ //系统随机
  217. // $fault_key = $this->rand_fault($fault_list);
  218. $fault_key1 = rand(0, 1);
  219. $fault_key2 = rand(2, 3);
  220. $fault_key3 = rand(4, 5);
  221. $fault_key = [$fault_key1,$fault_key2,$fault_key3];
  222. }else if($params['question_setting_method']==2){//教师自选
  223. $question_ids = $params['question_ids'];
  224. }else if($params['question_setting_method']==3){ //任务自选
  225. $task_ids = Db::name('task_fault')->where(['task_id'=>['in',$params['task_id']]])->column('fault_id');
  226. }
  227. Db::name('real_exam_fault')->where(['exam_id'=>$examid])->delete();
  228. foreach ($fault_list as $key=> $item1){
  229. $flag = 0;
  230. if(!empty($fault_key) && in_array($key,$fault_key)){
  231. $flag = 1;
  232. }
  233. if(!empty($question_ids) && in_array($item1['fault_id'],explode(',',$question_ids))){
  234. $flag = 1;
  235. }
  236. if(!empty($task_ids) && in_array($item1['fault_id'],$task_ids)){
  237. $flag = 1;
  238. }
  239. $add= [
  240. 'exam_id'=>$examid,
  241. 'fault_id'=>$item1['fault_id'],
  242. 'ref_type'=>2,
  243. 'flag'=>$flag,
  244. 'ref_state'=>0,
  245. 'createtime'=>time(),
  246. 'updatetime'=>time(),
  247. 'create_time'=>date('Y-m-d H:i:s'),
  248. 'update_time'=>date('Y-m-d H:i:s'),
  249. ];
  250. Db::name('real_exam_fault')->insert($add);
  251. }
  252. unset($item1);
  253. }
  254. }
  255. Db::commit();
  256. } catch (ValidateException|PDOException|Exception $e) {
  257. Db::rollback();
  258. $this->error($e->getMessage());
  259. }
  260. if (false === $result) {
  261. $this->error(__('No rows were updated'));
  262. }
  263. $this->success();
  264. }
  265. public function multi($ids = null)
  266. {
  267. if (false === $this->request->isPost()) {
  268. $this->error(__('Invalid parameters'));
  269. }
  270. $ids = $ids ?: $this->request->post('ids');
  271. if (empty($ids)) {
  272. $this->error(__('Parameter %s can not be empty', 'ids'));
  273. }
  274. if (false === $this->request->has('params')) {
  275. $this->error(__('No rows were updated'));
  276. }
  277. parse_str($this->request->post('params'), $values);
  278. $values = true ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  279. // $values = $this->auth->isSuperAdmin() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  280. if (empty($values)) {
  281. $this->error(__('You have no permission'));
  282. }
  283. $adminIds = $this->getDataLimitAdminIds();
  284. if (is_array($adminIds)) {
  285. $this->model->where($this->dataLimitField, 'in', $adminIds);
  286. }
  287. $count = 0;
  288. if(!empty($values['exam_collection_state'])){
  289. //开始考试集合
  290. if($values['exam_collection_state']==2){
  291. //触发Java端后台的接口数据,
  292. //dev-api/sim/real-exam-collection/teacher/exam/open/{examcollectionId} $ids put
  293. if(Env::get('app.is_fault')){
  294. $url = config('site.url_type').'/sim/real-exam-collection/teacher/exam/open/'.$ids;
  295. $ret = json_decode(send_post($url),true);
  296. if($ret['code']!=200){
  297. $this->error($ret['msg']);
  298. }
  299. }else{
  300. $state_count = $this->model->where(['exam_collection_state'=>$values['exam_collection_state'],'exam_collection_type'=>3])->count();
  301. if($state_count>0){
  302. $this->error('已有启用的考试,请先关闭原来的考试');
  303. }
  304. }
  305. $count = 1;
  306. }
  307. //关闭
  308. if($values['exam_collection_state']==3){
  309. //触发Java端后台的接口数据,
  310. //dev-api/sim/real-exam-collection/teacher/exam/open/{examcollectionId} $ids put
  311. if(Env::get('app.is_fault')){
  312. $url = config('site.url_type').'/sim/real-exam-collection/teacher/exam/close/'.$ids;
  313. $ret = json_decode(send_post($url),true);
  314. if($ret['code']!=200){
  315. $this->error($ret['msg']);
  316. }
  317. }
  318. $count = 1;
  319. }
  320. }
  321. Db::startTrans();
  322. try {
  323. $list = $this->model->where($this->model->getPk(), 'in', $ids)->select();
  324. foreach ($list as $item) {
  325. $count += $item->allowField(true)->isUpdate(true)->save($values);
  326. }
  327. Db::commit();
  328. } catch (PDOException|Exception $e) {
  329. Db::rollback();
  330. $this->error($e->getMessage());
  331. }
  332. if ($count) {
  333. $this->success();
  334. }
  335. $this->error(__('No rows were updated'));
  336. }
  337. //选择考题
  338. public function faults($type = null)
  339. {
  340. //设置过滤方法
  341. $this->request->filter(['strip_tags', 'trim']);
  342. if (false === $this->request->isAjax()) {
  343. $this->assignConfig('sim_type', $type);
  344. return $this->view->fetch();
  345. }
  346. //如果发送的来源是 Selectpage,则转发到 Selectpage
  347. if ($this->request->request('keyField')) {
  348. return $this->selectpage();
  349. }
  350. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  351. $where_fault = [
  352. 'sim_type' => $type,
  353. 'fault_type' => 3,
  354. 'fault_state'=>0
  355. ];
  356. $list = Fault::where($where)->where($where_fault)
  357. ->order($sort, $order)
  358. ->paginate($limit);
  359. foreach ($list as $k => $v){
  360. $exam = Fault::where(['fault_id'=>$v['parent_fault_id'],'fault_type'=>1])->find();
  361. if(isset($exam)){
  362. $v->parent_name = $exam['name'];
  363. }else{
  364. $v->parent_name = '';
  365. }
  366. }
  367. unset($v);
  368. unset($v);
  369. $result = ['total' => $list->total(), 'rows' => $list->items()];
  370. return json($result);
  371. }
  372. //选择任务
  373. public function task($type= null)
  374. {
  375. //设置过滤方法
  376. $this->request->filter(['strip_tags', 'trim']);
  377. if (false === $this->request->isAjax()) {
  378. $this->assignConfig('sim_type', $type);
  379. return $this->view->fetch();
  380. }
  381. //如果发送的来源是 Selectpage,则转发到 Selectpage
  382. if ($this->request->request('keyField')) {
  383. return $this->selectpage();
  384. }
  385. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  386. $list = Task::where($where)->where('sim_type',$type)
  387. ->order($sort, $order)
  388. ->paginate($limit);
  389. unset($v);
  390. $result = ['total' => $list->total(), 'rows' => $list->items()];
  391. return json($result);
  392. }
  393. /**
  394. 老师开启考试
  395. */
  396. public function start($ids = null){
  397. $row = $this->model->get($ids);
  398. if (!$row) {
  399. $this->error(__('No Results were found'));
  400. }
  401. $params['start_time'] = date('Y-m-d H:i:s');
  402. $params['starttime'] = time();
  403. $result = $row->allowField(true)->save($params);
  404. $this->success('开始成功');
  405. }
  406. public function del($ids = null)
  407. {
  408. if (false === $this->request->isPost()) {
  409. $this->error(__("Invalid parameters"));
  410. }
  411. $ids = $ids ?: $this->request->post("ids");
  412. if (empty($ids)) {
  413. $this->error(__('Parameter %s can not be empty', 'ids'));
  414. }
  415. $pk = $this->model->getPk();
  416. $adminIds = $this->getDataLimitAdminIds();
  417. if (is_array($adminIds)) {
  418. $this->model->where($this->dataLimitField, 'in', $adminIds);
  419. }
  420. $list = $this->model->where($pk, 'in', $ids)->select();
  421. $count = 0;
  422. Db::startTrans();
  423. try {
  424. //删除考试集合 mx_real_exam_collection
  425. foreach ($list as $item) {
  426. if($item['yikao_count']>0){
  427. $this->error('已有学生参加考试,无法删除');
  428. }
  429. $count += $item->delete();
  430. }
  431. //删除学员考试 mx_real_exam
  432. $exam_list = $this->exam_model->where('exam_collection_id',$ids)->select();
  433. if(!empty($exam_list)){
  434. foreach ($exam_list as $k=>$it)
  435. {
  436. //删除学员考试故障表 mx_real_exam_fault
  437. Db::name('real_exam_fault')->where('exam_id',$it['exam_id'])->delete();
  438. $it->delete();
  439. }
  440. }
  441. //删除考试关联区队表 mx_real_exam_collection_dept
  442. Db::name('real_exam_collection_dept')->where('exam_collection_id',$ids)->delete();
  443. Db::commit();
  444. } catch (PDOException|Exception $e) {
  445. Db::rollback();
  446. $this->error($e->getMessage());
  447. }
  448. if ($count) {
  449. $this->success();
  450. }
  451. $this->error(__('No rows were deleted'));
  452. }
  453. public function rand_fault($fault_list = null){
  454. // $fault_key = array_rand($fault_list,3);
  455. // $fault_key1 = array_rand($fault_list);
  456. // $fault_key2 = array_rand($fault_list);
  457. // $fault_key3 = array_rand($fault_list);
  458. // if($fault_list[$fault_key1]['parent_fault_id']==$fault_list[$fault_key2]['parent_fault_id']){
  459. // $fault_key2 = array_rand($fault_list);
  460. // }
  461. // if($fault_list[$fault_key2]['parent_fault_id']==$fault_list[$fault_key3]['parent_fault_id']){
  462. // $fault_key3 = array_rand($fault_list);
  463. // }
  464. // $arr = [$fault_key1,$fault_key2,$fault_key3];
  465. $arr = [1,5,7];
  466. return $arr;
  467. }
  468. }