Collection.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. if($params['sim_type']=='0002'){
  103. $fault_key1 = rand(0, 1);
  104. $fault_key2 = 4;
  105. $fault_key3 = rand(10,11);
  106. }else{
  107. $fault_key1 = rand(0, 1);
  108. $fault_key2 = rand(2, 3);
  109. $fault_key3 = rand(4, 5);
  110. }
  111. $fault_key = [$fault_key1,$fault_key2,$fault_key3];
  112. }else if($params['question_setting_method']==2){//教师自选
  113. $question_ids = $params['question_ids'];
  114. }else if($params['question_setting_method']==3){ //任务自选
  115. $task_ids = Db::name('task_fault')->where(['task_id'=>['in',$params['task_id']]])->column('fault_id');
  116. }
  117. foreach ($fault_list as $key=> $item1){
  118. $flag = 0;
  119. if(!empty($fault_key) && in_array($key,$fault_key)){
  120. $flag = 1;
  121. }
  122. if(!empty($question_ids) && in_array($item1['fault_id'],explode(',',$question_ids))){
  123. $flag = 1;
  124. }
  125. if(!empty($task_ids) && in_array($item1['fault_id'],$task_ids)){
  126. $flag = 1;
  127. }
  128. $add= [
  129. 'exam_id'=>$examid,
  130. 'fault_id'=>$item1['fault_id'],
  131. 'ref_type'=>2,
  132. 'flag'=>$flag,
  133. 'ref_state'=>0,
  134. 'createtime'=>time(),
  135. 'updatetime'=>time(),
  136. 'create_time'=>date('Y-m-d H:i:s'),
  137. 'update_time'=>date('Y-m-d H:i:s'),
  138. ];
  139. Db::name('real_exam_fault')->insert($add);
  140. }
  141. unset($item1);
  142. }
  143. Db::commit();
  144. } catch (ValidateException|PDOException|Exception $e) {
  145. Db::rollback();
  146. $this->error($e->getMessage());
  147. }
  148. if ($result === false) {
  149. $this->error(__('No rows were inserted'));
  150. }
  151. $this->success();
  152. }
  153. public function edit($ids = null)
  154. {
  155. $row = $this->model->get($ids);
  156. if (!$row) {
  157. $this->error(__('No Results were found'));
  158. }
  159. if (false === $this->request->isPost()) {
  160. // $examing_count = $this->exam_model->where(['exam_collection_id'=>$ids,'exam_status'=>4])->count();
  161. // if($examing_count>0){
  162. // $this->error('有学员考试中,无法修改');
  163. // }
  164. $this->view->assign('row', $row);
  165. $this->assignConfig('row_info', $row);
  166. return $this->view->fetch();
  167. }
  168. $params = $this->request->post('row/a');
  169. if (empty($params)) {
  170. $this->error(__('Parameter %s can not be empty', ''));
  171. }
  172. if(!empty($params['question_setting_method'])){
  173. if($params['question_setting_method']==2 && empty($params['question_ids'])){
  174. $this->error('出题方式为考题自选,请选择考题');
  175. }
  176. if($params['question_setting_method']==3 && empty($params['task_id'])){
  177. $this->error('出题方式为任务自选,请选择任务');
  178. }
  179. }
  180. $params = $this->preExcludeFields($params);
  181. $result = false;
  182. Db::startTrans();
  183. try {
  184. $depart_ids = $params['depart_ids'];
  185. if($params['question_setting_method']==3 && !empty($params['fault_name'])){
  186. $params['question_name'] = $params['fault_name'];
  187. unset($params['fault_name']);
  188. }
  189. $result = $row->allowField(true)->save($params);
  190. Db::name('real_exam_collection_dept')->where(['exam_collection_id'=>$row->exam_collection_id])->delete();
  191. foreach(explode(',',$depart_ids) as $item){
  192. if(!empty($item)){
  193. $arr = [
  194. 'exam_collection_id' => $row->exam_collection_id,
  195. 'dept_id' => $item,
  196. 'update_time' => date('Y-m-d H:i:s'),
  197. ];
  198. Db::name('real_exam_collection_dept')->insert($arr);
  199. }
  200. }
  201. Db::name('real_exam')->where(['exam_collection_id'=>$row->exam_collection_id,'starttime'=>0])->delete();
  202. //匹配的学员自动加入 mx_real_exam 考试表 总分
  203. $admin_list = Db::name('admin')->where('depart_id','in',$depart_ids)->select();
  204. foreach ($admin_list as $it){
  205. $real_exam = Db::name('real_exam')->where(['exam_collection_id'=>$row->exam_collection_id,'user_id'=>$it['id']])->find();
  206. if(empty($real_exam)){
  207. $arr1 = [
  208. 'exam_collection_id' => $row->exam_collection_id,
  209. 'exam_collection_name' => $params['exam_collection_name'],
  210. 'exam_collection_type' => $row['exam_collection_type'],
  211. 'sim_type' => $params['sim_type'],
  212. 'user_username' => $it['username'],
  213. 'user_nickname' => $it['nickname'],
  214. 'user_id' => $it['id'],
  215. 'user_depart_id' => $it['depart_id'],
  216. 'create_time'=>date('Y-m-d H:i:s'),
  217. 'update_time' => date('Y-m-d H:i:s'),
  218. ];
  219. $examid = Db::name('real_exam')->insertGetId($arr1);
  220. //匹配的学员自动加入 mx_real_exam_fault 考试故障关联表
  221. $fault_list = Fault::where(['sim_type' => $params['sim_type'], 'fault_state' => 0,'fault_type'=>3])->select();
  222. if($params['question_setting_method']==1){ //系统随机
  223. // $fault_key = $this->rand_fault($fault_list);
  224. if($params['sim_type']=='0002'){
  225. $fault_key1 = rand(0, 1);
  226. $fault_key2 = 4;
  227. $fault_key3 = rand(10,11);
  228. }else{
  229. $fault_key1 = rand(0, 1);
  230. $fault_key2 = rand(2, 3);
  231. $fault_key3 = rand(4, 5);
  232. }
  233. $fault_key = [$fault_key1,$fault_key2,$fault_key3];
  234. }else if($params['question_setting_method']==2){//教师自选
  235. $question_ids = $params['question_ids'];
  236. }else if($params['question_setting_method']==3){ //任务自选
  237. $task_ids = Db::name('task_fault')->where(['task_id'=>['in',$params['task_id']]])->column('fault_id');
  238. }
  239. Db::name('real_exam_fault')->where(['exam_id'=>$examid])->delete();
  240. foreach ($fault_list as $key=> $item1){
  241. $flag = 0;
  242. if(!empty($fault_key) && in_array($key,$fault_key)){
  243. $flag = 1;
  244. }
  245. if(!empty($question_ids) && in_array($item1['fault_id'],explode(',',$question_ids))){
  246. $flag = 1;
  247. }
  248. if(!empty($task_ids) && in_array($item1['fault_id'],$task_ids)){
  249. $flag = 1;
  250. }
  251. $add= [
  252. 'exam_id'=>$examid,
  253. 'fault_id'=>$item1['fault_id'],
  254. 'ref_type'=>2,
  255. 'flag'=>$flag,
  256. 'ref_state'=>0,
  257. 'createtime'=>time(),
  258. 'updatetime'=>time(),
  259. 'create_time'=>date('Y-m-d H:i:s'),
  260. 'update_time'=>date('Y-m-d H:i:s'),
  261. ];
  262. Db::name('real_exam_fault')->insert($add);
  263. }
  264. unset($item1);
  265. }
  266. }
  267. Db::commit();
  268. } catch (ValidateException|PDOException|Exception $e) {
  269. Db::rollback();
  270. $this->error($e->getMessage());
  271. }
  272. if (false === $result) {
  273. $this->error(__('No rows were updated'));
  274. }
  275. $this->success();
  276. }
  277. public function multi($ids = null)
  278. {
  279. if (false === $this->request->isPost()) {
  280. $this->error(__('Invalid parameters'));
  281. }
  282. $ids = $ids ?: $this->request->post('ids');
  283. if (empty($ids)) {
  284. $this->error(__('Parameter %s can not be empty', 'ids'));
  285. }
  286. if (false === $this->request->has('params')) {
  287. $this->error(__('No rows were updated'));
  288. }
  289. parse_str($this->request->post('params'), $values);
  290. $values = true ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  291. // $values = $this->auth->isSuperAdmin() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  292. if (empty($values)) {
  293. $this->error(__('You have no permission'));
  294. }
  295. $adminIds = $this->getDataLimitAdminIds();
  296. if (is_array($adminIds)) {
  297. $this->model->where($this->dataLimitField, 'in', $adminIds);
  298. }
  299. $count = 0;
  300. if(!empty($values['exam_collection_state'])){
  301. //开始考试集合
  302. if($values['exam_collection_state']==2){
  303. //触发Java端后台的接口数据,
  304. //dev-api/sim/real-exam-collection/teacher/exam/open/{examcollectionId} $ids put
  305. if(Env::get('app.is_fault')){
  306. $url = config('site.url_type').'/sim/real-exam-collection/teacher/exam/open/'.$ids;
  307. $ret = json_decode(send_post($url),true);
  308. if($ret['code']!=200){
  309. $this->error($ret['msg']);
  310. }
  311. }else{
  312. $state_count = $this->model->where(['exam_collection_state'=>$values['exam_collection_state'],'exam_collection_type'=>3])->count();
  313. if($state_count>0){
  314. $this->error('已有启用的考试,请先关闭原来的考试');
  315. }
  316. }
  317. $count = 1;
  318. }
  319. //关闭
  320. if($values['exam_collection_state']==3){
  321. //触发Java端后台的接口数据,
  322. //dev-api/sim/real-exam-collection/teacher/exam/open/{examcollectionId} $ids put
  323. if(Env::get('app.is_fault')){
  324. $url = config('site.url_type').'/sim/real-exam-collection/teacher/exam/close/'.$ids;
  325. $ret = json_decode(send_post($url),true);
  326. if($ret['code']!=200){
  327. $this->error($ret['msg']);
  328. }
  329. }
  330. $count = 1;
  331. }
  332. }
  333. Db::startTrans();
  334. try {
  335. $list = $this->model->where($this->model->getPk(), 'in', $ids)->select();
  336. foreach ($list as $item) {
  337. $count += $item->allowField(true)->isUpdate(true)->save($values);
  338. }
  339. Db::commit();
  340. } catch (PDOException|Exception $e) {
  341. Db::rollback();
  342. $this->error($e->getMessage());
  343. }
  344. if ($count) {
  345. $this->success();
  346. }
  347. $this->error(__('No rows were updated'));
  348. }
  349. //选择考题
  350. public function faults($type = null)
  351. {
  352. //设置过滤方法
  353. $this->request->filter(['strip_tags', 'trim']);
  354. if (false === $this->request->isAjax()) {
  355. $this->assignConfig('sim_type', $type);
  356. return $this->view->fetch();
  357. }
  358. //如果发送的来源是 Selectpage,则转发到 Selectpage
  359. if ($this->request->request('keyField')) {
  360. return $this->selectpage();
  361. }
  362. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  363. $where_fault = [
  364. 'sim_type' => $type,
  365. 'fault_type' => 3,
  366. 'fault_state'=>0
  367. ];
  368. $list = Fault::where($where)->where($where_fault)
  369. ->order($sort, $order)
  370. ->paginate($limit);
  371. foreach ($list as $k => $v){
  372. $exam = Fault::where(['fault_id'=>$v['parent_fault_id'],'fault_type'=>1])->find();
  373. if(isset($exam)){
  374. $v->parent_name = $exam['name'];
  375. }else{
  376. $v->parent_name = '';
  377. }
  378. }
  379. unset($v);
  380. unset($v);
  381. $result = ['total' => $list->total(), 'rows' => $list->items()];
  382. return json($result);
  383. }
  384. //选择任务
  385. public function task($type= null)
  386. {
  387. //设置过滤方法
  388. $this->request->filter(['strip_tags', 'trim']);
  389. if (false === $this->request->isAjax()) {
  390. $this->assignConfig('sim_type', $type);
  391. return $this->view->fetch();
  392. }
  393. //如果发送的来源是 Selectpage,则转发到 Selectpage
  394. if ($this->request->request('keyField')) {
  395. return $this->selectpage();
  396. }
  397. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  398. $list = Task::where($where)->where('sim_type',$type)
  399. ->order($sort, $order)
  400. ->paginate($limit);
  401. unset($v);
  402. $result = ['total' => $list->total(), 'rows' => $list->items()];
  403. return json($result);
  404. }
  405. /**
  406. 老师开启考试
  407. */
  408. public function start($ids = null){
  409. $row = $this->model->get($ids);
  410. if (!$row) {
  411. $this->error(__('No Results were found'));
  412. }
  413. $params['start_time'] = date('Y-m-d H:i:s');
  414. $params['starttime'] = time();
  415. $result = $row->allowField(true)->save($params);
  416. $this->success('开始成功');
  417. }
  418. public function del($ids = null)
  419. {
  420. if (false === $this->request->isPost()) {
  421. $this->error(__("Invalid parameters"));
  422. }
  423. $ids = $ids ?: $this->request->post("ids");
  424. if (empty($ids)) {
  425. $this->error(__('Parameter %s can not be empty', 'ids'));
  426. }
  427. $pk = $this->model->getPk();
  428. $adminIds = $this->getDataLimitAdminIds();
  429. if (is_array($adminIds)) {
  430. $this->model->where($this->dataLimitField, 'in', $adminIds);
  431. }
  432. $list = $this->model->where($pk, 'in', $ids)->select();
  433. $count = 0;
  434. Db::startTrans();
  435. try {
  436. //删除考试集合 mx_real_exam_collection
  437. foreach ($list as $item) {
  438. if($item['yikao_count']>0){
  439. $this->error('已有学生参加考试,无法删除');
  440. }
  441. $count += $item->delete();
  442. }
  443. //删除学员考试 mx_real_exam
  444. $exam_list = $this->exam_model->where('exam_collection_id',$ids)->select();
  445. if(!empty($exam_list)){
  446. foreach ($exam_list as $k=>$it)
  447. {
  448. //删除学员考试故障表 mx_real_exam_fault
  449. Db::name('real_exam_fault')->where('exam_id',$it['exam_id'])->delete();
  450. $it->delete();
  451. }
  452. }
  453. //删除考试关联区队表 mx_real_exam_collection_dept
  454. Db::name('real_exam_collection_dept')->where('exam_collection_id',$ids)->delete();
  455. Db::commit();
  456. } catch (PDOException|Exception $e) {
  457. Db::rollback();
  458. $this->error($e->getMessage());
  459. }
  460. if ($count) {
  461. $this->success();
  462. }
  463. $this->error(__('No rows were deleted'));
  464. }
  465. public function rand_fault($fault_list = null){
  466. // $fault_key = array_rand($fault_list,3);
  467. // $fault_key1 = array_rand($fault_list);
  468. // $fault_key2 = array_rand($fault_list);
  469. // $fault_key3 = array_rand($fault_list);
  470. // if($fault_list[$fault_key1]['parent_fault_id']==$fault_list[$fault_key2]['parent_fault_id']){
  471. // $fault_key2 = array_rand($fault_list);
  472. // }
  473. // if($fault_list[$fault_key2]['parent_fault_id']==$fault_list[$fault_key3]['parent_fault_id']){
  474. // $fault_key3 = array_rand($fault_list);
  475. // }
  476. // $arr = [$fault_key1,$fault_key2,$fault_key3];
  477. $arr = [1,5,7];
  478. return $arr;
  479. }
  480. }