From bed2db97f9d381056fe0b5a0d7c863c6cb5998fb Mon Sep 17 00:00:00 2001 From: acanas Date: Thu, 30 Sep 2021 00:04:41 +0200 Subject: [PATCH] Version 21.20: Sep 30, 2021 New module swad_nickname_database for database queries related to nicknames. --- swad_nickname_database.c | 180 +++++++++++++++++++++++++++++++++++++++ swad_nickname_database.h | 53 ++++++++++++ 2 files changed, 233 insertions(+) create mode 100644 swad_nickname_database.c create mode 100644 swad_nickname_database.h diff --git a/swad_nickname_database.c b/swad_nickname_database.c new file mode 100644 index 00000000..b5df68b5 --- /dev/null +++ b/swad_nickname_database.c @@ -0,0 +1,180 @@ +// swad_nickname_database.c: Users' nicknames 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 . +*/ +/*****************************************************************************/ +/********************************* Headers ***********************************/ +/*****************************************************************************/ + +// #include // For string functions + +// #include "swad_account.h" +// #include "swad_box.h" +#include "swad_database.h" +// #include "swad_form.h" +// #include "swad_global.h" +// #include "swad_HTML.h" +#include "swad_nickname_database.h" +// #include "swad_parameter.h" +// #include "swad_QR.h" +// #include "swad_user.h" + +/*****************************************************************************/ +/************** External global variables from others modules ****************/ +/*****************************************************************************/ + +// extern struct Globals Gbl; + +/*****************************************************************************/ +/***************************** Private constants *****************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/******************************* Private types *******************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/***************************** Private variables *****************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/***************************** Private prototypes ****************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/******************* Update user's nickname in database **********************/ +/*****************************************************************************/ + +void Nck_DB_UpdateNick (long UsrCod,const char *NewNickname) + { + DB_QueryREPLACE ("can not update nickname", + "REPLACE INTO usr_nicknames" + " (UsrCod,Nickname,CreatTime)" + " VALUES" + " (%ld,'%s',NOW())", + UsrCod, + NewNickname); + } + +/*****************************************************************************/ +/************** Get user's code of a user from his/her nickname **************/ +/*****************************************************************************/ + +long Nck_DB_GetUsrCodFromNickname (const char NickWithoutArroba[Cns_MAX_BYTES_USR_LOGIN + 1]) + { + return DB_QuerySELECTCode ("can not get user's code", + "SELECT usr_nicknames.UsrCod" + " FROM usr_nicknames," + "usr_data" + " WHERE usr_nicknames.Nickname='%s'" + " AND usr_nicknames.UsrCod=usr_data.UsrCod", + NickWithoutArroba); + } + +/*****************************************************************************/ +/************* Get nickname of a user from his/her user's code ***************/ +/*****************************************************************************/ + +void Nck_DB_GetNicknameFromUsrCod (long UsrCod, + char NickWithoutArroba[Cns_MAX_BYTES_USR_LOGIN + 1]) + { + /***** Get current (last updated) user's nickname from database *****/ + DB_QuerySELECTString (NickWithoutArroba,Cns_MAX_BYTES_USR_LOGIN, + "can not get nickname", + "SELECT Nickname" + " FROM usr_nicknames" + " WHERE UsrCod=%ld" + " ORDER BY CreatTime DESC" + " LIMIT 1", + UsrCod); + } + +/*****************************************************************************/ +/***************************** Get my nicknames ******************************/ +/*****************************************************************************/ + +unsigned Nck_DB_GetUsrNicknames (MYSQL_RES **mysql_res,long UsrCod) + { + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get nicknames of a user", + "SELECT Nickname" // row[0] + " FROM usr_nicknames" + " WHERE UsrCod=%ld" + " ORDER BY CreatTime DESC", + UsrCod); + } + +/*****************************************************************************/ +/************ Check if nickname matches any of a user's nicknames ************/ +/*****************************************************************************/ + +bool Nck_DB_CheckIfNickMatchesAnyUsrNick (long UsrCod,const char NickWithoutArr[Cns_MAX_BYTES_USR_LOGIN + 1]) + { + return (DB_QueryCOUNT ("can not check if nickname already existed", + "SELECT COUNT(*)" + " FROM usr_nicknames" + " WHERE UsrCod=%ld" + " AND Nickname='%s'", + UsrCod, + NickWithoutArr) != 0); + } + +/*****************************************************************************/ +/********* Check if nickname matches any of other user's nicknames ***********/ +/*****************************************************************************/ + +bool Nck_DB_CheckIfNickMatchesAnyOtherUsrsNicks (long UsrCod,const char NickWithoutArr[Cns_MAX_BYTES_USR_LOGIN + 1]) + { + return (DB_QueryCOUNT ("can not check if nickname already existed", + "SELECT COUNT(*)" + " FROM usr_nicknames" + " WHERE Nickname='%s'" + " AND UsrCod<>%ld", + NickWithoutArr, + UsrCod) != 0); // A nickname of another user is the same that user's nickname + } + +/*****************************************************************************/ +/********************** Remove a nickname from database **********************/ +/*****************************************************************************/ + +void Nck_DB_RemoveNickname (long UsrCod,const char *Nickname) + { + DB_QueryREPLACE ("can not remove a nickname", + "DELETE FROM usr_nicknames" + " WHERE UsrCod=%ld" + " AND Nickname='%s'", + UsrCod, + Nickname); + } + +/*****************************************************************************/ +/************************** Remove user's nicknames **************************/ +/*****************************************************************************/ + +void Nck_DB_RemoveUsrNicknames (long UsrCod) + { + DB_QueryDELETE ("can not remove user's nicknames", + "DELETE FROM usr_nicknames" + " WHERE UsrCod=%ld", + UsrCod); + } diff --git a/swad_nickname_database.h b/swad_nickname_database.h new file mode 100644 index 00000000..a86f8f53 --- /dev/null +++ b/swad_nickname_database.h @@ -0,0 +1,53 @@ +// swad_nickname_database.h: Users' nicknames operations with database + +#ifndef _SWAD_NCK_DB +#define _SWAD_NCK_DB +/* + SWAD (Shared Workspace At a Distance in Spanish), + 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 . +*/ + +/*****************************************************************************/ +/********************************* Headers ***********************************/ +/*****************************************************************************/ + +#include "swad_nickname.h" + +/*****************************************************************************/ +/************************* Public types and constants ************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/***************************** Public prototypes *****************************/ +/*****************************************************************************/ + +void Nck_DB_UpdateNick (long UsrCod,const char *NewNickname); + +long Nck_DB_GetUsrCodFromNickname (const char NickWithoutArroba[Cns_MAX_BYTES_USR_LOGIN + 1]); +void Nck_DB_GetNicknameFromUsrCod (long UsrCod, + char Nickname[Cns_MAX_BYTES_USR_LOGIN + 1]); +unsigned Nck_DB_GetUsrNicknames (MYSQL_RES **mysql_res,long UsrCod); +bool Nck_DB_CheckIfNickMatchesAnyUsrNick (long UsrCod,const char NickWithoutArr[Cns_MAX_BYTES_USR_LOGIN + 1]); +bool Nck_DB_CheckIfNickMatchesAnyOtherUsrsNicks (long UsrCod,const char NickWithoutArr[Cns_MAX_BYTES_USR_LOGIN + 1]); + +void Nck_DB_RemoveNickname (long UsrCod,const char *Nickname); +void Nck_DB_RemoveUsrNicknames (long UsrCod); + +#endif