Practice.php 20 KB

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