Version 20.77: May 18, 2021 New module swad_banner_database for database queries related to banners.

This commit is contained in:
acanas 2021-05-18 14:40:34 +02:00
parent 5ee25dc5cf
commit dbfbac03e3
2 changed files with 311 additions and 0 deletions

252
swad_banner_database.c Normal file
View File

@ -0,0 +1,252 @@
// 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-2021 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 <stddef.h> // For NULL
// #include <stdlib.h> // For calloc
// #include <string.h> // For string functions
#include "swad_banner.h"
#include "swad_banner_database.h"
// #include "swad_box.h"
// #include "swad_constant.h"
#include "swad_config.h"
#include "swad_database.h"
// #include "swad_error.h"
// #include "swad_form.h"
#include "swad_global.h"
// #include "swad_HTML.h"
// #include "swad_language.h"
// #include "swad_link.h"
// #include "swad_parameter.h"
// #include "swad_photo.h"
/*****************************************************************************/
/************** External global variables from others modules ****************/
/*****************************************************************************/
extern struct Globals Gbl;
/*****************************************************************************/
/***************************** Private constants *****************************/
/*****************************************************************************/
/*****************************************************************************/
/******************************* Private types *******************************/
/*****************************************************************************/
/*****************************************************************************/
/***************************** Private variables *****************************/
/*****************************************************************************/
/*****************************************************************************/
/***************************** Private prototypes ****************************/
/*****************************************************************************/
/*****************************************************************************/
/************************** 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) (Gbl.StartExecutionTimeUTC /
Cfg_TIME_TO_CHANGE_BANNER),
Cfg_NUMBER_OF_BANNERS);
}
/*****************************************************************************/
/********************* Get data of a banner from database ********************/
/*****************************************************************************/
unsigned Ban_DB_GetDataOfBannerByCod (MYSQL_RES **mysql_res,long BanCod)
{
return (unsigned)
DB_QuerySELECT (mysql_res,"can not get data of a banner",
"SELECT Hidden," // row[0]
"ShortName," // row[1]
"FullName," // row[2]
"Img," // row[3]
"WWW" // row[4]
" FROM ban_banners"
" WHERE BanCod=%ld",
BanCod);
}
/*****************************************************************************/
/********************* Check if the name of banner exists ********************/
/*****************************************************************************/
bool Ban_DB_CheckIfBannerNameExists (const char *FieldName,const char *Name,long BanCod)
{
/***** Get number of banners with a name from database *****/
return (DB_QueryCOUNT ("can not check if the name of a banner already existed",
"SELECT COUNT(*)"
" FROM ban_banners"
" WHERE %s='%s'"
" AND BanCod<>%ld",
FieldName,Name,
BanCod) != 0);
}
/*****************************************************************************/
/**************************** 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);
}
/*****************************************************************************/
/************************* Change hiddeness of banner ************************/
/*****************************************************************************/
void Ban_DB_ShowOrHideBanner (long BanCod,bool Hide)
{
DB_QueryUPDATE ("can not change status of a banner in database",
"UPDATE ban_banners"
" SET Hidden='%c'"
" WHERE BanCod=%ld",
Hide ? 'Y' :
'N',
BanCod);
}
/*****************************************************************************/
/***************** Update banner name in table of banners ********************/
/*****************************************************************************/
void Ban_DB_UpdateBanName (long BanCod,const char *FieldName,
const char *NewBanName)
{
DB_QueryUPDATE ("can not update the name of a banner",
"UPDATE ban_banners"
" SET %s='%s'"
" WHERE BanCod=%ld",
FieldName,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[Cns_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);
}

59
swad_banner_database.h Normal file
View File

@ -0,0 +1,59 @@
// swad_banner_database.h: banners operations with database
#ifndef _SWAD_BAN_DB
#define _SWAD_BAN_DB
/*
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-2021 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 <mysql/mysql.h> // To access MySQL databases
#include "swad_banner.h"
/*****************************************************************************/
/************************** Public types and constants ***********************/
/*****************************************************************************/
/*****************************************************************************/
/***************************** Public prototypes *****************************/
/*****************************************************************************/
unsigned Ban_DB_GetAllBanners (MYSQL_RES **mysql_res);
unsigned Ban_DB_GetVisibleBanners (MYSQL_RES **mysql_res);
unsigned Ban_DB_GetRandomBanners (MYSQL_RES **mysql_res);
unsigned Ban_DB_GetDataOfBannerByCod (MYSQL_RES **mysql_res,long BanCod);
bool Ban_DB_CheckIfBannerNameExists (const char *FieldName,const char *Name,long BanCod);
void Ban_DB_CreateBanner (const struct Ban_Banner *Ban);
void Ban_DB_ShowOrHideBanner (long BanCod,bool Hide);
void Ban_DB_UpdateBanName (long BanCod,const char *FieldName,
const char *NewBanName);
void Ban_DB_UpdateBanImg (long BanCod,
const char NewImg[Ban_MAX_BYTES_IMAGE + 1]);
void Ban_DB_UpdateBanWWW (long BanCod,
const char NewWWW[Cns_MAX_BYTES_WWW + 1]);
void Ban_DB_RemoveBanner (long BanCod);
#endif