Practice.php 19 KB

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