-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathswad_log_database.c
389 lines (346 loc) · 13.8 KB
/
swad_log_database.c
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// swad_log_database.c: access log, operations with database
/*
SWAD (Shared Workspace At a Distance),
is a web platform developed at the University of Granada (Spain),
and used to support university teaching.
This file is part of SWAD core.
Copyright (C) 1999-2025 Antonio Cañas Vargas
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General 3 License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*****************************************************************************/
/*********************************** Headers *********************************/
/*****************************************************************************/
#include "swad_config.h"
#include "swad_database.h"
#include "swad_global.h"
#include "swad_log.h"
#include "swad_parameter.h"
/*****************************************************************************/
/************** External global variables from others modules ****************/
/*****************************************************************************/
extern struct Globals Gbl;
/*****************************************************************************/
/*********************** Log access in historical log ************************/
/*****************************************************************************/
long Log_DB_LogAccessInHistoricalLog (long ActCod,Rol_Role_t RoleToStore)
{
return
DB_QueryINSERTandReturnCode ("can not log access",
"INSERT INTO log"
" (ActCod,CtyCod,InsCod,CtrCod,DegCod,CrsCod,"
"UsrCod,Role,ClickTime,"
"TimeToGenerate,TimeToSend,IP)"
" VALUES"
" (%ld,%ld,%ld,%ld,%ld,%ld,"
"%ld,%u,NOW(),"
"%ld,%ld,'%s')",
ActCod,
Gbl.Hierarchy.Node[Hie_CTY].HieCod,
Gbl.Hierarchy.Node[Hie_INS].HieCod,
Gbl.Hierarchy.Node[Hie_CTR].HieCod,
Gbl.Hierarchy.Node[Hie_DEG].HieCod,
Gbl.Hierarchy.Node[Hie_CRS].HieCod,
Gbl.Usrs.Me.UsrDat.UsrCod,
(unsigned) RoleToStore,
Dat_GetTimeGenerationInMicroseconds (),
Dat_GetTimeSendInMicroseconds (),
Par_GetIP ());
}
/*****************************************************************************/
/************************* Log access in recent log **************************/
/*****************************************************************************/
void Log_DB_LogAccessInRecentLog (long LogCod,long ActCod,Rol_Role_t RoleToStore)
{
DB_QueryINSERT ("can not log access (recent)",
"INSERT INTO log_recent"
" (LogCod,ActCod,CtyCod,InsCod,CtrCod,DegCod,CrsCod,"
"UsrCod,Role,ClickTime,"
"TimeToGenerate,TimeToSend,IP)"
" VALUES"
" (%ld,%ld,%ld,%ld,%ld,%ld,%ld,"
"%ld,%u,NOW(),"
"%ld,%ld,'%s')",
LogCod,
ActCod,
Gbl.Hierarchy.Node[Hie_CTY].HieCod,
Gbl.Hierarchy.Node[Hie_INS].HieCod,
Gbl.Hierarchy.Node[Hie_CTR].HieCod,
Gbl.Hierarchy.Node[Hie_DEG].HieCod,
Gbl.Hierarchy.Node[Hie_CRS].HieCod,
Gbl.Usrs.Me.UsrDat.UsrCod,
(unsigned) RoleToStore,
Dat_GetTimeGenerationInMicroseconds (),
Dat_GetTimeSendInMicroseconds (),
Par_GetIP ());
}
/*****************************************************************************/
/*********************************** Log comments ****************************/
/*****************************************************************************/
void Log_DB_LogComments (long LogCod,const char *CommentsDB)
{
DB_QueryINSERT ("can not log access (comments)",
"INSERT INTO log_comments"
" (LogCod,Comments)"
" VALUES"
" (%ld,'%s')",
LogCod,
CommentsDB);
}
/*****************************************************************************/
/****************************** Log search string ****************************/
/*****************************************************************************/
void Log_DB_LogSearchString (long LogCod,const struct Sch_Search *Search)
{
DB_QueryINSERT ("can not log access (search)",
"INSERT INTO log_search"
" (LogCod,SearchStr)"
" VALUES"
" (%ld,'%s')",
LogCod,Search->Str);
}
/*****************************************************************************/
/***************** Log web service plugin and API function *******************/
/*****************************************************************************/
void Log_DB_LogAPI (long LogCod)
{
DB_QueryINSERT ("can not log access (API)",
"INSERT INTO log_api"
" (LogCod,PlgCod,FunCod)"
" VALUES"
" (%ld,%ld,%u)",
LogCod,
Gbl.WebService.PlgCod,
(unsigned) Gbl.WebService.Function);
}
/*****************************************************************************/
/********************************* Log banner ********************************/
/*****************************************************************************/
void Log_DB_LogBanner (long LogCod,long BanCodClicked)
{
DB_QueryINSERT ("can not log banner clicked",
"INSERT INTO log_banners"
" (LogCod,BanCod)"
" VALUES"
" (%ld,%ld)",
LogCod,
BanCodClicked);
}
/*****************************************************************************/
/****************************** Get last clicks ******************************/
/*****************************************************************************/
unsigned Log_DB_GetLastClicks (MYSQL_RES **mysql_res)
{
return (unsigned)
DB_QuerySELECT (mysql_res,"can not get last clicks",
"SELECT LogCod," // row[0]
"ActCod," // row[1]
"UNIX_TIMESTAMP()-"
"UNIX_TIMESTAMP(ClickTime)," // row[2]
"Role," // row[3]
"CtyCod," // row[4]
"InsCod," // row[5]
"CtrCod," // row[6]
"DegCod" // row[7]
" FROM log_recent"
" ORDER BY LogCod DESC"
" LIMIT 20");
}
/*****************************************************************************/
/*** Get first click of a user from log table and store in user's figures ****/
/*****************************************************************************/
unsigned Log_DB_GetUsrFirstClick (MYSQL_RES **mysql_res,long UsrCod)
{
return (unsigned)
DB_QuerySELECT (mysql_res,"can not get user's first click",
"SELECT UNIX_TIMESTAMP((SELECT MIN(ClickTime)"
" FROM log"
" WHERE UsrCod=%ld))",
UsrCod);
}
/*****************************************************************************/
/********************** Get number of clicks of a user ***********************/
/*****************************************************************************/
unsigned Log_DB_GetUsrNumClicks (long UsrCod)
{
return (unsigned)
DB_QueryCOUNT ("can not get number of clicks",
"SELECT COUNT(*)"
" FROM log"
" WHERE UsrCod=%ld",
UsrCod);
}
/*****************************************************************************/
/********************** Get my clicks grouped by action **********************/
/*****************************************************************************/
unsigned Log_DB_GetMyClicksGroupedByAction (MYSQL_RES **mysql_res,
time_t FirstClickTimeUTC,
unsigned MaxActions)
{
return (unsigned)
DB_QuerySELECT (mysql_res,"can not get clicks",
"SELECT SQL_NO_CACHE ActCod," // row[0]
"COUNT(*) AS N" // row[1]
" FROM log"
" WHERE ClickTime>=FROM_UNIXTIME(%ld)"
" AND UsrCod=%ld"
" GROUP BY ActCod"
" ORDER BY N DESC"
" LIMIT %u",
(long) FirstClickTimeUTC,
Gbl.Usrs.Me.UsrDat.UsrCod,
MaxActions);
}
/*****************************************************************************/
/************ Get my maximum number of hits per course-year-role ************/
/*****************************************************************************/
unsigned Log_DB_GetMyMaxHitsPerYear (MYSQL_RES **mysql_res,
time_t FirstClickTimeUTC)
{
return (unsigned)
DB_QuerySELECT (mysql_res,"can not get last question index",
"SELECT MAX(N) FROM ("
// Clicks without course selected --------------------------
"SELECT -1 AS CrsCod,"
"YEAR(CONVERT_TZ(ClickTime,@@session.time_zone,'UTC')) AS Year,"
"%u AS Role,"
"COUNT(*) AS N"
" FROM log"
" WHERE ClickTime>=FROM_UNIXTIME(%ld)"
" AND UsrCod=%ld"
" AND CrsCod<=0"
" GROUP BY Year"
// ---------------------------------------------------------
" UNION "
// Clicks as student, non-editing teacher or teacher in courses
"SELECT CrsCod,"
"YEAR(CONVERT_TZ(ClickTime,@@session.time_zone,'UTC')) AS Year,"
"Role,"
"COUNT(*) AS N"
" FROM log"
" WHERE ClickTime>=FROM_UNIXTIME(%ld)"
" AND UsrCod=%ld"
" AND Role>=%u" // Student
" AND Role<=%u" // Teacher
" AND CrsCod>0"
" GROUP BY CrsCod,"
"Year,"
"Role"
// ---------------------------------------------------------
") AS hits_per_crs_year",
(unsigned) Rol_UNK,
(long) FirstClickTimeUTC,
Gbl.Usrs.Me.UsrDat.UsrCod,
(long) FirstClickTimeUTC,
Gbl.Usrs.Me.UsrDat.UsrCod,
(unsigned) Rol_STD,
(unsigned) Rol_TCH);
}
/*****************************************************************************/
/************ Get my courses with number of hits per course-role ************/
/*****************************************************************************/
unsigned Log_DB_GetMyCrssAndHitsPerCrs (MYSQL_RES **mysql_res,Rol_Role_t Role)
{
return (unsigned)
DB_QuerySELECT (mysql_res,"can not get courses of a user",
"SELECT my_courses.CrsCod," // row[0]
"COUNT(*) AS N" // row[1]
" FROM (SELECT CrsCod"
" FROM crs_users"
" WHERE UsrCod=%ld"
" AND Role=%u) AS my_courses" // It's imperative to use a derived table to not block crs_usr!
" LEFT JOIN log"
" ON my_courses.CrsCod=log.CrsCod"
" WHERE log.UsrCod=%ld"
" AND log.Role=%u"
" GROUP BY my_courses.CrsCod"
" ORDER BY N DESC,"
"my_courses.CrsCod DESC",
Gbl.Usrs.Me.UsrDat.UsrCod,(unsigned) Role,
Gbl.Usrs.Me.UsrDat.UsrCod,(unsigned) Role);
}
/*****************************************************************************/
/************************** Get my historic courses **************************/
/*****************************************************************************/
unsigned Log_DB_GetMyHistoricCrss (MYSQL_RES **mysql_res,
Rol_Role_t Role,unsigned MinClicksCrs)
{
return (unsigned)
DB_QuerySELECT (mysql_res,"can not get courses of a user",
"SELECT CrsCod," // row[0]
"COUNT(*) AS N" // row[1]
" FROM log"
" WHERE UsrCod=%ld"
" AND Role=%u"
" AND CrsCod>0"
" GROUP BY CrsCod"
" HAVING N>%u"
" ORDER BY N DESC",
Gbl.Usrs.Me.UsrDat.UsrCod,
(unsigned) Role,
MinClicksCrs);
}
/*****************************************************************************/
/********************** Write my hits grouped by years ***********************/
/*****************************************************************************/
unsigned Log_DB_GetMyHitsPerYear (MYSQL_RES **mysql_res,
bool AnyCourse,long CrsCod,Rol_Role_t Role,
time_t FirstClickTimeUTC)
{
char SubQueryCrs[128];
char SubQueryRol[128];
if (AnyCourse)
SubQueryCrs[0] = '\0';
else
sprintf (SubQueryCrs," AND CrsCod=%ld",CrsCod);
if (Role == Rol_UNK) // Here Rol_UNK means any role
SubQueryRol[0] = '\0';
else
sprintf (SubQueryRol," AND Role=%u",(unsigned) Role);
return (unsigned)
DB_QuerySELECT (mysql_res,"can not get clicks",
"SELECT SQL_NO_CACHE YEAR(CONVERT_TZ(ClickTime,@@session.time_zone,'UTC')) AS Year," // row[0]
"COUNT(*)" // row[1]
" FROM log"
" WHERE ClickTime>=FROM_UNIXTIME(%ld)"
" AND UsrCod=%ld"
"%s"
"%s"
" GROUP BY Year"
" ORDER BY Year DESC",
(long) FirstClickTimeUTC,
Gbl.Usrs.Me.UsrDat.UsrCod,
SubQueryCrs,
SubQueryRol);
}
/*****************************************************************************/
/********************* Get the comments of a hit from log ********************/
/*****************************************************************************/
void Log_DB_GetLogComments (long LogCod,char Comments[Cns_MAX_BYTES_TEXT + 1])
{
DB_QuerySELECTString (Comments,Cns_MAX_BYTES_TEXT,
"can not get log comments",
"SELECT Comments"
" FROM log_comments"
" WHERE LogCod=%ld",
LogCod);
}
/*****************************************************************************/
/************ Sometimes, we delete old entries in recent log table ***********/
/*****************************************************************************/
void Log_DB_RemoveOldEntriesRecentLog (void)
{
/***** Remove all expired clipboards *****/
DB_QueryDELETE ("can not remove old entries from recent log",
"DELETE LOW_PRIORITY FROM log_recent"
" WHERE ClickTime<FROM_UNIXTIME(UNIX_TIMESTAMP()-%lu)",
Log_SECONDS_IN_RECENT_LOG);
}