Collection.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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.endtime=0 and r.user_id='.$this->auth->id)->find();
  60. if(empty($isset)){
  61. //提示页面,没有开始的考试
  62. return $this->view->fetch('tishi');
  63. }else{
  64. // $this->redirect('/ZQOtIMLKud.php/student/collection/examing/ids/'.$isset['exam_id'].'?ref=addtabs');
  65. echo "<script>location.href='/ZQOtIMLKud.php/student/collection/examing/ids/".$isset['exam_id']."';</script>";
  66. //$this->redirect('/hotelmanage/room/displayindex/ids/1');
  67. exit();
  68. }
  69. die();
  70. //设置过滤方法
  71. $this->request->filter(['strip_tags', 'trim']);
  72. if (false === $this->request->isAjax()) {
  73. return $this->view->fetch();
  74. }
  75. //如果发送的来源是 Selectpage,则转发到 Selectpage
  76. if ($this->request->request('keyField')) {
  77. return $this->selectpage();
  78. }
  79. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  80. $list = $this->model
  81. ->where($where)->where($this->whereExtend)
  82. ->order($sort, $order)
  83. ->paginate($limit);
  84. foreach ($list as $k => $v){
  85. $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();
  86. if(isset($exam_isset)){
  87. $v->is_user_examed = 1;
  88. }else{
  89. $v->is_user_examed = 0;
  90. }
  91. }
  92. unset($v);
  93. $result = ['total' => $list->total(), 'rows' => $list->items()];
  94. return json($result);
  95. }
  96. //进入考试
  97. public function into($ids = null)
  98. {
  99. $row = $this->model->get($ids);
  100. if(!$row){
  101. $this->error('未找到记录');
  102. }
  103. $info = $this->exam_model->where(['user_id'=>$this->auth->id,'exam_collection_id'=>$ids])->find();
  104. if(empty($info)){
  105. //考试表
  106. $arr = [
  107. 'exam_collection_id' => $ids,
  108. 'user_id' => $this->auth->id,
  109. 'user_username' => $this->auth->username,
  110. 'user_nickname' => $this->auth->nickname,
  111. 'user_depart_id' => $this->auth->depart_id,
  112. 'exam_collection_name' => $row->exam_collection_name,
  113. 'exam_collection_type' => 3,
  114. 'sim_type' => $row->sim_type,
  115. 'seat_id' => 1,
  116. 'sim_id' => intval($row->sim_type),
  117. ];
  118. $this->exam_model->save($arr);
  119. //考试和故障关联表
  120. $fault_list = Fault::where(['sim_type' => $row->sim_type, 'fault_state' => 0,'fault_type'=>3])->select();
  121. $fault_key = array_rand($fault_list, 3);
  122. foreach ($fault_list as $key=> $item){
  123. $flag = 0;
  124. if(in_array($key,$fault_key)){
  125. $flag = 1;
  126. }
  127. $add= [
  128. 'exam_id'=>$this->exam_model->exam_id,
  129. 'fault_id'=>$item->fault_id,
  130. 'ref_type'=>2,
  131. 'flag'=>$flag,
  132. 'ref_state'=>3,
  133. 'sim_fault_question_value'=>'0000000'.rand(1,2),
  134. 'createtime'=>time(),
  135. 'updatetime'=>time(),
  136. 'create_time'=>date('Y-m-d H:i:s'),
  137. 'update_time'=>date('Y-m-d H:i:s'),
  138. ];
  139. Db::name('real_exam_fault')->insert($add);
  140. }
  141. unset($item);
  142. }
  143. if ($this->request->isPost()) {
  144. $exam_collection_id = $this->request->post('exam_collection_id');
  145. if(empty($info->starttime)){
  146. $info->start_time = date('Y-m-d H:i:s');
  147. $info->starttime = time();
  148. }
  149. $info->exam_status = 4;
  150. $info->save();
  151. $this->success('开始成功','/ZQOtIMLKud.php/student/collection/examing/ids/'.$info['exam_id']);
  152. }
  153. $this->view->assign('row', $row);
  154. return $this->view->fetch();
  155. }
  156. public function examing($ids = null)
  157. {
  158. $row = $this->exam_model->get($ids);
  159. if(!$row){
  160. $this->error('未找到记录');
  161. }
  162. if ($this->request->isPost()) {
  163. //计算分数,保存记录
  164. $params = $this->request->post('row/a');
  165. $result = false;
  166. $score = 100;
  167. $fault_one_score = 0;
  168. $fault_two_score = 0;
  169. $fault_three_score = 0;
  170. $xianxian_score = 0;
  171. $yuanyin_socre= 0;
  172. $buwei_score= 0;
  173. $fangfa_score= 0;
  174. $jielun_score = 0;
  175. Db::startTrans();
  176. try {
  177. //更新考试结束时间
  178. $row->end_time = date('Y-m-d H:i:s');
  179. $row->endtime = time();
  180. $row->exam_status = 5;
  181. $row->other_report = $params['other_report'];
  182. $result = $row->save();
  183. $info = $this->exam_model->where(['exam_id'=>$ids])->find();
  184. //更新考试关联表,计算得分 mx_real_exam_fault
  185. $fault_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1])->select();
  186. foreach ($fault_list as $it)
  187. {
  188. $update['sim_fault_answer_value'] ='0000000'.rand(1,2);
  189. if($update['sim_fault_answer_value']==$it['sim_fault_question_value']){
  190. $update['answer_right'] = 1;
  191. }else{
  192. $update['answer_right'] = 2;
  193. }
  194. Db::name('real_exam_fault')->where('ref_id',$it['ref_id'])->update($update);
  195. }
  196. unset($it);
  197. //计算得分,故障是否有扣分 扣分制
  198. $fault_right_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1,'answer_right'=>['>',0]])->select();
  199. if($fault_right_list[0]['answer_right']==2){
  200. $fault_one_score = 25;
  201. }
  202. if($fault_right_list[1]['answer_right']==2){
  203. $fault_two_score = 25;
  204. }
  205. if($fault_right_list[2]['answer_right']==2){
  206. $fault_three_score = 25;
  207. }
  208. $xianxian_score = rand(1,3);
  209. $yuanyin_socre= rand(1,3);
  210. $buwei_score= rand(1,3);
  211. $fangfa_score= rand(1,3);
  212. if(empty($params['other_jielun'])){
  213. $jielun_score = rand(1,3);
  214. }
  215. //是否超时
  216. $overtime_fen = intval((time()-$info['endtime']) / 60);
  217. if($overtime_fen>=10){
  218. $overtime_score = 10;
  219. }else if($overtime_fen>0 && $overtime_fen<10){
  220. $overtime_score = $overtime_fen ;
  221. }else{
  222. $overtime_score =0;
  223. }
  224. $fault_score = $score-$fault_one_score-$fault_two_score-$fault_three_score-$xianxian_score-$yuanyin_socre-$buwei_score-$fangfa_score-$jielun_score-$overtime_score;
  225. //更新考试结果表
  226. $params['exam_id'] = $ids;
  227. $params['total'] = $fault_score;//得分;
  228. $params['fault_one_score'] = $fault_one_score;//得分;
  229. $params['fault_two_score'] = $fault_two_score;//得分;
  230. $params['fault_three_score'] = $fault_three_score;//得分;
  231. $params['xianxian_score'] = $xianxian_score;//得分;
  232. $params['yuanyin_socre'] = $yuanyin_socre;//得分;
  233. $params['buwei_score'] = $buwei_score;//得分;
  234. $params['fangfa_score'] = $fangfa_score;//得分;
  235. $params['jielun_score'] = $jielun_score;//得分;
  236. $params['overtime_score'] = $overtime_score;//得分;
  237. Db::name('real_exam_score')->insert($params);
  238. $this->exam_model->where(['exam_id'=>$ids])->update(['total_score'=>$fault_score]);
  239. Db::commit();
  240. } catch (ValidateException|PDOException|Exception $e) {
  241. Db::rollback();
  242. $this->error($e->getMessage());
  243. }
  244. if ($result === false) {
  245. $this->error(__('No rows were inserted'));
  246. }
  247. $this->success('交卷成功,待老师确认成绩','/ZQOtIMLKud.php/student/exam/index');
  248. }
  249. //还未开始考试
  250. $row->limit_duration = $this->model->where(['exam_collection_id'=>$row['exam_collection_id']])->value('limit_duration');
  251. if(empty($row->starttime)){
  252. $timer = 60*$row->limit_duration;
  253. }else{
  254. $fen = $row->limit_duration - intval((time()-$row->starttime) / 60);
  255. $timer = 60*$fen;
  256. }
  257. $row->replace_list =Db::name('real_exam_comp_request')->where(['exam_id'=>$ids])->select();
  258. $this->assignConfig('ids',$ids);
  259. $this->assignConfig('timer',$timer);
  260. $this->view->assign('row', $row);
  261. $departmentdata = [];
  262. $departmentdata = Fault::where(['replace_part'=>1,'sim_type'=>$row->sim_type])->select();
  263. //更换件数据
  264. $this->view->assign('departmentdata', $departmentdata);
  265. //故障现象
  266. $xianxiang = Fault::where(['fault_type'=>1,'sim_type'=>$row->sim_type])->select();
  267. $this->view->assign('xianxiang', $xianxiang);
  268. //可能原因
  269. $yuanyin = Fault::where(['fault_type'=>2,'sim_type'=>$row->sim_type])->select();
  270. $this->view->assign('yuanyin', $yuanyin);
  271. //故障部位
  272. $buwei = Fault::where(['fault_type'=>3,'sim_type'=>$row->sim_type])->select();
  273. $this->view->assign('buwei', $buwei);
  274. //排除方法
  275. $paichu = Fault::where(['fault_type'=>4,'sim_type'=>$row->sim_type])->select();
  276. $this->view->assign('paichu', $paichu);
  277. return $this->view->fetch();
  278. }
  279. public function replace($ids = null)
  280. {
  281. if ($this->request->isPost()) {
  282. $params = $this->request->post('other_replace','');
  283. $param = json_decode($params,true);
  284. if(!empty($param)){
  285. foreach ($param as $key=>$item){
  286. $isreplace = Db::name('real_exam_comp_request')->where(['exam_id'=>$ids,'fault_id'=>$item['fault_id']])->find();
  287. if(empty($isreplace)){
  288. $add = [
  289. 'exam_id'=>$ids,
  290. 'fault_id'=>$item['fault_id'],
  291. 'request_status'=>1,
  292. 'fault_name'=>Db::name('fault')->where('fault_id',$item['fault_id'])->value('replace_name'),
  293. 'create_by_user_id'=>$this->auth->id,
  294. 'create_by'=>$this->auth->nickname,
  295. 'createtime'=>time(),
  296. 'updatetime'=>time(),
  297. 'create_time'=>date('Y-m-d H:i:s'),
  298. 'update_time'=>date('Y-m-d H:i:s'),
  299. ];
  300. Db::name('real_exam_comp_request')->insert($add);
  301. $status = 1;
  302. }else{
  303. $status = $isreplace['request_status'];
  304. }
  305. $faultid_arr[] = [
  306. 'fault_id'=>$item['fault_id'],
  307. 'request_status'=>$status,
  308. ];
  309. }
  310. unset($item);
  311. $row = $this->exam_model->get($ids);
  312. $row-> other_replace = json_encode($faultid_arr);
  313. $result = $row->save();
  314. }
  315. $this->success('申请成功,待老师审批');
  316. }
  317. }
  318. public function analysis($ids = null)
  319. {
  320. $row = $this->exam_model->get($ids);
  321. if(!$row){
  322. $this->error('未找到记录');
  323. }
  324. if ($this->request->isPost()) {
  325. $params = $this->request->post('row/a');
  326. //计算总分到学员考试表
  327. $this->success('操作成功','/ZQOtIMLKud.php/student/collection/index');
  328. }
  329. return $this->view->fetch();
  330. }
  331. }