Practice.php 20 KB

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