From f2cc9b600f20ac17dd181e188ea0d03c40478278 Mon Sep 17 00:00:00 2001 From: acanas Date: Wed, 13 Oct 2021 22:52:59 +0200 Subject: [PATCH] Version 21.32.1: Oct 13, 2021 Queries moved to module swad_role_database. --- Makefile | 2 +- swad_building.c | 1 + swad_center.c | 1 + swad_changelog.h | 3 +- swad_group_database.c | 13 ++ swad_group_database.h | 1 + swad_room.c | 301 ++++----------------------------- swad_room.h | 6 +- swad_room_database.c | 382 ++++++++++++++++++++++++++++++++++++++++++ swad_room_database.h | 62 +++++++ 10 files changed, 495 insertions(+), 277 deletions(-) create mode 100644 swad_room_database.c create mode 100644 swad_room_database.h diff --git a/Makefile b/Makefile index 64fd79c8..b3cc513f 100644 --- a/Makefile +++ b/Makefile @@ -77,7 +77,7 @@ OBJS = swad_account.o swad_account_database.o swad_action.o swad_admin.o \ swad_QR.o \ swad_record.o swad_record_database.o swad_report.o \ swad_report_database.o swad_role.o swad_role_database.o swad_room.o \ - swad_RSS.o \ + swad_room_database.o swad_RSS.o \ swad_scope.o swad_search.o swad_session.o swad_setting.o \ swad_statistic.o swad_string.o swad_survey.o swad_syllabus.o \ swad_system_config.o \ diff --git a/swad_building.c b/swad_building.c index 52a9e17a..a212b6e3 100644 --- a/swad_building.c +++ b/swad_building.c @@ -37,6 +37,7 @@ #include "swad_form.h" #include "swad_global.h" #include "swad_HTML.h" +#include "swad_room_database.h" /*****************************************************************************/ /************** External global variables from others modules ****************/ diff --git a/swad_center.c b/swad_center.c index 0b12fd4b..4cd5b5a6 100644 --- a/swad_center.c +++ b/swad_center.c @@ -49,6 +49,7 @@ #include "swad_logo.h" #include "swad_message.h" #include "swad_place.h" +#include "swad_room_database.h" #include "swad_survey.h" /*****************************************************************************/ diff --git a/swad_changelog.h b/swad_changelog.h index 29ca59ae..c7a004e2 100644 --- a/swad_changelog.h +++ b/swad_changelog.h @@ -602,13 +602,14 @@ TODO: FIX BUG, URGENT! En las fechas como par TODO: En las encuestas, que los estudiantes no puedan ver los resultados hasta que no finalice el plazo. */ -#define Log_PLATFORM_VERSION "SWAD 21.32 (2021-10-13)" +#define Log_PLATFORM_VERSION "SWAD 21.32.1 (2021-10-13)" #define CSS_FILE "swad20.45.css" #define JS_FILE "swad20.69.1.js" /* TODO: Rename CENTRE to CENTER in help wiki. TODO: Rename ASSESSMENT.Announcements to ASSESSMENT.Calls_for_exams + Version 21.32.1: Oct 13, 2021 Queries moved to module swad_role_database. (319787 lines) Version 21.32: Oct 13, 2021 New module swad_role_database for database queries related to user roles. (319611 lines) Version 21.31.2: Oct 13, 2021 Queries moved to module swad_log_database. (319542 lines) Version 21.31.1: Oct 13, 2021 Queries moved to module swad_log_database. (319520 lines) diff --git a/swad_group_database.c b/swad_group_database.c index 07a5c5c4..aad3abff 100644 --- a/swad_group_database.c +++ b/swad_group_database.c @@ -850,6 +850,19 @@ void Grp_DB_ChangeRoomOfGrp (long GrpCod,long NewRooCod) GrpCod); } +/*****************************************************************************/ +/********************* Update groups assigned to a room **********************/ +/*****************************************************************************/ + +void Grp_DB_ResetRoomInGrps (long RooCod) + { + DB_QueryUPDATE ("can not update room in groups", + "UPDATE grp_groups" + " SET RooCod=0" // 0 means another room + " WHERE RooCod=%ld", + RooCod); + } + /*****************************************************************************/ /******************* Change maximum of students in a group *******************/ /*****************************************************************************/ diff --git a/swad_group_database.h b/swad_group_database.h index 15e5446d..a7df8597 100644 --- a/swad_group_database.h +++ b/swad_group_database.h @@ -103,6 +103,7 @@ void Grp_DB_EnableFileZonesGrp (long GrpCod); void Grp_DB_DisableFileZonesGrp (long GrpCod); void Grp_DB_ChangeGrpTypOfGrp (long GrpCod,long NewGrpTypCod); void Grp_DB_ChangeRoomOfGrp (long GrpCod,long NewRooCod); +void Grp_DB_ResetRoomInGrps (long RooCod); void Grp_DB_ChangeMaxStdsOfGrp (long GrpCod,unsigned NewMaxStds); bool Grp_DB_CheckIfAvailableGrpTyp (long GrpTypCod); diff --git a/swad_room.c b/swad_room.c index 971548e5..f341b29a 100644 --- a/swad_room.c +++ b/swad_room.c @@ -35,9 +35,11 @@ #include "swad_error.h" #include "swad_form.h" #include "swad_global.h" +#include "swad_group_database.h" #include "swad_HTML.h" #include "swad_MAC.h" #include "swad_room.h" +#include "swad_room_database.h" /*****************************************************************************/ /************** External global variables from others modules ****************/ @@ -49,35 +51,6 @@ extern struct Globals Gbl; /***************************** Private constants *****************************/ /*****************************************************************************/ -static const char *Roo_TypesDB[Roo_NUM_TYPES] = - { - [Roo_NO_TYPE ] = "no_type", - [Roo_ADMINISTRATION] = "administration", - [Roo_AUDITORIUM ] = "auditorium", - [Roo_CAFETERIA ] = "cafeteria", - [Roo_CANTEEN ] = "canteen", - [Roo_CLASSROOM ] = "classroom", - [Roo_CONCIERGE ] = "concierge", - [Roo_CORRIDOR ] = "corridor", - [Roo_GYM ] = "gym", - [Roo_HALL ] = "hall", - [Roo_KINDERGARTEN ] = "kindergarten", - [Roo_LABORATORY ] = "laboratory", - [Roo_LIBRARY ] = "library", - [Roo_OFFICE ] = "office", - [Roo_OUTDOORS ] = "outdoors", - [Roo_PARKING ] = "parking", - [Roo_PAVILION ] = "pavilion", - [Roo_ROOM ] = "room", - [Roo_SECRETARIAT ] = "secretariat", - [Roo_SEMINAR ] = "seminar", - [Roo_SHOP ] = "shop", - [Roo_STORE ] = "store", - [Roo_TOILETS ] = "toilets", - [Roo_VIRTUAL ] = "virtual", - [Roo_YARD ] = "yard", - }; - static const char *Roo_TypesIcons[Roo_NUM_TYPES] = { [Roo_NO_TYPE ] = "question.svg", @@ -123,7 +96,6 @@ static struct Roo_Room *Roo_EditingRoom = NULL; // Static variable to keep the r static void Roo_GetAndListMACAddresses (long RooCod); static void Roo_GetAndEditMACAddresses (long RooCod,const char *Anchor); -static unsigned Roo_DB_GetMACAddresses (long RooCod,MYSQL_RES **mysql_res); static Roo_Order_t Roo_GetParamRoomOrder (void); static bool Roo_CheckIfICanCreateRooms (void); @@ -149,10 +121,6 @@ static int Roo_GetParamFloor (void); static Roo_RoomType_t Roo_GetParamType (void); static void Roo_RenameRoom (Cns_ShrtOrFullName_t ShrtOrFullName); -static bool Roo_DB_CheckIfRoomNameExists (long RooCod, - const char *FieldName,const char *Name); -static void Roo_DB_UpdateRoomName (long RooCod, - const char *FieldName,const char *NewRoomName); static void Roo_WriteCapacity (char Str[Cns_MAX_DECIMAL_DIGITS_UINT + 1],unsigned Capacity); @@ -331,7 +299,7 @@ static void Roo_GetAndListMACAddresses (long RooCod) unsigned NumMACs; /***** Get MAC addresses from database *****/ - NumMACs = Roo_DB_GetMACAddresses (RooCod,&mysql_res); + NumMACs = Roo_DB_GetMACAddresses (&mysql_res,RooCod); /***** Write the MAC addresses *****/ MAC_ListMACAddresses (NumMACs,&mysql_res); @@ -347,28 +315,12 @@ static void Roo_GetAndEditMACAddresses (long RooCod,const char *Anchor) unsigned NumMACs; /***** Get MAC addresses from database *****/ - NumMACs = Roo_DB_GetMACAddresses (RooCod,&mysql_res); + NumMACs = Roo_DB_GetMACAddresses (&mysql_res,RooCod); /***** Write the MAC addresses *****/ MAC_EditMACAddresses (RooCod,Anchor,NumMACs,&mysql_res); } -/*****************************************************************************/ -/***************** Get the MAC addresses associated to a room ****************/ -/*****************************************************************************/ - -static unsigned Roo_DB_GetMACAddresses (long RooCod,MYSQL_RES **mysql_res) - { - /***** Get MAC addresses from database *****/ - return (unsigned) - DB_QuerySELECT (mysql_res,"can not get MAC addresses", - "SELECT MAC" // row[0] - " FROM roo_macs" - " WHERE RooCod=%ld" - " ORDER BY MAC", - RooCod); - } - /*****************************************************************************/ /**************************** Change MAC of a room ***************************/ /*****************************************************************************/ @@ -397,21 +349,10 @@ void Roo_ChangeMAC (void) /***** Check if the new MAC is different from the old MAC *****/ if (OldMACnum) - DB_QueryDELETE ("can not remove MAC address", - "DELETE FROM roo_macs" - " WHERE RooCod=%ld" - " AND MAC=%llu", - Roo_EditingRoom->RooCod, - OldMACnum); + Roo_DB_RemoveMACAddress (Roo_EditingRoom->RooCod,OldMACnum); if (NewMACnum) /***** Update the table of rooms-MACs changing the old MAC for the new one *****/ - DB_QueryREPLACE ("can not change MAC address", - "REPLACE INTO roo_macs" - " (RooCod,MAC)" - " VALUES" - " (%ld,%llu)", - Roo_EditingRoom->RooCod, - NewMACnum); + Roo_DB_UpdateMACAddress (Roo_EditingRoom->RooCod,NewMACnum); Roo_EditingRoom->MACnum = NewMACnum; } @@ -537,74 +478,16 @@ void Roo_PutIconToViewRooms (void) /**************************** List all the rooms *****************************/ /*****************************************************************************/ -void Roo_GetListRooms (struct Roo_Rooms *Rooms, - Roo_WhichData_t WhichData) +void Roo_GetListRooms (struct Roo_Rooms *Rooms,Roo_WhichData_t WhichData) { - static const char *OrderBySubQuery[Roo_NUM_ORDERS] = - { - [Roo_ORDER_BY_BUILDING ] = "bld_buildings.ShortName," - "roo_rooms.Floor," - "roo_rooms.ShortName", - [Roo_ORDER_BY_FLOOR ] = "roo_rooms.Floor," - "bld_buildings.ShortName," - "roo_rooms.ShortName", - [Roo_ORDER_BY_TYPE ] = "roo_rooms.Type," - "bld_buildings.ShortName," - "roo_rooms.Floor," - "roo_rooms.ShortName", - [Roo_ORDER_BY_SHRT_NAME] = "roo_rooms.ShortName," - "roo_rooms.FullName", - [Roo_ORDER_BY_FULL_NAME] = "roo_rooms.FullName," - "roo_rooms.ShortName", - [Roo_ORDER_BY_CAPACITY ] = "roo_rooms.Capacity DESC," - "bld_buildings.ShortName," - "roo_rooms.Floor," - "roo_rooms.ShortName", - }; MYSQL_RES *mysql_res; MYSQL_ROW row; unsigned NumRoom; struct Roo_Room *Room; /***** Get rooms from database *****/ - switch (WhichData) - { - case Roo_ALL_DATA: - Rooms->Num = (unsigned) - DB_QuerySELECT (&mysql_res,"can not get rooms", - "SELECT roo_rooms.RooCod," // row[0] - "roo_rooms.BldCod," // row[1] - "bld_buildings.ShortName," // row[2] - "roo_rooms.Floor," // row[3] - "roo_rooms.Type," // row[4] - "roo_rooms.ShortName," // row[5] - "roo_rooms.FullName," // row[6] - "roo_rooms.Capacity" // row[7] - " FROM roo_rooms" - " LEFT JOIN bld_buildings" - " ON roo_rooms.BldCod=bld_buildings.BldCod" - " WHERE roo_rooms.CtrCod=%ld" - " ORDER BY %s", - Gbl.Hierarchy.Ctr.CtrCod, - OrderBySubQuery[Rooms->SelectedOrder]); - break; - case Roo_ONLY_SHRT_NAME: - default: - Rooms->Num = (unsigned) - DB_QuerySELECT (&mysql_res,"can not get rooms", - "SELECT roo_rooms.RooCod," // row[0] - "roo_rooms.ShortName" // row[1] - " FROM roo_rooms LEFT JOIN bld_buildings" - " ON roo_rooms.BldCod=bld_buildings.BldCod" - " WHERE roo_rooms.CtrCod=%ld" - " ORDER BY %s", - Gbl.Hierarchy.Ctr.CtrCod, - OrderBySubQuery[Roo_ORDER_DEFAULT]); - break; - } - - /***** Count number of rows in result *****/ - if (Rooms->Num) // Rooms found... + if ((Rooms->Num = Roo_DB_GetListRooms (&mysql_res,Gbl.Hierarchy.Ctr.CtrCod, + WhichData,Rooms->SelectedOrder))) // Rooms found... { /***** Create list with courses in center *****/ if ((Rooms->Lst = calloc ((size_t) Rooms->Num, @@ -675,18 +558,7 @@ static void Roo_GetDataOfRoomByCod (struct Roo_Room *Room) return; /***** Get data of a room from database *****/ - if (DB_QuerySELECT (&mysql_res,"can not get data of a room", - "SELECT roo_rooms.BldCod," // row[0] - "bld_buildings.ShortName," // row[1] - "roo_rooms.Floor," // row[2] - "roo_rooms.Type," // row[3] - "roo_rooms.ShortName," // row[4] - "roo_rooms.FullName," // row[5] - "roo_rooms.Capacity" // row[6] - " FROM roo_rooms LEFT JOIN bld_buildings" - " ON roo_rooms.BldCod=bld_buildings.BldCod" - " WHERE roo_rooms.RooCod=%ld", - Room->RooCod)) // Room found... + if (Roo_DB_GetDataOfRoomByCod (&mysql_res,Room->RooCod)) // Room found... { /* Get row */ row = mysql_fetch_row (mysql_res); @@ -746,6 +618,7 @@ static void Roo_GetBldShrtName (struct Roo_Room *Room,const char *BldShrtNameFro static Roo_RoomType_t Roo_GetTypeFromString (const char *Str) { + extern const char *Roo_TypesDB[Roo_NUM_TYPES]; Roo_RoomType_t Type; /***** Compare string with all string types *****/ @@ -1016,17 +889,10 @@ void Roo_RemoveRoom (void) Roo_GetDataOfRoomByCod (Roo_EditingRoom); /***** Update groups assigned to this room *****/ - DB_QueryUPDATE ("can not update room in groups", - "UPDATE grp_groups" - " SET RooCod=0" // 0 means another room - " WHERE RooCod=%ld", - Roo_EditingRoom->RooCod); + Grp_DB_ResetRoomInGrps (Roo_EditingRoom->RooCod); /***** Remove room *****/ - DB_QueryDELETE ("can not remove a room", - "DELETE FROM roo_rooms" - " WHERE RooCod=%ld", - Roo_EditingRoom->RooCod); + Roo_DB_RemoveRoom (Roo_EditingRoom->RooCod); /***** Create message to show the change made *****/ Ale_CreateAlert (Ale_SUCCESS,NULL, @@ -1034,19 +900,6 @@ void Roo_RemoveRoom (void) Roo_EditingRoom->FullName); } -/*****************************************************************************/ -/********************** Remove all rooms in a center *************************/ -/*****************************************************************************/ - -void Roo_DB_RemoveAllRoomsInCtr (long CtrCod) - { - /***** Remove all rooms in center *****/ - DB_QueryDELETE ("can not remove rooms", - "DELETE FROM roo_rooms" - " WHERE CtrCod=%ld", - CtrCod); - } - /*****************************************************************************/ /********************* Change sitting capacity of a room *********************/ /*****************************************************************************/ @@ -1083,12 +936,7 @@ void Roo_ChangeBuilding (void) else { /***** Update the table of rooms changing the old building for the new one *****/ - DB_QueryUPDATE ("can not update the building of a room", - "UPDATE roo_rooms" - " SET BldCod=%ld" - " WHERE RooCod=%ld", - NewBldCod, - Roo_EditingRoom->RooCod); + Roo_DB_UpdateRoomBuilding (Roo_EditingRoom->RooCod,NewBldCod); /***** Get updated data of the room from database *****/ Roo_GetDataOfRoomByCod (Roo_EditingRoom); @@ -1134,12 +982,7 @@ void Roo_ChangeFloor (void) else { /***** Update the table of rooms changing the old floor for the new one *****/ - DB_QueryUPDATE ("can not update the capacity of a room", - "UPDATE roo_rooms" - " SET Floor=%d" - " WHERE RooCod=%ld", - NewFloor, - Roo_EditingRoom->RooCod); + Roo_DB_UpdateRoomFloor (Roo_EditingRoom->RooCod,NewFloor); /***** Get updated data of the room from database *****/ Roo_GetDataOfRoomByCod (Roo_EditingRoom); @@ -1188,12 +1031,7 @@ void Roo_ChangeType (void) else { /***** Update the table of rooms changing the old type for the new one *****/ - DB_QueryUPDATE ("can not update the type of a room", - "UPDATE roo_rooms" - " SET Type='%s'" - " WHERE RooCod=%ld", - Roo_TypesDB[NewType], - Roo_EditingRoom->RooCod); + Roo_DB_UpdateRoomType (Roo_EditingRoom->RooCod,NewType); /***** Get updated data of the room from database *****/ Roo_GetDataOfRoomByCod (Roo_EditingRoom); @@ -1281,7 +1119,9 @@ static void Roo_RenameRoom (Cns_ShrtOrFullName_t ShrtOrFullName) if (strcmp (CurrentClaName,NewClaName)) // Different names { /***** If room was in database... *****/ - if (Roo_DB_CheckIfRoomNameExists (Roo_EditingRoom->RooCod,ParamName,NewClaName)) + if (Roo_DB_CheckIfRoomNameExists (Gbl.Hierarchy.Ctr.CtrCod, + Roo_EditingRoom->RooCod, + ParamName,NewClaName)) Ale_CreateAlert (Ale_WARNING,NULL, Txt_The_room_X_already_exists, NewClaName); @@ -1308,42 +1148,6 @@ static void Roo_RenameRoom (Cns_ShrtOrFullName_t ShrtOrFullName) Str_Copy (CurrentClaName,NewClaName,MaxBytes); } -/*****************************************************************************/ -/********************** Check if the name of room exists *********************/ -/*****************************************************************************/ - -static bool Roo_DB_CheckIfRoomNameExists (long RooCod, - const char *FieldName,const char *Name) - { - /***** Get number of rooms with a name from database *****/ - return (DB_QueryCOUNT ("can not check if the name of a room" - " already existed", - "SELECT COUNT(*)" - " FROM roo_rooms" - " WHERE CtrCod=%ld" - " AND %s='%s'" - " AND RooCod<>%ld", - Gbl.Hierarchy.Ctr.CtrCod, - FieldName,Name, - RooCod) != 0); - } - -/*****************************************************************************/ -/******************** Update room name in table of rooms *********************/ -/*****************************************************************************/ - -static void Roo_DB_UpdateRoomName (long RooCod, - const char *FieldName,const char *NewRoomName) - { - /***** Update room changing old name by new name */ - DB_QueryUPDATE ("can not update the name of a room", - "UPDATE roo_rooms" - " SET %s='%s'" - " WHERE RooCod=%ld", - FieldName,NewRoomName, - RooCod); - } - /*****************************************************************************/ /********************* Change sitting capacity of a room *********************/ /*****************************************************************************/ @@ -1383,12 +1187,7 @@ void Roo_ChangeCapacity (void) else { /***** Update the table of rooms changing the old capacity for the new one *****/ - DB_QueryUPDATE ("can not update the capacity of a room", - "UPDATE roo_rooms" - " SET Capacity=%u" - " WHERE RooCod=%ld", - NewCapacity, - Roo_EditingRoom->RooCod); + Roo_DB_UpdateRoomCapacity (Roo_EditingRoom->RooCod,NewCapacity); Roo_EditingRoom->Capacity = NewCapacity; /***** Message to show the change made *****/ @@ -1563,20 +1362,14 @@ void Roo_ReceiveFormNewRoom (void) Roo_EditingRoomConstructor (); /***** Get parameters from form *****/ - /* Get room building */ + /* Get room building, floor and type */ Roo_EditingRoom->BldCod = Bld_GetParamBldCod (); + Roo_EditingRoom->Floor = Roo_GetParamFloor (); + Roo_EditingRoom->Type = Roo_GetParamType (); - /* Get room floor */ - Roo_EditingRoom->Floor = Roo_GetParamFloor (); - - /* Get room type */ - Roo_EditingRoom->Type = Roo_GetParamType (); - - /* Get room short name */ + /* Get room short name and full name */ Par_GetParToText ("ShortName",Roo_EditingRoom->ShrtName,Roo_MAX_BYTES_SHRT_NAME); - - /* Get room full name */ - Par_GetParToText ("FullName",Roo_EditingRoom->FullName,Roo_MAX_BYTES_FULL_NAME); + Par_GetParToText ("FullName" ,Roo_EditingRoom->FullName,Roo_MAX_BYTES_FULL_NAME); /* Get seating capacity */ Roo_EditingRoom->Capacity = (unsigned) @@ -1588,16 +1381,17 @@ void Roo_ReceiveFormNewRoom (void) /* Get MAC address */ Roo_EditingRoom->MACnum = MAC_GetMACnumFromForm ("MAC"); - if (Roo_EditingRoom->ShrtName[0] && Roo_EditingRoom->FullName[0]) // If there's a room name { /***** If name of room was in database... *****/ - if (Roo_DB_CheckIfRoomNameExists (-1L,"ShortName",Roo_EditingRoom->ShrtName)) + if (Roo_DB_CheckIfRoomNameExists (Gbl.Hierarchy.Ctr.CtrCod,-1L, + "ShortName",Roo_EditingRoom->ShrtName)) Ale_CreateAlert (Ale_WARNING,NULL, Txt_The_room_X_already_exists, Roo_EditingRoom->ShrtName); - else if (Roo_DB_CheckIfRoomNameExists (-1L,"FullName",Roo_EditingRoom->FullName)) + else if (Roo_DB_CheckIfRoomNameExists (Gbl.Hierarchy.Ctr.CtrCod,-1L, + "FullName",Roo_EditingRoom->FullName)) Ale_CreateAlert (Ale_WARNING,NULL, Txt_The_room_X_already_exists, Roo_EditingRoom->FullName); @@ -1625,31 +1419,11 @@ static void Roo_CreateRoom (struct Roo_Room *Room) Room->Type = Roo_NO_TYPE; /***** Create a new room *****/ - Room->RooCod = - DB_QueryINSERTandReturnCode ("can not create room", - "INSERT INTO roo_rooms" - " (CtrCod,BldCod,Floor,Type," - "ShortName,FullName,Capacity)" - " VALUES" - " (%ld,%ld,%d,'%s'," - "'%s','%s',%u)", - Gbl.Hierarchy.Ctr.CtrCod, - Room->BldCod, - Room->Floor, - Roo_TypesDB[Room->Type], - Room->ShrtName, - Room->FullName, - Room->Capacity); + Room->RooCod = Roo_DB_CreateRoom (Gbl.Hierarchy.Ctr.CtrCod,Room); /***** Create MAC address *****/ if (Room->MACnum) - DB_QueryINSERT ("can not create MAC address", - "INSERT INTO roo_macs" - " (RooCod,MAC)" - " VALUES" - " (%ld,%llu)", - Room->RooCod, - Room->MACnum); + Roo_DB_CreateMACAddress (Room->RooCod,Room->MACnum); } /*****************************************************************************/ @@ -1687,16 +1461,3 @@ static void Roo_EditingRoomDestructor (void) Roo_EditingRoom = NULL; } } - -/*****************************************************************************/ -/********************* Update rooms assigned to a building *******************/ -/*****************************************************************************/ - -void Roo_DB_RemoveBuildingFromRooms (long BldCod) - { - DB_QueryUPDATE ("can not update building of rooms", - "UPDATE roo_rooms" - " SET BldCod=0" // 0 means another building - " WHERE BldCod=%ld", - BldCod); - } diff --git a/swad_room.h b/swad_room.h index 085f5efa..4e55cf9b 100644 --- a/swad_room.h +++ b/swad_room.h @@ -125,8 +125,7 @@ void Roo_SeeRooms (void); void Roo_ChangeMAC (void); void Roo_EditRooms (void); void Roo_PutIconToViewRooms (void); -void Roo_GetListRooms (struct Roo_Rooms *Rooms, - Roo_WhichData_t WhichData); +void Roo_GetListRooms (struct Roo_Rooms *Rooms,Roo_WhichData_t WhichData); void Roo_FreeListRooms (struct Roo_Rooms *Rooms); void Roo_GetListRoomsInThisCtr (void); @@ -134,7 +133,6 @@ void Roo_GetListRoomsInThisCtr (void); long Roo_GetParamRooCod (void); void Roo_RemoveRoom (void); -void Roo_DB_RemoveAllRoomsInCtr (long CtrCod); void Roo_ChangeBuilding (void); void Roo_ChangeFloor (void); void Roo_ChangeType (void); @@ -145,6 +143,4 @@ void Roo_ContEditAfterChgRoom (void); void Roo_ReceiveFormNewRoom (void); -void Roo_DB_RemoveBuildingFromRooms (long BldCod); - #endif diff --git a/swad_room_database.c b/swad_room_database.c new file mode 100644 index 00000000..a3a736b5 --- /dev/null +++ b/swad_room_database.c @@ -0,0 +1,382 @@ +// swad_room_database.c: classrooms, laboratories, offices or other rooms in a center, 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 "swad_database.h" +#include "swad_room.h" + +/*****************************************************************************/ +/************** External global variables from others modules ****************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/****************************** Public constants *****************************/ +/*****************************************************************************/ + +const char *Roo_TypesDB[Roo_NUM_TYPES] = + { + [Roo_NO_TYPE ] = "no_type", + [Roo_ADMINISTRATION] = "administration", + [Roo_AUDITORIUM ] = "auditorium", + [Roo_CAFETERIA ] = "cafeteria", + [Roo_CANTEEN ] = "canteen", + [Roo_CLASSROOM ] = "classroom", + [Roo_CONCIERGE ] = "concierge", + [Roo_CORRIDOR ] = "corridor", + [Roo_GYM ] = "gym", + [Roo_HALL ] = "hall", + [Roo_KINDERGARTEN ] = "kindergarten", + [Roo_LABORATORY ] = "laboratory", + [Roo_LIBRARY ] = "library", + [Roo_OFFICE ] = "office", + [Roo_OUTDOORS ] = "outdoors", + [Roo_PARKING ] = "parking", + [Roo_PAVILION ] = "pavilion", + [Roo_ROOM ] = "room", + [Roo_SECRETARIAT ] = "secretariat", + [Roo_SEMINAR ] = "seminar", + [Roo_SHOP ] = "shop", + [Roo_STORE ] = "store", + [Roo_TOILETS ] = "toilets", + [Roo_VIRTUAL ] = "virtual", + [Roo_YARD ] = "yard", + }; + +/*****************************************************************************/ +/***************************** Private constants *****************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/******************************* Private types *******************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/***************************** Private variables *****************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/***************************** Private prototypes ****************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/**************************** Create a new room ******************************/ +/*****************************************************************************/ + +long Roo_DB_CreateRoom (long CtrCod,const struct Roo_Room *Room) + { + return + DB_QueryINSERTandReturnCode ("can not create room", + "INSERT INTO roo_rooms" + " (CtrCod,BldCod,Floor,Type," + "ShortName,FullName,Capacity)" + " VALUES" + " (%ld,%ld,%d,'%s'," + "'%s','%s',%u)", + CtrCod, + Room->BldCod, + Room->Floor, + Roo_TypesDB[Room->Type], + Room->ShrtName, + Room->FullName, + Room->Capacity); + } + +/*****************************************************************************/ +/**************************** Create a new room ******************************/ +/*****************************************************************************/ + +void Roo_DB_CreateMACAddress (long RooCod,unsigned long long MACnum) + { + DB_QueryINSERT ("can not create MAC address", + "INSERT INTO roo_macs" + " (RooCod,MAC)" + " VALUES" + " (%ld,%llu)", + RooCod, + MACnum); + } + +/*****************************************************************************/ +/**** Update the table of rooms-MACs changing the old MAC for the new one ****/ +/*****************************************************************************/ + +void Roo_DB_UpdateMACAddress (long RooCod,unsigned long long NewMACnum) + { + DB_QueryREPLACE ("can not change MAC address", + "REPLACE INTO roo_macs" + " (RooCod,MAC)" + " VALUES" + " (%ld,%llu)", + RooCod, + NewMACnum); + } + +/*****************************************************************************/ +/**** Update the table of rooms changing the old building for the new one ****/ +/*****************************************************************************/ + +void Roo_DB_UpdateRoomBuilding (long RooCod,long NewBldCod) + { + DB_QueryUPDATE ("can not update the building of a room", + "UPDATE roo_rooms" + " SET BldCod=%ld" + " WHERE RooCod=%ld", + NewBldCod, + RooCod); + } + +/*****************************************************************************/ +/***** Update the table of rooms changing the old floor for the new one ******/ +/*****************************************************************************/ + +void Roo_DB_UpdateRoomFloor (long RooCod,int NewFloor) + { + DB_QueryUPDATE ("can not update the capacity of a room", + "UPDATE roo_rooms" + " SET Floor=%d" + " WHERE RooCod=%ld", + NewFloor, + RooCod); + } + +/*****************************************************************************/ +/****** Update the table of rooms changing the old type for the new one ******/ +/*****************************************************************************/ + +void Roo_DB_UpdateRoomType (long RooCod,Roo_RoomType_t NewType) + { + DB_QueryUPDATE ("can not update the type of a room", + "UPDATE roo_rooms" + " SET Type='%s'" + " WHERE RooCod=%ld", + Roo_TypesDB[NewType], + RooCod); + } + +/*****************************************************************************/ +/******************** Update room name in table of rooms *********************/ +/*****************************************************************************/ + +void Roo_DB_UpdateRoomName (long RooCod, + const char *FieldName,const char *NewRoomName) + { + DB_QueryUPDATE ("can not update the name of a room", + "UPDATE roo_rooms" + " SET %s='%s'" + " WHERE RooCod=%ld", + FieldName,NewRoomName, + RooCod); + } + +/*****************************************************************************/ +/**** Update the table of rooms changing the old capacity for the new one ****/ +/*****************************************************************************/ + +void Roo_DB_UpdateRoomCapacity (long RooCod,unsigned NewCapacity) + { + DB_QueryUPDATE ("can not update the capacity of a room", + "UPDATE roo_rooms" + " SET Capacity=%u" + " WHERE RooCod=%ld", + NewCapacity, + RooCod); + } + +/*****************************************************************************/ +/**************************** List all the rooms *****************************/ +/*****************************************************************************/ + +unsigned Roo_DB_GetListRooms (MYSQL_RES **mysql_res, + long CtrCod, + Roo_WhichData_t WhichData, + Roo_Order_t SelectedOrder) + { + static const char *OrderBySubQuery[Roo_NUM_ORDERS] = + { + [Roo_ORDER_BY_BUILDING ] = "bld_buildings.ShortName," + "roo_rooms.Floor," + "roo_rooms.ShortName", + [Roo_ORDER_BY_FLOOR ] = "roo_rooms.Floor," + "bld_buildings.ShortName," + "roo_rooms.ShortName", + [Roo_ORDER_BY_TYPE ] = "roo_rooms.Type," + "bld_buildings.ShortName," + "roo_rooms.Floor," + "roo_rooms.ShortName", + [Roo_ORDER_BY_SHRT_NAME] = "roo_rooms.ShortName," + "roo_rooms.FullName", + [Roo_ORDER_BY_FULL_NAME] = "roo_rooms.FullName," + "roo_rooms.ShortName", + [Roo_ORDER_BY_CAPACITY ] = "roo_rooms.Capacity DESC," + "bld_buildings.ShortName," + "roo_rooms.Floor," + "roo_rooms.ShortName", + }; + + /***** Get rooms from database *****/ + switch (WhichData) + { + case Roo_ALL_DATA: + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get rooms", + "SELECT roo_rooms.RooCod," // row[0] + "roo_rooms.BldCod," // row[1] + "bld_buildings.ShortName," // row[2] + "roo_rooms.Floor," // row[3] + "roo_rooms.Type," // row[4] + "roo_rooms.ShortName," // row[5] + "roo_rooms.FullName," // row[6] + "roo_rooms.Capacity" // row[7] + " FROM roo_rooms" + " LEFT JOIN bld_buildings" + " ON roo_rooms.BldCod=bld_buildings.BldCod" + " WHERE roo_rooms.CtrCod=%ld" + " ORDER BY %s", + CtrCod, + OrderBySubQuery[SelectedOrder]); + case Roo_ONLY_SHRT_NAME: + default: + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get rooms", + "SELECT roo_rooms.RooCod," // row[0] + "roo_rooms.ShortName" // row[1] + " FROM roo_rooms LEFT JOIN bld_buildings" + " ON roo_rooms.BldCod=bld_buildings.BldCod" + " WHERE roo_rooms.CtrCod=%ld" + " ORDER BY %s", + CtrCod, + OrderBySubQuery[Roo_ORDER_DEFAULT]); + } + } + +/*****************************************************************************/ +/********************** Check if the name of room exists *********************/ +/*****************************************************************************/ + +bool Roo_DB_CheckIfRoomNameExists (long CtrCod,long RooCod, + const char *FieldName,const char *Name) + { + /***** Get number of rooms with a name from database *****/ + return (DB_QueryCOUNT ("can not check if the name of a room" + " already existed", + "SELECT COUNT(*)" + " FROM roo_rooms" + " WHERE CtrCod=%ld" + " AND %s='%s'" + " AND RooCod<>%ld", + CtrCod, + FieldName,Name, + RooCod) != 0); + } + +/*****************************************************************************/ +/************* Get room data from database using the room code ***************/ +/*****************************************************************************/ + +unsigned Roo_DB_GetDataOfRoomByCod (MYSQL_RES **mysql_res,long RooCod) + { + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get data of a room", + "SELECT roo_rooms.BldCod," // row[0] + "bld_buildings.ShortName," // row[1] + "roo_rooms.Floor," // row[2] + "roo_rooms.Type," // row[3] + "roo_rooms.ShortName," // row[4] + "roo_rooms.FullName," // row[5] + "roo_rooms.Capacity" // row[6] + " FROM roo_rooms LEFT JOIN bld_buildings" + " ON roo_rooms.BldCod=bld_buildings.BldCod" + " WHERE roo_rooms.RooCod=%ld", + RooCod); + } + +/*****************************************************************************/ +/***************** Get the MAC addresses associated to a room ****************/ +/*****************************************************************************/ + +unsigned Roo_DB_GetMACAddresses (MYSQL_RES **mysql_res,long RooCod) + { + /***** Get MAC addresses from database *****/ + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get MAC addresses", + "SELECT MAC" // row[0] + " FROM roo_macs" + " WHERE RooCod=%ld" + " ORDER BY MAC", + RooCod); + } + +/*****************************************************************************/ +/********************************* Remove room *******************************/ +/*****************************************************************************/ + +void Roo_DB_RemoveRoom (long RooCod) + { + DB_QueryDELETE ("can not remove a room", + "DELETE FROM roo_rooms" + " WHERE RooCod=%ld", + RooCod); + } + +/*****************************************************************************/ +/********************************* Remove room *******************************/ +/*****************************************************************************/ + +void Roo_DB_RemoveMACAddress (long RooCod,unsigned long long MACnum) + { + DB_QueryDELETE ("can not remove MAC address", + "DELETE FROM roo_macs" + " WHERE RooCod=%ld" + " AND MAC=%llu", + RooCod, + MACnum); + } + +/*****************************************************************************/ +/********************* Update rooms assigned to a building *******************/ +/*****************************************************************************/ + +void Roo_DB_RemoveBuildingFromRooms (long BldCod) + { + DB_QueryUPDATE ("can not update building of rooms", + "UPDATE roo_rooms" + " SET BldCod=0" // 0 means another building + " WHERE BldCod=%ld", + BldCod); + } + +/*****************************************************************************/ +/********************** Remove all rooms in a center *************************/ +/*****************************************************************************/ + +void Roo_DB_RemoveAllRoomsInCtr (long CtrCod) + { + /***** Remove all rooms in center *****/ + DB_QueryDELETE ("can not remove rooms", + "DELETE FROM roo_rooms" + " WHERE CtrCod=%ld", + CtrCod); + } diff --git a/swad_room_database.h b/swad_room_database.h new file mode 100644 index 00000000..1280baf3 --- /dev/null +++ b/swad_room_database.h @@ -0,0 +1,62 @@ +// swad_room_database.h: classrooms, laboratories, offices or other rooms in a center, operations with database + +#ifndef _SWAD_ROO_DB +#define _SWAD_ROO_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 **********************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/************************** Public types and constants ***********************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/***************************** Public prototypes *****************************/ +/*****************************************************************************/ + +long Roo_DB_CreateRoom (long CtrCod,const struct Roo_Room *Room); +void Roo_DB_CreateMACAddress (long RooCod,unsigned long long MACnum); +void Roo_DB_UpdateMACAddress (long RooCod,unsigned long long NewMACnum); +void Roo_DB_UpdateRoomBuilding (long RooCod,long NewBldCod); +void Roo_DB_UpdateRoomFloor (long RooCod,int NewFloor); +void Roo_DB_UpdateRoomType (long RooCod,Roo_RoomType_t NewType); +void Roo_DB_UpdateRoomName (long RooCod, + const char *FieldName,const char *NewRoomName); +void Roo_DB_UpdateRoomCapacity (long RooCod,unsigned NewCapacity); + +unsigned Roo_DB_GetListRooms (MYSQL_RES **mysql_res, + long CtrCod, + Roo_WhichData_t WhichData, + Roo_Order_t SelectedOrder); +bool Roo_DB_CheckIfRoomNameExists (long CtrCod,long RooCod, + const char *FieldName,const char *Name); +unsigned Roo_DB_GetDataOfRoomByCod (MYSQL_RES **mysql_res,long RooCod); +unsigned Roo_DB_GetMACAddresses (MYSQL_RES **mysql_res,long RooCod); + +void Roo_DB_RemoveRoom (long RooCod); +void Roo_DB_RemoveMACAddress (long RooCod,unsigned long long MACnum); +void Roo_DB_RemoveBuildingFromRooms (long BldCod); +void Roo_DB_RemoveAllRoomsInCtr (long CtrCod); + +#endif