Practice.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. if($params['sim_type']=='0002'){
  106. $fault_key1 = rand(0, 1);
  107. $fault_key2 = 4;
  108. $fault_key3 = rand(10,11);
  109. }else{
  110. $fault_key1 = rand(0, 1);
  111. $fault_key2 = rand(2, 3);
  112. $fault_key3 = rand(4, 5);
  113. }
  114. $fault_key = [$fault_key1,$fault_key2,$fault_key3];
  115. }else if($params['question_setting_method']==2){//教师自选
  116. $question_ids = $params['question_ids'];
  117. }else if($params['question_setting_method']==3){ //任务自选
  118. $task_ids = Db::name('task_fault')->where(['task_id'=>['in',$params['task_id']]])->column('fault_id');
  119. }
  120. foreach ($fault_list as $key=> $item1){
  121. $flag = 0;
  122. if(!empty($fault_key) && in_array($key,$fault_key)){
  123. $flag = 1;
  124. }
  125. if(!empty($question_ids) && in_array($item1['fault_id'],explode(',',$question_ids))){
  126. $flag = 1;
  127. }
  128. if(!empty($task_ids) && in_array($item1['fault_id'],$task_ids)){
  129. $flag = 1;
  130. }
  131. $add= [
  132. 'exam_id'=>$examid,
  133. 'fault_id'=>$item1['fault_id'],
  134. 'ref_type'=>2,
  135. 'flag'=>$flag,
  136. 'ref_state'=>3,
  137. // 'sim_fault_question_value'=>'0000000'.rand(1,2),
  138. 'createtime'=>time(),
  139. 'updatetime'=>time(),
  140. 'create_time'=>date('Y-m-d H:i:s'),
  141. 'update_time'=>date('Y-m-d H:i:s'),
  142. ];
  143. Db::name('real_exam_fault')->insert($add);
  144. }
  145. unset($item1);
  146. }
  147. Db::commit();
  148. } catch (ValidateException|PDOException|Exception $e) {
  149. Db::rollback();
  150. $this->error($e->getMessage());
  151. }
  152. if ($result === false) {
  153. $this->error(__('No rows were inserted'));
  154. }
  155. $this->success();
  156. }
  157. public function edit($ids = null)
  158. {
  159. $row = $this->model->get($ids);
  160. if (!$row) {
  161. $this->error(__('No Results were found'));
  162. }
  163. if (false === $this->request->isPost()) {
  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. if($params['sim_type']=='0002'){
  224. $fault_key1 = rand(0, 1);
  225. $fault_key2 = 4;
  226. $fault_key3 = rand(10,11);
  227. }else{
  228. $fault_key1 = rand(0, 1);
  229. $fault_key2 = rand(2, 3);
  230. $fault_key3 = rand(4, 5);
  231. }
  232. $fault_key = [$fault_key1,$fault_key2,$fault_key3];
  233. }else if($params['question_setting_method']==2){//教师自选
  234. $question_ids = $params['question_ids'];
  235. }else if($params['question_setting_method']==3){ //任务自选
  236. $task_ids = Db::name('task_fault')->where(['task_id'=>['in',$params['task_id']]])->column('fault_id');
  237. }
  238. Db::name('real_exam_fault')->where(['exam_id'=>$examid])->delete();
  239. foreach ($fault_list as $key=> $item1){
  240. $flag = 0;
  241. if(!empty($fault_key) && in_array($key,$fault_key)){
  242. $flag = 1;
  243. }
  244. if(!empty($question_ids) && in_array($item1['fault_id'],explode(',',$question_ids))){
  245. $flag = 1;
  246. }
  247. if(!empty($task_ids) && in_array($item1['fault_id'],$task_ids)){
  248. $flag = 1;
  249. }
  250. $add= [
  251. 'exam_id'=>$examid,
  252. 'fault_id'=>$item1['fault_id'],
  253. 'ref_type'=>2,
  254. 'flag'=>$flag,
  255. 'ref_state'=>3,
  256. // 'sim_fault_question_value'=>'0000000'.rand(1,2),
  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 del($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. $pk = $this->model->getPk();
  287. $adminIds = $this->getDataLimitAdminIds();
  288. if (is_array($adminIds)) {
  289. $this->model->where($this->dataLimitField, 'in', $adminIds);
  290. }
  291. $list = $this->model->where($pk, 'in', $ids)->select();
  292. $count = 0;
  293. Db::startTrans();
  294. try {
  295. //删除考试集合 mx_real_exam_collection
  296. foreach ($list as $item) {
  297. if($item['yikao_count']>0){
  298. $this->error('已有学生参加考试,无法删除');
  299. }
  300. $count += $item->delete();
  301. }
  302. //删除学员考试 mx_real_exam
  303. $exam_list = $this->exam_model->where('exam_collection_id',$ids)->select();
  304. if(!empty($exam_list)){
  305. foreach ($exam_list as $k=>$it)
  306. {
  307. //删除学员考试故障表 mx_real_exam_fault
  308. Db::name('real_exam_fault')->where('exam_id',$it['exam_id'])->delete();
  309. $it->delete();
  310. }
  311. }
  312. //删除考试关联区队表 mx_real_exam_collection_dept
  313. Db::name('real_exam_collection_dept')->where('exam_collection_id',$ids)->delete();
  314. Db::commit();
  315. } catch (PDOException|Exception $e) {
  316. Db::rollback();
  317. $this->error($e->getMessage());
  318. }
  319. if ($count) {
  320. $this->success();
  321. }
  322. $this->error(__('No rows were deleted'));
  323. }
  324. public function multi($ids = null)
  325. {
  326. if (false === $this->request->isPost()) {
  327. $this->error(__('Invalid parameters'));
  328. }
  329. $ids = $ids ?: $this->request->post('ids');
  330. if (empty($ids)) {
  331. $this->error(__('Parameter %s can not be empty', 'ids'));
  332. }
  333. if (false === $this->request->has('params')) {
  334. $this->error(__('No rows were updated'));
  335. }
  336. parse_str($this->request->post('params'), $values);
  337. $values = true ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  338. // $values = $this->auth->isSuperAdmin() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  339. if (empty($values)) {
  340. $this->error(__('You have no permission'));
  341. }
  342. $adminIds = $this->getDataLimitAdminIds();
  343. if (is_array($adminIds)) {
  344. $this->model->where($this->dataLimitField, 'in', $adminIds);
  345. }
  346. $count = 0;
  347. if(!empty($values['exam_collection_state'])){
  348. //开始考试集合
  349. if($values['exam_collection_state']==2){
  350. //触发Java端后台的接口数据,
  351. //dev-api/sim/real-exam-collection/teacher/exam/open/{examcollectionId} $ids put
  352. if(Env::get('app.is_fault')){
  353. $url = config('site.url_type').'/sim/real-exam-collection/teacher/exam/open/'.$ids;
  354. $ret = json_decode(send_post($url),true);
  355. if($ret['code']!=200){
  356. $this->error($ret['msg']);
  357. }
  358. }else{
  359. // $state_count = $this->model->where(['exam_collection_state'=>$values['exam_collection_state'],'exam_collection_type'=>1])->count();
  360. // if($state_count>0){
  361. // $this->error('已有启用的考试,请先关闭原来的考试');
  362. // }
  363. }
  364. $count = 1;
  365. }
  366. //关闭
  367. if($values['exam_collection_state']==3){
  368. //触发Java端后台的接口数据,
  369. //dev-api/sim/real-exam-collection/teacher/exam/open/{examcollectionId} $ids put
  370. if(Env::get('app.is_fault')){
  371. $url = config('site.url_type').'/sim/real-exam-collection/teacher/exam/close/'.$ids;
  372. $ret = json_decode(send_post($url),true);
  373. if($ret['code']!=200){
  374. $this->error($ret['msg']);
  375. }
  376. }
  377. $count = 1;
  378. }
  379. }
  380. Db::startTrans();
  381. try {
  382. $list = $this->model->where($this->model->getPk(), 'in', $ids)->select();
  383. foreach ($list as $item) {
  384. $count += $item->allowField(true)->isUpdate(true)->save($values);
  385. }
  386. Db::commit();
  387. } catch (PDOException|Exception $e) {
  388. Db::rollback();
  389. $this->error($e->getMessage());
  390. }
  391. if ($count) {
  392. $this->success();
  393. }
  394. $this->error(__('No rows were updated'));
  395. }
  396. public function persent($ids = null)
  397. {
  398. //设置过滤方法
  399. $this->request->filter(['strip_tags', 'trim']);
  400. if (false === $this->request->isAjax()) {
  401. $this->assignConfig('ids', $ids);
  402. return $this->view->fetch();
  403. }
  404. $exam_ids = $this->exam_model->where('exam_collection_id',$ids)->column('exam_id');
  405. //如果发送的来源是 Selectpage,则转发到 Selectpage
  406. if ($this->request->request('keyField')) {
  407. return $this->selectpage();
  408. }
  409. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  410. $list = Db::name('real_exam_comp_request')
  411. ->where($where)->where('exam_id','in',$exam_ids)
  412. ->order($sort, $order)
  413. ->paginate($limit);
  414. unset($v);
  415. $result = ['total' => $list->total(), 'rows' => $list->items()];
  416. return json($result);
  417. }
  418. public function examing($ids = null)
  419. {
  420. //设置过滤方法
  421. $this->request->filter(['strip_tags', 'trim']);
  422. if (false === $this->request->isAjax()) {
  423. $this->assignConfig('ids', $ids);
  424. return $this->view->fetch();
  425. }
  426. //如果发送的来源是 Selectpage,则转发到 Selectpage
  427. if ($this->request->request('keyField')) {
  428. return $this->selectpage();
  429. }
  430. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  431. $sim_type = Db::name('real_exam_collection')->where('exam_collection_id',$ids)->value('sim_type');
  432. $list = Db::name('seat')->select();
  433. foreach ($list as $key => $value) {
  434. $sim = Db::name('sim')->where('seat_id',$value['seat_id'])->where('sim_type',$sim_type)->find();
  435. if(!empty($sim['sim_state'])){
  436. switch ($sim['sim_state']) {
  437. case '1':
  438. $sim_state = '在线';
  439. break;
  440. case '2':
  441. $sim_state = '离线';
  442. break;
  443. default:
  444. $sim_state = '';
  445. break;
  446. }
  447. }else{
  448. $sim_state = '';
  449. }
  450. $exam = $this->exam_model->where('exam_collection_id',$ids)->order('exam_id desc')->where('seat_id',$value['seat_id'])->find();
  451. $user_username = '';
  452. $user_nickname = '';
  453. $exam_status = '未登录';
  454. $total_score = 0;
  455. $fault_names = '';
  456. $exam_id = 0;
  457. if(!empty($exam)){
  458. $user_username = $exam['user_username']??'';
  459. $user_nickname = $exam['user_nickname']??'';
  460. if($exam['exam_status']==1){
  461. $exam_status= '已登录未开始考试';
  462. }else if($exam['exam_status']==4){
  463. $exam_status= '已开始考试';
  464. }else if($exam['exam_status']==5){
  465. $exam_status= '已交卷';
  466. }
  467. $total_score = $exam['total_score']??'';
  468. $fault_names = $exam['fault_names']??'';
  469. $exam_id = $exam['exam_id']??0;
  470. }
  471. $list[$key]['user_username'] = $user_username;
  472. $list[$key]['user_nickname'] = $user_nickname;
  473. $list[$key]['exam_status'] = $exam_status;
  474. $list[$key]['total_score'] = $total_score;
  475. $list[$key]['fault_names'] = $fault_names;
  476. $list[$key]['exam_id'] = $exam_id;
  477. $list[$key]['sim_state'] = $sim_state;
  478. }
  479. $result = ['total' => count($list), 'rows' => $list];
  480. return json($result);
  481. }
  482. public function score($ids = null)
  483. {
  484. //设置过滤方法
  485. $this->request->filter(['strip_tags', 'trim']);
  486. if (false === $this->request->isAjax()) {
  487. $this->assignConfig('ids', $ids);
  488. return $this->view->fetch();
  489. }
  490. //如果发送的来源是 Selectpage,则转发到 Selectpage
  491. if ($this->request->request('keyField')) {
  492. return $this->selectpage();
  493. }
  494. $exam_ids = $this->exam_model->where('exam_collection_id',$ids)->column('exam_id');
  495. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  496. $list = ExamsScore::where($where)->where('exam_id','in',$exam_ids)
  497. ->order($sort, $order)
  498. ->paginate($limit);
  499. foreach ($list as $k => $v){
  500. $exam = $this->exam_model->where(['exam_id'=>$v['exam_id']])->find();
  501. if($exam){
  502. $v->seat_id = $exam['seat_id'];
  503. $v->user_username = $exam['user_username'];
  504. $v->user_nickname = $exam['user_nickname'];
  505. }
  506. }
  507. unset($v);
  508. $result = ['total' => $list->total(), 'rows' => $list->items()];
  509. return json($result);
  510. }
  511. public function rand_fault($fault_list = null){
  512. // $fault_key = array_rand($fault_list,3);
  513. // $fault_key1 = array_rand($fault_list);
  514. // $fault_key2 = array_rand($fault_list);
  515. // $fault_key3 = array_rand($fault_list);
  516. // if($fault_list[$fault_key1]['parent_fault_id']==$fault_list[$fault_key2]['parent_fault_id']){
  517. // $fault_key2 = array_rand($fault_list);
  518. // }
  519. // if($fault_list[$fault_key2]['parent_fault_id']==$fault_list[$fault_key3]['parent_fault_id']){
  520. // $fault_key3 = array_rand($fault_list);
  521. // }
  522. // $arr = [$fault_key1,$fault_key2,$fault_key3];
  523. $arr = [1,5,7];
  524. return $arr;
  525. }
  526. // public function start($ids = null){
  527. //// $row = $this->model->get($ids);
  528. //// if (!$row) {
  529. //// $this->error(__('No Results were found'));
  530. //// }
  531. //// $params['starttime'] = time();
  532. //// $result = $row->allowField(true)->save($params);
  533. // $this->success('开始成功');
  534. // }
  535. }