-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathCache.php
executable file
·172 lines (160 loc) · 8.78 KB
/
Cache.php
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/*
* This file is part of the phpems/phpems.
*
* (c) oiuv <[email protected]>
*
* 项目维护:oiuv(QQ:7300637) | 定制服务:火眼(QQ:278768688)
*
* This source file is subject to the MIT license that is bundled.
*/
require __DIR__.'/../vendor/autoload.php';
class Cache
{
// 根据已做试题统计错题率
public static function questions()
{
$eh = \Model\ExamHistory::all();
$stats = [];
$os = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'];
// 获取试题类型并转为数组
$questiontype = \Model\Questype::all();
$questiontype = json_decode($questiontype->toJson(), true);
foreach ($eh as $p) {
// 统计普通试题记录
foreach ($p->ehquestion['questions'] as $questions) {
foreach ($questions as $key => $question) {
if (!isset($stats[$question['questionid']]['right'])) {
$stats[$question['questionid']]['right'] = 0;
}
if (!isset($stats[$question['questionid']]['number'])) {
$stats[$question['questionid']]['number'] = 0;
}
$stats[$question['questionid']]['title'] = $question['question'];
$stats[$question['questionid']]['id'] = $question['questionid'];
// 统计正确回答数量
if (isset($p->ehscorelist[$question['questionid']]) && $p->ehscorelist[$question['questionid']] > 0) {
$stats[$question['questionid']]['right'] = intval($stats[$question['questionid']]['right']) + 1;
}
// 统计总回答人数
$stats[$question['questionid']]['number'] = intval($stats[$question['questionid']]['number']) + 1;
// 统计各选项回答数量
if (isset($p->ehuseranswer[$question['questionid']]) && 0 == $questiontype[$question['questiontype'] - 1]['questsort'] && $questiontype[$question['questiontype'] - 1]['questchoice'] < 5) {
foreach ($os as $o) {
if (!isset($stats[$question['questionid']][$o])) {
$stats[$question['questionid']][$o] = 0;
}
// 多选
if (is_array($p->ehuseranswer[$question['questionid']]) && in_array($o, $p->ehuseranswer[$question['questionid']])) {
$stats[$question['questionid']][$o] = intval($stats[$question['questionid']][$o]) + 1;
}
// 单选
if (!is_array($p->ehuseranswer[$question['questionid']]) && false !== strpos($p->ehuseranswer[$question['questionid']], $o)) {
$stats[$question['questionid']][$o] = intval($stats[$question['questionid']][$o]) + 1;
}
}
}
}
}
// 统计题冒题记录(材料分析题)
foreach ($p->ehquestion['questionrows'] as $questionrows) {
foreach ($questionrows as $questionrow) {
foreach ($questionrow['data'] as $key => $question) {
if (!isset($stats[$question['questionid']]['right'])) {
$stats[$question['questionid']]['right'] = 0;
}
if (!isset($stats[$question['questionid']]['number'])) {
$stats[$question['questionid']]['number'] = 0;
}
if (!$key) {
$stats[$question['questionid']]['title'] = $questionrow['qrquestion'].'<br />'.$question['question'];
}
$stats[$question['questionid']]['id'] = $question['questionid'];
if (isset($p->ehscorelist[$question['questionid']]) && $p->ehscorelist[$question['questionid']] > 0) {
$stats[$question['questionid']]['right'] = intval($stats[$question['questionid']]['right']) + 1;
}
$stats[$question['questionid']]['number'] = intval($stats[$question['questionid']]['number']) + 1;
if (isset($p->ehuseranswer[$question['questionid']]) && 0 == $questiontype[$question['questiontype'] - 1]['questsort'] && $questiontype[$question['questiontype'] - 1]['questchoice'] < 5) {
foreach ($os as $o) {
if (!isset($stats[$question['questionid']][$o])) {
$stats[$question['questionid']][$o] = 0;
}
if (is_array($p->ehuseranswer[$question['questionid']]) && in_array($o, $p->ehuseranswer[$question['questionid']])) {
$stats[$question['questionid']][$o] = intval($stats[$question['questionid']][$o]) + 1;
} elseif (!is_array($p->ehuseranswer[$question['questionid']]) && false !== strpos($p->ehuseranswer[$question['questionid']], $o)) {
$stats[$question['questionid']][$o] = intval($stats[$question['questionid']][$o]) + 1;
}
}
}
}
}
}
}
return $stats;
}
// 根据知识点统计错题率
public static function knows()
{
$eh = \Model\ExamHistory::all();
$stats = [];
foreach ($eh as $p) {
// 普通试题知识点
foreach ($p->ehquestion['questions'] as $questions) {
foreach ($questions as $key => $question) {
foreach ($question['questionknowsid'] as $knows) {
if (!isset($stats[$knows['knowsid']]['number'])) {
$stats[$knows['knowsid']]['number'] = 0;
}
if (!isset($stats[$knows['knowsid']]['right'])) {
$stats[$knows['knowsid']]['right'] = 0;
}
$stats[$knows['knowsid']]['knowsid'] = $knows['knowsid'];
$stats[$knows['knowsid']]['knows'] = $knows['knows'];
$stats[$knows['knowsid']]['number'] = intval($stats[$knows['knowsid']]['number']) + 1;
if (isset($p->ehscorelist[$question['questionid']]) && $p->ehscorelist[$question['questionid']] > 0) {
$stats[$knows['knowsid']]['right'] = intval($stats[$knows['knowsid']]['right']) + 1;
}
}
}
}
// 材料分析题知识点
foreach ($p->ehquestion['questionrows'] as $questionrows) {
foreach ($questionrows as $questionrow) {
foreach ($questionrow['data'] as $key => $question) {
foreach ($questionrow['qrknowsid'] as $knows) {
if (!isset($stats[$knows['knowsid']]['number'])) {
$stats[$knows['knowsid']]['number'] = 0;
}
if (!isset($stats[$knows['knowsid']]['right'])) {
$stats[$knows['knowsid']]['right'] = 0;
}
$stats[$knows['knowsid']]['knowsid'] = $knows['knowsid'];
$stats[$knows['knowsid']]['knows'] = $knows['knows'];
$stats[$knows['knowsid']]['number'] = intval($stats[$knows['knowsid']]['number']) + 1;
if (isset($p->ehscorelist[$question['questionid']]) && $p->ehscorelist[$question['questionid']] > 0) {
$stats[$knows['knowsid']]['right'] = intval($stats[$knows['knowsid']]['right']) + 1;
}
}
}
}
}
}
return $stats;
}
}
// 查看待缓存的已考知识点数据
dump(Cache::knows());
// 缓存统计结果到 redis
$client = new Predis\Client('tcp://127.0.0.1:6379');
$client->set('phpems:questions', json_encode(Cache::questions()));
$client->set('phpems:knows', json_encode(Cache::knows()));
echo '已考试题和知识点缓存完成^_^';
/* 在控制器中可以直接读取缓存并在模板中使用
$client = new Predis\Client('tcp://127.0.0.1:6379');
$questions = $client->get('phpems:questions');
$questions = json_decode($questions, true);
$knows = $client->get('phpems:knows');
$knows = json_decode($knows, true);
// $this->tpl->assign('questions', $questions);
dd($knows);
*/