123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490 |
- <?php
- namespace app\admin\controller\train;
- use app\admin\model\train\QuestionModel;
- use app\admin\model\train\TrainAdmin;
- use app\admin\model\train\TrainExamReAdmin;
- use app\admin\model\train\TrainExamReQuestion;
- use app\admin\model\train\TrainExamResult;
- use app\admin\model\train\TrainExamResultRecord;
- use app\admin\library\DingTalk;
- use app\admin\service\train\TrainService;
- use app\common\controller\Backend;
- use think\Db;
- use think\Exception;
- use think\exception\PDOException;
- use think\exception\ValidateException;
- /**
- * 培训-考试
- *
- * @icon fa fa-circle-o
- */
- class Exam extends Backend
- {
-
- /**
- * TrainExam模型对象
- * @var \app\admin\model\train\TrainExam
- */
- protected $model = null;
- protected $noNeedRight = ['managequestions', 'examquestionlist', 'removequestions', 'adminindex', 'manageadmins', 'examadminlist', 'removeadmins', 'pushexam', 'setscore', 'updateexamtotalscore', 'showmistakes', 'getexam'];
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\train\TrainExam;
- }
-
- public function manageQuestions($ids)
- {
- if($this->request->isAjax()){
- $question_ids = $this->request->param('question_ids/a');
- $exam_question_ids = model(TrainExamReQuestion::class)
- ->where('exam_id', $ids)
- ->column('question_id');
- $new_question_ids = array_diff($question_ids, $exam_question_ids);
- $save = [];
- foreach ($new_question_ids as $question_id){
- $save[] = [
- 'exam_id' => $ids,
- 'question_id' => $question_id,
- ];
- }
- $res = model(TrainExamReQuestion::class)->insertAll($save);
- if(!$res){
- $this->error('没有可添加的题目');
- }
- $this->updateExamTotalScore($ids);
- $this->success('成功添加' . $res . '条');
- }
- $this->view->ids = $ids;
- return $this->view->fetch('manage_questions');
- }
- public function examQuestionList($ids)
- {
- $exam = $this->model->get($ids);
- $this->view->ids = $ids;
- $this->view->exam_titile = $exam['title'];
- $this->view->status = $exam['status'];
- $this->view->total = $exam['total'];
- $this->view->qualified = $exam['qualified'];
- return $this->view->fetch('question_list');
- }
- public function removeQuestions($ids)
- {
- $question_ids = $this->request->param('question_ids/a');
- $res = model(TrainExamReQuestion::class)
- ->where('exam_id', $ids)
- ->whereIn('question_id', $question_ids)
- ->delete();
- if(!$res){
- $this->error('没有可移除的题目');
- }
- $this->success('成功移除' . $res . '条');
- }
- /**
- * 查看
- */
- public function adminIndex()
- {
- //设置过滤方法
- $this->request->filter(['strip_tags']);
- if ($this->request->isAjax()) {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams('name');
- $exam_where = false;
- if($this->request->has('show_exam_id')){
- $admin_ids = model(TrainExamReAdmin::class)
- ->whereIn('exam_id', $this->request->param('show_exam_id'))
- ->column('admin_id');
- $exam_where = [
- 'id' => ['in', $admin_ids]
- ];
- }
- $total = model(TrainAdmin::class)
- ->where($where)
- ->where($exam_where)
- ->order($sort, $order)
- ->count();
- $list = model(TrainAdmin::class)
- ->where($where)
- ->where($exam_where)
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
- if($list){
- if($this->request->has('exam_id')){
- $admin_ids = model(TrainExamReAdmin::class)
- ->whereIn('exam_id', $this->request->param('exam_id'))
- ->column('admin_id');
- foreach ($list as &$val){
- $val['checkbox'] = in_array($val['id'], $admin_ids) ? 1 : 0;
- }
- unset($val);
- }
- if($this->request->has('show_exam_id')){
- $admin_ids = array_column($list,'id');
- $result_arr = TrainService::instance()->getResultByAdminIds($this->request->param('show_exam_id'), $admin_ids);
- foreach ($list as &$val){
- $val['result_id'] = $result_arr[$val['id']]['result_id'] ?? NULL;
- $val['status'] = $result_arr[$val['id']]['status'] ?? 0;
- $val['scores'] = $result_arr[$val['id']]['scores'] ?? 0;
- $val['is_qualified'] = $result_arr[$val['id']]['is_qualified'] ?? 0;
- }
- unset($val);
- }
- }
- $list = collection($list)->toArray();
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- return $this->view->fetch();
- }
- public function manageAdmins($ids)
- {
- if($this->request->isAjax()){
- $admin_ids = $this->request->param('admin_ids/a');
- $exam_admins_ids = model(TrainExamReAdmin::class)
- ->where('exam_id', $ids)
- ->column('admin_id');
- $new_admin_ids = array_diff($admin_ids, $exam_admins_ids);
- $save = [];
- foreach ($new_admin_ids as $admin_id){
- $save[] = [
- 'exam_id' => $ids,
- 'admin_id' => $admin_id,
- ];
- }
- $res = model(TrainExamReAdmin::class)->insertAll($save);
- if(!$res){
- $this->error('没有可添加的考生');
- }
- $this->success('成功添加' . $res . '条');
- }
- $this->view->ids = $ids;
- return $this->view->fetch('manage_admins');
- }
- public function examAdminList($ids)
- {
- $exam = $this->model->get($ids);
- $this->view->ids = $ids;
- $this->view->exam_titile = $exam['title'];
- $this->view->total = $exam['total'];
- $this->view->qualified = $exam['qualified'];
- $this->view->status = $exam['status'];
- return $this->view->fetch('admin_list');
- }
- public function removeAdmins($ids)
- {
- $admin_ids = $this->request->param('admin_ids/a');
- $res = model(TrainExamReAdmin::class)
- ->where('exam_id', $ids)
- ->whereIn('admin_id', $admin_ids)
- ->delete();
- if(!$res){
- $this->error('没有可移除的考生');
- }
- $this->success('成功移除' . $res . '人');
- }
- public function pushExam($ids, DingTalk $dingTalkService)
- {
- Db::startTrans();
- $res = $this->model
- ->where('id', $ids)
- ->update([
- 'status' => 1,
- 'push_time' => date('Y-m-d H:i:s'),
- 'push_admin_id' => $this->auth->id
- ]);
- if(!$res){
- Db::rollback();
- $this->error('修改考试状态失败');
- }
- $admin_ids = model(TrainExamReAdmin::class)
- ->whereIn('exam_id', $ids)
- ->column('admin_id');
- $save = [];
- foreach ($admin_ids as $admin_id){
- $save[] = [
- 'exam_id' => $ids,
- 'admin_id' => $admin_id
- ];
- }
- $res = model(TrainExamResult::class)
- ->insertAll($save);
- if(!$res){
- Db::rollback();
- $this->error('创建考试成绩表数据失败');
- }
- // 发送钉钉消息
- $exam = $this->model->get($ids);
- $token = $dingTalkService->getToken();
- $user_ids = implode(',', model(TrainAdmin::class)->whereIn('id', $admin_ids)->column('userid'));
- $dingTalkService->sendWorkNotice($token, [
- 'exam_title' => $exam['title'],
- 'start_time' => date('Y-m-d H:i', strtotime($exam['start_time'])),
- 'end_time' => date('Y-m-d H:i', strtotime($exam['end_time'])),
- 'qualified' => $exam['qualified'],
- 'single_title' => "中世大学",
- ], $user_ids);
- Db::commit();
- $this->success('发布成功');
- }
- public function setScore()
- {
- $params = $this->request->param();
- $score = (int) $params['score'];
- if(!is_numeric($score) || $score <= 0 ){
- $this->error('设置分数格式错误,请输入大于0的数字');
- }
- Db::startTrans();
- $res = model(TrainExamReQuestion::class)
- ->where('exam_id', $params['ids'])
- ->whereIn('question_id', $params['question_ids'])
- ->update([
- 'score' => $params['score']
- ]);
- if($res === false){
- Db::rollback();
- $this->error('设置分数失败');
- }
- $res = $this->updateExamTotalScore($params['ids']);
- if(!$res){
- Db::rollback();
- $this->error('更新考试总分失败');
- }
- Db::commit();
- $this->success('设置成功');
- }
- /**
- * 更新考试总分
- * @param $exam_id
- * @return bool
- */
- protected function updateExamTotalScore($exam_id)
- {
- $total = model(TrainExamReQuestion::class)
- ->where('exam_id', $exam_id)
- ->sum('score');
- if(!is_numeric($total) || $total <= 0){
- return false;
- }
- $res = $this->model
- ->where('id', $exam_id)
- ->update([
- 'total' => $total
- ]);
- return $res !== false;
- }
- public function del($ids = "")
- {
- if ($ids) {
- $pk = $this->model->getPk();
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds)) {
- $this->model->where($this->dataLimitField, 'in', $adminIds);
- }
- Db::startTrans();
- try {
- $count = $this->model->where($pk, 'in', $ids)
- ->update([
- 'is_del' => 1
- ]);
- } catch (PDOException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($count) {
- $this->success();
- } else {
- $this->error(__('No rows were deleted'));
- }
- }
- $this->error(__('Parameter %s can not be empty', 'ids'));
- }
- public function showMistakes($result_id)
- {
- // 本次考试题目
- $result = model(TrainExamResult::class)->get($result_id);
- $data = model(TrainExamReQuestion::class)->alias('exam_question')
- ->join(model(QuestionModel::class)->getTable().' question', 'exam_question.question_id = question.id')
- ->where('exam_question.exam_id', $result['exam_id'])
- ->field('question_id, question.title, score as question_score, NULL as my_answer, type, question.result as right_result, answer1, answer2, answer3, answer4, answer5, answer6')
- ->order('question.type')
- ->select();
- $data = collection($data)->toArray();
- // 答题数据
- $record_data = model(TrainExamResultRecord::class)
- ->where('result_id', $result_id)
- // ->where('result', 0)
- ->column('question_id, answer, result');
- // 组合错题结果
- $trainService = TrainService::instance();
- $new_data = [];
- foreach ($data as $val){
- if (isset($record_data[$val['question_id']])) {
- if($record_data[$val['question_id']]['result'] === 0){
- $val['my_answer'] = $trainService->formatResult($record_data[$val['question_id']]['answer'], $val['type']);
- $val['right_result'] = $trainService->formatResult($val['right_result'], $val['type']);
- $val['type'] = $trainService->formatType($val['type']);
- $new_data[] = $val;
- }
- } else{
- $val['my_answer'] = '未填';
- $val['right_result'] = $trainService->formatResult($val['right_result'], $val['type']);
- $val['type'] = $trainService->formatType($val['type']);
- $new_data[] = $val;
- }
- }
- $this->view->assign('data', $new_data);
- return $this->view->fetch();
- }
- public function getExam($ids)
- {
- $data = $this->model->get($ids);
- $this->success('ok','',$data);
- }
- public function updateQueryOnJobUser(DingTalk $dingTalkService)
- {
- // 获取access_token
- $token = $dingTalkService->getToken();
- if(!$token){
- $this->error('获取access_token失败:' . $dingTalkService->getError());
- }
- // 获取用户id列表
- $user_ids = $dingTalkService->getQueryOnJob($token);
- if(!$user_ids){
- $this->error('获取用户id列表:' . $dingTalkService->getError());
- }
- $user_data = [];
- foreach ($user_ids as $user_id){
- $user = $dingTalkService->getUserDetail($user_id, $token);
- if($user){
- $user_data[] = [
- 'name' => $user['name'],
- 'mobile' => $user['mobile'],
- 'avatar' => $user['avatar'],
- 'title' => $user['title'] ?? '',
- 'userid' => $user['userid'],
- ];
- }
- }
- $model = model(TrainAdmin::class);
- $exist_arr = $model->column('userid, id');
- foreach ($user_data as $val){
- $save = [
- 'name' => $val['name'],
- 'mobile' => $val['mobile'],
- 'avatar' => $val['avatar'],
- 'title' => $val['title'] ?? '',
- 'userid' => $val['userid']
- ];
- if(isset($exist_arr[$val['userid']])){
- $model->where('id', $exist_arr[$val['userid']])->update($save);
- } else {
- $model->insert($save);
- }
- }
- $this->success();
- }
- public function sendNotice(DingTalk $dingTalkService)
- {
- }
- /**
- * 添加
- */
- public function add()
- {
- if ($this->request->isPost()) {
- $params = $this->request->post("row/a");
- if ($params) {
- $params = $this->preExcludeFields($params);
- if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
- $params[$this->dataLimitField] = $this->auth->id;
- }
- $result = false;
- Db::startTrans();
- try {
- //是否采用模型验证
- if ($this->modelValidate) {
- $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
- $this->model->validateFailException(true)->validate($validate);
- }
- $result = $this->model->allowField(true)->save($params);
- $exam_id = $this->model->getLastInsID();
- if($params['class_id'] && $params['question_count']){
- $question_ids = model(QuestionModel::class)
- ->where('class_id', $params['class_id'])
- ->where('is_del', 0)
- ->column('id');
- $checked_ids = array_rand($question_ids, $params['question_count']);
- $save = [];
- $score = $params['question_point'] ?? 1;
- foreach ($checked_ids as $checked_id){
- $save[] = [
- 'exam_id' => $exam_id,
- 'question_id' => $question_ids[$checked_id],
- 'score' => $score
- ];
- }
- $res = model(TrainExamReQuestion::class)->insertAll($save);
- if(!$res){
- Db::rollback();
- $this->error('创建考题失败');
- }
- }
- Db::commit();
- } catch (ValidateException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (PDOException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- $this->success();
- } else {
- $this->error(__('No rows were inserted'));
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- return $this->view->fetch();
- }
- }
|