Collection.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php
  2. namespace app\admin\controller\student;
  3. use app\common\controller\Backend;
  4. use app\common\model\Config as ConfigModel;
  5. use app\admin\model\Report;
  6. use app\admin\model\Fault;
  7. use think\exception\DbException;
  8. use think\exception\PDOException;
  9. use think\exception\ValidateException;
  10. use think\response\Json;
  11. use think\Db;
  12. /**
  13. * sim-考试集合管理
  14. *
  15. * @icon fa fa-circle-o
  16. */
  17. class Collection extends Backend
  18. {
  19. /**
  20. * Collection模型对象
  21. * @var \app\admin\model\teacher\Collection
  22. */
  23. protected $model = null;
  24. protected $exam_model = null;
  25. protected $whereExtend = null;
  26. public function _initialize()
  27. {
  28. parent::_initialize();
  29. $this->model = new \app\admin\model\teacher\Collection;
  30. $this->exam_model = new \app\admin\model\teacher\Exams;
  31. $this->assignConfig('sim_sim_type', ConfigModel::getSimTypeList());
  32. $this->assignConfig('sim_question_setting_method', ConfigModel::getSimQuestionList());
  33. $this->whereExtend['exam_collection_type'] = 3;
  34. $this->whereExtend['exam_collection_state'] = 2;
  35. $this->assignConfig('sim_sim_type', ConfigModel::getSimTypeList());
  36. $groupIds = $this->auth->getGroupIds();
  37. //学员查看自己区队的考试集合
  38. if(in_array(8, $groupIds)){
  39. $departid = $this->auth->depart_id;
  40. $this->whereExtend[] = ['exp',Db::raw("FIND_IN_SET($departid,depart_ids)")];
  41. }
  42. }
  43. /**
  44. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  45. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  46. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  47. */
  48. /**
  49. * 查看
  50. *
  51. * @return string|Json
  52. * @throws \think\Exception
  53. * @throws DbException
  54. */
  55. public function index()
  56. {
  57. $isset = $this->model->alias('z')
  58. ->join('real_exam r','z.exam_collection_id=r.exam_collection_id','left')
  59. ->where('z.exam_collection_type=3 and z.exam_collection_state=2 and r.user_id='.$this->auth->id)->find();
  60. if(empty($isset)){
  61. //提示页面,没有开始的考试
  62. return $this->view->fetch('tishi');
  63. }else{
  64. // halt('22222');
  65. $this->redirect('/ZQOtIMLKud.php/student/collection/examing/ids/'.$isset['exam_id'].'?ref=addtabs');
  66. // echo "<script>location.href='/lAoJtOcmYN.php/hotelmanage/room/displayindex/ids/".$this->auth->street_id."';</script>";
  67. // //$this->redirect('/hotelmanage/room/displayindex/ids/1');
  68. // exit();
  69. }
  70. die();
  71. //设置过滤方法
  72. $this->request->filter(['strip_tags', 'trim']);
  73. if (false === $this->request->isAjax()) {
  74. return $this->view->fetch();
  75. }
  76. //如果发送的来源是 Selectpage,则转发到 Selectpage
  77. if ($this->request->request('keyField')) {
  78. return $this->selectpage();
  79. }
  80. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  81. $list = $this->model
  82. ->where($where)->where($this->whereExtend)
  83. ->order($sort, $order)
  84. ->paginate($limit);
  85. foreach ($list as $k => $v){
  86. $exam_isset = $this->exam_model->where(['user_id'=>$this->auth->id,'exam_collection_id'=>$v['exam_collection_id'],'exam_collection_type'=>3,'endtime'=>['>',0]])->find();
  87. if(isset($exam_isset)){
  88. $v->is_user_examed = 1;
  89. }else{
  90. $v->is_user_examed = 0;
  91. }
  92. }
  93. unset($v);
  94. $result = ['total' => $list->total(), 'rows' => $list->items()];
  95. return json($result);
  96. }
  97. //进入考试
  98. public function into($ids = null)
  99. {
  100. $row = $this->model->get($ids);
  101. if(!$row){
  102. $this->error('未找到记录');
  103. }
  104. $info = $this->exam_model->where(['user_id'=>$this->auth->id,'exam_collection_id'=>$ids])->find();
  105. if(empty($info)){
  106. //考试表
  107. $arr = [
  108. 'exam_collection_id' => $ids,
  109. 'user_id' => $this->auth->id,
  110. 'user_username' => $this->auth->username,
  111. 'user_nickname' => $this->auth->nickname,
  112. 'user_depart_id' => $this->auth->depart_id,
  113. 'exam_collection_name' => $row->exam_collection_name,
  114. 'exam_collection_type' => 3,
  115. 'sim_type' => $row->sim_type,
  116. 'seat_id' => 1,
  117. 'sim_id' => intval($row->sim_type),
  118. ];
  119. $this->exam_model->save($arr);
  120. //考试和故障关联表
  121. $fault_list = Fault::where(['sim_type' => $row->sim_type, 'fault_state' => 0,'fault_type'=>3])->select();
  122. $fault_key = array_rand($fault_list, 3);
  123. foreach ($fault_list as $key=> $item){
  124. $flag = 0;
  125. if(in_array($key,$fault_key)){
  126. $flag = 1;
  127. }
  128. $add= [
  129. 'exam_id'=>$this->exam_model->exam_id,
  130. 'fault_id'=>$item->fault_id,
  131. 'ref_type'=>2,
  132. 'flag'=>$flag,
  133. 'ref_state'=>3,
  134. 'sim_fault_question_value'=>'0000000'.rand(1,2),
  135. 'createtime'=>time(),
  136. 'updatetime'=>time(),
  137. 'create_time'=>date('Y-m-d H:i:s'),
  138. 'update_time'=>date('Y-m-d H:i:s'),
  139. ];
  140. Db::name('real_exam_fault')->insert($add);
  141. }
  142. unset($item);
  143. }
  144. if ($this->request->isPost()) {
  145. $exam_collection_id = $this->request->post('exam_collection_id');
  146. if(empty($info->starttime)){
  147. $info->start_time = date('Y-m-d H:i:s');
  148. $info->starttime = time();
  149. }
  150. $info->exam_status = 4;
  151. $info->save();
  152. $this->success('开始成功','/ZQOtIMLKud.php/student/collection/examing/ids/'.$info['exam_id']);
  153. }
  154. $this->view->assign('row', $row);
  155. return $this->view->fetch();
  156. }
  157. public function examing($ids = null)
  158. {
  159. $row = $this->exam_model->get($ids);
  160. if(!$row){
  161. $this->error('未找到记录');
  162. }
  163. if ($this->request->isPost()) {
  164. //计算分数,保存记录
  165. $params = $this->request->post('row/a');
  166. $result = false;
  167. $score = 100;
  168. $fault_one_score = 0;
  169. $fault_two_score = 0;
  170. $fault_three_score = 0;
  171. $xianxian_score = 0;
  172. $yuanyin_socre= 0;
  173. $buwei_score= 0;
  174. $fangfa_score= 0;
  175. $jielun_score = 0;
  176. Db::startTrans();
  177. try {
  178. //更新考试结束时间
  179. $row->end_time = date('Y-m-d H:i:s');
  180. $row->endtime = time();
  181. $row->exam_status = 5;
  182. $row->other_report = $params['other_report'];
  183. $result = $row->save();
  184. $info = $this->exam_model->where(['exam_id'=>$ids])->find();
  185. //更新考试关联表,计算得分 mx_real_exam_fault
  186. $fault_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1])->select();
  187. foreach ($fault_list as $it)
  188. {
  189. $update['sim_fault_answer_value'] ='0000000'.rand(1,2);
  190. if($update['sim_fault_answer_value']==$it['sim_fault_question_value']){
  191. $update['answer_right'] = 1;
  192. }else{
  193. $update['answer_right'] = 2;
  194. }
  195. Db::name('real_exam_fault')->where('ref_id',$it['ref_id'])->update($update);
  196. }
  197. unset($it);
  198. //计算得分,故障是否有扣分 扣分制
  199. $fault_right_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1,'answer_right'=>['>',0]])->select();
  200. if($fault_right_list[0]['answer_right']==2){
  201. $fault_one_score = 25;
  202. }
  203. if($fault_right_list[1]['answer_right']==2){
  204. $fault_two_score = 25;
  205. }
  206. if($fault_right_list[2]['answer_right']==2){
  207. $fault_three_score = 25;
  208. }
  209. $xianxian_score = rand(1,3);
  210. $yuanyin_socre= rand(1,3);
  211. $buwei_score= rand(1,3);
  212. $fangfa_score= rand(1,3);
  213. if(empty($params['other_jielun'])){
  214. $jielun_score = rand(1,3);
  215. }
  216. //是否超时
  217. $overtime_fen = intval((time()-$info['endtime']) / 60);
  218. if($overtime_fen>=10){
  219. $overtime_score = 10;
  220. }else if($overtime_fen>0 && $overtime_fen<10){
  221. $overtime_score = $overtime_fen ;
  222. }else{
  223. $overtime_score =0;
  224. }
  225. $fault_score = $score-$fault_one_score-$fault_two_score-$fault_three_score-$xianxian_score-$yuanyin_socre-$buwei_score-$fangfa_score-$jielun_score-$overtime_score;
  226. //更新考试结果表
  227. $params['exam_id'] = $ids;
  228. $params['total'] = $fault_score;//得分;
  229. $params['fault_one_score'] = $fault_one_score;//得分;
  230. $params['fault_two_score'] = $fault_two_score;//得分;
  231. $params['fault_three_score'] = $fault_three_score;//得分;
  232. $params['xianxian_score'] = $xianxian_score;//得分;
  233. $params['yuanyin_socre'] = $yuanyin_socre;//得分;
  234. $params['buwei_score'] = $buwei_score;//得分;
  235. $params['fangfa_score'] = $fangfa_score;//得分;
  236. $params['jielun_score'] = $jielun_score;//得分;
  237. $params['overtime_score'] = $overtime_score;//得分;
  238. Db::name('real_exam_score')->insert($params);
  239. $this->exam_model->where(['exam_id'=>$ids])->update(['total_score'=>$fault_score]);
  240. Db::commit();
  241. } catch (ValidateException|PDOException|Exception $e) {
  242. Db::rollback();
  243. $this->error($e->getMessage());
  244. }
  245. if ($result === false) {
  246. $this->error(__('No rows were inserted'));
  247. }
  248. $this->success('交卷成功,待老师确认成绩','/ZQOtIMLKud.php/student/exam/index');
  249. }
  250. //还未开始考试
  251. $row->limit_duration = $this->model->where(['exam_collection_id'=>$row['exam_collection_id']])->value('limit_duration');
  252. if(empty($row->starttime)){
  253. $timer = 60*$row->limit_duration;
  254. }else{
  255. $fen = $row->limit_duration - intval((time()-$row->starttime) / 60);
  256. $timer = 60*$fen;
  257. }
  258. $row->replace_list =Db::name('real_exam_comp_request')->where(['exam_id'=>$ids])->select();
  259. $this->assignConfig('ids',$ids);
  260. $this->assignConfig('timer',$timer);
  261. $this->view->assign('row', $row);
  262. $departmentdata = [];
  263. $departmentdata = Fault::where(['replace_part'=>1,'sim_type'=>$row->sim_type])->select();
  264. //更换件数据
  265. $this->view->assign('departmentdata', $departmentdata);
  266. //故障现象
  267. $xianxiang = Fault::where(['fault_type'=>1,'sim_type'=>$row->sim_type])->select();
  268. $this->view->assign('xianxiang', $xianxiang);
  269. //可能原因
  270. $yuanyin = Fault::where(['fault_type'=>2,'sim_type'=>$row->sim_type])->select();
  271. $this->view->assign('yuanyin', $yuanyin);
  272. //故障部位
  273. $buwei = Fault::where(['fault_type'=>3,'sim_type'=>$row->sim_type])->select();
  274. $this->view->assign('buwei', $buwei);
  275. //排除方法
  276. $paichu = Fault::where(['fault_type'=>4,'sim_type'=>$row->sim_type])->select();
  277. $this->view->assign('paichu', $paichu);
  278. return $this->view->fetch();
  279. }
  280. public function replace($ids = null)
  281. {
  282. if ($this->request->isPost()) {
  283. $params = $this->request->post('other_replace','');
  284. $param = json_decode($params,true);
  285. if(!empty($param)){
  286. foreach ($param as $key=>$item){
  287. $isreplace = Db::name('real_exam_comp_request')->where(['exam_id'=>$ids,'fault_id'=>$item['fault_id']])->find();
  288. if(empty($isreplace)){
  289. $add = [
  290. 'exam_id'=>$ids,
  291. 'fault_id'=>$item['fault_id'],
  292. 'request_status'=>1,
  293. 'fault_name'=>Db::name('fault')->where('fault_id',$item['fault_id'])->value('replace_name'),
  294. 'create_by_user_id'=>$this->auth->id,
  295. 'create_by'=>$this->auth->nickname,
  296. 'createtime'=>time(),
  297. 'updatetime'=>time(),
  298. 'create_time'=>date('Y-m-d H:i:s'),
  299. 'update_time'=>date('Y-m-d H:i:s'),
  300. ];
  301. Db::name('real_exam_comp_request')->insert($add);
  302. $status = 1;
  303. }else{
  304. $status = $isreplace['request_status'];
  305. }
  306. $faultid_arr[] = [
  307. 'fault_id'=>$item['fault_id'],
  308. 'request_status'=>$status,
  309. ];
  310. }
  311. unset($item);
  312. $row = $this->exam_model->get($ids);
  313. $row-> other_replace = json_encode($faultid_arr);
  314. $result = $row->save();
  315. }
  316. $this->success('申请成功,待老师审批');
  317. }
  318. }
  319. public function analysis($ids = null)
  320. {
  321. $row = $this->exam_model->get($ids);
  322. if(!$row){
  323. $this->error('未找到记录');
  324. }
  325. if ($this->request->isPost()) {
  326. $params = $this->request->post('row/a');
  327. //计算总分到学员考试表
  328. $this->success('操作成功','/ZQOtIMLKud.php/student/collection/index');
  329. }
  330. return $this->view->fetch();
  331. }
  332. }