2021-10-05 22:55:32 +02:00
|
|
|
|
// swad_place_database.c: places, 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.
|
2024-02-07 00:40:28 +01:00
|
|
|
|
Copyright (C) 1999-2024 Antonio Ca<EFBFBD>as Vargas
|
2021-10-05 22:55:32 +02:00
|
|
|
|
|
|
|
|
|
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_database.h"
|
|
|
|
|
#include "swad_global.h"
|
|
|
|
|
#include "swad_place.h"
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/************** External global variables from others modules ****************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
extern struct Globals Gbl;
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/**************************** Create a new place *****************************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
void Plc_DB_CreatePlace (const struct Plc_Place *Plc)
|
|
|
|
|
{
|
|
|
|
|
DB_QueryINSERT ("can not create place",
|
2022-05-30 19:03:56 +02:00
|
|
|
|
"INSERT INTO plc_places"
|
2021-10-05 22:55:32 +02:00
|
|
|
|
" (InsCod,ShortName,FullName)"
|
|
|
|
|
" VALUES"
|
|
|
|
|
" (%ld,'%s','%s')",
|
2023-09-22 14:47:56 +02:00
|
|
|
|
Gbl.Hierarchy.Node[Hie_INS].HieCod,
|
2021-10-05 22:55:32 +02:00
|
|
|
|
Plc->ShrtName,
|
|
|
|
|
Plc->FullName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/****************** Update place name in table of places *********************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2023-10-11 09:55:06 +02:00
|
|
|
|
void Plc_DB_UpdatePlcName (long PlcCod,
|
|
|
|
|
const char *FldName,const char *NewPlcName)
|
2021-10-05 22:55:32 +02:00
|
|
|
|
{
|
|
|
|
|
/***** Update place changing old name by new name */
|
|
|
|
|
DB_QueryUPDATE ("can not update the name of a place",
|
|
|
|
|
"UPDATE plc_places"
|
|
|
|
|
" SET %s='%s'"
|
|
|
|
|
" WHERE PlcCod=%ld",
|
2023-03-10 00:13:55 +01:00
|
|
|
|
FldName,NewPlcName,
|
2021-10-05 22:55:32 +02:00
|
|
|
|
PlcCod);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/************************** Get list of all places ***************************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
unsigned Plc_DB_GetListPlaces (MYSQL_RES **mysql_res,Plc_Order_t SelectedOrder)
|
|
|
|
|
{
|
|
|
|
|
static const char *OrderBySubQuery[Plc_NUM_ORDERS] =
|
|
|
|
|
{
|
|
|
|
|
[Plc_ORDER_BY_PLACE ] = "FullName",
|
|
|
|
|
[Plc_ORDER_BY_NUM_CTRS] = "NumCtrs DESC,"
|
|
|
|
|
"FullName",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (unsigned)
|
|
|
|
|
DB_QuerySELECT (mysql_res,"can not get places",
|
|
|
|
|
"(SELECT plc_places.PlcCod," // row[0]
|
|
|
|
|
"plc_places.ShortName," // row[1]
|
|
|
|
|
"plc_places.FullName," // row[2]
|
|
|
|
|
"COUNT(*) AS NumCtrs" // row[3]
|
|
|
|
|
" FROM plc_places,"
|
|
|
|
|
"ctr_centers"
|
|
|
|
|
" WHERE plc_places.InsCod=%ld"
|
|
|
|
|
" AND plc_places.PlcCod=ctr_centers.PlcCod"
|
|
|
|
|
" AND ctr_centers.InsCod=%ld"
|
2023-03-03 00:28:27 +01:00
|
|
|
|
" GROUP BY plc_places.PlcCod)"
|
2021-11-06 12:13:05 +01:00
|
|
|
|
" UNION "
|
2021-10-05 22:55:32 +02:00
|
|
|
|
"(SELECT PlcCod,"
|
|
|
|
|
"ShortName,"
|
|
|
|
|
"FullName,"
|
|
|
|
|
"0 AS NumCtrs"
|
|
|
|
|
" FROM plc_places"
|
|
|
|
|
" WHERE InsCod=%ld"
|
|
|
|
|
" AND PlcCod NOT IN"
|
2021-11-06 12:13:05 +01:00
|
|
|
|
" (SELECT DISTINCT "
|
|
|
|
|
"PlcCod"
|
2021-10-05 22:55:32 +02:00
|
|
|
|
" FROM ctr_centers"
|
|
|
|
|
" WHERE InsCod=%ld))"
|
2023-03-03 00:28:27 +01:00
|
|
|
|
" ORDER BY %s",
|
2023-09-22 14:47:56 +02:00
|
|
|
|
Gbl.Hierarchy.Node[Hie_INS].HieCod,
|
|
|
|
|
Gbl.Hierarchy.Node[Hie_INS].HieCod,
|
|
|
|
|
Gbl.Hierarchy.Node[Hie_INS].HieCod,
|
|
|
|
|
Gbl.Hierarchy.Node[Hie_INS].HieCod,
|
2021-10-05 22:55:32 +02:00
|
|
|
|
OrderBySubQuery[SelectedOrder]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/********************* Get data of a place from database *********************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2023-03-23 09:35:20 +01:00
|
|
|
|
unsigned Plc_DB_GetPlaceDataByCod (MYSQL_RES **mysql_res,long PlcCod)
|
2021-10-05 22:55:32 +02:00
|
|
|
|
{
|
|
|
|
|
return (unsigned)
|
|
|
|
|
DB_QuerySELECT (mysql_res,"can not get data of a place",
|
2023-03-23 18:28:56 +01:00
|
|
|
|
"(SELECT plc_places.PlcCod," // row[0]
|
|
|
|
|
"plc_places.ShortName," // row[1]
|
|
|
|
|
"plc_places.FullName," // row[2]
|
|
|
|
|
"COUNT(*)" // row[3]
|
2021-10-05 22:55:32 +02:00
|
|
|
|
" FROM plc_places,"
|
|
|
|
|
"ctr_centers"
|
|
|
|
|
" WHERE plc_places.PlcCod=%ld"
|
|
|
|
|
" AND plc_places.PlcCod=ctr_centers.PlcCod"
|
|
|
|
|
" AND ctr_centers.PlcCod=%ld"
|
2023-03-03 00:28:27 +01:00
|
|
|
|
" GROUP BY plc_places.PlcCod)"
|
2021-11-06 12:13:05 +01:00
|
|
|
|
" UNION "
|
2023-03-23 18:28:56 +01:00
|
|
|
|
"(SELECT PlcCod," // row[0]
|
|
|
|
|
"ShortName," // row[1]
|
|
|
|
|
"FullName," // row[2]
|
|
|
|
|
"0" // row[3]
|
2021-10-05 22:55:32 +02:00
|
|
|
|
" FROM plc_places"
|
|
|
|
|
" WHERE PlcCod=%ld"
|
|
|
|
|
" AND PlcCod NOT IN"
|
2021-11-06 12:13:05 +01:00
|
|
|
|
" (SELECT DISTINCT "
|
|
|
|
|
"PlcCod"
|
2021-10-05 22:55:32 +02:00
|
|
|
|
" FROM ctr_centers))",
|
|
|
|
|
PlcCod,
|
|
|
|
|
PlcCod,
|
|
|
|
|
PlcCod);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/********************** Check if the name of place exists ********************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2023-10-10 19:12:17 +02:00
|
|
|
|
bool Plc_DB_CheckIfPlaceNameExists (const char *FldName,const char *Name,long Cod,
|
|
|
|
|
long PrtCod,
|
|
|
|
|
__attribute__((unused)) unsigned Year)
|
2021-10-05 22:55:32 +02:00
|
|
|
|
{
|
2021-11-05 23:41:26 +01:00
|
|
|
|
return
|
|
|
|
|
DB_QueryEXISTS ("can not check if the name of a place already existed",
|
|
|
|
|
"SELECT EXISTS"
|
|
|
|
|
"(SELECT *"
|
|
|
|
|
" FROM plc_places"
|
|
|
|
|
" WHERE InsCod=%ld"
|
|
|
|
|
" AND %s='%s'"
|
|
|
|
|
" AND PlcCod<>%ld)",
|
2023-10-10 19:12:17 +02:00
|
|
|
|
PrtCod,
|
2023-03-10 00:13:55 +01:00
|
|
|
|
FldName,Name,
|
2023-10-10 19:12:17 +02:00
|
|
|
|
Cod);
|
2021-10-05 22:55:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/******************************** Remove place *******************************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
void Plc_DB_RemovePlace (long PlcCod)
|
|
|
|
|
{
|
|
|
|
|
DB_QueryDELETE ("can not remove a place",
|
|
|
|
|
"DELETE FROM plc_places"
|
|
|
|
|
" WHERE PlcCod=%ld",
|
|
|
|
|
PlcCod);
|
|
|
|
|
}
|