-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb_operator_class.php
More file actions
321 lines (278 loc) · 12.4 KB
/
Copy pathdb_operator_class.php
File metadata and controls
321 lines (278 loc) · 12.4 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
require_once ('config.php');
function mydb_query_without_return($query){
$connection = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
if(!$connection){
die("Could not connect to the database:<br/>".mysql_error());
}
$db_select = mysql_select_db(DB_NAME);
if(!$db_select){
die("Could not select the database:<br/>".mysql_error());
}
mysql_query("SET NAMES 'utf8'");
mysql_query("SET time_zone = '+8:00'");
mysql_query($query);
$insert_id = mysql_insert_id();
mysql_close($connection);
return $insert_id;
}
function mydb_query_return_double_array($query){
$connection = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
if(!$connection){
die("Could not connect to the database:<br/>".mysql_error());
}
$db_select = mysql_select_db(DB_NAME);
if(!$db_select){
die("Could not select the database:<br/>".mysql_error());
}
mysql_query("SET NAMES 'utf8'");
mysql_query("SET time_zone = '+8:00'");
$result = mysql_query($query);
if(!$result){
die("Could not query the database:<br/>".mysql_error());
}
$result_array=array();
while($tmp = mysql_fetch_array($result,MYSQL_ASSOC)){
$result_array[] = $tmp;
}
mysql_close($connection);
return $result_array;
}
function mydb_query_return_first_item($query){
$connection = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
if(!$connection){
die("Could not connect to the database:<br/>".mysql_error());
}
$db_select = mysql_select_db(DB_NAME);
if(!$db_select){
die("Could not select the database:<br/>".mysql_error());
}
mysql_query("SET NAMES 'utf8'");
mysql_query("SET time_zone = '+8:00'");
$result = mysql_query($query);
if(!$result){
die("Could not query the database:<br/>".mysql_error());
}
$result_array = mysql_fetch_array($result,MYSQL_ASSOC);
mysql_close($connection);
return $result_array;
}
//User Operator
function get_Users() {
$query = "SELECT * FROM user AS u WHERE u.stat=0;";
return mydb_query_return_double_array($query);
}
function get_UserById($id) {
$query = "SELECT * FROM user AS u WHERE u.stat=0 and u.id = $id LIMIT 1;";
return mydb_query_return_first_item($query);
}
function get_UserByEmail($email) {
if (!get_magic_quotes_gpc()){
$email = addslashes($email);
}
$query = "SELECT * FROM user AS u WHERE u.stat=0 and u.mail = '$email' LIMIT 1";
return mydb_query_return_first_item($query);
}
function get_UserByUid($uid) {
$query = "SELECT * FROM user AS u WHERE u.stat=0 and u.uid = $uid LIMIT 1";
return mydb_query_return_first_item($query);
}
function add_User($uid,$name,$sex,$nickname,$pojusername,$email,$team){
$name = htmlspecialchars($name);
$nickname = htmlspecialchars($nickname);
$pojusername = htmlspecialchars($pojusername);
$email = htmlspecialchars($email);
if (!get_magic_quotes_gpc()){
$name = addslashes($name);
$nickname = addslashes($nickname);
$pojusername = addslashes($pojusername);
$email = addslashes($email);
}
$query = "insert into user(uid,stat,sex,name,nickname,POJ_user_name,mail,type,team) values($uid,0,'$sex','$name','$nickname','$pojusername','$email',2,$team);";
return mydb_query_without_return($query);
}
function update_User($id,$name,$sex,$nickname,$pojusername,$email,$team){
$name = htmlspecialchars($name);
$nickname = htmlspecialchars($nickname);
$pojusername = htmlspecialchars($pojusername);
$email = htmlspecialchars($email);
if (!get_magic_quotes_gpc()){
$name = addslashes($name);
$nickname = addslashes($nickname);
$pojusername = addslashes($pojusername);
$email = addslashes($email);
}
$query = "update user as u set u.name='$name' , u.sex = '$sex' , u.nickname='$nickname',u.POJ_user_name='$pojusername',u.mail='$email',u.team=$team where u.id=$id";
mydb_query_without_return($query);
}
//End User Operator
//Problem Operator
function get_ProblemsOnWeek($week,$level) {
$query = "SELECT p.id,p.userID,u.name,u.nickname,u.photoPath,p.pojProblemID,p.title,p.time,p.source FROM problems AS p,user AS u where p.stat=0 and p.week=$week and p.level=$level and p.userID = u.id and u.stat = 0 order by p.time;";
return mydb_query_return_double_array($query);
}
function get_ProblemContentById($id){
$query = "SELECT p.id,p.userID,u.name,u.nickname,u.photoPath,p.pojProblemID,p.title,p.Context,p.time,p.source FROM problems AS p,user AS u where p.stat=0 and p.id=$id and u.stat=0 and p.userID=u.id;";
return mydb_query_return_first_item($query);
}
function get_ProblemSimpleById($id){
$query = "SELECT p.id,p.userID,u.name,u.nickname,u.photoPath,p.pojProblemID,p.title,p.time,p.source FROM problems AS p,user AS u where p.stat=0 and p.id=$id and u.stat=0 and p.userID=u.id;";
return mydb_query_return_first_item($query);
}
function update_ProblemContent($id,$pojID,$title,$content,$source){
$title = htmlspecialchars($title);
$content = htmlspecialchars($content);
$source = htmlspecialchars($source);
if (!get_magic_quotes_gpc()){
$title = addslashes($title);
$content = addslashes($content);
$source = addslashes($source);
}
$query = "UPDATE problems AS p SET p.pojProblemID = $pojID , p.title = '$title' , p.Context='$content' , p.source = '$source' where p.stat=0 and p.id=$id;";
mydb_query_without_return($query);
}
function add_Problem($userId,$pojID,$title,$content,$week,$source,$level){
$title = htmlspecialchars($title);
$content = htmlspecialchars($content);
$source = htmlspecialchars($source);
if (!get_magic_quotes_gpc()){
$title = addslashes($title);
$content = addslashes($content);
$source = addslashes($source);
}
$query = "insert into problems(stat,userID,pojProblemID,title,Context,time,week,source,level) values (0,$userId,$pojID,'$title','$content',now(),$week,'$source',$level);";
return mydb_query_without_return($query);
}
//End Problem Operator
//Score Operator
function get_ScoresByWeek($week){
$query = "SELECT s.id,s.userID,u.name,u.nickname,u.photoPath,s.AC,s.ACtime,s.lastModify FROM score AS s, user AS u WHERE s.stat=0 AND u.stat = 0 AND s.userID = u.id AND s.probID IN (SELECT id FROM problems AS p WHERE p.stat=0 AND p.week=$week) ORDER BY s.probID;";
return mydb_query_return_double_array($query);
}
function get_ScoresByProb($probId) {
$query = "SELECT s.id,s.userID,u.name,u.nickname,u.photoPath,s.AC,s.ACtime,s.lastModify FROM score AS s, user AS u where s.stat=0 and s.probID=$probId AND u.stat = 0 AND s.userID = u.id AND s.AC=1 GROUP BY s.userID order by s.AC DESC,s.ACtime;";
return mydb_query_return_double_array($query);
}
function get_ScoreContent($id){
$query = "SELECT s.id,p.title,s.userID,u.name,u.nickname,u.photoPath,s.AC,s.code,s.language,s.ACtime,s.lastModify FROM score AS s, user AS u, problems AS p where s.stat=0 and s.id=$id AND u.stat = 0 AND s.userID = u.id AND p.stat=0 AND p.id = s.probID;";
return mydb_query_return_first_item($query);
}
function update_Score($id,$code,$ac,$language){
$code = htmlspecialchars($code);
if (!get_magic_quotes_gpc()){
$code = addslashes($code);
}
$query = "UPDATE score AS s SET s.code='$code' , s.AC=$ac , s.lastModify=now() , s.language = '$language' WHERE s.stat=0 and s.id=$id;";
mydb_query_without_return($query);
}
function add_Score($probId,$userId,$code,$ac,$language){
$code = htmlspecialchars($code);
if (!get_magic_quotes_gpc()){
$code = addslashes($code);
}
if($ac>0){
$query = "INSERT INTO score(probID,userID,AC,stat,code,language,ACtime,lastModify) values($probId,$userId,1,0,'$code','$language',now(),now());";
return mydb_query_without_return($query);
}else{
$query = "INSERT INTO score(probID,userID,AC,stat,code,language,lastModify) values($probId,$userId,0,0,'$code','$language',now());";
return mydb_query_without_return($query);
}
}
//End Score Operator
//Comment for problem operator
function get_CommentsByProb($probId) {
$query = "SELECT c.id,c.userID,u.name,u.nickname,u.photoPath,c.content,c.time FROM commentsforproblem AS c,user AS u where c.stat=0 and c.probID=$probId AND c.userID=u.id AND u.stat=0 order by c.time;";
return mydb_query_return_double_array($query);
}
function get_CommentsCountByProb($probId){
$query = "SELECT count(*) FROM commentsforproblem AS c where c.stat=0 and c.probID=$probId;";
return mydb_query_return_first_item($query);
}
function update_CommentInProb($id,$content){
$content = htmlspecialchars($content);
if (!get_magic_quotes_gpc()){
$content = addslashes($content);
}
$query = "UPDATE commentsforproblem AS c SET c.content='$content' , c.lastModify=now() WHERE c.stat=0 and c.id=$id;";
mydb_query_without_return($query);
}
function add_CommentInProb($probId,$userId,$content){
$content = htmlspecialchars($content);
if (!get_magic_quotes_gpc()){
$content = addslashes($content);
}
$query = "INSERT INTO commentsforproblem(stat,probID,userID,content,time,lastModify) values(0,$probId,$userId,'$content',now(),now());";
return mydb_query_without_return($query);
}
//End Comment for problem operator
//Comment for spring operator
function get_CommentsByWeek($week,$team) {
$query = "SELECT c.id,c.userID,c.content,c.time, u.nickname, u.photoPath FROM commentsforspring AS c,user AS u where c.stat=0 and c.week=$week and c.team=$team AND c.userID=u.id AND u.stat=0 order by c.time;";
return mydb_query_return_double_array($query);
}
function get_CommentsCountByWeek($week,$team){
$query = "SELECT count(*) FROM commentsforspring AS c where c.stat=0 and c.week=$week and c.team=$team;";
return mydb_query_return_first_item($query);
}
function update_CommentInSpring($id,$content){
$content = htmlspecialchars($content);
if (!get_magic_quotes_gpc()){
$content = addslashes($content);
}
$query = "UPDATE commentsforspring AS c SET c.content='$content' , c.lastModify=now() WHERE c.stat=0 and c.id=$id;";
mydb_query_without_return($query);
}
function add_CommentInSpring($week,$team,$userId,$content){
$content = htmlspecialchars($content);
if (!get_magic_quotes_gpc()){
$content = addslashes($content);
}
$query = "INSERT INTO commentsforspring(stat,userID,content,week,time,team,lastModify) values(0,$userId,'$content',$week,now(),$team,now());";
return mydb_query_without_return($query);
}
//End Comment for spring operator
//Rank
function getRandOnWeek($week,$team){
$query = "SELECT s.userID,s.probID,u.name,u.nickname,u.photoPath,count(distinct s.probID) 'count' FROM score AS s,user AS u where s.stat=0 and s.userID=u.id and u.team=$team and s.ac=1 AND s.probID IN (SELECT id FROM problems AS p WHERE p.stat=0 AND p.week=$week AND p.level=$team) group by s.userID order by count(distinct s.probID) desc;";
return mydb_query_return_double_array($query);
}
function getWorstOnWeek($week,$team){
$query = "SELECT u.id,u.name,u.nickname,u.photoPath FROM user as u where u.team=$team and u.id not in (select distinct s.userID from score as s ,problems as p where s.stat=0 and s.AC=1 and p.stat=0 and p.level=$team and p.id=s.probID and p.week=$week);";
return mydb_query_return_double_array($query);
}
function getACRate($userID){
$query = "select
(select count(distinct p.pojProblemID) from problems as p where p.stat=0 ) as allPcount,
(select count(distinct p.pojProblemID) from problems as p, score as s ,user as u where p.stat=0 and s.stat=0 and u.stat=0 and p.id=s.probID and s.AC=1 and s.userID=$userID) as allAC,
(select count(distinct p.pojProblemID) from problems as p where p.stat=0 and p.level=1) as teamPcount,
(select count(distinct p.pojProblemID) from problems as p, score as s ,user as u where p.stat=0 and s.stat=0 and u.stat=0 and p.id=s.probID and s.AC=1 and p.level=u.team and s.userID=u.id and u.id=$userID) as teamAC,
(select count(distinct p.pojProblemID) from problems as p where p.userID=$userID) as userPCount,
(select count(distinct p.pojProblemID) from problems as p ,score as s,user as u where p.stat=0 and s.stat=0 and u.stat=0 and p.userID=$userID and s.probID=p.id and s.AC=1) as ACed";
return mydb_query_return_first_item($query);
}
//End Rank
function getWeek(){
$dateStart = strtotime('2012-12-2');
$dateNow = strtotime(date('Y-m-j'));
$betweenDate = floor(($dateNow - $dateStart)/604800)+1;
return $betweenDate;
}
//urlencode
function my_urlencode_double($array){
for ($i=0;$i<count($array);$i++){
foreach ($array[$i] as $key=>$value){
$array[$i][$key]=urlencode($value);
}
}
return $array;
}
function my_urlencode_single($array){
foreach ($array as $key=>$value){
$array[$key]=urlencode($value);
}
return $array;
}
//End urlencode
//login
//End login
?>