Practice.php 20 KB

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