request()->employee->root_id, 'name' => 'questioncate' ]; $setting = Setting::where($where)->find(); $list = !empty($setting->content) ? json_decode($setting->content, true) : []; View::assign('cate', $list); return View::fetch(); } $param = Request::only(['page', 'limit', 'cate', 'keyword','type'=>'']); $root_id = request()->employee->root_id; $where[] = ['state', '=', 1]; $where[] = ['root_id', '=', $root_id]; if (!empty($param['keyword'])) { $where[] = ['ask', 'like', '%' . $param['keyword'] . '%']; } if (!empty($param['cate'])) { $where[] = ['cate', '=', $param['cate']]; } if (!empty($param['type'])) { $where[] = ['type', '=', $param['type']]; } $list = ExamQuestion::where($where)->page($param['page'], $param['limit'])->order('addtime desc')->select(); foreach ($list as $k => $v) { } $count = ExamQuestion::where($where)->count(); return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']); } /* * 分类管理 */ function catelist() { return View::fetch(); } function catealllist() { $page = input('page', 1); $limit = input('limit', 10); $where = [ 'root_id' => request()->employee->root_id, 'name' => 'questioncate' ]; $setting = Setting::where($where)->find(); $for = ($setting) ? json_decode($setting->content) : []; $list = []; //试题 $questionCount = ExamQuestion::where(['root_id' => request()->employee->root_id, 'state' => 1])->group('cate')->column('count(id) as count', 'cate'); $questionMaxTime = ExamQuestion::where(['root_id' => request()->employee->root_id, 'state' => 1])->group('cate')->column('max(addtime)', 'cate'); foreach ($for as $k => $v) { $cate = [ 'name' => $v, 'value' => $k, 'questionCount' => isset($questionCount[$v]) ? $questionCount[$v] : 0, 'addtimeMax' => isset($questionMaxTime[$v]) ? $questionMaxTime[$v] : '', 'show' => $v == '其它' ? 1 : 0 ]; $list[] = $cate; } $count = count($list); $list = array_slice($list, $limit * ($page - 1), $limit); return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']); } /* * 分类删除 */ public function catedel() { $name = input('name'); $where = ['root_id' => request()->employee->root_id, 'name' => 'questioncate']; $setting = Setting::where($where)->find(); $content = json_decode($setting->content, true); $key = array_search($name, $content); unset($content[$key]); $content[] = '其它'; $content = array_unique($content); Setting::where($where)->update(['content' => json_encode($content)]); ExamQuestion::where(['root_id' => request()->employee->root_id, 'cate' => $name])->update(['cate' => '其它']); return json(['code' => 0, 'msg' => '删除成功']); } function cateedit() { $name = input('name', ''); $yname = input('yname', ''); if ($name == $yname) return json(['code' => 1, 'msg' => '请修改分类名称']); $w[] = ['root_id', '=', request()->employee->root_id]; $w[] = ['name', '=', 'questioncate']; $setting = Setting::where($w)->find(); if ($setting) { $for = ($setting) ? json_decode($setting->content, true) : []; if (in_array($name, $for)) return json(['code' => 1, 'msg' => '分类已存在']); $key = array_search($yname, $for); $for[$key] = $name; // if ($key!==false) unset($for[$key]); // array_unshift($for,$name); Setting::where($w)->update(['content' => json_encode($for)]); $cw[] = ['cate', '=', $yname]; $cw[] = ['root_id', '=', request()->employee->root_id]; ExamQuestion::where($cw)->update(['cate' => $name]); } else { $save['root_id'] = request()->employee->root_id; $save['name'] = 'questioncate'; $save['content'] = json_encode([$name]); Setting::insert($save); } return json(['code' => 0, 'data' => '', 'msg' => '编辑成功']); } function cateadd() { $name = input('name', ''); $w[] = ['root_id', '=', request()->employee->root_id]; $w[] = ['name', '=', 'questioncate']; $setting = Setting::where($w)->findOrEmpty(); if (!$setting->isEmpty()) { $for = ($setting->content) ? json_decode($setting->content, true) : []; if (in_array($name, $for)) return json(['code' => 1, 'msg' => '分类已存在']); array_unshift($for, $name); Setting::where($w)->update(['content' => json_encode($for)]); } else { $save['root_id'] = request()->employee->root_id; $save['name'] = 'questioncate'; $save['content'] = json_encode([$name]); Setting::insert($save); } return json(['code' => 0, 'data' => '', 'msg' => '编辑成功']); } /** * 试题列表 */ public function questionadd() { $where = [ 'root_id' => request()->employee->root_id, 'name' => 'questioncate' ]; $setting = Setting::where($where)->find(); $list = !empty($setting->content) ? json_decode($setting->content, true) : []; View::assign('cate', $list); return View::fetch(); } /** * 添加试题 */ public function questionsave() { $root_id = request()->employee->root_id; // $param = Request::param(); $type = input('type'); $save['ask'] = $ask = input('ask'); $ask = str_replace(' ', '', $ask); if (trim($ask) == '') { return json(['code' => 1, 'msg' => '题干内容不能为空']); } $imgs = input('imgs/a'); $save['ask'] = str_replace('"', '\\"', $save['ask']); $save['type'] = $type; $save['cate'] = input('cate'); $save['imgs'] = empty($imgs) ? '' : implode(',', $imgs); if ($type == '单选') { $radio = input('checkbox0', ''); if (!$radio) return json(['code' => 1, 'msg' => '请选择正确答案']); $save['type'] = '单选'; $save['answer'] = $radio; $answer = input('answer0/a'); $option = input('option0/a'); $op = []; foreach ($option as $k => $v) { $o = []; if (isset($answer[$k]) && $answer[$k]) { $o['title'] = $v; $o['content'] = $answer[$k]; $o['ansower'] = $radio == $v ? 1 : 0; } $op[] = $o; } $save['content'] = json_encode($op); } elseif ($type == '多选') { //选择 $checkbox = input('checkbox1/a'); // var_dump($checkbox);die; if (!$checkbox) return json(['code' => 1, 'msg' => '请勾选答案']); $answer = input('answer1/a'); $save['type'] = '多选'; $option = input('option1/a'); $op = []; foreach ($option as $k => $v) { $o = []; if (isset($answer[$k]) && $answer[$k]) { $o['title'] = $v; $o['content'] = $answer[$k]; $o['ansower'] = isset($checkbox[$k]) ? 1 : 0; } $op[] = $o; } $save['content'] = json_encode($op); $save['answer'] = implode(',', $checkbox); } elseif ($type == '判断') { $save['content'] = $save['ask']; $save['answer'] = input('answer2') == 1 ? '正确' : '错误'; } elseif ($type == '简答') { // if (!input('content3', '')) return json(['code' => 1, 'msg' => '请输入参考答案']); if (!input('answer3', '')) return json(['code' => 1, 'msg' => '请输入答案关键词']); $save['content'] = input('content3', ''); $save['answer'] = input('answer3'); } $save['root_id'] = request()->employee->root_id; $quesObj = new ExamQuestion(); if ($quesObj->save($save)) { return json(['code' => 0, 'msg' => '添加成功']); } else { return json(['code' => 1, 'msg' => '添加失败']); } } /** * 试题编辑 */ public function questionedit() { $root_id = request()->employee->root_id; if (!Request::isAjax()) { $where[] = ['root_id', '=', $root_id]; $where[] = ['name', '=', 'questioncate']; $questioncateobj = Setting::where($where)->find(); $cate = ($questioncateobj && $questioncateobj->content) ? json_decode($questioncateobj->content, true) : []; View::assign('cate', $cate); $questionid = Request::param('qid'); $question = ExamQuestion::find($questionid); // var_dump($question);die; if ($question['type'] == "单选") { $question['type'] = 0; $question['content'] = json_decode($question['content'], true); $question['answer'] = [$question['answer']]; } elseif ($question['type'] == '多选') { $question['type'] = 1; $question['content'] = json_decode($question['content'], true); $question['answer'] = explode(',', $question['answer']); } elseif ($question['type'] == '判断') { $question['type'] = 2; $question['answer'] = $question['answer'] == '正确' ? 1 : 2; } elseif ($question['type'] == '简答') { $question['type'] = 3; } $question['vo_imgs'] = array_filter(explode(',', $question->getData('imgs'))); View::assign('data', $question); $url = config('app.ali_oss_bindurl'); View::assign('url', 'https://' . $url . '/'); return View::fetch(); } $param = Request::param(); $info = ExamQuestion::find($param['qid'])->toArray(); $save['ask'] = $param['ask']; $save['ask'] = str_replace('"', '\\"', $save['ask']); $save['cate'] = $param['cate']; $data = Request::only(['imgs', 'article_image_exist']); $data['imgs'] = isset($data['imgs']) ? $data['imgs'] : []; $data['article_image_exist'] = isset($data['article_image_exist']) ? $data['article_image_exist'] : []; $data['imgs'] = array_filter(array_merge($data['article_image_exist'], $data['imgs'])); $save['imgs'] = empty($data['imgs']) ? '' : implode(',', $data['imgs']); //选择 if ($info['type'] == '单选') { $checkbox = input('checkbox0', ''); if (!$checkbox) return json(['code' => 1, 'msg' => '请选择正确答案']); $save['answer'] = $checkbox; $answer = input('answer0/a'); $option = input('option0/a'); $op = []; foreach ($option as $k => $v) { $o = []; if (isset($answer[$k]) && $answer[$k]) { $o['title'] = $v; $o['content'] = $answer[$k]; $o['ansower'] = $v == $checkbox ? 1 : 0; } $op[] = $o; } $save['content'] = json_encode($op); $save['type'] = '单选'; } elseif ($info['type'] == 1 || $info['type'] == '多选') { $checkbox = input('checkbox1/a'); // var_dump($checkbox);die; if ($checkbox == null) return json(['code' => 1, 'msg' => '请勾选答案']); $answer = input('answer1/a'); $option = input('option1/a'); $op = []; foreach ($option as $k => $v) { $o = []; if (isset($answer[$k]) && $answer[$k]) { $o['title'] = $v; $o['content'] = $answer[$k]; $o['ansower'] = isset($checkbox[$k]) ? 1 : 0; } $op[] = $o; } $save['content'] = json_encode($op); $save['answer'] = implode(',', $checkbox); $save['type'] = '多选'; } elseif ($info['type'] == 2 || $info['type'] == '判断') { //判断 $save['content'] = $save['ask']; $save['answer'] = input('answer2') == 1 ? '正确' : '错误'; } elseif ($info['type'] == '简答' || $info['type'] == 3) { //简单 // if (!input('content3', '')) return json(['code' => 1, 'msg' => '请输入参考答案']); if (!input('answer3', '')) return json(['code' => 1, 'msg' => '请输入答案关键词']); $save['content'] = input('content3', ''); $save['answer'] = input('answer3'); } $w[] = ['root_id', '=', $root_id]; $w[] = ['id', '=', $param['qid']]; ExamQuestion::where($w)->update($save); return json(['code' => 0, 'msg' => '编辑成功']); // if (ExamQuestion::where($w)->update($save)) { // return json(['code' => 0, 'msg' => '编辑成功']); // } else { // return json(['code' => 1, 'msg' => '编辑失败']); // } } /** * 试题删除 */ public function questionpredel() { $qid = Request::param('qid'); $root_id = request()->employee->root_id; $w[] = ['root_id', '=', $root_id]; $w[] = ['question_id', '=', $qid]; $records_num = ExamPaperQuestion::where($w)->count(); if ($records_num > 0) { return json(['code' => 0, 'msg' => '此试题已被试卷试用,确定要删除么?']); } else { return json(['code' => 0, 'msg' => '确定要删除试题么?']); } } public function questiondel() { $qid = Request::param('qid'); $root_id = request()->employee->root_id; $w[] = ['root_id', '=', $root_id]; $w[] = ['question_id', '=', $qid]; $records_num = ExamPaperQuestion::where($w)->count(); // if($records_num > 0){ // ExamPaperQuestion::where($w)->delete(); // } $w1[] = ['root_id', '=', $root_id]; $w1[] = ['id', '=', $qid]; if (ExamQuestion::where($w1)->update(['state' => 0])) { return json(['code' => 0, 'msg' => '删除成功']); } else { return json(['code' => 1, 'msg' => '删除失败']); } } public function addquestioncate() { $param = Request::only(['cate']); $root_id = request()->employee->root_id; $w[] = ['root_id', '=', $root_id]; $w[] = ['name', '=', 'questioncate']; $cate = Setting::where($w)->find(); if (!$cate) { $save['root_id'] = $root_id; $save['content'] = json_encode([$param['cate']]); $save['name'] = 'questioncate'; Setting::insert($save); // Setting::where()->save(['content' => json_encode([$param['cate']])]); } else { $catearr = json_decode($cate['content'], true); array_push($catearr, $param['cate']); $catearr = array_unique($catearr); Setting::where('id', $cate['id'])->save(['content' => json_encode($catearr)]); } return json(['code' => 0, 'msg' => '添加成功']); } public function check_excel($title, $type) { $res = true; foreach ($type as $k => $v) { if ($v != $title[$k]) { $res = $v; break; } } return $res; } /** * 测试图片上传 */ public function extract_img($sheet) { // $filename = $_FILES['file']['tmp_name']; // $objReader = IOFactory::createReader('Xlsx'); // $objPHPExcel = $objReader->load($filename); // $sheet = $objPHPExcel->getSheet(0); //excel中的第一张sheet $data = $sheet->toArray(); $imageFilePath='./images/'.date('Y-m-d').'/';//图片在本地存储的路径 if (!file_exists( $imageFilePath )) { mkdir("$imageFilePath", 0777, true); } $text = []; //处理图片 try { foreach ($sheet->getDrawingCollection() as $drawing) { list($startColumn, $startRow) = Coordinate::coordinateFromString($drawing->getCoordinates()); //$imageFileName = $drawing->getCoordinates() . mt_rand(1000, 9999); $imageFileName = $drawing->getCoordinates() . uniqid(); switch ($drawing->getExtension()){ case 'jpg': case 'jpeg': $imageFileName .= '.jpg'; $source = imagecreatefromjpeg($drawing->getPath()); imagejpeg($source, $imageFilePath . $imageFileName); break; case 'gif': $imageFileName .= '.gif'; $source = imagecreatefromgif($drawing->getPath()); imagegif($source, $imageFilePath . $imageFileName); break; case 'png': $imageFileName .= '.png'; $source = imagecreatefrompng($drawing->getPath()); imagepng($source, $imageFilePath . $imageFileName); break; } $startColumn = $this->ABC2decimal($startColumn); $url = 'exam/'. $imageFileName; $imgUrlOss[] = ['tmp_name' => $imageFilePath . $imageFileName, 'name' => $url]; if($data[$startRow-1][$startColumn] && !isset($text[$startRow-1][$startColumn])) $text[$startRow-1][$startColumn] = $data[$startRow-1][$startColumn]; $data[$startRow-1][$startColumn] .= '

'; } if(!empty($text)){ foreach($text as $key=>$val){ foreach($val as $k=>$v){ $data[$key][$k] = '

'.$v.'

'.str_replace($v,'',$data[$key][$k]); } } } if(!empty($imgUrlOss)) $this->upossimg($imgUrlOss); }catch (\Exception $e) { throw $e; var_dump($e->getMessage()); } return $data; } public function ABC2decimal($abc){ $ten = 0; $len = strlen($abc); for($i=1;$i<=$len;$i++){ $char = substr($abc,0-$i,1);//反向获取单个字符 $int = ord($char); $ten += ($int-65)*pow(26,$i-1); } return $ten; } /** * 将图片上传oss保存 */ public function upossimg($imgUrlOss) { // 将上传到图片保存 if (!empty($imgUrlOss)) { $accessKeyId = config('app.ali_oss_access_key_id'); $accessKeySecret = config('app.ali_oss_access_key_secret'); $endpoint = config('app.ali_oss_end_point'); $bucket = config('app.ali_oss_bucket'); $oss = new OssClient($accessKeyId, $accessKeySecret, $endpoint); try { foreach ($imgUrlOss as $key => $v) { $oss->uploadFile($bucket, $v['name'], $v['tmp_name']); unlink($v['tmp_name']); } } catch (OssException $e) { Log::record('文件上传失败,error:' . $e->getErrorCode(), 'error'); //return json(['code' => 1, 'msg' => $e->getErrorMessage()]); } } } /// // 试题导入importing questions /// public function importing() { $root_id = request()->employee->root_id; $request = request(); if (!$request->isAjax()) { return View::fetch(); } $title0 = ['分类', '题干(必填)', '选项 A', '选项 B', '选项 C', '选项 D', '选项E', '选项F', '选项G', '选项H', '正确答案(必填)']; $title1 = ['分类', '题干(必填)', '选项 A', '选项 B', '选项 C', '选项 D', '选项E', '选项F', '选项G', '选项H', '正确答案(必填)']; $title2 = ['分类', '题干(必填)', '正确答案(必填)']; $title3 = ['分类', '题干(必填)', '参考答案(必填)', '答案关键字(必填)']; $fileExtendName = substr(strrchr($_FILES['file']["name"], '.'), 1); $filename = $_FILES['file']['tmp_name']; if ($fileExtendName == 'xlsx') { $objReader = IOFactory::createReader('Xlsx'); } else { $objReader = IOFactory::createReader('Xls'); } $objPHPExcel = $objReader->load($filename); //$filename可以是上传的表格,或者是指定的表格 $cates = $saves = []; $error = 0; $remark = []; //单选题 $sheet = $objPHPExcel->getSheet(0); //excel中的第一张sheet $data = $this->extract_img($sheet); //var_dump($data);exit; //$data = $sheet->toArray(); if (count($data) > 1) { $check = $this->check_excel($data[0], $title0); if ($check !== true) return json(['code' => 1, 'data' => $check, 'msg' => '模板标题异常,请使用官方提供的模板']); unset($data[0]); $arr = [2 => 'A', 3 => 'B', 4 => 'C', 5 => 'D', 6 => 'E', 7 => 'F', 8 => 'G', 9 => 'H']; foreach ($data as $k => $v) { if(empty(array_filter(array_slice($v,0,11)))) continue; //题目,答案必传才能保存 if ($v[1] && $v[10] && $v[0]) { //如果存在选项才能保存 $options = []; $answer = explode(',', trim($v[10])); foreach ($arr as $k2 => $v2) { $option = []; if (!empty($v[$k2])) { $option['title'] = $v2; $option['content'] = $v[$k2]; $option['ansower'] = in_array($v2, $answer) ? 1 : 0; // $option[$v2] = $v[$k2]; $options[] = $option; } else { break; //选项不能中断 } } if (count($options) > 0) { $cates[] = trim($v[0]); //分类 $save = []; $save['cate'] = $v[0]; $save['ask'] = $v[1]; $save['content'] = json_encode($options); $save['answer'] = trim($v[10]); $save['type'] = '单选'; $save['root_id'] = $root_id; $saves[] = $save; } } else { $error += 1; } } if($error) $remark[] = '单选题有'.$error.'道试题上传格式错误
'; } //多选题 $sheet = $objPHPExcel->getSheet(1); //excel中的第二张sheet $data = $this->extract_img($sheet); //$data = $sheet->toArray(); $error = 0; if (count($data) > 1) { $check = $this->check_excel($data[0], $title1); if ($check !== true) return json(['code' => 1, 'data' => $check, 'msg' => '模板标题异常,请使用官方提供的模板']); unset($data[0]); $arr = [2 => 'A', 3 => 'B', 4 => 'C', 5 => 'D', 6 => 'E', 7 => 'F', 8 => 'G', 9 => 'H']; foreach ($data as $k => $v) { if(empty(array_filter(array_slice($v,0,11)))) continue; //题目,答案必传才能保存 if ($v[1] && $v[10] && $v[0]) { //如果存在选项才能保存 $options = []; $answer = explode(',', trim($v[10])); foreach ($arr as $k2 => $v2) { $option = []; if (!empty($v[$k2])) { $option['title'] = $v2; $option['content'] = $v[$k2]; $option['ansower'] = in_array($v2, $answer) ? 1 : 0; // $option[$v2] = $v[$k2]; $options[] = $option; } else { break; //选项不能中断 } } if (count($options) > 0) { $cates[] = trim($v[0]); //分类 $save = []; $save['cate'] = $v[0]; $save['ask'] = $v[1]; $save['content'] = json_encode($options); $save['answer'] = trim($v[10]); $save['type'] = '多选'; $save['root_id'] = $root_id; $saves[] = $save; } } else { $error += 1; } } if($error) $remark[] = '多选题有'.$error.'道试题上传格式错误
'; } // var_dump($saves);die; //判断题 $sheet = $objPHPExcel->getSheet(2); //第二章sheet $data = $sheet->toArray(); $error = 0; if (count($data) > 1) { $check = $this->check_excel($data[0], $title2); if ($check !== true) return json(['code' => 1, 'data' => $check, 'msg' => '模板标题异常,请使用官方提供的模板']); unset($data[0]); foreach ($data as $k => $v) { if(empty(array_filter(array_slice($v,0,3)))) continue; $save = []; if ($v[1] && $v[2] && $v[0]) { $cates[] = trim($v[0]); //分类 $save = []; $save['cate'] = $v[0]; $save['ask'] = $save['content'] = $v[1]; $save['answer'] = trim($v[2]); $save['type'] = '判断'; $save['root_id'] = $root_id; $saves[] = $save; } else { $error += 1; } } if($error) $remark[] = '判断题有'.$error.'道试题上传格式错误
'; } //问答题 $sheet = $objPHPExcel->getSheet(3); //第三章sheet $data = $sheet->toArray(); $error = 0; if (count($data) > 1) { // var_dump($data[0]);die; $check = $this->check_excel($data[0], $title3); if ($check !== true) return json(['code' => 1, 'data' => $check, 'msg' => '模板标题异常,请使用官方提供的模板']); unset($data[0]); foreach ($data as $k => $v) { if(empty(array_filter(array_slice($v,0,4)))) continue; $save = []; if ($v[1] && $v[2] && $v[0]) { $cates[] = trim($v[0]); //分类 $save = []; $save['cate'] = $v[0]; $save['ask'] = $v[1]; $save['answer'] = trim($v[3]); $save['content'] = trim($v[2]); $save['type'] = '简答'; $save['root_id'] = $root_id; $saves[] = $save; } else { $error += 1; } } if($error) $remark[] = '问答题有'.$error.'道试题上传格式错误
'; } $cates = array_unique(array_filter($cates)); //保存分类 $cw[] = ['root_id', '=', request()->employee->root_id]; $cw[] = ['name', '=', 'questioncate']; $ques_cate = Setting::where($cw)->find(); if (!$ques_cate) { Setting::insert(['content' => json_encode($cates), 'name' => 'questioncate', 'root_id' => request()->employee->root_id]); } else { $catearr = json_decode($ques_cate['content'], true); $cates = array_unique(array_merge($catearr, $cates)); Setting::where($cw)->save(['content' => json_encode($cates)]); } //保存问题 (new ExamQuestion())->saveAll($saves); if ($remark) { return json(["code" => 1, 'msg' => implode('',$remark)]); }else{ return json(["code" => 0, 'msg' => '导入成功']); } } /** * paper列表 */ public function paperlist() { if (!Request::isAjax()) { return View::fetch(); } $root_id = request()->employee->root_id; $param = Request::only(['page', 'limit', 'keyword']); // $where[] = ['state','=', 1]; $where[] = ['root_id', '=', $root_id]; if (!empty($param['keyword'])) { $where[] = ['name', 'like', '%' . $param['keyword'] . '%']; } $list = ExamPaper::with('result')->where($where)->page($param['page'], $param['limit'])->order('addtime desc')->select()->toArray(); foreach ($list as &$v) { $time = date('Y-m-d H:i:s'); if ($time >= $v['starttime'] && $time <= $v['endtime'] && $v['result']) { $v['done'] = 1; } else { $v['done'] = 0; } $v['attendee_num'] = $v['assessment'] ? count(explode(',', $v['assessment'])) : 0; } $count = ExamPaper::where($where)->count(); return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']); } /** * 阅卷批改,试卷列表 */ public function checkpaperlist() { if (!Request::isAjax()) { return View::fetch(); } $root_id = request()->employee->root_id; $param = Request::only(['page', 'limit', 'keyword']); // $where[] = ['state','=', 1]; $where[] = ['root_id', '=', $root_id]; if (!empty($param['keyword'])) { $where[] = ['name', 'like', '%' . $param['keyword'] . '%']; } //只查询人工审核的试卷列表 // $where[] = ['checkway','=','employee']; $list = ExamPaper::withCount(['result'=>function($query){ $query->where([['approve_status', '>=', 0]]); // 考试中的不在统计范围内 }])->where($where)->page($param['page'], $param['limit'])->order('addtime desc')->select()->toArray(); foreach ($list as &$v) { $time = date('Y-m-d H:i:s'); if ($time >= $v['starttime'] && $time <= $v['endtime'] && $v['result_count']) { $v['done'] = 1; } else { $v['done'] = 0; } } $count = ExamPaper::where($where)->count(); return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']); } // (更新操作逻辑,此接口不在使用) // public function paperadd() // { // $root_id = request()->employee->root_id; // //退回上一步处理 // $paperid = input('paperid', 0); // $data['paperid'] = $paperid ?: 0; // if ($paperid) { // $info = ExamPaper::where('id', $paperid)->find()->toArray(); // $data['info'] = $info; // view::assign('data', $data); // return View::fetch(); // } else { // $data['info'] = []; // } // view::assign('data', $data); // if (!Request::isAjax()) { // return View::fetch(); // } // if (!input('name', '')) return json(['code' => 1, 'msg' => '请填写考卷名称']); // $quesObj = new ExamPaper; // $quesObj->name = input('name'); // $quesObj->for_newbie = input('for_newbie', 0); // $quesObj->desc = input('desc'); // $quesObj->root_id = $root_id; // $quesObj->state = 0; // $quesObj->approve_employee_ids = ''; // $quesObj->assessment = ''; // $quesObj->save(); // $id = $quesObj->id; // if ($id) { // return json(['code' => 0, 'data' => $id, 'msg' => '添加成功']); // } else { // return json(['code' => 1, 'msg' => '添加失败']); // } // } //退回后再次点击下一步 public function paperaddt() { $root_id = request()->employee->root_id; $paperid = input('paperid', 0); $name = input('name', ''); if ($paperid && $name) { $u['desc'] = input('desc', ''); $u['name'] = $name; ExamPaper::where('id', $paperid)->update($u); return json(['code' => 0, 'data' => $paperid, 'msg' => '添加成功']); } else { return json(['code' => 1, 'msg' => '参数错误']); } } public function paperaddquestion() { $root_id = request()->employee->root_id; if (!Request::isAjax()) { View::assign('paperid', Request::param('paperid')); //查询已经保存的题目 $cw[] = ['root_id', '=', $root_id]; $cw[] = ['paper_id', '=', Request::param('paperid')]; $ids = ExamPaperQuestion::where($cw)->column('question_id'); $wherec[] = ['id', 'in', $ids]; $wherec[] = ['root_id', '=', $root_id]; $wherec[] = ['state', '=', 1]; $data['data2'] = ExamQuestion::where($wherec)->field('id,type,ask,cate')->order('type', 'desc')->select()->toArray(); //查询剩余的题目 $where[] = ['root_id', '=', $root_id]; $where[] = ['id', 'not in', $ids]; $where[] = ['state', '=', 1]; $questList = ExamQuestion::where($where)->field('id,type,ask,cate')->order('type', 'desc')->select()->toArray(); $data['count'] = count($data['data2']); //选中题目的数量 $data['data2'] = json_encode($data['data2']); //分类 $cate_w[] = ['root_id', '=', $root_id]; $cate_w[] = ['name', '=', 'questioncate']; $cate = Setting::where($cate_w)->value('content'); $data['cate'] = $cate ? json_decode($cate, true) : []; View::assign('data', $data); // var_dump(json_encode($questList));die; View::assign('questListstr', strip_tags(json_encode($questList))); return View::fetch(); } $param = Request::param(); //var_dump($param); $idarr = array_unique(explode(',', $param['idstr'])); $old_where[] = ['root_id', '=', $root_id]; $old_where[] = ['paper_id', '=', Request::param('paperid')]; $old_questions = ExamPaperQuestion::with('questions')->where($old_where)->field('question_id,score,id')->select()->toArray(); $types = ['type0' => '单选', 'type1' => '多选', 'type2' => '判断', 'type3' => '简答']; $score = []; foreach ($old_questions as $k => $v) { foreach ($types as $kk => $vv) { if ($v['questions']['type'] == $vv) { $score[$kk] = $v['score']; } } } $dw[] = ['root_id', '=', $root_id]; $dw[] = ['paper_id', '=', Request::param('paperid')]; $del = ExamPaperQuestion::where($dw)->delete(); $new_questions = ExamQuestion::where([['id', 'in', $idarr]])->select(); $save_data = []; foreach ($new_questions as $k => $v) { $once_data['paper_id'] = $param['paperid']; $once_data['question_id'] = $v['id']; $once_data['root_id'] = $root_id; foreach ($types as $kk => $vv) { if ($v['type'] == $vv) { $once_data['score'] = $score[$kk] ?? ''; } } $save_data[] = $once_data; } (new ExamPaperQuestion())->saveAll($save_data); /*$dw[] = ['root_id','=',$root_id]; $dw[] = ['paper_id','=',Request::param('paperid')]; $del = ExamPaperQuestion::where($dw)->delete(); foreach($idarr as $qid){ $pivot = new ExamPaperQuestion(); $pivot->save(['paper_id'=>$param['paperid'],'question_id' => $qid,'root_id'=>$root_id]); }*/ return json(['code' => 0, 'paperid' => $param['paperid'], 'msg' => '添加成功']); } //搜索题目 public function searchquestion() { $root_id = request()->employee->root_id; $type = input('type', ''); $keyword = input('keyword', ''); $data2 = input('data2', ''); $cate = input('cate', ''); $w[] = ['root_id', '=', $root_id]; if ($type) { $w[] = ['type', 'in', array_filter(explode(',', $type))]; } if ($keyword) { $w[] = ['ask', 'like', '%' . $keyword . '%']; } if ($data2) { $w[] = ['id', 'not in', array_column($data2, 'id')]; } if ($cate) { $w[] = ['cate', '=', $cate]; } $w[] = ['state', '=', 1]; $questList = ExamQuestion::where($w)->field('id,type,ask,cate')->order('type', 'desc')->select()->toArray(); $data['data2'] = $data2 ?: []; $data['data1'] = $questList; $data['count'] = ($data2) ? count($data2) : 0; return json(['code' => 0, 'data' => $data, 'msg' => '添加成功']); } // 设置分值 // public function paperscore() // { // $root_id = request()->employee->root_id; // $typeEn = ['单选' => 'single', '多选' => 'multi', '判断' => 'judge', '简答' => 'answer']; // if (!Request::isAjax()) { // $paperid = Request::param('paperid'); // View::assign('paperid', $paperid); // $w[] = ['root_id', '=', $root_id]; // $w[] = ['paper_id', '=', $paperid]; // // $idsarr = ExamPaperQuestion::where($w)->column('question_id'); // // $types = ExamQuestion::where([['id','in',$idsarr]])->column('type'); // $questions = ExamPaperQuestion::with('questions')->where($w)->field('question_id,score,id')->select()->toArray(); // $types = ['type0' => '单选', 'type1' => '多选', 'type2' => '判断', 'type3' => '简答']; // $sum = array_sum(array_column($questions, 'score')); // foreach ($types as $k => $v) { // $data[$k]['count'] = 0; //分类下题目数量 // $data[$k]['score_sum'] = 0; //合计分值 // $data[$k]['grawth'] = 0; //分值占比 // $data[$k]['score'] = ''; //每题分值 // $data[$k]['questions'] = []; // foreach ($questions as $k2 => $v2) { // $v2['questions']['score'] = $v2['score'] ? $v2['score'] . '分' : ''; // $v2['questions']['del_id'] = $v2['id']; // $v2['questions']['ask'] = strip_tags($v2['questions']['ask']); // if ($v2['questions']['type'] == '简答') { // $v2['questions']['answer_j'] = $v2['questions']['answer']; // $v2['questions']['answer'] = $v2['questions']['content']; // } // if ($v2['questions']['type'] == $v) { // $data[$k]['count'] += 1; // $data[$k]['score_sum'] = $data[$k]['score_sum'] + $v2['score']; // $data[$k]['score'] = $v2['score'] ?: ''; // if ($k == 'type0' || $k == 'type1') { // $v2['questions']['content'] = json_decode($v2['questions']['content'], true); // } // $data[$k]['questions'][] = $v2['questions']; // } // } // $data[$k]['grawth'] = !$sum ? '0%' : round($data[$k]['score_sum'] / $sum * 100, 2) . '%'; // } // $w1[] = ['root_id', '=', $root_id]; // $w1[] = ['id', '=', $paperid]; // $paperInfo = ExamPaper::where($w1)->field('target_group,base_score,starttime,endtime,checkway,desc,state,approve_employee_ids,scope_org,duringtime')->find()->toArray(); // $data['base_score'] = $paperInfo['base_score'] ?: 0; // View::assign('data', $data); // return View::fetch(); // } // $param = Request::param(); // $paperid = $param['paperid']; // $w[] = ['root_id', '=', $root_id]; // $w[] = ['paper_id', '=', $paperid]; // $idsarr = ExamPaperQuestion::where($w)->column('question_id'); // $questions = ExamQuestion::where([['id', 'in', $idsarr]])->select(); // $totalscore = 0; // foreach ($questions as $item) { // ExamPaperQuestion::where(['paper_id' => $paperid, 'question_id' => $item->id, 'root_id' => $root_id])->update(['score' => $param[$typeEn[$item->type]]]); // $totalscore += $param[$typeEn[$item->type]]; // } // $w1[] = ['root_id', '=', $root_id]; // $w1[] = ['id', '=', $paperid]; // ExamPaper::where($w1)->update(['total_score' => $totalscore, 'base_score' => $param['base_score']]); // return json(['code' => 0, 'paperid' => $paperid, 'msg' => '设置成功']); // } /** * 设置分值 */ public function paperscore() { $root_id = request()->employee->root_id; $paper_id = request()->param('paper_id',0); $question_ids = request()->param('question_ids'); $questions = ExamQuestion::where([['root_id', '=', $root_id], ['id', 'in', explode(',',$question_ids)]])->select()->toArray(); $types = ['type0' => '单选', 'type1' => '多选', 'type2' => '判断', 'type3' => '简答']; $sum = 0; $data = []; foreach ($types as $k => $v) { $data[$k]['count'] = 0; //分类下题目数量 $data[$k]['score_sum'] = 0; //合计分值 $data[$k]['grawth'] = 0; //分值占比 $data[$k]['score'] = ''; //每题分值 $data[$k]['questions'] = []; foreach ($questions as $k2 => $v2) { $v2['score'] = ''; $v2['del_id'] = $v2['id']; $v2['ask'] = strip_tags($v2['ask']); if ($v2['type'] == '简答') { $v2['answer_j'] = $v2['answer']; $v2['answer'] = $v2['content']; } if ($v2['type'] == $v) { $data[$k]['count'] += 1; $data[$k]['score_sum'] = $data[$k]['score_sum']; $data[$k]['score'] = $v2['score'] ?: ''; if ($k == 'type0' || $k == 'type1') { $v2['content'] = json_decode($v2['content'], true); } $data[$k]['questions'][] = $v2; } } $data[$k]['grawth'] = !$sum ? '0%' : round($data[$k]['score_sum'] / $sum * 100, 2) . '%'; } //及格分数 $paper = ExamPaper::where(['root_id' => $root_id, 'id' => $paper_id])->field('total_score,base_score')->find(); $data['base_score'] = $paper && $paper['base_score'] ? $paper['base_score'] : 0; $data['total_score'] = $paper && $paper['total_score'] ? $paper['total_score'] : 0; return json(['code' => 0, 'data' => $data, 'msg' => '获取成功']); } //删除题目 public function delquestion() { $id = input('id', 0); if ($id) { $root_id = request()->employee->root_id; $w[] = ['root_id', '=', $root_id]; $w[] = ['id', '=', $id]; $info = ExamPaperQuestion::with('questions')->where($w)->find()->toArray(); $data['type'] = $info['questions']['type']; $cw[] = ['exam_paper_question.root_id', '=', $root_id]; $cw[] = ['exam_paper_question.paper_id', '=', $info['paper_id']]; $cw[] = ['questions.type', '=', input('type')]; $cw[] = ['questions.root_id', '=', $root_id]; $cw[] = ['exam_paper_question.id', '<>', $id]; $data['count'] = ExamPaperQuestion::withJoin('questions', 'inner')->where($cw)->count(); $res = ExamPaperQuestion::where($w)->delete(); if ($res) { return json(['code' => 0, 'data' => $data, 'msg' => '删除成功']); } else { return json(['code' => 1, 'data' => '删除失败', 'msg' => '删除失败']); } } return json(['code' => 1, 'data' => '删除失败', 'msg' => '删除失败']); } public function paperpublish() { $root_id = request()->employee->root_id; $request = request(); if (!$request->isAjax()) { $org = OrgLogic::struc(request()->employee->root_id); View::assign('org', $org); View::assign('orgids', json_encode($request->org)); View::assign('empid', $request->employee->id); // View::assign('manager', $request->employee->is_manager); $paperid = $request->param('paperid'); View::assign('paperid', $paperid); //及格分数设置 $w[] = ['root_id', '=', $root_id]; $w[] = ['paper_id', '=', $paperid]; $sum = ExamPaperQuestion::where($w)->sum('score'); $sum = $sum ? $sum * 0.6 : 0; $w1[] = ['root_id', '=', $root_id]; $w1[] = ['id', '=', $paperid]; $paperInfo = ExamPaper::where($w1)->field('target_group,base_score,starttime,endtime,checkway,desc,state,approve_employee_ids,scope_org,duringtime')->find()->toArray(); $data['fenshu'] = $sum; $data['starttime'] = date('Y-m-d H:i:s'); $data['endtime'] = date('Y-m-d H:i:s', strtotime('+2 day')); $data['base_score'] = $paperInfo['base_score']; $data['total_score'] = input('total_score', 0); View::assign('data', $data); return View::fetch(); } $param = $request->post(); $paperid = $param['paperid']; $w[] = ['root_id', '=', $root_id]; $w[] = ['paper_id', '=', $paperid]; $sum = ExamPaperQuestion::where($w)->sum('score'); if ($param['base_score'] <= 0 || $param['base_score'] > $sum) { return json(['code' => 1, 'msg' => '及格分设置错误']); } unset($param['paperid']); // $org_id = $param['scope_org'] = 0; $select = input('select', ''); $selects = explode(',', $select); // $attendee_num = EmployeeLogic::count($org_id); $param['attendee_num'] = count($selects); $param['assessment'] = $select; //考试人员 $seconds = strtotime($param['endtime']) - strtotime($param['starttime']); // $param['duringtime'] = ceil($seconds/60); $param['duringtime'] = $param['duringtime']; unset($param['desc']); $a = ExamPaper::update($param, ['id' => $paperid, 'root_id' => $root_id]); if (is_object($a)) { //消息通知 $info = ExamPaper::find($paperid); if ($info['state'] == 1){ foreach($selects as $eid) { event(new Msg($eid, '有新的试卷指派给您!', 'assignPaper', $paperid)); } } return json(['code' => 0, 'paperid' => $paperid, 'msg' => '考卷发布成功']); } else { return json(['code' => 1, 'msg' => '考卷发布失败']); } } public function paperpreview() { $root_id = request()->employee->root_id; $request = request(); if (!$request->isAjax()) { //$org = OrgLogic::struc(request()->employee->root_id); //View::assign('org', $org); //View::assign('orgids', json_encode($request->org)); //View::assign('empid', $request->employee->id); //View::assign('manager', $request->employee->is_manager); $paperid = $request->param('paperid'); View::assign('paperid', $paperid); $paperInfo = ExamPaper::with('questions')->find($paperid)->toArray(); // var_dump($paperInfo);die; foreach ($paperInfo['questions'] as &$v) { if ($v['type'] == '单选' || $v['type'] == '多选') { $v['types'] = 1; $con = json_decode($v['content'], true); $op = []; foreach ($con as $v2) { $op[$v2['title']] = $v2['content']; } $v['content'] = $op; // $v['answer'] = explode(',',$v['answer']); } elseif ($v['type'] == '判断') { $v['types'] = 2; } elseif ($v['type'] == '简答') { $v['types'] = 3; //简单题表字段 内容存的是参考答案 答案字段存的是关键词 $answer = $v['answer']; $content = $v['content']; $v['answer'] = $v['content']; $v['content'] = ''; } } View::assign('info', $paperInfo); return View::fetch(); } } // public function paperedit() // { // if (!Request::isAjax()) { // $paperid = Request::param('paperid'); // View::assign('paperid', $paperid); // $paperfirstinfo = ExamPaper::field('name,for_newbie,desc')->find($paperid); // View::assign('datastr', json_encode($paperfirstinfo)); // return View::fetch(); // } // $root_id = request()->employee->root_id; // $param = Request::param(); // $newdata = [ // 'name' => $param['name'], // 'for_newbie' => (int)$param['for_newbie'], // 'desc' => $param['desc'] // ]; // $w[] = ['root_id', '=', $root_id]; // $w[] = ['id', '=', $param['paperid']]; // ExamPaper::where($w)->update($newdata); // return json(['code' => 0, 'id' => $param['paperid'], 'msg' => '基本信息修改成功']); // } public function papereditquestion() { $root_id = request()->employee->root_id; if (!Request::isAjax()) { $paperid = Request::param('paperid'); View::assign('paperid', $paperid); //查询已经保存的题目 $cw[] = ['root_id', '=', $root_id]; $cw[] = ['paper_id', '=', Request::param('paperid')]; $ids = ExamPaperQuestion::where($cw)->column('question_id'); $wherec[] = ['id', 'in', $ids]; $wherec[] = ['root_id', '=', $root_id]; $wherec[] = ['state', '=', 1]; $data['data2'] = ExamQuestion::where($wherec)->field('id,type,ask,cate')->order('type', 'desc')->select()->toArray(); //查询剩余的题目 $where[] = ['root_id', '=', $root_id]; $where[] = ['id', 'not in', $ids]; $where[] = ['state', '=', 1]; $questList = ExamQuestion::where($where)->field('id,type,ask,cate')->order('type', 'desc')->select()->toArray(); $data['count'] = count($data['data2']); //选中题目的数量 $data['data2'] = json_encode($data['data2']); //分类 $cate_w[] = ['root_id', '=', $root_id]; $cate_w[] = ['name', '=', 'questioncate']; $cate = Setting::where($cate_w)->value('content'); $data['cate'] = $cate ? json_decode($cate, true) : []; View::assign('data', $data); View::assign('questListstr', strip_tags(json_encode($questList))); View::assign('choosedquestListstr', strip_tags($data['data2'])); return View::fetch(); } $param = Request::param(); $idarr = array_unique(explode(',', $param['idstr'])); $old_where[] = ['root_id', '=', $root_id]; $old_where[] = ['paper_id', '=', Request::param('paperid')]; $old_questions = ExamPaperQuestion::with('questions')->where($old_where)->field('question_id,score,id')->select()->toArray(); $types = ['type0' => '单选', 'type1' => '多选', 'type2' => '判断', 'type3' => '简答']; $score = []; foreach ($old_questions as $k => $v) { foreach ($types as $kk => $vv) { if ($v['questions']['type'] == $vv) { $score[$kk] = $v['score']; } } } $dw[] = ['root_id', '=', $root_id]; $dw[] = ['paper_id', '=', Request::param('paperid')]; $del = ExamPaperQuestion::where($dw)->delete(); $new_questions = ExamQuestion::where([['id', 'in', $idarr]])->select(); $save_data = []; foreach ($new_questions as $k => $v) { $once_data['paper_id'] = $param['paperid']; $once_data['question_id'] = $v['id']; $once_data['root_id'] = $root_id; foreach ($types as $kk => $vv) { if ($v['type'] == $vv) { $once_data['score'] = $score[$kk] ?? ''; } } $save_data[] = $once_data; } (new ExamPaperQuestion())->saveAll($save_data); /*foreach($idarr as $qid){ $pivot = new ExamPaperQuestion(); $pivot->save(['paper_id'=>$param['paperid'],'question_id' => $qid,'root_id'=>$root_id]); }*/ // $idarr = explode(',',$param['idstr']); // foreach($idarr as $qid){ // if(!ExamPaperQuestion::where(['paper_id'=>$param['paperid'],'question_id' => $qid])->find()){ // $pivot = new ExamPaperQuestion(); // $pivot->save(['paper_id'=>$param['paperid'],'question_id' => $qid,'root_id'=>$root_id]); // } // } // $qidArr = ExamPaperQuestion::where('paper_id',$param['paperid'])->column('question_id'); // foreach($qidArr as $oldqid){ // if(!in_array($oldqid, $idarr)){ // ExamPaperQuestion::where(['paper_id'=>$param['paperid'],'question_id' => $oldqid])->delete(); // } // } return json(['code' => 0, 'paperid' => $param['paperid'], 'msg' => '修改成功']); } // public function papereditscore() // { // $root_id = request()->employee->root_id; // $typeEn = ['单选' => 'single', '多选' => 'multi', '判断' => 'judge', '简答' => 'answer']; // if (!Request::isAjax()) { // $paperid = Request::param('paperid'); // View::assign('paperid', $paperid); // $w[] = ['root_id', '=', $root_id]; // $w[] = ['paper_id', '=', $paperid]; // // $idsarr = ExamPaperQuestion::where($w)->column('question_id'); // // $types = ExamQuestion::where([['id','in',$idsarr]])->column('type'); // $questions = ExamPaperQuestion::with('questions')->where($w)->field('question_id,score,id')->select()->toArray(); // $types = ['type0' => '单选', 'type1' => '多选', 'type2' => '判断', 'type3' => '简答']; // $sum = array_sum(array_column($questions, 'score')); // foreach ($types as $k => $v) { // $data[$k]['count'] = 0; //分类下题目数量 // $data[$k]['score_sum'] = 0; //合计分值 // $data[$k]['grawth'] = 0; //分值占比 // $data[$k]['score'] = ''; //每题分值 // $data[$k]['questions'] = []; // foreach ($questions as $k2 => $v2) { // $v2['questions']['score'] = $v2['score'] ? $v2['score'] . '分' : ''; // $v2['questions']['del_id'] = $v2['id']; // $v2['questions']['ask'] = strip_tags($v2['questions']['ask']); // if ($v2['questions']['type'] == '简答') { // $v2['questions']['answer_j'] = $v2['questions']['answer']; // $v2['questions']['answer'] = $v2['questions']['content']; // } // if ($v2['questions']['type'] == $v) { // $data[$k]['count'] += 1; // $data[$k]['score_sum'] = $data[$k]['score_sum'] + $v2['score']; // $data[$k]['score'] = $v2['score'] ?: ''; // if ($k == 'type0' || $k == 'type1') { // $v2['questions']['content'] = json_decode($v2['questions']['content'], true); // } // $data[$k]['questions'][] = $v2['questions']; // } // } // $data[$k]['grawth'] = !$sum ? '0%' : round($data[$k]['score_sum'] / $sum * 100, 2) . '%'; // } // $w1[] = ['root_id', '=', $root_id]; // $w1[] = ['id', '=', $paperid]; // $paperInfo = ExamPaper::where($w1)->field('target_group,base_score,starttime,endtime,checkway,desc,state,approve_employee_ids,scope_org,duringtime')->find()->toArray(); // $data['base_score'] = $paperInfo['base_score'] ?: 0; // View::assign('data', $data); // return View::fetch(); // } // $param = Request::param(); // $paperid = $param['paperid']; // $w[] = ['root_id', '=', $root_id]; // $w[] = ['paper_id', '=', $paperid]; // $idsarr = ExamPaperQuestion::where($w)->column('question_id'); // $questions = ExamQuestion::where([['id', 'in', $idsarr]])->select(); // $totalscore = 0; // foreach ($questions as $item) { // ExamPaperQuestion::where(['paper_id' => $paperid, 'question_id' => $item->id, 'root_id' => $root_id])->update(['score' => $param[$typeEn[$item->type]]]); // $totalscore += $param[$typeEn[$item->type]]; // } // $w1[] = ['root_id', '=', $root_id]; // $w1[] = ['id', '=', $paperid]; // ExamPaper::where($w1)->update(['total_score' => $totalscore, 'base_score' => $param['base_score']]); // return json(['code' => 0, 'paperid' => $paperid, 'msg' => '设置成功']); // } /** * 编辑时获取设置分值 */ public function papereditscore() { $root_id = request()->employee->root_id; $paperid = Request::param('paper_id'); $questions = ExamPaperQuestion::with('questions')->where([['root_id', '=', $root_id], ['paper_id', '=', $paperid]])->field('question_id,score,id')->select()->toArray(); $types = ['type0' => '单选', 'type1' => '多选', 'type2' => '判断', 'type3' => '简答']; $sum = array_sum(array_column($questions, 'score')); foreach ($types as $k => $v) { $data[$k]['count'] = 0; //分类下题目数量 $data[$k]['score_sum'] = 0; //合计分值 $data[$k]['grawth'] = 0; //分值占比 $data[$k]['score'] = 0; //每题分值 $data[$k]['questions'] = []; foreach ($questions as $k2 => $v2) { $v2['questions']['score'] = $v2['score']; $v2['questions']['del_id'] = $v2['id']; $v2['questions']['ask'] = strip_tags($v2['questions']['ask']); if ($v2['questions']['type'] == '简答') { $v2['questions']['answer_j'] = $v2['questions']['answer']; $v2['questions']['answer'] = $v2['questions']['content']; } if ($v2['questions']['type'] == $v) { $data[$k]['count'] += 1; $data[$k]['score_sum'] = $data[$k]['score_sum'] + $v2['score']; $data[$k]['score'] = $v2['score'] ?: ''; if ($k == 'type0' || $k == 'type1') { $v2['questions']['content'] = json_decode($v2['questions']['content'], true); } $data[$k]['questions'][] = $v2['questions']; } } $data[$k]['grawth'] = !$sum ? '0%' : round($data[$k]['score_sum'] / $sum * 100, 2) . '%'; } $paperInfo = ExamPaper::where([['root_id', '=', $root_id], ['id', '=', $paperid]])->field('target_group,total_score,base_score,starttime,endtime,checkway,desc,state,approve_employee_ids,scope_org,duringtime')->find(); $data['base_score'] = $paperInfo && $paperInfo['base_score'] ? $paperInfo['base_score'] : 0; $data['total_score'] = $paperInfo && $paperInfo['total_score'] ? $paperInfo['total_score'] : 0; return json(['code' => 0, 'data' => $data, 'msg' => '获取成功']); } public function papereditpublish() { $root_id = request()->employee->root_id; $request = request(); if (!$request->isAjax()) { $org = OrgLogic::struc(request()->employee->root_id); View::assign('org', $org); View::assign('orgids', json_encode($request->org)); View::assign('empid', $request->employee->id); // View::assign('manager', $request->employee->is_manager); $paperid = $request->param('paperid'); View::assign('paperid', $paperid); $w1[] = ['root_id', '=', $root_id]; $w1[] = ['id', '=', $paperid]; $paperInfo = ExamPaper::where($w1)->field('target_group,base_score,starttime,endtime,checkway,desc,state,approve_employee_ids,scope_org,duringtime,assessment')->find()->toArray(); $paperInfo['state'] = (string)$paperInfo['state']; $data['duringtime'] = $paperInfo['duringtime'] = $paperInfo['duringtime'] ?: 45; View::assign('paperinfostr', json_encode($paperInfo)); $data['base_score'] = $paperInfo['base_score']; $data['total_score'] = input('total_score', 0); $data['assessment'] = $paperInfo['assessment']; View::assign('data', $data); return View::fetch(); } $param = input('post.'); //exit; $paperid = $param['paperid']; $w[] = ['root_id', '=', $root_id]; $w[] = ['paper_id', '=', $paperid]; $sum = ExamPaperQuestion::where($w)->sum('score'); if ($param['base_score'] <= 0 || $param['base_score'] > $sum) { return json(['code' => 1, 'msg' => '及格分设置错误']); } unset($param['paperid']); // $org_id = $param['scope_org'] = 0; // $attendee_num = EmployeeLogic::count($org_id); $select = input('select', ''); $param['attendee_num'] = count(explode(',', $select)); $param['assessment'] = $select; unset($param['select']); // $seconds = strtotime($param['endtime']) - strtotime($param['starttime']); // $param['duringtime'] = ceil($seconds/60); $w1[] = ['root_id', '=', $root_id]; $w1[] = ['id', '=', $paperid]; $info = ExamPaper::where($w1)->find(); ExamPaper::where($w1)->update($param); if ($info['state'] == 1){ $old_select = array_filter(explode(',', $info['assessment'])); $new_select = array_filter(explode(',', $select)); $diff_select = array_diff($new_select, $old_select); foreach($diff_select as $eid) { event(new Msg($eid, '有新的试卷指派给您!', 'assignPaper', $paperid)); } } return json(['code' => 0, 'paperid' => $paperid, 'msg' => '考卷发布成功']); } ///批卷部分 public function checklist() { $root_id = request()->employee->root_id; $request = request(); $paperid = input('paperid'); if (!$request->isAjax()) { $org = OrgLogic::struc(request()->employee->root_id); View::assign('org', $org); View::assign('orgids', json_encode($request->org)); View::assign('empid', $request->employee->id); // View::assign('manager', $request->employee->is_manager); $pw[] = ['root_id', '=', $root_id]; $paperlist = ExamPaper::where($pw)->field('id,name')->select()->toArray(); $w[] = ['state', '=', '在职']; $w[] = ['root_id', '=', request()->employee->root_id]; $employee = Employee::where($w)->field('id,name')->select()->toArray(); $data['employee'] = $employee; View::assign('data', $data); view::assign('paperid', $paperid); View::assign('paperlist', $paperlist); return View::fetch(); } $param = Request::param(); $where[] = ['state', '=', 1]; $where[] = ['root_id', '=', $root_id]; if (isset($param['paper_id']) && $param['paper_id']) { $where[] = ['paper_id', '=', $param['paper_id']]; } if (isset($param['eid']) && $param['eid']) { $where[] = ['employee_id', '=', $param['eid']]; } if (isset($param['approve_status']) && $param['approve_status'] !== '') { $where[] = ['approve_status', '=', $param['approve_status']]; } $where[] = ['from', 'in', [0, 1]]; $list = ExamEmpResult::where($where)->with(['paper', 'employee'])->page($param['page'], $param['limit'])->order('addtime desc')->select(); $count = ExamEmpResult::where($where)->count(); return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']); } public function checkdetail() { $root_id = request()->employee->root_id; $request = request(); if (!$request->isAjax()) { $resultid = $request->param('resultid'); $w[] = ['id', '=', $resultid]; // $w[] = ['from','=',0]; $empresult = ExamEmpResult::with(['employee', 'employees'])->where($w)->find(); if(empty($empresult->p_name)){ $empresult->p_name = Employee::where([['root_id','=',$root_id],['state','=','在职'],['id','=',$empresult->aprove_employee_id]])->value('opt_name'); } View::assign('result_checked', $empresult->checked); View::assign('employee_name', $empresult->emp_name); View::assign('final_score', $empresult->final_score); View::assign('time_spend', $empresult->time_spend); View::assign('resultid', $resultid); View::assign('result', $empresult); //批改详情 $correct = $empresult->data ? json_decode($empresult->data,true) : []; $emp_answer_arr = []; $emp_answer_json = $empresult->answer; if ($emp_answer_json) { $emp_answer_arr = json_decode($emp_answer_json, true); } $paperInfo = ExamPaper::with('questions')->find($empresult->paper_id)->toArray(); $paperInfo['got_total_score'] = 0; foreach ($paperInfo['questions'] as &$item) { $item['ask'] = strip_tags($item['ask']); //$item['content'] = strip_tags($item['content']); $item['content'] = $item['content']; $item['emp_answer'] = isset($emp_answer_arr[$item['id']]) ? $emp_answer_arr[$item['id']] : ''; //填写的答案 if ($item['type'] == '单选' || $item['type'] == '多选' || $item['type'] == '判断') { if (isset($emp_answer_arr[$item['id']]) && $emp_answer_arr[$item['id']]) { $item['got_score'] = ($emp_answer_arr[$item['id']] == $item['answer']) ? $item['pivot']['score'] : 0; } else { $item['got_score'] = 0; } } elseif ($item['type'] == '简答') { if (isset($emp_answer_arr[$item['id']]) && $emp_answer_arr[$item['id']]) { $calldata = ExamEmpResult::autoPaperAnswerCheck($emp_answer_arr[$item['id']], $item['answer'], $item['pivot']['score']); $item['got_score'] = $calldata['got_score']; } else { $item['got_score'] = 0; } } $paperInfo['got_total_score'] += $item['got_score']; //得分总和 if ($item['type'] == '单选' || $item['type'] == '多选') { $item['types'] = 1; $con = json_decode($item['content'], true); $op = []; foreach ($con as $v2) { $op[$v2['title']] = $v2['content']; } $item['content'] = $op; // $v['answer'] = explode(',',$v['answer']); } elseif ($item['type'] == '判断') { $item['types'] = 2; } elseif ($item['type'] == '简答') { $item['types'] = 3; } //考生得分 $item['correct_score'] = 0; if (isset($correct[$item['id']])) { $item['correct_score'] = is_array($correct[$item['id']]) ? $correct[$item['id']]['score'] : $correct[$item['id']]; } } View::assign('info', $paperInfo); return View::fetch(); } } /** * 试卷的试题列表 */ public function correct_papers_question_list() { $root_id = request()->employee->root_id; $param = request()->only(['paper_id' => 0, 'employee_id','resultid'=>0]); //是否参加考试 $w[] = ['paper_id', '=', $param['paper_id']]; $w[] = ['employee_id', '=', $param['employee_id']]; $w[] = ['root_id', '=', $root_id]; $w[] = ['from', 'in', [0, 1]]; $empresult = ExamEmpResult::where($w)->with(['employee'])->find(); if (!$empresult) return json(['code' => 1, 'msg' => '没有参加考试']); //批改详情 $correct = $empresult->data ? json_decode($empresult->data, true) : []; //试卷题目列表 $emp_answer_arr = []; $emp_answer_json = $empresult->answer; if ($emp_answer_json) { $emp_answer_arr = json_decode($emp_answer_json, true); } $paperInfo = ExamPaper::with('questions')->find($param['paper_id'])->toArray(); $paperInfo['got_total_score'] = 0; // $options = []; $is_answer = 0; foreach ($paperInfo['questions'] as &$item) { $item['ask'] = str_replace('\\', '', $item['ask']); $item['emp_answer'] = isset($emp_answer_arr[$item['id']]) ? $emp_answer_arr[$item['id']] : ''; //填写的答案 //得分 if ($item['type'] == '单选' || $item['type'] == '多选') { if (isset($emp_answer_arr[$item['id']]) && $emp_answer_arr[$item['id']]) { $item['got_score'] = ($emp_answer_arr[$item['id']] == $item['answer']) ? $item['pivot']['score'] : 0; $item['c_type'] = $item['got_score'] > 0 ? 1 : 0; //1正确,0错误 } else { $item['got_score'] = 0; } $con = json_decode($item['content'], true); $op = []; foreach ($con as $v2) { $op[$v2['title']] = $v2['content']; } $item['content'] = $op; $item['types'] = 1; // } elseif ($item['type'] == '判断') { if (isset($emp_answer_arr[$item['id']]) && $emp_answer_arr[$item['id']]) { $item['got_score'] = ($emp_answer_arr[$item['id']] == $item['answer']) ? $item['pivot']['score'] : 0; $item['c_type'] = $item['got_score'] > 0 ? 1 : 0; //1正确,0错误 } else { $item['got_score'] = 0; } $item['types'] = 2; } elseif ($item['type'] == '简答') { if (isset($emp_answer_arr[$item['id']]) && $emp_answer_arr[$item['id']]) { $calldata = ExamEmpResult::autoPaperAnswerCheck($emp_answer_arr[$item['id']], $item['answer'], $item['pivot']['score']); $item['got_score'] = $calldata['got_score']; } else { $item['got_score'] = 0; } $item['c_type'] = $item['got_score'] > 0 ? 1 : 0; //1正确,0错误 //简单题表字段 内容存的是参考答案 答案字段存的是关键词 $answer = $item['answer']; $content = $item['content']; $item['answer'] = $item['content']; $item['content'] = $item['ask']; $item['types'] = 3; $is_answer = 1; } $paperInfo['got_total_score'] += $item['got_score']; //得分总和 //是否批改过 $item['is_correct'] = isset($correct[$item['id']]) ? 1 : 0; //批改分数 $item['correct_score'] = 0; if (isset($correct[$item['id']])) { $item['correct_score'] = is_array($correct[$item['id']]) ? $correct[$item['id']]['score'] : $correct[$item['id']]; } $options[] = $item; } View::assign('is_answer',$is_answer); View::assign('options',$options); View::assign('resultid',$param['resultid']); View::assign('employee_id',$param['employee_id']); View::assign('paperid',$param['paper_id']); return View::fetch(); //return json(['code' => 0, 'data' => $options, 'msg' => '获取成功']); } /** * 批改试卷 */ public function correct_papers() { $root_id = request()->employee->root_id; $param = request()->only(['paperid' => 0, 'employee_id','answer'=>'','result_id']); //权限验证 //$paper = ExamPaper::with('questions')->where('id', $param['paperid'])->whereRaw("FIND_IN_SET(" . request()->employee->id. " , approve_employee_ids)")->find(); //if (!$paper) return json(['code' => 1, 'msg' => '无权限批改']); $paper = ExamPaper::with('questions')->where('id', $param['paperid'])->find(); if ($paper->checkway == 'sys') return json(['code' => 1, 'msg' => '该试卷由系统批改']); //是否参加考试 $w[] = ['paper_id', '=', $param['paperid']]; $w[] = ['employee_id', '=', $param['employee_id']]; $w[] = ['root_id', '=', $root_id]; $w[] = ['id', '=', $param['result_id']]; $w[] = ['approve_status', 'in', [0, 1]]; // 只查询考试完毕,未批改的试卷 $empresult = ExamEmpResult::where($w)->with(['employee'])->find(); if (!$empresult) return json(['code' => 1, 'msg' => '未考试或已批改']); if ($empresult['state'] == 0) return json(['code' => 1, 'msg' => '答题失效,无法批改']); if ($empresult['approve_status'] == 0 && $empresult->aprove_employee_id != 0 && request()->employee->id != $empresult->aprove_employee_id) { return json(['code' => 1, 'msg' => '试卷已在批改中,请选择其他试卷进行批改']); } // 题目卷面分(人工批改) $answer = json_decode($param['answer'], true); $aids = array_column($answer,'id'); $ascore = array_column($answer,'mannal'); $json = array_combine($aids,$ascore); //考生填写的答案 $jsona = $empresult->answer; $jsona = json_decode($jsona, true); // 分数结果 $data = []; $score = 0; foreach ($paper->questions as $question) { $questionScore = 0; // 系统打分 if (isset($jsona[$question->id])) $questionScore = $this->fraction($jsona[$question->id], $question->answer, $question->type, (int)$question->pivot->score); if (isset($json[$question->id])) { // 人工打分 $questionScore = $json[$question->id]; $data[$question->id] = [ 'id' => $question->id, 'score' => $questionScore, 'time' => date('Y-m-d H:i:s'), ]; } $score += $questionScore; } $u['answer'] = json_encode($jsona); $u['final_score'] = $score; //确认批改完成 $u['approve_status'] = 2; $u['data'] = json_encode($data); $u['aprove_employee_id'] = request()->employee->id; $u['aprove_time'] = date('Y-m-d H:i:s'); ExamEmpResult::where($w)->update($u); //发送消息通知 event(new Msg($param['employee_id'], '您提交的试卷“' . $paper['name'] . '”' . '已批改', 'correctionPaper', $paper['id'])); //下一张,待批改的试卷 // $w = []; // $root_id = $token['root_org']; // $w[] = ['exam_emp_result.root_id', '=', $root_id]; // $w[] = ['paper.root_id', '=', $root_id]; // $w[] = ['exam_emp_result.state', '=', 1]; // $w[] = ['exam_emp_result.approve_status', '<>', 2]; // $w[] = ['paper.id', '=', $paperid]; // $id = ExamEmpResult::withJoin('paper')->where($w)->whereRaw("FIND_IN_SET(" . $token['employee_id'] . " , approve_employee_ids)")->order('exam_emp_result.id asc')->find(); // if ($id) { // $id = $id->toArray(); // } else { // $id = []; // } return json(['code' => 0, 'data' =>'', 'msg' => '批改成功']); //return View::fetch(); } //计算分数 public function fraction($emp_answer, $answer, $type, $score = 0, $requestid = 0, $questionsid = 0) { $emp_score = 0; if (!$emp_answer) return $emp_score; if ($type == '判断') { if (trim($answer) == trim($emp_answer)) { $emp_score = $score; } } elseif ($type == '单选') { if ($answer == $emp_answer) { $emp_score = $score; } } elseif ($type == '多选') { $answer = explode(',', $answer); $emp_answer = explode(',', $emp_answer); sort($answer); sort($emp_answer); if (implode(',', $answer) == implode(',', $emp_answer)) { $emp_score = $score; } else { $danf = $score / count($answer); $i = 0; foreach ($emp_answer as $k => $v) { if (in_array($v, $answer)) { $i += 1; } } $emp_score = $i * $danf; } } elseif ($type == '简答' || $type == '问答') { $emp_score = $answer = ExamEmpResult::autoPaperAnswerCheck($emp_answer, $answer, $score); $emp_score = $emp_score['got_score']; $emp_answer = $answer['emp_answer']; if ($requestid && $questionsid) { $tw[] = ['id', '=', $requestid]; $tw[] = ['from', '=', 0]; $json = ExamEmpResult::where($tw)->value('answer'); $json = json_decode($json, true); if (isset($json[$questionsid])) { $json[$questionsid] = $emp_answer; ExamEmpResult::where($tw)->update(['answer' => json_encode($json)]); } } } return $emp_score; } /** * 修改视图 */ public function makecheck() { $root_id = request()->employee->root_id; $request = request(); $param = $request->post(); $updatedata = [ 'check_result' => $param['data'], 'final_score' => $param['finalscore'], 'checked' => 1 ]; $w1[] = ['root_id', '=', $root_id]; $w1[] = ['id', '=', $param['resultid']]; $w1[] = ['from', '=', 0]; if (ExamEmpResult::where($w1)->update($updatedata)) { return json(['code' => 0, 'msg' => '审核成功']); } else { return json(['code' => 1, 'msg' => '审核失败']); } } public function tree($data, $pid = 0, $persons) { $new_arr = []; foreach ($data as $k => $v) { if ($v['pid'] == $pid) { $persions = isset($persons[$v['id']]) ? $persons[$v['id']] : []; $children = $this->tree($data, $v['id'], $persons); $v['children'] = array_merge_recursive($children, $persions); if (empty($v['children'])) $v['disabled'] = true; $new_arr[] = $v; } } return $new_arr; } /* * 审核人员 */ public function get_person() { $ids = input('id', ''); $root_id = request()->employee->root_id; $type = input('type', 0); //树形 if ($type == 1) { //所有员工 $w1[] = ['root_id', '=', $root_id]; $w1[] = ['state', '=', '在职']; $w1[] = ['uid', '<>', 0]; $w1[] = ['org_id', '>', 0]; $employee = Employee::where($w1)->field('id value,name,org_id')->select()->toArray(); if ($ids) { $ids = explode(',', $ids); foreach ($employee as $k => &$v) { $v['selected'] = in_array($v['value'], $ids); } } else { foreach ($employee as $k => &$v) { $v['selected'] = false; } } $persons = []; foreach ($employee as $k => $v1) { $persons[$v1['org_id']][] = $v1; } $where = [ ['path', 'like', $root_id . '-%'], ['status', '=', 1] ]; $allnodes = Org::where($where)->field('id value,id,pid,name')->order('level asc, id asc')->select()->toArray(); $tree = $this->tree($allnodes, 0, $persons); return json($tree); } $w[] = ['root_id', '=', $root_id]; $w[] = ['state', '=', '在职']; $w[] = ['role', '=', '领导']; $w[] = ['uid', '<>', 0]; $employee = Employee::where($w)->field('id value,name')->select()->toArray(); if ($ids) { $ids = explode(',', $ids); foreach ($employee as $k => &$v) { $v['selected'] = in_array($v['value'], $ids); } } else { foreach ($employee as $k => &$v) { $v['selected'] = false; } } return json($employee); } /* * 发布 */ public function show() { $id = input('id', ''); $info = $paper = ExamPaper::where('id', $id)->find(); if ($info->state == 0) { ExamPaper::where('id', $id)->update(['state' => 1]); $selects = explode(',', $info['assessment']); foreach($selects as $eid) { event(new Msg($eid, '有新的试卷指派给您!', 'assignPaper', $id)); } return json(['code' => 0, 'msg' => '发布成功']); } $root_id = request()->employee->root_id; $w = []; $w[] = ['exam_emp_result.root_id', '=', $root_id]; $w[] = ['paper.root_id', '=', $root_id]; // $w[] = ['exam_emp_result.state','=',1]; // $w[] = ['exam_emp_result.approve_status','<>',2]; $w[] = ['paper.id', '=', $id]; $info = ExamEmpResult::where('paper_id', $id)->find(); $time = date('Y-m-d H:i:s'); if ($info && $paper['starttime'] <= $time && $paper['endtime'] >= $time) { return json(['code' => 1, 'msg' => '试卷正在考试中无法修改']); } $res = ExamPaper::where('id', $id)->update(['state' => 0]); return json(['code' => 0, 'msg' => '取消成功']); } public function statistics() { $paperid = input('paperid', 0); $root_id = request()->employee->root_id; if (!Request::isAjax() && input('ajax', 0) != 1) { View::assign('paperid', $paperid); $info = ExamPaper::where('id', $paperid)->find()->toArray(); $data['count'] = $info['assessment'] ? count(explode(',', $info['assessment'])) : 0; //应考总数 $tw[] = ['paper_id', '=', $paperid]; $tw[] = ['from', '=', 0]; $data['result_count'] = ExamEmpResult::where($tw)->count(); //实际考试数量 $data['que_count'] = $data['count'] - $data['result_count']; //缺考人数 $data['que_count'] = $data['que_count'] >= 0 ? $data['que_count'] : 0; $w[] = ['paper_id', '=', $paperid]; $w[] = ['root_id', '=', $root_id]; $w[] = ['approve_status', '=', 2]; $w[] = ['from', '=', 0]; $result = ExamEmpResult::where($w)->field('employee_id,paper_id,final_score,state,addtime')->order('final_score desc')->select()->toArray(); $data['jg_count'] = $data['bjg_count'] = $data['avg'] = $data['top_score'] = $data['down_score'] = 0; $data['grawth'] = '0%'; if ($result) { $data['down_score'] = 999999999999; foreach ($result as $k => $v) { if ($v['final_score'] >= $info['base_score']) { $data['jg_count'] += 1; } else { $data['bjg_count'] += 1; } if ($v['final_score'] > $data['top_score']) { $data['top_score'] = $v['final_score']; } if ($v['final_score'] < $data['down_score']) { $data['down_score'] = $v['final_score']; } } $data['avg'] = round(array_sum(array_column($result, 'final_score')) / count($result), 2); $data['grawth'] = round($data['jg_count'] / ($data['jg_count'] + $data['bjg_count']) * 100, 2) . '%'; } View::assign('data', $data); $org = Org::where([['path', 'like', request()->employee->root_id . '-%']])->field('id,name')->select()->toArray(); View::assign('org', $org); return View::fetch(); } $type = input('type', 0); $page = input('page', 0); $limit = input('limit', 0); $org = input('org', 0); $ew[] = ['id', '=', $paperid]; $ew[] = ['root_id', '=', $root_id]; $info = ExamPaper::where($ew)->find(); if (!$info) return json(['code' => 1, 'data' => '未查询到该试卷', 'msg' => '未查询到该试卷']); // if (empty($info->assessment)) return json(['code' => 0,'data'=>[],'count'=>0]); if ($org) { $w[] = ['org_id', '=', $org]; } $w[] = ['id', 'in', explode(',', $info->assessment)]; $w[] = ['root_id', '=', $root_id]; $res = Employee::with(['examResult' => function ($query) use ($paperid) { $query->where([['paper_id', '=', $paperid]])->bind(['final_score', 'addtime', 'approve_status']); // ->visible(['approve_status','final_score','addtime']); }])->where($w)->field('id,name')->select(); $complete = $nocomplete = []; foreach ($res as $k => $v) { if ($v['approve_status'] == 2) { $complete[] = $v; } else { $v['addtime'] = '-------'; $v['final_score'] = 0; $nocomplete[] = $v; } } array_multisort(array_column($complete, 'final_score'), SORT_DESC, $complete); array_multisort(array_column($nocomplete, 'id'), SORT_DESC, $nocomplete); $res = array_merge($complete, $nocomplete); foreach ($res as $k2 => $v2) { $res[$k2]['ranking'] = $k2 + 1; if ($type == 1 && $v2['final_score'] < $info->base_score) unset($res[$k2]); if ($type == 2 && $v2['final_score'] >= $info->base_score) unset($res[$k2]); } $count = count($res); $res = array_slice($res, ($page - 1) * $limit, $limit); return json(['code' => 0, 'data' => $res, 'count' => $count, 'msg' => '']); } /** * 获取试题列表 */ public function getExamQuestion(){ $root_id = request()->employee->root_id; $questList = ExamQuestion::where([['root_id', '=', $root_id], ['state', '=', 1]])->field('id,type,ask,cate')->order('type', 'desc')->select()->toArray(); return json(['code'=> 0, 'data'=> $questList, 'msg'=> '获取成功']); } /** * 创建试卷 */ public function paperadd() { $root_id = request()->employee->root_id; if (!Request::isAjax()) { $data = []; //查询题目 // $questList = ExamQuestion::where([['root_id', '=', $root_id], ['state', '=', 1]])->field('id,type,ask,cate')->order('type', 'desc')->select()->toArray(); $data['data2'] = json_encode([]); //分类 $cate = Setting::where([['root_id', '=', $root_id], ['name', '=', 'questioncate']])->value('content'); $data['cate'] = $cate ? json_decode($cate, true) : []; // View::assign('questListstr', strip_tags(json_encode($questList))); //发布考试 $org = OrgLogic::struc($root_id); View::assign('org', $org); View::assign('orgids', json_encode(request()->org)); View::assign('empid', request()->employee->id); $data['starttime'] = date('Y-m-d H:i:s'); $data['endtime'] = date('Y-m-d H:i:s', strtotime('+2 day')); $data['total_score'] = input('total_score', 0); View::assign('data', $data); return View::fetch(); } $param = request()->only(['name', 'for_newbie', 'desc', 'idstr', 'total_score', 'base_score', 'single' => '', 'multi' => '', 'judge' => '', 'answer' => '', 'select', 'target_group', 'duringtime', 'starttime', 'endtime', 'checkway', 'state', 'approve_employee_ids', 'show_real_name']); if (!trim($param['name'])) return json(['code' => 1, 'msg' => '请填写考卷名称']); $type_questions = ExamQuestion::where([['id', 'in', $param['idstr']], ['root_id', '=', $root_id]])->column('type'); if($param['checkway'] == 'employee' && !in_array('简答',$type_questions)){ return json(['code' => 1, 'msg' => '人工审核时,必须选择简答题']); } $examPaperAdd = [ 'name' => $param['name'], 'for_newbie' => $param['for_newbie'], 'desc' => $param['desc'], 'root_id' => $root_id, 'state' => 0, 'approve_employee_ids' => '', 'assessment' => '' ]; Db::startTrans(); try { //试卷 $new_paper_id = ExamPaper::insertGetId($examPaperAdd); //2023-01-30 修改逻辑 每道题可以单独设置分数 fraction=1@1,2@2 $scores = request()->only(['fraction'=>'']); if(empty($scores['fraction'])) return json(['code' => 1, 'msg' => '请完善题目分数']); $scores = explode(',',$scores['fraction']); $questionIds = []; foreach ($scores as $key => $val) { $ls = explode('@',$val); if(!isset($ls[1]) || $ls[1]=='0') return json(['code' => 1, 'msg' => '题目分数不能为0分']); $questionIds[$ls[0]] = $ls[1]; } $param['idstr'] = implode(',',array_keys($questionIds)); $totalscore = array_sum(array_values($questionIds)); //试题 $idarr = array_unique(explode(',', $param['idstr'])); $new_questions = ExamQuestion::where([['id', 'in', $idarr], ['root_id', '=', $root_id]])->select(); $save_data = []; foreach ($new_questions as $k => $v) { $once_data['paper_id'] = $new_paper_id; $once_data['question_id'] = $v['id']; $once_data['root_id'] = $root_id; $once_data['score'] = $questionIds[$v['id']]; $save_data[] = $once_data; } (new ExamPaperQuestion())->saveAll($save_data); //设置分值 // $typeEn = ['单选' => 'single', '多选' => 'multi', '判断' => 'judge', '简答' => 'answer']; // $questions = ExamQuestion::where([['id', 'in', $idarr], ['root_id', '=', $root_id]])->select(); // $totalscore = 0; // foreach ($questions as $item) { // if (isset($param[$typeEn[$item->type]]) && !empty($param[$typeEn[$item->type]])) { // ExamPaperQuestion::where(['paper_id' => $new_paper_id, 'question_id' => $item->id, 'root_id' => $root_id])->update(['score' => $param[$typeEn[$item->type]]]); // $totalscore += $param[$typeEn[$item->type]]; // } // } ExamPaper::where([['root_id', '=', $root_id], ['id', '=', $new_paper_id]])->update(['total_score' => $totalscore, 'base_score' => $param['base_score']]); //保存试卷 if ($param['base_score'] <= 0 || $param['base_score'] > $totalscore) { return json(['code' => 1, 'msg' => '及格分设置错误']); } $selects = explode(',', $param['select']); ExamPaper::where(['id' => $new_paper_id, 'root_id' => $root_id])->update([ 'assessment' => $param['select'], //考试人员 'attendee_num' => count($selects), 'duringtime' => $param['duringtime'], 'starttime' => $param['starttime'], 'endtime' => $param['endtime'], 'target_group' => $param['target_group'], 'base_score' => $param['base_score'], 'state' => $param['state'], 'checkway' => $param['checkway'], 'approve_employee_ids' => $param['approve_employee_ids'], 'show_real_name' => $param['show_real_name'] ]); //消息通知 $state = ExamPaper::where('id', $new_paper_id)->value('state'); if ($state == 1){ foreach($selects as $eid) { event(new Msg($eid, '有新的试卷指派给您!', 'assignPaper', $new_paper_id)); } } Db::commit(); return json(['code' => 0, 'msg' => '考卷发布成功']); } catch (\Exception $e) { Db::rollback(); return json(['code' => 1, 'msg' => '考卷发布失败']); } } /** * 试卷编辑 */ public function paperedit() { $root_id = request()->employee->root_id; if (!Request::isAjax()) { $paperid = Request::param('paperid'); View::assign('paperid', $paperid); $data = ExamPaper::find($paperid); $data['duringtime'] = $data['duringtime'] ?: 45; //分类 $cate = Setting::where([['root_id', '=', $root_id], ['name', '=', 'questioncate']])->value('content'); $data['cate'] = $cate ? json_decode($cate, true) : []; //左侧试题列表 $ids = ExamPaperQuestion::where([['root_id', '=', $root_id], ['paper_id', '=', $paperid]])->column('question_id'); $questList = ExamQuestion::where([['root_id', '=', $root_id], ['id', 'not in', $ids], ['state', '=', 1]])->field('id,type,ask,cate')->order('type', 'desc')->select()->toArray(); View::assign('questListstr', strip_tags(json_encode($questList))); //右侧试题列表 $choosedquestListstr = ExamQuestion::where([['root_id', '=', $root_id], ['id', 'in', $ids], ['state', '=', 1]])->field('id,type,ask,cate')->order('type', 'desc')->select()->toArray(); View::assign('choosedquestListstr', strip_tags(json_encode($choosedquestListstr))); //选择题目数量 $data['count'] = count($choosedquestListstr); //考试人员 $org = OrgLogic::struc(request()->employee->root_id); View::assign('org', $org); View::assign('orgids', json_encode(request()->org)); View::assign('data', $data); return View::fetch(); } $param = Request::param(); $param = request()->only(['paper_id', 'name', 'for_newbie', 'desc', 'idstr', 'total_score', 'base_score', 'single' => '', 'multi' => '', 'judge' => '', 'answer' => '', 'select', 'target_group', 'duringtime', 'starttime', 'endtime', 'checkway', 'state', 'approve_employee_ids', 'show_real_name']); if (!trim($param['name'])) return json(['code' => 1, 'msg' => '请填写考卷名称']); $examPaperEdit = [ 'name' => $param['name'], 'for_newbie' => $param['for_newbie'], 'desc' => $param['desc'], ]; Db::startTrans(); try { //试卷 ExamPaper::where(['id'=>$param['paper_id'], 'root_id'=>$root_id])->update($examPaperEdit); //试题 ExamPaperQuestion::where([['root_id', '=', $root_id], ['paper_id', '=', $param['paper_id']]])->delete(); $idarr = array_unique(explode(',', $param['idstr'])); //2023-01-30 修改逻辑 每道题可以单独设置分数 fraction=1@1,2@2 $scores = request()->only(['fraction'=>'']); if(empty($scores['fraction'])) return json(['code' => 1, 'msg' => '请完善题目分数']); $scores = explode(',',$scores['fraction']); $questionIds = []; foreach ($scores as $key => $val) { $ls = explode('@',$val); if(!isset($ls[1]) || $ls[1]=='0') return json(['code' => 1, 'msg' => '题目分数不能为0分']); $questionIds[$ls[0]] = $ls[1]; } $idarr = array_keys($questionIds); $totalscore = array_sum(array_values($questionIds)); $new_questions = ExamQuestion::where([['id', 'in', $idarr], ['root_id', '=', $root_id]])->select(); $save_data = []; foreach ($new_questions as $k => $v) { $once_data['paper_id'] = $param['paper_id']; $once_data['question_id'] = $v['id']; $once_data['root_id'] = $root_id; $once_data['score'] = $questionIds[$v['id']]; $save_data[] = $once_data; } (new ExamPaperQuestion())->saveAll($save_data); //设置分值 // $typeEn = ['单选' => 'single', '多选' => 'multi', '判断' => 'judge', '简答' => 'answer']; // $questions = ExamQuestion::where([['id', 'in', $idarr], ['root_id', '=', $root_id]])->select(); // $totalscore = 0; // foreach ($questions as $item) { // if (isset($param[$typeEn[$item->type]]) && !empty($param[$typeEn[$item->type]])) { // ExamPaperQuestion::where(['paper_id' => $param['paper_id'], 'question_id' => $item->id, 'root_id' => $root_id])->update(['score' => $param[$typeEn[$item->type]]]); // $totalscore += $param[$typeEn[$item->type]]; // } // } ExamPaper::where([['root_id', '=', $root_id], ['id', '=', $param['paper_id']]])->update(['total_score' => $totalscore, 'base_score' => $param['base_score']]); //保存试卷 if ($param['base_score'] <= 0 || $param['base_score'] > $totalscore) { return json(['code' => 1, 'msg' => '及格分设置错误']); } //原有指派人员 $exmaPaperAssessment = ExamPaper::where(['id' => $param['paper_id'], 'root_id' => $root_id])->value('assessment'); $selects = explode(',', $param['select']); ExamPaper::where(['id' => $param['paper_id'], 'root_id' => $root_id])->update([ 'assessment' => $param['select'], //考试人员 'attendee_num' => count($selects), 'duringtime' => $param['duringtime'], 'starttime' => $param['starttime'], 'endtime' => $param['endtime'], 'target_group' => $param['target_group'], 'base_score' => $param['base_score'], 'state' => $param['state'], 'checkway' => $param['checkway'], 'approve_employee_ids' => $param['approve_employee_ids'], 'show_real_name' => $param['show_real_name'] ]); //消息通知 if ($param['state'] == 1){ $old_select = array_filter(explode(',', $exmaPaperAssessment)); $new_select = array_filter(explode(',', $param['select'])); $diff_select = array_diff($new_select, $old_select); foreach($diff_select as $eid) { event(new Msg($eid, '有新的试卷指派给您!', 'assignPaper', $param['paper_id'])); } } Db::commit(); return json(['code' => 0, 'msg' => '考卷发布成功']); } catch (\Exception $e) { Db::rollback(); return json(['code' => 1, 'msg' => '考卷发布失败']); } } }