<?php /** * 【考试中心-后台】试卷参加考试人员分布接口 * PassAnalysisController.class.php * @author : wanghuan * @date : 2017-07-17 * @version: $Id$ */ namespace Apicp\Controller\Answer; use Common\Common\User; use Common\Common\Department; use Common\Service\PaperService; use Common\Service\RightService; use Common\Service\AnswerService; class PassAnalysisController extends AbstractController { // 岗位 const TYPE_JOY = 1; // 角色 const TYPE_ROLE = 2; // 标签 const TYPE_TAG = 3; /** @var PaperService */ protected $paper_serv; /** @var RightService */ protected $right_serv; /** @var AnswerService */ protected $answer_serv; public function before_action($action = '') { if (!parent::before_action($action)) { return false; } // 初始化User $this->user = User::instance(); // 实例化试卷service $this->paper_serv = new PaperService(); // 实例化权限service $this->right_serv = new RightService(); // 实例化答卷service $this->answer_serv = new AnswerService(); return true; } public function Index_post() { set_time_limit(0); ini_set("memory_limit", "1128M"); // 接收post参数 $params = I('post.'); // 参数为空,返回提示"参数不能为空" if (empty($params)) { E('_ERR_PARAMS_NOT_NULL'); } // 试卷id $ep_id = intval($params['ep_id']); // 试卷id为空,返回提示"试卷ID不能为空" if (!$ep_id) { E('_EMPTY_EP_ID'); } // 根据试卷id获取试卷详情 $paper = $this->paper_serv->get($ep_id); // 试卷不存在 if (empty($paper)) { E('_ERR_PAPER_NOT_FOUND'); } // 查询条件初始化 $join_conds = [ 'ep_id' => $ep_id, 'my_time > ?' => 0 ]; // 【部门】 if (isset($params['dpIds']) && !empty($params['dpIds']) && is_array($params['dpIds'])) { $params_dpids = $params['dpIds']; $dpServ = &Department::instance(); // 取子级部门ID $child_ids = $dpServ->list_childrens_by_cdid($params_dpids); // 合并部门ID $dp_ids = array_merge($params_dpids, array_values($child_ids)); $dp_conds = [ 'dpIdList' => $dp_ids ]; $dp_users = $this->user->listAll($dp_conds); // 选择部门下的员工id集合 $d_uids = array_column($dp_users, 'memUid'); // 组装查询条件 $dp_uids = $this->answer_serv->get_uids($paper['ep_id'], $d_uids); if (!empty($dp_uids)) { $join_conds['uid'] = $dp_uids; } else { // 空查询所有 为了避免查询所有传入0 $join_conds['uid'] = [0]; } } // 总分 $score_total = 0; // 部门下考试的员工考试信息 $join = []; // 试卷类型 $paper_type = $paper['paper_type']; // 分页 $page_option = null; // 排序 $order_by = []; // 查询字段 $fields = 'ea_id,uid,my_score,created'; // 模拟考试 if (PaperService::SIMULATION_PAPER_TYPE == $paper_type) { // 部门下模拟考试的员工考试信息 $join = $this->answer_serv->get_mock_join_list($join_conds, $page_option, $order_by, $fields); if (!empty($join)) { // 取考分列 $my_max_score = array_column($join, 'my_max_score'); // 考分之和 $score_total = array_sum($my_max_score); } } elseif (PaperService::EVALUATION_PAPER_TYPE == $paper_type) { // 测评考试 // 只用最高分进行计算 $answer_conds['is_score_top'] = AnswerService::IS_SCORE_TOP_TRUE; // 部门测评考试通过的员工考试信息 $join = $this->answer_serv->list_by_conds($join_conds, $page_option, $order_by, $fields); if (!empty($join)) { // 取考分列 $my_score = array_column($join, 'my_score'); // 考分之和 $score_total = array_sum($my_score); } } // 总数 $count = 0; // 平均分 $average = 0; // 待返数据 $list = []; // 部门下考试的员工列表不为空 if (!empty($join)) { // 部门下考试员工id集合 $join_uids = array_column($join, 'uid'); // 部门下参与考试的员工列表 $user_list = $this->answer_serv->getUser($join_uids); // 参加考试员工不为空,获取统计数据 if (!empty($user_list)) { // 【部门下参加考试总人数】 $count = count($join); // 【平均分】 $average = round(($score_total / $count), 2); // 默认类型:岗位 $type = self::TYPE_JOY; // 类型参数不为空,重新赋值 if (isset($params['type']) && $params['type']) { $type = intval($params['type']); } // 参加考试人数不为空,获取返回数据 if ($count) { $list = $this->get_list_type_data($user_list, $type, $join, $paper_type, $count); } } } // 返回结果 $this->_result = [ 'count' => intval($count), 'average' => $average, 'list' => $list ]; } /** * 组装list数据 * * @param $user_list array 部门下参加考试的员工列表 * @param $type int 请求数据类型:1=岗位,2=角色 * @param $join array 部门下有权限考试的员工的考试信息 * @param $paper_type int 试卷类型:0=测评考试,1=模拟考试 * @param $count int 总数 * * @return array 返回list数据 */ protected function get_list_type_data($user_list, $type, $join, $paper_type, $count) { // 初始化返回数据 $list = []; // 将答题记录转换为以用户ID为主键的二维数组 $join = array_combine_by_key($join, 'uid'); // 循环用户列表 foreach ($user_list as $val) { $answer_info = $join[$val['memUid']]; $score = 0; if (!empty($answer_info)) { // 模拟考试 if (PaperService::SIMULATION_PAPER_TYPE == $paper_type) { // 累加模拟考试同一岗位/角色的考分 $score = $answer_info['my_max_score']; } elseif (PaperService::EVALUATION_PAPER_TYPE == $paper_type) { // 测评考试 // 累加模测评考试同一岗位/角色的考分 $score = $answer_info['my_score']; } } // 岗位 if (self::TYPE_JOY == $type) { if(empty($val['memJob'])){ continue; } $arr = []; if (array_key_exists($val['memJob'], $list)) { $arr['name'] = $val['memJob']; $arr['count'] = $list[$val['memJob']]['count'] +1; $arr['score'] = $list[$val['memJob']]['score'] +$score; $list[$val['memJob']] = $arr; } else { $arr['name'] = $val['memJob']; $arr['count'] = 1; $arr['score'] = $score; $list[$val['memJob']] = $arr; } } elseif (self::TYPE_ROLE == $type) { // 角色 if(empty($val['memRole'])){ continue; } $arr = []; if (array_key_exists($val['memRole'], $list)) { $arr['name'] = $val['memRole']; $arr['count'] = $list[$val['memRole']]['count'] +1; $arr['score'] = $list[$val['memRole']]['score'] +1; $list[$val['memRole']] = $arr; } else { $arr['name'] = $val['memRole']; $arr['count'] = 1; $arr['score'] = $score; $list[$val['memRole']] = $arr; } } elseif (self::TYPE_TAG == $type) { // 标签 if(empty($val['memTag'])){ continue; } $arr = []; if (array_key_exists($val['memTag'], $list)) { $arr['name'] = $val['memTag']; $arr['count'] = $list[$val['memTag']]['count'] +1; $arr['score'] = $list[$val['memTag']]['score'] +1; $list[$val['memTag']] = $arr; } else { $arr['name'] = $val['memTag']; $arr['count'] = 1; $arr['score'] = $score; $list[$val['memTag']] = $arr; } } } // 循环处理数据 $return_list = []; foreach ($list as $v) { $res = []; $res['name'] = $v['name']; $res['count'] = $v['count']; $res['average'] = round($v['score'] / $v['count'],2); // 百分比 $res['percentage'] = round((($v['count'] / $count) * 100), 2) . '%'; $return_list[] = $res; } return $return_list; } }