Practice.php 21 KB

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