-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathswad_agenda_database.c
525 lines (485 loc) · 18.9 KB
/
swad_agenda_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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
// swad_agenda_database.c: user's agenda (personal organizer) 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 ***********************************/
/*****************************************************************************/
#define _GNU_SOURCE // For asprintf
#include <stdio.h> // For asprintf
#include <stdlib.h> // For free
#include "swad_agenda.h"
#include "swad_agenda_database.h"
#include "swad_database.h"
#include "swad_error.h"
#include "swad_global.h"
#include "swad_hierarchy_type.h"
/*****************************************************************************/
/************** External global variables from others modules ****************/
/*****************************************************************************/
extern struct Globals Gbl;
/*****************************************************************************/
/************************* Get list of agenda events *************************/
/*****************************************************************************/
#define Agd_MAX_BYTES_SUBQUERY 128
unsigned Agd_DB_GetListEvents (MYSQL_RES **mysql_res,
const struct Agd_Agenda *Agenda,
Agd_AgendaType_t AgendaType)
{
char *UsrSubQuery;
char Past__FutureEventsSubQuery[Agd_MAX_BYTES_SUBQUERY + 1];
char PrivatPublicEventsSubQuery[Agd_MAX_BYTES_SUBQUERY + 1];
char HiddenVisiblEventsSubQuery[Agd_MAX_BYTES_SUBQUERY + 1];
static const char *OrderBySubQuery[Dat_NUM_START_END_TIME] =
{
[Dat_STR_TIME] = "StartTime,"
"EndTime,"
"Event,"
"Location",
[Dat_END_TIME] = "EndTime,"
"StartTime,"
"Event,"
"Location",
};
unsigned NumEvents;
/***** Get list of events from database *****/
/* Build events subqueries */
switch (AgendaType)
{
case Agd_MY_AGENDA_TODAY:
case Agd_MY_AGENDA:
if (asprintf (&UsrSubQuery,"UsrCod=%ld",
Gbl.Usrs.Me.UsrDat.UsrCod) < 0)
Err_NotEnoughMemoryExit ();
if (AgendaType == Agd_MY_AGENDA_TODAY)
Str_Copy (Past__FutureEventsSubQuery,
" AND DATE(StartTime)<=CURDATE()"
" AND DATE(EndTime)>=CURDATE()",
sizeof (Past__FutureEventsSubQuery) - 1); // Today events
else
switch (Agenda->Past__FutureEvents)
{
case (1 << Agd_PAST___EVENTS):
Str_Copy (Past__FutureEventsSubQuery,
" AND DATE(StartTime)<=CURDATE()",
sizeof (Past__FutureEventsSubQuery) - 1); // Past and today events
break;
case (1 << Agd_FUTURE_EVENTS):
Str_Copy (Past__FutureEventsSubQuery,
" AND DATE(EndTime)>=CURDATE()",
sizeof (Past__FutureEventsSubQuery) - 1); // Today and future events
break;
default:
Past__FutureEventsSubQuery[0] = '\0'; // All events
break;
}
switch (Agenda->PrivatPublicEvents)
{
case (1 << Agd_PRIVAT_EVENTS):
Str_Copy (PrivatPublicEventsSubQuery," AND Public='N'",
sizeof (PrivatPublicEventsSubQuery) - 1); // Private events
break;
case (1 << Agd_PUBLIC_EVENTS):
Str_Copy (PrivatPublicEventsSubQuery," AND Public='Y'",
sizeof (PrivatPublicEventsSubQuery) - 1); // Public events
break;
default:
PrivatPublicEventsSubQuery[0] = '\0'; // All events
break;
}
switch (Agenda->HiddenVisiblEvents)
{
case (1 << HidVis_HIDDEN):
Str_Copy (HiddenVisiblEventsSubQuery," AND Hidden='Y'",
sizeof (HiddenVisiblEventsSubQuery) - 1); // Hidden events
break;
case (1 << HidVis_VISIBLE):
Str_Copy (HiddenVisiblEventsSubQuery," AND Hidden='N'",
sizeof (HiddenVisiblEventsSubQuery) - 1); // Visible events
break;
default:
HiddenVisiblEventsSubQuery[0] = '\0'; // All events
break;
}
break;
case Agd_ANOTHER_AGENDA_TODAY:
case Agd_ANOTHER_AGENDA:
if (asprintf (&UsrSubQuery,"UsrCod=%ld",
Gbl.Usrs.Other.UsrDat.UsrCod) < 0)
Err_NotEnoughMemoryExit ();
if (AgendaType == Agd_ANOTHER_AGENDA_TODAY)
Str_Copy (Past__FutureEventsSubQuery,
" AND DATE(StartTime)<=CURDATE()"
" AND DATE(EndTime)>=CURDATE()",
sizeof (Past__FutureEventsSubQuery) - 1); // Today events
else
Str_Copy (Past__FutureEventsSubQuery,
" AND DATE(EndTime)>=CURDATE()",
sizeof (Past__FutureEventsSubQuery) - 1); // Today and future events
Str_Copy (PrivatPublicEventsSubQuery," AND Public='Y'",
sizeof (PrivatPublicEventsSubQuery) - 1); // Public events
Str_Copy (HiddenVisiblEventsSubQuery," AND Hidden='N'",
sizeof (HiddenVisiblEventsSubQuery) - 1); // Visible events
}
/* Make query */
NumEvents = (unsigned)
DB_QuerySELECT (mysql_res,"can not get agenda events",
"SELECT AgdCod" // row[0]
" FROM agd_agendas"
" WHERE %s%s%s%s"
" ORDER BY %s",
UsrSubQuery,
Past__FutureEventsSubQuery,
PrivatPublicEventsSubQuery,
HiddenVisiblEventsSubQuery,
OrderBySubQuery[Agenda->SelectedOrder]);
/* Free allocated memory for subquery */
free (UsrSubQuery);
return NumEvents;
}
/*****************************************************************************/
/*********************** Get event data using its code ***********************/
/*****************************************************************************/
unsigned Agd_DB_GetEventDataByCod (MYSQL_RES **mysql_res,
const struct Agd_Event *AgdEvent)
{
/***** Get data of event from database *****/
return (unsigned)
DB_QuerySELECT (mysql_res,"can not get agenda event data",
"SELECT AgdCod," // row[0]
"Public," // row[1]
"Hidden," // row[2]
"UNIX_TIMESTAMP(StartTime)," // row[3]
"UNIX_TIMESTAMP(EndTime)," // row[4]
"NOW()>EndTime," // row[5] Past event?
"NOW()<StartTime," // row[6] Future event?
"Event," // row[7]
"Location" // row[8]
" FROM agd_agendas"
" WHERE AgdCod=%ld"
" AND UsrCod=%ld",
AgdEvent->AgdCod,
AgdEvent->UsrCod);
}
/*****************************************************************************/
/*********************** Get event text from database ************************/
/*****************************************************************************/
void Agd_DB_GetEventTxt (const struct Agd_Event *AgdEvent,
char Txt[Cns_MAX_BYTES_TEXT + 1])
{
DB_QuerySELECTString (Txt,Cns_MAX_BYTES_TEXT,"can not get event text",
"SELECT Txt"
" FROM agd_agendas"
" WHERE AgdCod=%ld"
" AND UsrCod=%ld",
AgdEvent->AgdCod,
AgdEvent->UsrCod);
}
/*****************************************************************************/
/************************* Remove event from database ************************/
/*****************************************************************************/
void Agd_DB_RemoveEvent (const struct Agd_Event *AgdEvent)
{
DB_QueryDELETE ("can not remove event",
"DELETE FROM agd_agendas"
" WHERE AgdCod=%ld"
" AND UsrCod=%ld",
AgdEvent->AgdCod,
AgdEvent->UsrCod);
}
/*****************************************************************************/
/************************** Create a new event *******************************/
/*****************************************************************************/
long Agd_DB_CreateEvent (const struct Agd_Event *AgdEvent,const char *Txt)
{
return
DB_QueryINSERTandReturnCode ("can not create new event",
"INSERT INTO agd_agendas"
" (UsrCod,StartTime,EndTime,"
"Event,Location,Txt)"
" VALUES"
" (%ld,FROM_UNIXTIME(%ld),FROM_UNIXTIME(%ld),"
"'%s','%s','%s')",
AgdEvent->UsrCod,
AgdEvent->TimeUTC[Dat_STR_TIME],
AgdEvent->TimeUTC[Dat_END_TIME],
AgdEvent->Title,
AgdEvent->Location,
Txt);
}
/*****************************************************************************/
/************************ Update an existing event ***************************/
/*****************************************************************************/
void Agd_DB_UpdateEvent (const struct Agd_Event *AgdEvent,const char *Txt)
{
/***** Update the data of the event *****/
DB_QueryUPDATE ("can not update event",
"UPDATE agd_agendas"
" SET StartTime=FROM_UNIXTIME(%ld),"
"EndTime=FROM_UNIXTIME(%ld),"
"Event='%s',"
"Location='%s',"
"Txt='%s'"
" WHERE AgdCod=%ld"
" AND UsrCod=%ld",
AgdEvent->TimeUTC[Dat_STR_TIME],
AgdEvent->TimeUTC[Dat_END_TIME],
AgdEvent->Title,
AgdEvent->Location,
Txt,
AgdEvent->AgdCod,
AgdEvent->UsrCod);
}
/*****************************************************************************/
/*************************** Hide/unhide an event ****************************/
/*****************************************************************************/
void Agd_DB_HideOrUnhideEvent (long AgdCod,long UsrCod,
HidVis_HiddenOrVisible_t HiddenOrVisible)
{
extern const char HidVis_YN[HidVis_NUM_HIDDEN_VISIBLE];
DB_QueryUPDATE ("can not hide/unhide event",
"UPDATE agd_agendas"
" SET Hidden='%c'"
" WHERE AgdCod=%ld"
" AND UsrCod=%ld",
HidVis_YN[HiddenOrVisible],
AgdCod,
UsrCod);
}
/*****************************************************************************/
/***************** Set an existing event as public/private *******************/
/*****************************************************************************/
void Agd_DB_MakeEventPublic (const struct Agd_Event *AgdEvent)
{
DB_QueryUPDATE ("can not make event public",
"UPDATE agd_agendas"
" SET Public='Y'"
" WHERE AgdCod=%ld"
" AND UsrCod=%ld",
AgdEvent->AgdCod,
AgdEvent->UsrCod);
}
void Agd_DB_MakeEventPrivate (const struct Agd_Event *AgdEvent)
{
DB_QueryUPDATE ("can not make event private",
"UPDATE agd_agendas"
" SET Public='N'"
" WHERE AgdCod=%ld"
" AND UsrCod=%ld",
AgdEvent->AgdCod,
AgdEvent->UsrCod);
}
/*****************************************************************************/
/************************ Remove all events of a user ************************/
/*****************************************************************************/
void Agd_DB_RemoveUsrEvents (long UsrCod)
{
DB_QueryDELETE ("can not remove all events of a user",
"DELETE FROM agd_agendas"
" WHERE UsrCod=%ld",
UsrCod);
}
/*****************************************************************************/
/********************* Get number of events from a user **********************/
/*****************************************************************************/
unsigned Agd_DB_GetNumEventsFromUsr (long UsrCod)
{
return (unsigned)
DB_QueryCOUNT ("can not get number of events from user",
"SELECT COUNT(*)"
" FROM agd_agendas"
" WHERE UsrCod=%ld",
UsrCod);
}
/*****************************************************************************/
/********************** Get number of users with events **********************/
/*****************************************************************************/
// Returns the number of users with events in a given hierarchy level
unsigned Agd_DB_GetNumUsrsWithEvents (Hie_Level_t Level)
{
/***** Get number of users with events from database *****/
switch (Level)
{
case Hie_SYS:
return (unsigned)
DB_QueryCOUNT ("can not get number of users with events",
"SELECT COUNT(DISTINCT UsrCod)"
" FROM agd_agendas"
" WHERE UsrCod>0");
case Hie_CTY:
return (unsigned)
DB_QueryCOUNT ("can not get number of users with events",
"SELECT COUNT(DISTINCT agd_agendas.UsrCod)"
" FROM ins_instits,"
"ctr_centers,"
"deg_degrees,"
"crs_courses,"
"crs_users,"
"agd_agendas"
" WHERE ins_instits.CtyCod=%ld"
" AND ins_instits.InsCod=ctr_centers.InsCod"
" AND ctr_centers.CtrCod=deg_degrees.CtrCod"
" AND deg_degrees.DegCod=crs_courses.DegCod"
" AND crs_courses.CrsCod=crs_users.CrsCod"
" AND crs_users.UsrCod=agd_agendas.UsrCod",
Gbl.Hierarchy.Node[Hie_CTY].HieCod);
case Hie_INS:
return (unsigned)
DB_QueryCOUNT ("can not get number of users with events",
"SELECT COUNT(DISTINCT agd_agendas.UsrCod)"
" FROM ctr_centers,"
"deg_degrees,"
"crs_courses,"
"crs_users,"
"agd_agendas"
" WHERE ctr_centers.InsCod=%ld"
" AND ctr_centers.CtrCod=deg_degrees.CtrCod"
" AND deg_degrees.DegCod=crs_courses.DegCod"
" AND crs_courses.CrsCod=crs_users.CrsCod"
" AND crs_users.UsrCod=agd_agendas.UsrCod",
Gbl.Hierarchy.Node[Hie_INS].HieCod);
case Hie_CTR:
return (unsigned)
DB_QueryCOUNT ("can not get number of users with events",
"SELECT COUNT(DISTINCT agd_agendas.UsrCod)"
" FROM deg_degrees,"
"crs_courses,"
"crs_users,"
"agd_agendas"
" WHERE deg_degrees.CtrCod=%ld"
" AND deg_degrees.DegCod=crs_courses.DegCod"
" AND crs_courses.CrsCod=crs_users.CrsCod"
" AND crs_users.UsrCod=agd_agendas.UsrCod",
Gbl.Hierarchy.Node[Hie_CTR].HieCod);
case Hie_DEG:
return (unsigned)
DB_QueryCOUNT ("can not get number of users with events",
"SELECT COUNT(DISTINCT agd_agendas.UsrCod)"
" FROM crs_courses,"
"crs_users,"
"agd_agendas"
" WHERE crs_courses.DegCod=%ld"
" AND crs_courses.CrsCod=crs_users.CrsCod"
" AND crs_users.UsrCod=agd_agendas.UsrCod",
Gbl.Hierarchy.Node[Hie_DEG].HieCod);
case Hie_CRS:
return (unsigned)
DB_QueryCOUNT ("can not get number of users with events",
"SELECT COUNT(DISTINCT agd_agendas.UsrCod)"
" FROM crs_users,"
"agd_agendas"
" WHERE crs_users.CrsCod=%ld"
" AND crs_users.UsrCod=agd_agendas.UsrCod",
Gbl.Hierarchy.Node[Hie_CRS].HieCod);
default:
Err_WrongHierarchyLevelExit ();
return 0; // Not reached
}
}
/*****************************************************************************/
/*************************** Get number of events ****************************/
/*****************************************************************************/
// Returns the number of events in a given hierarchy level
unsigned Agd_DB_GetNumEvents (Hie_Level_t Level)
{
/***** Get number of events from database *****/
switch (Level)
{
case Hie_SYS:
return (unsigned)
DB_QueryCOUNT ("can not get number of events",
"SELECT COUNT(*)"
" FROM agd_agendas"
" WHERE UsrCod>0");
case Hie_CTY:
return (unsigned)
DB_QueryCOUNT ("can not get number of events",
"SELECT COUNT(*)"
" FROM (SELECT DISTINCT "
"crs_users.UsrCod"
" FROM ins_instits,"
"ctr_centers,"
"deg_degrees,"
"crs_courses,"
"crs_users"
" WHERE ins_instits.CtyCod=%ld"
" AND ins_instits.InsCod=ctr_centers.InsCod"
" AND ctr_centers.CtrCod=deg_degrees.CtrCod"
" AND deg_degrees.DegCod=crs_courses.DegCod"
" AND crs_courses.CrsCod=crs_users.CrsCod) AS users,"
"agd_agendas"
" WHERE users.UsrCod=agd_agendas.UsrCod",
Gbl.Hierarchy.Node[Hie_CTY].HieCod);
case Hie_INS:
return (unsigned)
DB_QueryCOUNT ("can not get number of events",
"SELECT COUNT(*)"
" FROM (SELECT DISTINCT "
"crs_users.UsrCod"
" FROM ctr_centers,"
"deg_degrees,"
"crs_courses,"
"crs_users"
" WHERE ctr_centers.InsCod=%ld"
" AND ctr_centers.CtrCod=deg_degrees.CtrCod"
" AND deg_degrees.DegCod=crs_courses.DegCod"
" AND crs_courses.CrsCod=crs_users.CrsCod) AS users,"
"agd_agendas"
" WHERE users.UsrCod=agd_agendas.UsrCod",
Gbl.Hierarchy.Node[Hie_INS].HieCod);
case Hie_CTR:
return (unsigned)
DB_QueryCOUNT ("can not get number of events",
"SELECT COUNT(*)"
" FROM (SELECT DISTINCT "
"crs_users.UsrCod"
" FROM deg_degrees,"
"crs_courses,"
"crs_users"
" WHERE deg_degrees.CtrCod=%ld"
" AND deg_degrees.DegCod=crs_courses.DegCod"
" AND crs_courses.CrsCod=crs_users.CrsCod) AS users,"
"agd_agendas"
" WHERE users.UsrCod=agd_agendas.UsrCod",
Gbl.Hierarchy.Node[Hie_CTR].HieCod);
case Hie_DEG:
return (unsigned)
DB_QueryCOUNT ("can not get number of events",
"SELECT COUNT(*)"
" FROM (SELECT DISTINCT "
"crs_users.UsrCod"
" FROM crs_courses,"
"crs_users"
" WHERE crs_courses.DegCod=%ld"
" AND crs_courses.CrsCod=crs_users.CrsCod) AS users,"
"agd_agendas"
" WHERE users.UsrCod=agd_agendas.UsrCod",
Gbl.Hierarchy.Node[Hie_DEG].HieCod);
case Hie_CRS:
return (unsigned)
DB_QueryCOUNT ("can not get number of events",
"SELECT COUNT(*)"
" FROM crs_users,"
"agd_agendas"
" WHERE crs_users.CrsCod=%ld"
" AND crs_users.UsrCod=agd_agendas.UsrCod",
Gbl.Hierarchy.Node[Hie_CRS].HieCod);
default:
Err_WrongHierarchyLevelExit ();
return 0; // Not reached
}
}