ListController.class.php
4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
/**
* 【考试中心-手机端】2-获取考试列表接口
* ListController.class.php
* @author: 蔡建华
* @date: 2017-05-23
*/
namespace Api\Controller\Paper;
use Common\Service\AnswerService;
use Common\Service\PaperService;
use Common\Service\RightService;
class ListController extends AbstractController
{
public function Index_post()
{
// 接收post参数
$params = I('post.');
// 分类id
$ec_id = 0;
if (isset($params['ec_id']) && $params['ec_id']) {
$ec_id = intval($params['ec_id']);
}
// 实例化试卷service
$paper_s = new PaperService();
$answer_serv = new AnswerService();
// 查询当前用户的答卷记录:条件1-》当前用户,条件2-》常规考试
$answer_list = $answer_serv->list_by_conds(['uid' => $this->uid, 'obj_id' => 0], null, [], 'ep_id');
// 已参加考试ep_id集合
$ep_ids = array_filter(array_unique(array_column($answer_list, 'ep_id')));
unset($answer_list);
// 参与类型(0:未参与,1:已参与),默认0
$join_type = intval($params['join_type']);
if (!$join_type) {
// 未参与
// 实例化权限service
$right_s = new RightService();
// 登录用户信息
$user = $this->_login->user;
// 获取当前登录用户的标签,部门,岗位,用户ID
$right = $right_s->get_by_right($user);
// 查询权限为全公司的试卷ID集合
$conds_all = [
'is_all' => PaperService::AUTH_ALL, // 权限为全公司
'exam_type' => PaperService::NOMAL_TYPE, // 试卷类型:常规试卷
'exam_status' => [PaperService::PAPER_PUBLISH, PaperService::PAPER_STOP], // 状态为已发布或者已终止
];
// 权限为全公司的试卷列表
$paper_auth_all_list = $paper_s->list_by_conds($conds_all, null, [], 'ep_id');
// 根据权限查询有权参与的试卷列表
$paper_right_list = $paper_s->list_by_right(['right' => $right], 'ep_id');
// 用户有权限访问的所有试卷的列表
$paper_list = array_merge($paper_auth_all_list, $paper_right_list);
// 应参加考试ep_id集合
$all_ep_ids = array_column($paper_list, 'ep_id');
// 未参与考试ep_id集合
$ep_ids = array_diff($all_ep_ids, $ep_ids);
}
sort($ep_ids);
$conds = [
'exam_type' => PaperService::NOMAL_TYPE, // 试卷类型:常规试卷
'cate_status' => PaperService::EC_OPEN_STATES, // 分类未被禁用
'exam_status' => [PaperService::PAPER_PUBLISH, PaperService::PAPER_STOP], // 状态为已发布或者已终止
'ep_id' => $ep_ids ? $ep_ids : 0,
];
if ($ec_id) {
// 如果分类ID不为空
$conds['ec_id'] = $ec_id;
}
// 获取记录总数
$total = $paper_s->count_by_conds($conds);
$list = [];
// 记录总数不为空:有数据
if ($total) {
// 判断分页参数是否为空,为空赋默认值
$page = !empty($params['page']) ? intval($params['page']) : self::DEFAULT_PAGE;
$limit = !empty($params['limit']) ? intval($params['limit']) : self::DEFAULT_LIMIT;
// 分页
list($start, $limit) = page_limit($page, $limit);
$conds['uid'] = $this->uid;
$conds['join_type'] = intval($params['join_type']); // 参与类型(0:未参与,1:已参与)
// 分页参数
$page_option = [$start, $limit];
// 排序:最后更新时间倒序
$order_option['last_time'] = 'DESC';
// 获取列表数据
$list = $paper_s->paper_api_list($conds, $page_option, $order_option);
} else {
// 没有数据,返回提示"暂无信息"
E('_ERR_DATA_NOT_EXIST');
}
// 返回数据不为空
if (!empty($list)) {
// 格式化
$list = $paper_s->paper_format($list, $join_type, $this->uid);
}
// 返回结果
$this->_result = [
'page' => intval($page),
'limit' => intval($limit),
'total' => intval($total),
'list' => $list
];
}
}