-
Notifications
You must be signed in to change notification settings - Fork 17
/
swad_banner_database.c
221 lines (192 loc) · 7.61 KB
/
swad_banner_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
// swad_banner_database.c: banners 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-2024 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 Public 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_banner_database.h"
#include "swad_config.h"
#include "swad_database.h"
#include "swad_www.h"
/*****************************************************************************/
/************************** Get list of all banners **************************/
/*****************************************************************************/
unsigned Ban_DB_GetAllBanners (MYSQL_RES **mysql_res)
{
return (unsigned)
DB_QuerySELECT (mysql_res,"can not get banners",
"SELECT BanCod," // row[0]
"Hidden," // row[1]
"ShortName," // row[2]
"FullName," // row[3]
"Img," // row[4]
"WWW" // row[5]
" FROM ban_banners"
" ORDER BY ShortName");
}
/*****************************************************************************/
/********************** Get list of visible banners **************************/
/*****************************************************************************/
unsigned Ban_DB_GetVisibleBanners (MYSQL_RES **mysql_res)
{
return (unsigned)
DB_QuerySELECT (mysql_res,"can not get banners",
"SELECT BanCod," // row[0]
"Hidden," // row[1]
"ShortName," // row[2]
"FullName," // row[3]
"Img," // row[4]
"WWW" // row[5]
" FROM ban_banners"
" WHERE Hidden='N'"
" ORDER BY ShortName");
}
/*****************************************************************************/
/********************** Get list of visible banners **************************/
/*****************************************************************************/
unsigned Ban_DB_GetRandomBanners (MYSQL_RES **mysql_res)
{
// The banner(s) will change once in a while
return (unsigned)
DB_QuerySELECT (mysql_res,"can not get banners",
"SELECT BanCod," // row[0]
"Hidden," // row[1]
"ShortName," // row[2]
"FullName," // row[3]
"Img," // row[4]
"WWW" // row[5]
" FROM ban_banners"
" WHERE Hidden='N'"
" ORDER BY RAND(%lu)"
" LIMIT %u",
(unsigned long) (Dat_GetStartExecutionTimeUTC () /
Cfg_TIME_TO_CHANGE_BANNER),
Cfg_NUMBER_OF_BANNERS);
}
/*****************************************************************************/
/********************* Get data of a banner from database ********************/
/*****************************************************************************/
unsigned Ban_DB_GetBannerDataByCod (MYSQL_RES **mysql_res,long BanCod)
{
return (unsigned)
DB_QuerySELECT (mysql_res,"can not get data of a banner",
"SELECT BanCod," // row[0]
"Hidden," // row[1]
"ShortName," // row[2]
"FullName," // row[3]
"Img," // row[4]
"WWW" // row[5]
" FROM ban_banners"
" WHERE BanCod=%ld",
BanCod);
}
/*****************************************************************************/
/********************* Check if the name of banner exists ********************/
/*****************************************************************************/
bool Ban_DB_CheckIfBannerNameExists (const char *FldName,const char *Name,long Cod,
__attribute__((unused)) long PrtCod,
__attribute__((unused)) unsigned Year)
{
return
DB_QueryEXISTS ("can not check if the name of a banner already existed",
"SELECT EXISTS"
"(SELECT *"
" FROM ban_banners"
" WHERE %s='%s'"
" AND BanCod<>%ld)",
FldName,Name,
Cod);
}
/*****************************************************************************/
/**************************** Create a new banner ****************************/
/*****************************************************************************/
void Ban_DB_CreateBanner (const struct Ban_Banner *Ban)
{
/***** Create a new banner *****/
DB_QueryINSERT ("can not create banner",
"INSERT INTO ban_banners"
" (Hidden,ShortName,FullName,Img,WWW)"
" VALUES"
" ('N','%s','%s','%s','%s')",
Ban->ShrtName,
Ban->FullName,
Ban->Img,
Ban->WWW);
}
/*****************************************************************************/
/**************************** Hide/unhide a banner ***************************/
/*****************************************************************************/
void Ban_DB_HideOrUnhideBanner (long BanCod,HidVis_HiddenOrVisible_t HiddenOrVisible)
{
extern const char HidVis_YN[HidVis_NUM_HIDDEN_VISIBLE];
DB_QueryUPDATE ("can not hide/unhide banner",
"UPDATE ban_banners"
" SET Hidden='%c'"
" WHERE BanCod=%ld",
HidVis_YN[HiddenOrVisible],
BanCod);
}
/*****************************************************************************/
/***************** Update banner name in table of banners ********************/
/*****************************************************************************/
void Ban_DB_UpdateBanName (long BanCod,
const char *FldName,const char *NewBanName)
{
DB_QueryUPDATE ("can not update the name of a banner",
"UPDATE ban_banners"
" SET %s='%s'"
" WHERE BanCod=%ld",
FldName,NewBanName,
BanCod);
}
/*****************************************************************************/
/************** Update the table changing old image by new image *************/
/*****************************************************************************/
void Ban_DB_UpdateBanImg (long BanCod,
const char NewImg[Ban_MAX_BYTES_IMAGE + 1])
{
DB_QueryUPDATE ("can not update the image of a banner",
"UPDATE ban_banners"
" SET Img='%s'"
" WHERE BanCod=%ld",
NewImg,
BanCod);
}
/*****************************************************************************/
/*************** Update the table changing old WWW by new WWW ****************/
/*****************************************************************************/
void Ban_DB_UpdateBanWWW (long BanCod,
const char NewWWW[WWW_MAX_BYTES_WWW + 1])
{
DB_QueryUPDATE ("can not update the web of a banner",
"UPDATE ban_banners"
" SET WWW='%s'"
" WHERE BanCod=%ld",
NewWWW,
BanCod);
}
/*****************************************************************************/
/******************************* Remove a banner *****************************/
/*****************************************************************************/
void Ban_DB_RemoveBanner (long BanCod)
{
DB_QueryDELETE ("can not remove a banner",
"DELETE FROM ban_banners"
" WHERE BanCod=%ld",
BanCod);
}