Practice.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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'];
  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 add()
  44. {
  45. if (false === $this->request->isPost()) {
  46. return $this->view->fetch();
  47. }
  48. $params = $this->request->post('row/a');
  49. if (empty($params)) {
  50. $this->error(__('Parameter %s can not be empty', ''));
  51. }
  52. if(empty($params['depart_ids'])){
  53. $this->error('请选择区队名称');
  54. }
  55. if(!empty($params['question_setting_method'])){
  56. if($params['question_setting_method']==2 && empty($params['question_ids'])){
  57. $this->error('出题方式为考题自选,请选择考题');
  58. }
  59. if($params['question_setting_method']==3 && empty($params['task_id'])){
  60. $this->error('出题方式为任务自选,请选择任务');
  61. }
  62. }
  63. $params = $this->preExcludeFields($params);
  64. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  65. $params[$this->dataLimitField] = $this->auth->id;
  66. }
  67. $result = false;
  68. Db::startTrans();
  69. try {
  70. $depart_ids = $params['depart_ids'];
  71. if($params['question_setting_method']==3 && !empty($params['fault_name'])){
  72. $params['question_name'] = $params['fault_name'];
  73. unset($params['fault_name']);
  74. }
  75. $result = $this->model->allowField(true)->save($params);
  76. foreach(explode(',',$depart_ids) as $item){
  77. if(!empty($item)){
  78. $arr = [
  79. 'exam_collection_id' => $this->model->exam_collection_id,
  80. 'dept_id' => $item,
  81. 'update_time' => date('Y-m-d H:i:s'),
  82. ];
  83. Db::name('real_exam_collection_dept')->insert($arr);
  84. }
  85. }
  86. //匹配的学员自动加入 mx_real_exam 考试表 总分
  87. $admin_list = Db::name('admin')->where('depart_id','in',$depart_ids)->select();
  88. foreach ($admin_list as $it){
  89. $arr1 = [
  90. 'exam_collection_id' => $this->model->exam_collection_id,
  91. 'exam_collection_name' => $params['exam_collection_name'],
  92. 'exam_collection_type' => $params['exam_collection_type'],
  93. 'sim_type' => $params['sim_type'],
  94. 'user_username' => $it['username'],
  95. 'user_nickname' => $it['nickname'],
  96. 'user_id' => $it['id'],
  97. 'user_depart_id' => $it['depart_id'],
  98. 'create_time'=>date('Y-m-d H:i:s'),
  99. 'update_time' => date('Y-m-d H:i:s'),
  100. ];
  101. $examid = Db::name('real_exam')->insertGetId($arr1);
  102. //匹配的学员自动加入 mx_real_exam_fault 考试故障关联表
  103. $fault_list = Fault::where(['sim_type' => $params['sim_type'], 'fault_state' => 0,'fault_type'=>3])->select();
  104. if($params['question_setting_method']==1){ //系统随机
  105. $fault_key1 = rand(0, 1);
  106. $fault_key2 = rand(2, 3);
  107. $fault_key3 = rand(4, 5);
  108. $fault_key = [$fault_key1,$fault_key2,$fault_key3];
  109. // $fault_key = array_rand($fault_list, 3);
  110. // $fault_key = $this->rand_fault($fault_list);
  111. }else if($params['question_setting_method']==2){//教师自选
  112. $question_ids = $params['question_ids'];
  113. }else if($params['question_setting_method']==3){ //任务自选
  114. $task_ids = Db::name('task_fault')->where(['task_id'=>['in',$params['task_id']]])->column('fault_id');
  115. }
  116. foreach ($fault_list as $key=> $item1){
  117. $flag = 0;
  118. if(!empty($fault_key) && in_array($key,$fault_key)){
  119. $flag = 1;
  120. }
  121. if(!empty($question_ids) && in_array($item1['fault_id'],explode(',',$question_ids))){
  122. $flag = 1;
  123. }
  124. if(!empty($task_ids) && in_array($item1['fault_id'],$task_ids)){
  125. $flag = 1;
  126. }
  127. $add= [
  128. 'exam_id'=>$examid,
  129. 'fault_id'=>$item1['fault_id'],
  130. 'ref_type'=>2,
  131. 'flag'=>$flag,
  132. 'ref_state'=>3,
  133. // 'sim_fault_question_value'=>'0000000'.rand(1,2),
  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. $this->view->assign('row', $row);
  161. $this->assignConfig('row_info', $row);
  162. return $this->view->fetch();
  163. }
  164. $params = $this->request->post('row/a');
  165. if (empty($params)) {
  166. $this->error(__('Parameter %s can not be empty', ''));
  167. }
  168. if(!empty($params['question_setting_method'])){
  169. if($params['question_setting_method']==2 && empty($params['question_ids'])){
  170. $this->error('出题方式为考题自选,请选择考题');
  171. }
  172. if($params['question_setting_method']==3 && empty($params['task_id'])){
  173. $this->error('出题方式为任务自选,请选择任务');
  174. }
  175. }
  176. $params = $this->preExcludeFields($params);
  177. $result = false;
  178. Db::startTrans();
  179. try {
  180. $depart_ids = $params['depart_ids'];
  181. if($params['question_setting_method']==3 && !empty($params['fault_name'])){
  182. $params['question_name'] = $params['fault_name'];
  183. unset($params['fault_name']);
  184. }
  185. $result = $row->allowField(true)->save($params);
  186. Db::name('real_exam_collection_dept')->where(['exam_collection_id'=>$row->exam_collection_id])->delete();
  187. foreach(explode(',',$depart_ids) as $item){
  188. if(!empty($item)){
  189. $arr = [
  190. 'exam_collection_id' => $row->exam_collection_id,
  191. 'dept_id' => $item,
  192. 'update_time' => date('Y-m-d H:i:s'),
  193. ];
  194. Db::name('real_exam_collection_dept')->insert($arr);
  195. }
  196. }
  197. Db::name('real_exam')->where(['exam_collection_id'=>$row->exam_collection_id,'starttime'=>0])->delete();
  198. //匹配的学员自动加入 mx_real_exam 考试表 总分
  199. $admin_list = Db::name('admin')->where('depart_id','in',$depart_ids)->select();
  200. foreach ($admin_list as $it){
  201. $real_exam = Db::name('real_exam')->where(['exam_collection_id'=>$row->exam_collection_id,'user_id'=>$it['id']])->find();
  202. if(empty($real_exam)){
  203. $arr1 = [
  204. 'exam_collection_id' => $row->exam_collection_id,
  205. 'exam_collection_name' => $params['exam_collection_name'],
  206. 'exam_collection_type' => $row['exam_collection_type'],
  207. 'sim_type' => $params['sim_type'],
  208. 'user_username' => $it['username'],
  209. 'user_nickname' => $it['nickname'],
  210. 'user_id' => $it['id'],
  211. 'user_depart_id' => $it['depart_id'],
  212. 'create_time'=>date('Y-m-d H:i:s'),
  213. 'update_time' => date('Y-m-d H:i:s'),
  214. ];
  215. $examid = Db::name('real_exam')->insertGetId($arr1);
  216. //匹配的学员自动加入 mx_real_exam_fault 考试故障关联表
  217. $fault_list = Fault::where(['sim_type' => $params['sim_type'], 'fault_state' => 0,'fault_type'=>3])->select();
  218. if($params['question_setting_method']==1){ //系统随机
  219. $fault_key1 = rand(0, 1);
  220. $fault_key2 = rand(2, 3);
  221. $fault_key3 = rand(4, 5);
  222. $fault_key = [$fault_key1,$fault_key2,$fault_key3];
  223. // $fault_key = array_rand($fault_list, 3);
  224. // $fault_key = $this->rand_fault($fault_list);
  225. }else if($params['question_setting_method']==2){//教师自选
  226. $question_ids = $params['question_ids'];
  227. }else if($params['question_setting_method']==3){ //任务自选
  228. $task_ids = Db::name('task_fault')->where(['task_id'=>['in',$params['task_id']]])->column('fault_id');
  229. }
  230. Db::name('real_exam_fault')->where(['exam_id'=>$examid])->delete();
  231. foreach ($fault_list as $key=> $item1){
  232. $flag = 0;
  233. if(!empty($fault_key) && in_array($key,$fault_key)){
  234. $flag = 1;
  235. }
  236. if(!empty($question_ids) && in_array($item1['fault_id'],explode(',',$question_ids))){
  237. $flag = 1;
  238. }
  239. if(!empty($task_ids) && in_array($item1['fault_id'],$task_ids)){
  240. $flag = 1;
  241. }
  242. $add= [
  243. 'exam_id'=>$examid,
  244. 'fault_id'=>$item1['fault_id'],
  245. 'ref_type'=>2,
  246. 'flag'=>$flag,
  247. 'ref_state'=>3,
  248. // 'sim_fault_question_value'=>'0000000'.rand(1,2),
  249. 'createtime'=>time(),
  250. 'updatetime'=>time(),
  251. 'create_time'=>date('Y-m-d H:i:s'),
  252. 'update_time'=>date('Y-m-d H:i:s'),
  253. ];
  254. Db::name('real_exam_fault')->insert($add);
  255. }
  256. unset($item1);
  257. }
  258. }
  259. Db::commit();
  260. } catch (ValidateException|PDOException|Exception $e) {
  261. Db::rollback();
  262. $this->error($e->getMessage());
  263. }
  264. if (false === $result) {
  265. $this->error(__('No rows were updated'));
  266. }
  267. $this->success();
  268. }
  269. public function del($ids = null)
  270. {
  271. if (false === $this->request->isPost()) {
  272. $this->error(__("Invalid parameters"));
  273. }
  274. $ids = $ids ?: $this->request->post("ids");
  275. if (empty($ids)) {
  276. $this->error(__('Parameter %s can not be empty', 'ids'));
  277. }
  278. $pk = $this->model->getPk();
  279. $adminIds = $this->getDataLimitAdminIds();
  280. if (is_array($adminIds)) {
  281. $this->model->where($this->dataLimitField, 'in', $adminIds);
  282. }
  283. $list = $this->model->where($pk, 'in', $ids)->select();
  284. $count = 0;
  285. Db::startTrans();
  286. try {
  287. //删除考试集合 mx_real_exam_collection
  288. foreach ($list as $item) {
  289. if($item['yikao_count']>0){
  290. $this->error('已有学生参加考试,无法删除');
  291. }
  292. $count += $item->delete();
  293. }
  294. //删除学员考试 mx_real_exam
  295. $exam_list = $this->exam_model->where('exam_collection_id',$ids)->select();
  296. if(!empty($exam_list)){
  297. foreach ($exam_list as $k=>$it)
  298. {
  299. //删除学员考试故障表 mx_real_exam_fault
  300. Db::name('real_exam_fault')->where('exam_id',$it['exam_id'])->delete();
  301. $it->delete();
  302. }
  303. }
  304. //删除考试关联区队表 mx_real_exam_collection_dept
  305. Db::name('real_exam_collection_dept')->where('exam_collection_id',$ids)->delete();
  306. Db::commit();
  307. } catch (PDOException|Exception $e) {
  308. Db::rollback();
  309. $this->error($e->getMessage());
  310. }
  311. if ($count) {
  312. $this->success();
  313. }
  314. $this->error(__('No rows were deleted'));
  315. }
  316. public function multi($ids = null)
  317. {
  318. if (false === $this->request->isPost()) {
  319. $this->error(__('Invalid parameters'));
  320. }
  321. $ids = $ids ?: $this->request->post('ids');
  322. if (empty($ids)) {
  323. $this->error(__('Parameter %s can not be empty', 'ids'));
  324. }
  325. if (false === $this->request->has('params')) {
  326. $this->error(__('No rows were updated'));
  327. }
  328. parse_str($this->request->post('params'), $values);
  329. $values = true ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  330. // $values = $this->auth->isSuperAdmin() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  331. if (empty($values)) {
  332. $this->error(__('You have no permission'));
  333. }
  334. $adminIds = $this->getDataLimitAdminIds();
  335. if (is_array($adminIds)) {
  336. $this->model->where($this->dataLimitField, 'in', $adminIds);
  337. }
  338. $count = 0;
  339. if(!empty($values['exam_collection_state'])){
  340. //开始考试集合
  341. if($values['exam_collection_state']==2){
  342. //触发Java端后台的接口数据,
  343. //dev-api/sim/real-exam-collection/teacher/exam/open/{examcollectionId} $ids put
  344. if(Env::get('app.is_fault')){
  345. $url = config('site.url_type').'/sim/real-exam-collection/teacher/exam/open/'.$ids;
  346. $ret = json_decode(send_post($url),true);
  347. if($ret['code']!=200){
  348. $this->error($ret['msg']);
  349. }
  350. }else{
  351. // $state_count = $this->model->where(['exam_collection_state'=>$values['exam_collection_state'],'exam_collection_type'=>1])->count();
  352. // if($state_count>0){
  353. // $this->error('已有启用的考试,请先关闭原来的考试');
  354. // }
  355. }
  356. $count = 1;
  357. }
  358. //关闭
  359. if($values['exam_collection_state']==3){
  360. //触发Java端后台的接口数据,
  361. //dev-api/sim/real-exam-collection/teacher/exam/open/{examcollectionId} $ids put
  362. if(Env::get('app.is_fault')){
  363. $url = config('site.url_type').'/sim/real-exam-collection/teacher/exam/close/'.$ids;
  364. $ret = json_decode(send_post($url),true);
  365. if($ret['code']!=200){
  366. $this->error($ret['msg']);
  367. }
  368. }
  369. $count = 1;
  370. }
  371. }
  372. Db::startTrans();
  373. try {
  374. $list = $this->model->where($this->model->getPk(), 'in', $ids)->select();
  375. foreach ($list as $item) {
  376. $count += $item->allowField(true)->isUpdate(true)->save($values);
  377. }
  378. Db::commit();
  379. } catch (PDOException|Exception $e) {
  380. Db::rollback();
  381. $this->error($e->getMessage());
  382. }
  383. if ($count) {
  384. $this->success();
  385. }
  386. $this->error(__('No rows were updated'));
  387. }
  388. public function persent($ids = null)
  389. {
  390. //设置过滤方法
  391. $this->request->filter(['strip_tags', 'trim']);
  392. if (false === $this->request->isAjax()) {
  393. $this->assignConfig('ids', $ids);
  394. return $this->view->fetch();
  395. }
  396. $exam_ids = $this->exam_model->where('exam_collection_id',$ids)->column('exam_id');
  397. //如果发送的来源是 Selectpage,则转发到 Selectpage
  398. if ($this->request->request('keyField')) {
  399. return $this->selectpage();
  400. }
  401. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  402. $list = Db::name('real_exam_comp_request')
  403. ->where($where)->where('exam_id','in',$exam_ids)
  404. ->order($sort, $order)
  405. ->paginate($limit);
  406. unset($v);
  407. $result = ['total' => $list->total(), 'rows' => $list->items()];
  408. return json($result);
  409. }
  410. public function examing($ids = null)
  411. {
  412. //设置过滤方法
  413. $this->request->filter(['strip_tags', 'trim']);
  414. if (false === $this->request->isAjax()) {
  415. $this->assignConfig('ids', $ids);
  416. return $this->view->fetch();
  417. }
  418. //如果发送的来源是 Selectpage,则转发到 Selectpage
  419. if ($this->request->request('keyField')) {
  420. return $this->selectpage();
  421. }
  422. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  423. $sim_type = Db::name('real_exam_collection')->where('exam_collection_id',$ids)->value('sim_type');
  424. $list = Db::name('seat')->select();
  425. foreach ($list as $key => $value) {
  426. $sim = Db::name('sim')->where('seat_id',$value['seat_id'])->where('sim_type',$sim_type)->find();
  427. if(!empty($sim['sim_state'])){
  428. switch ($sim['sim_state']) {
  429. case '1':
  430. $sim_state = '在线';
  431. break;
  432. case '2':
  433. $sim_state = '离线';
  434. break;
  435. default:
  436. $sim_state = '';
  437. break;
  438. }
  439. }else{
  440. $sim_state = '';
  441. }
  442. $exam = $this->exam_model->where('exam_collection_id',$ids)->order('exam_id desc')->where('seat_id',$value['seat_id'])->find();
  443. $user_username = '';
  444. $user_nickname = '';
  445. $exam_status = '未登录';
  446. $total_score = 0;
  447. $fault_names = '';
  448. $exam_id = 0;
  449. if(!empty($exam)){
  450. $user_username = $exam['user_username']??'';
  451. $user_nickname = $exam['user_nickname']??'';
  452. if($exam['exam_status']==1){
  453. $exam_status= '已登录未开始考试';
  454. }else if($exam['exam_status']==4){
  455. $exam_status= '已开始考试';
  456. }else if($exam['exam_status']==5){
  457. $exam_status= '已交卷';
  458. }
  459. $total_score = $exam['total_score']??'';
  460. $fault_names = $exam['fault_names']??'';
  461. $exam_id = $exam['exam_id']??0;
  462. }
  463. $list[$key]['user_username'] = $user_username;
  464. $list[$key]['user_nickname'] = $user_nickname;
  465. $list[$key]['exam_status'] = $exam_status;
  466. $list[$key]['total_score'] = $total_score;
  467. $list[$key]['fault_names'] = $fault_names;
  468. $list[$key]['exam_id'] = $exam_id;
  469. $list[$key]['sim_state'] = $sim_state;
  470. }
  471. $result = ['total' => count($list), 'rows' => $list];
  472. return json($result);
  473. }
  474. public function score($ids = null)
  475. {
  476. //设置过滤方法
  477. $this->request->filter(['strip_tags', 'trim']);
  478. if (false === $this->request->isAjax()) {
  479. $this->assignConfig('ids', $ids);
  480. return $this->view->fetch();
  481. }
  482. //如果发送的来源是 Selectpage,则转发到 Selectpage
  483. if ($this->request->request('keyField')) {
  484. return $this->selectpage();
  485. }
  486. $exam_ids = $this->exam_model->where('exam_collection_id',$ids)->column('exam_id');
  487. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  488. $list = ExamsScore::where($where)->where('exam_id','in',$exam_ids)
  489. ->order($sort, $order)
  490. ->paginate($limit);
  491. foreach ($list as $k => $v){
  492. $exam = $this->exam_model->where(['exam_id'=>$v['exam_id']])->find();
  493. if($exam){
  494. $v->seat_id = $exam['seat_id'];
  495. $v->user_username = $exam['user_username'];
  496. $v->user_nickname = $exam['user_nickname'];
  497. }
  498. }
  499. unset($v);
  500. $result = ['total' => $list->total(), 'rows' => $list->items()];
  501. return json($result);
  502. }
  503. public function rand_fault($fault_list = null){
  504. // $fault_key = array_rand($fault_list,3);
  505. // $fault_key1 = array_rand($fault_list);
  506. // $fault_key2 = array_rand($fault_list);
  507. // $fault_key3 = array_rand($fault_list);
  508. // if($fault_list[$fault_key1]['parent_fault_id']==$fault_list[$fault_key2]['parent_fault_id']){
  509. // $fault_key2 = array_rand($fault_list);
  510. // }
  511. // if($fault_list[$fault_key2]['parent_fault_id']==$fault_list[$fault_key3]['parent_fault_id']){
  512. // $fault_key3 = array_rand($fault_list);
  513. // }
  514. // $arr = [$fault_key1,$fault_key2,$fault_key3];
  515. $arr = [1,5,7];
  516. return $arr;
  517. }
  518. // public function start($ids = null){
  519. //// $row = $this->model->get($ids);
  520. //// if (!$row) {
  521. //// $this->error(__('No Results were found'));
  522. //// }
  523. //// $params['starttime'] = time();
  524. //// $result = $row->allowField(true)->save($params);
  525. // $this->success('开始成功');
  526. // }
  527. }