From 1dbe256cbde87965ab18900b884c7a55cc0ff355 Mon Sep 17 00:00:00 2001 From: acanas Date: Wed, 2 Jun 2021 23:48:43 +0200 Subject: [PATCH] Version 20.89: Jun 02, 2021 New module swad_department_database for database queries related to departments. --- Makefile | 2 +- swad_center_database.c | 16 -- swad_center_database.h | 4 - swad_changelog.h | 3 +- swad_department.c | 522 ++++++++++++++++--------------------- swad_department.h | 1 - swad_department_database.c | 131 ++++++++++ swad_department_database.h | 48 ++++ swad_institution_config.c | 4 +- 9 files changed, 412 insertions(+), 319 deletions(-) create mode 100644 swad_department_database.c create mode 100644 swad_department_database.h diff --git a/Makefile b/Makefile index 3e5465ff..fd240d9b 100644 --- a/Makefile +++ b/Makefile @@ -41,7 +41,7 @@ OBJS = swad_account.o swad_account_database.o swad_action.o swad_agenda.o \ swad_cryptography.o \ swad_database.o swad_date.o swad_degree.o swad_degree_config.o \ swad_degree_database.o swad_degree_type.o swad_department.o \ - swad_duplicate.o \ + swad_department_database.o swad_duplicate.o \ swad_enrolment.o swad_error.o swad_exam.o swad_exam_log.o \ swad_exam_print.o swad_exam_result.o swad_exam_session.o \ swad_exam_set.o \ diff --git a/swad_center_database.c b/swad_center_database.c index 9e4fefd3..342b00e9 100644 --- a/swad_center_database.c +++ b/swad_center_database.c @@ -25,27 +25,11 @@ /********************************* Headers ***********************************/ /*****************************************************************************/ -// #include // For boolean type -// #include // For NULL -// #include // For free -// #include // For string functions - #include "swad_center.h" #include "swad_center_config.h" #include "swad_database.h" #include "swad_error.h" -// #include "swad_figure.h" -// #include "swad_figure_cache.h" -// #include "swad_form.h" -// #include "swad_forum.h" #include "swad_global.h" -// #include "swad_hierarchy.h" -// #include "swad_hierarchy_level.h" -// #include "swad_HTML.h" -// #include "swad_logo.h" -// #include "swad_message.h" -// #include "swad_place.h" -// #include "swad_survey.h" /*****************************************************************************/ /************** External global variables from others modules ****************/ diff --git a/swad_center_database.h b/swad_center_database.h index 4efdc578..815a7418 100644 --- a/swad_center_database.h +++ b/swad_center_database.h @@ -29,13 +29,9 @@ #include // To access MySQL databases -// #include "swad_action.h" #include "swad_center.h" #include "swad_constant.h" -// #include "swad_degree.h" -// #include "swad_map.h" #include "swad_media.h" -// #include "swad_role_type.h" /*****************************************************************************/ /************************** Public types and constants ***********************/ diff --git a/swad_changelog.h b/swad_changelog.h index 1a793647..f42d2b15 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 20.88.3 (2021-06-02)" +#define Log_PLATFORM_VERSION "SWAD 20.89 (2021-06-02)" #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 20.89: Jun 02, 2021 New module swad_department_database for database queries related to departments. (312824 lines) Version 20.88.3: Jun 02, 2021 Queries moved to module swad_center_database. (312755 lines) Version 20.88.2: Jun 02, 2021 Queries moved to module swad_center_database. Fixed bug in visibility of exam results. Reported by Mancia Anguita López. (312738 lines) diff --git a/swad_department.c b/swad_department.c index fc9ab825..6611e5d3 100644 --- a/swad_department.c +++ b/swad_department.c @@ -34,6 +34,7 @@ #include "swad_constant.h" #include "swad_database.h" #include "swad_department.h" +#include "swad_department_database.h" #include "swad_error.h" #include "swad_form.h" #include "swad_global.h" @@ -79,12 +80,9 @@ static void Dpt_ListDepartmentsForEdition (const struct Dpt_Departments *Departm static void Dpt_PutParamDptCod (void *DptCod); static void Dpt_RenameDepartment (Cns_ShrtOrFullName_t ShrtOrFullName); -static bool Dpt_CheckIfDepartmentNameExists (const char *FieldName,const char *Name,long DptCod); -static void Dpt_UpdateDegNameDB (long DptCod,const char *FieldName,const char *NewDptName); static void Dpt_PutFormToCreateDepartment (void); static void Dpt_PutHeadDepartments (void); -static void Dpt_CreateDepartment (struct Dpt_Department *Dpt); static void Dpt_EditingDepartmentConstructor (void); static void Dpt_EditingDepartmentDestructor (void); @@ -144,88 +142,88 @@ void Dpt_SeeDepts (void) Hlp_INSTITUTION_Departments,Box_NOT_CLOSABLE,2); Str_FreeString (); - /***** Write heading *****/ - HTM_TR_Begin (NULL); - for (Order = Dpt_ORDER_BY_DEPARTMENT; - Order <= Dpt_ORDER_BY_NUM_TCHS; - Order++) - { - HTM_TH_Begin (1,1,Order == Dpt_ORDER_BY_NUM_TCHS ? "RM" : - "LM"); + /***** Write heading *****/ + HTM_TR_Begin (NULL); + for (Order = Dpt_ORDER_BY_DEPARTMENT; + Order <= Dpt_ORDER_BY_NUM_TCHS; + Order++) + { + HTM_TH_Begin (1,1,Order == Dpt_ORDER_BY_NUM_TCHS ? "RM" : + "LM"); - Frm_BeginForm (ActSeeDpt); - Par_PutHiddenParamUnsigned (NULL,"Order",(unsigned) Order); - HTM_BUTTON_SUBMIT_Begin (Txt_DEPARTMENTS_HELP_ORDER[Order],"BT_LINK TIT_TBL",NULL); - if (Order == Departments.SelectedOrder) - HTM_U_Begin (); - HTM_Txt (Txt_DEPARTMENTS_ORDER[Order]); - if (Order == Departments.SelectedOrder) - HTM_U_End (); - HTM_BUTTON_End (); - Frm_EndForm (); + Frm_BeginForm (ActSeeDpt); + Par_PutHiddenParamUnsigned (NULL,"Order",(unsigned) Order); + HTM_BUTTON_SUBMIT_Begin (Txt_DEPARTMENTS_HELP_ORDER[Order],"BT_LINK TIT_TBL",NULL); + if (Order == Departments.SelectedOrder) + HTM_U_Begin (); + HTM_Txt (Txt_DEPARTMENTS_ORDER[Order]); + if (Order == Departments.SelectedOrder) + HTM_U_End (); + HTM_BUTTON_End (); + Frm_EndForm (); - HTM_TH_End (); - } - HTM_TR_End (); + HTM_TH_End (); + } + HTM_TR_End (); + + /***** Write all the Dpt_GetListDepartmentsdepartments and their nuber of teachers *****/ + for (NumDpt = 0; + NumDpt < Departments.Num; + NumDpt++) + { + /* Write data of this department */ + HTM_TR_Begin (NULL); + + HTM_TD_Begin ("class=\"LM\""); + HTM_A_Begin ("href=\"%s\" target=\"_blank\" class=\"DAT\"", + Departments.Lst[NumDpt].WWW); + HTM_Txt (Departments.Lst[NumDpt].FullName); + HTM_A_End (); + HTM_TD_End (); + + HTM_TD_Begin ("class=\"DAT RM\""); + HTM_Unsigned (Departments.Lst[NumDpt].NumTchs); + HTM_TD_End (); + + HTM_TR_End (); + } + + /***** Separation row *****/ + HTM_TR_Begin (NULL); + HTM_TD_Begin ("colspan=\"3\" class=\"DAT\""); + HTM_NBSP (); + HTM_TD_End (); + HTM_TR_End (); + + /***** Write teachers of this institution with other department *****/ + NumTchsInsInOtherDpts = Usr_GetNumTchsCurrentInsInDepartment (0); - /***** Write all the Dpt_GetListDepartmentsdepartments and their nuber of teachers *****/ - for (NumDpt = 0; - NumDpt < Departments.Num; - NumDpt++) - { - /* Write data of this department */ HTM_TR_Begin (NULL); - HTM_TD_Begin ("class=\"LM\""); - HTM_A_Begin ("href=\"%s\" target=\"_blank\" class=\"DAT\"", - Departments.Lst[NumDpt].WWW); - HTM_Txt (Departments.Lst[NumDpt].FullName); - HTM_A_End (); - HTM_TD_End (); + HTM_TD_Begin ("class=\"DAT LM\""); + HTM_Txt (Txt_Other_departments); + HTM_TD_End (); - HTM_TD_Begin ("class=\"DAT RM\""); - HTM_Unsigned (Departments.Lst[NumDpt].NumTchs); - HTM_TD_End (); + HTM_TD_Begin ("class=\"DAT RM\""); + HTM_Unsigned (NumTchsInsInOtherDpts); + HTM_TD_End (); HTM_TR_End (); - } - /***** Separation row *****/ - HTM_TR_Begin (NULL); - HTM_TD_Begin ("colspan=\"3\" class=\"DAT\""); - HTM_NBSP (); - HTM_TD_End (); - HTM_TR_End (); + /***** Write teachers with no department *****/ + NumTchsInsWithNoDpt = Usr_GetNumTchsCurrentInsInDepartment (-1L); - /***** Write teachers of this institution with other department *****/ - NumTchsInsInOtherDpts = Usr_GetNumTchsCurrentInsInDepartment (0); + HTM_TR_Begin (NULL); - HTM_TR_Begin (NULL); + HTM_TD_Begin ("class=\"DAT LM\""); + HTM_Txt (Txt_Department_unspecified); + HTM_TD_End (); - HTM_TD_Begin ("class=\"DAT LM\""); - HTM_Txt (Txt_Other_departments); - HTM_TD_End (); + HTM_TD_Begin ("class=\"DAT RM\""); + HTM_Unsigned (NumTchsInsWithNoDpt); + HTM_TD_End (); - HTM_TD_Begin ("class=\"DAT RM\""); - HTM_Unsigned (NumTchsInsInOtherDpts); - HTM_TD_End (); - - HTM_TR_End (); - - /***** Write teachers with no department *****/ - NumTchsInsWithNoDpt = Usr_GetNumTchsCurrentInsInDepartment (-1L); - - HTM_TR_Begin (NULL); - - HTM_TD_Begin ("class=\"DAT LM\""); - HTM_Txt (Txt_Department_unspecified); - HTM_TD_End (); - - HTM_TD_Begin ("class=\"DAT RM\""); - HTM_Unsigned (NumTchsInsWithNoDpt); - HTM_TD_End (); - - HTM_TR_End (); + HTM_TR_End (); /***** End table and box *****/ Box_BoxTableEnd (); @@ -298,12 +296,12 @@ static void Dpt_EditDepartmentsInternal (void) Hlp_INSTITUTION_Departments_edit,Box_NOT_CLOSABLE); Str_FreeString (); - /***** Put a form to create a new department *****/ - Dpt_PutFormToCreateDepartment (); + /***** Put a form to create a new department *****/ + Dpt_PutFormToCreateDepartment (); - /***** Forms to edit current departments *****/ - if (Departments.Num) - Dpt_ListDepartmentsForEdition (&Departments); + /***** Forms to edit current departments *****/ + if (Departments.Num) + Dpt_ListDepartmentsForEdition (&Departments); /***** End box *****/ Box_BoxEnd (); @@ -506,21 +504,6 @@ void Dpt_FreeListDepartments (struct Dpt_Departments *Departments) Departments->Num = 0; } -/*****************************************************************************/ -/************** Get number of departments in an institution ******************/ -/*****************************************************************************/ - -unsigned Dpt_GetNumDepartmentsInInstitution (long InsCod) - { - /***** Get number of departments in an institution from database *****/ - return (unsigned) - DB_QueryCOUNT ("can not get number of departments in an institution", - "SELECT COUNT(*)" - " FROM dpt_departments" - " WHERE InsCod=%ld", - InsCod); - } - /*****************************************************************************/ /************************** List all the departments *************************/ /*****************************************************************************/ @@ -536,90 +519,90 @@ static void Dpt_ListDepartmentsForEdition (const struct Dpt_Departments *Departm /***** Begin table *****/ HTM_TABLE_BeginPadding (2); - /***** Write heading *****/ - Dpt_PutHeadDepartments (); + /***** Write heading *****/ + Dpt_PutHeadDepartments (); - /***** Write all the departments *****/ - for (NumDpt = 0; - NumDpt < Departments->Num; - NumDpt++) - { - Dpt = &Departments->Lst[NumDpt]; + /***** Write all the departments *****/ + for (NumDpt = 0; + NumDpt < Departments->Num; + NumDpt++) + { + Dpt = &Departments->Lst[NumDpt]; - /* Get data of institution of this department */ - Ins.InsCod = Dpt->InsCod; - Ins_GetDataOfInstitutionByCod (&Ins); + /* Get data of institution of this department */ + Ins.InsCod = Dpt->InsCod; + Ins_GetDataOfInstitutionByCod (&Ins); - HTM_TR_Begin (NULL); + HTM_TR_Begin (NULL); - /* Put icon to remove department */ - HTM_TD_Begin ("class=\"BM\""); - if (Dpt->NumTchs) // Department has teachers ==> deletion forbidden - Ico_PutIconRemovalNotAllowed (); - else - Ico_PutContextualIconToRemove (ActRemDpt,NULL, - Dpt_PutParamDptCod,&Dpt->DptCod); - HTM_TD_End (); + /* Put icon to remove department */ + HTM_TD_Begin ("class=\"BM\""); + if (Dpt->NumTchs) // Department has teachers ==> deletion forbidden + Ico_PutIconRemovalNotAllowed (); + else + Ico_PutContextualIconToRemove (ActRemDpt,NULL, + Dpt_PutParamDptCod,&Dpt->DptCod); + HTM_TD_End (); - /* Department code */ - HTM_TD_Begin ("class=\"DAT RM\""); - HTM_TxtF ("%ld ",Dpt->DptCod); - HTM_TD_End (); + /* Department code */ + HTM_TD_Begin ("class=\"DAT RM\""); + HTM_TxtF ("%ld ",Dpt->DptCod); + HTM_TD_End (); - /* Institution */ - HTM_TD_Begin ("class=\"CM\""); - Frm_BeginForm (ActChgDptIns); - Dpt_PutParamDptCod (&Dpt->DptCod); - HTM_SELECT_Begin (HTM_SUBMIT_ON_CHANGE, - "name=\"OthInsCod\" class=\"HIE_SEL_NARROW\""); - HTM_OPTION (HTM_Type_STRING,"0",Dpt->InsCod == 0,false, - "%s",Txt_Another_institution); - for (NumIns = 0; - NumIns < Gbl.Hierarchy.Inss.Num; - NumIns++) - HTM_OPTION (HTM_Type_LONG,&Gbl.Hierarchy.Inss.Lst[NumIns].InsCod, - Gbl.Hierarchy.Inss.Lst[NumIns].InsCod == Dpt->InsCod,false, - "%s",Gbl.Hierarchy.Inss.Lst[NumIns].ShrtName); - HTM_SELECT_End (); - Frm_EndForm (); - HTM_TD_End (); + /* Institution */ + HTM_TD_Begin ("class=\"CM\""); + Frm_BeginForm (ActChgDptIns); + Dpt_PutParamDptCod (&Dpt->DptCod); + HTM_SELECT_Begin (HTM_SUBMIT_ON_CHANGE, + "name=\"OthInsCod\" class=\"HIE_SEL_NARROW\""); + HTM_OPTION (HTM_Type_STRING,"0",Dpt->InsCod == 0,false, + "%s",Txt_Another_institution); + for (NumIns = 0; + NumIns < Gbl.Hierarchy.Inss.Num; + NumIns++) + HTM_OPTION (HTM_Type_LONG,&Gbl.Hierarchy.Inss.Lst[NumIns].InsCod, + Gbl.Hierarchy.Inss.Lst[NumIns].InsCod == Dpt->InsCod,false, + "%s",Gbl.Hierarchy.Inss.Lst[NumIns].ShrtName); + HTM_SELECT_End (); + Frm_EndForm (); + HTM_TD_End (); - /* Department short name */ - HTM_TD_Begin ("class=\"CM\""); - Frm_BeginForm (ActRenDptSho); - Dpt_PutParamDptCod (&Dpt->DptCod); - HTM_INPUT_TEXT ("ShortName",Cns_HIERARCHY_MAX_CHARS_SHRT_NAME,Dpt->ShrtName, - HTM_SUBMIT_ON_CHANGE, - "class=\"INPUT_SHORT_NAME\""); - Frm_EndForm (); - HTM_TD_End (); + /* Department short name */ + HTM_TD_Begin ("class=\"CM\""); + Frm_BeginForm (ActRenDptSho); + Dpt_PutParamDptCod (&Dpt->DptCod); + HTM_INPUT_TEXT ("ShortName",Cns_HIERARCHY_MAX_CHARS_SHRT_NAME,Dpt->ShrtName, + HTM_SUBMIT_ON_CHANGE, + "class=\"INPUT_SHORT_NAME\""); + Frm_EndForm (); + HTM_TD_End (); - /* Department full name */ - HTM_TD_Begin ("class=\"CM\""); - Frm_BeginForm (ActRenDptFul); - Dpt_PutParamDptCod (&Dpt->DptCod); - HTM_INPUT_TEXT ("FullName",Cns_HIERARCHY_MAX_CHARS_FULL_NAME,Dpt->FullName, - HTM_SUBMIT_ON_CHANGE, - "class=\"INPUT_FULL_NAME\""); - Frm_EndForm (); - HTM_TD_End (); + /* Department full name */ + HTM_TD_Begin ("class=\"CM\""); + Frm_BeginForm (ActRenDptFul); + Dpt_PutParamDptCod (&Dpt->DptCod); + HTM_INPUT_TEXT ("FullName",Cns_HIERARCHY_MAX_CHARS_FULL_NAME,Dpt->FullName, + HTM_SUBMIT_ON_CHANGE, + "class=\"INPUT_FULL_NAME\""); + Frm_EndForm (); + HTM_TD_End (); - /* Department WWW */ - HTM_TD_Begin ("class=\"CM\""); - Frm_BeginForm (ActChgDptWWW); - Dpt_PutParamDptCod (&Dpt->DptCod); - HTM_INPUT_URL ("WWW",Dpt->WWW,HTM_SUBMIT_ON_CHANGE, - "class=\"INPUT_WWW_NARROW\" required=\"required\""); - Frm_EndForm (); - HTM_TD_End (); + /* Department WWW */ + HTM_TD_Begin ("class=\"CM\""); + Frm_BeginForm (ActChgDptWWW); + Dpt_PutParamDptCod (&Dpt->DptCod); + HTM_INPUT_URL ("WWW",Dpt->WWW,HTM_SUBMIT_ON_CHANGE, + "class=\"INPUT_WWW_NARROW\" required=\"required\""); + Frm_EndForm (); + HTM_TD_End (); - /* Number of teachers */ - HTM_TD_Begin ("class=\"DAT RM\""); - HTM_Unsigned (Dpt->NumTchs); - HTM_TD_End (); + /* Number of teachers */ + HTM_TD_Begin ("class=\"DAT RM\""); + HTM_Unsigned (Dpt->NumTchs); + HTM_TD_End (); - HTM_TR_End (); - } + HTM_TR_End (); + } /***** End table *****/ HTM_TABLE_End (); @@ -797,14 +780,14 @@ static void Dpt_RenameDepartment (Cns_ShrtOrFullName_t ShrtOrFullName) if (strcmp (CurrentDptName,NewDptName)) // Different names { /***** If degree was in database... *****/ - if (Dpt_CheckIfDepartmentNameExists (ParamName,NewDptName,Dpt_EditingDpt->DptCod)) + if (Dpt_DB_CheckIfDepartmentNameExists (ParamName,NewDptName,Dpt_EditingDpt->DptCod)) Ale_CreateAlert (Ale_WARNING,NULL, Txt_The_department_X_already_exists, NewDptName); else { /* Update the table changing old name by new name */ - Dpt_UpdateDegNameDB (Dpt_EditingDpt->DptCod,FieldName,NewDptName); + Dpt_DB_UpdateDegName (Dpt_EditingDpt->DptCod,FieldName,NewDptName); /* Write message to show the change made */ Ale_CreateAlert (Ale_SUCCESS,NULL, @@ -824,37 +807,6 @@ static void Dpt_RenameDepartment (Cns_ShrtOrFullName_t ShrtOrFullName) Str_Copy (CurrentDptName,NewDptName,MaxBytes); } -/*****************************************************************************/ -/******************* Check if the name of department exists ******************/ -/*****************************************************************************/ - -static bool Dpt_CheckIfDepartmentNameExists (const char *FieldName,const char *Name,long DptCod) - { - /***** Get number of departments with a name from database *****/ - return (DB_QueryCOUNT ("can not check if the department name already existed", - "SELECT COUNT(*)" - " FROM dpt_departments" - " WHERE %s='%s'" - " AND DptCod<>%ld", - FieldName,Name, - DptCod) != 0); - } - -/*****************************************************************************/ -/************* Update department name in table of departments ****************/ -/*****************************************************************************/ - -static void Dpt_UpdateDegNameDB (long DptCod,const char *FieldName,const char *NewDptName) - { - /***** Update department changing old name by new name *****/ - DB_QueryUPDATE ("can not update the name of a department", - "UPDATE dpt_departments" - " SET %s='%s'" - " WHERE DptCod=%ld", - FieldName,NewDptName, - DptCod); - } - /******************************************************************************/ /*********************** Change the URL of a department *********************/ /*****************************************************************************/ @@ -934,62 +886,62 @@ static void Dpt_PutFormToCreateDepartment (void) /***** Begin form *****/ Frm_BeginForm (ActNewDpt); - /***** Begin box and table *****/ - Box_BoxTableBegin (NULL,Txt_New_department, - NULL,NULL, - NULL,Box_NOT_CLOSABLE,2); + /***** Begin box and table *****/ + Box_BoxTableBegin (NULL,Txt_New_department, + NULL,NULL, + NULL,Box_NOT_CLOSABLE,2); - /***** Write heading *****/ - HTM_TR_Begin (NULL); + /***** Write heading *****/ + HTM_TR_Begin (NULL); - HTM_TH (1,1,"LM",Txt_Institution); - HTM_TH (1,1,"LM",Txt_Short_name); - HTM_TH (1,1,"LM",Txt_Full_name); - HTM_TH (1,1,"LM",Txt_WWW); + HTM_TH (1,1,"LM",Txt_Institution); + HTM_TH (1,1,"LM",Txt_Short_name); + HTM_TH (1,1,"LM",Txt_Full_name); + HTM_TH (1,1,"LM",Txt_WWW); - HTM_TR_End (); + HTM_TR_End (); - HTM_TR_Begin (NULL); + HTM_TR_Begin (NULL); - /***** Institution *****/ - HTM_TD_Begin ("class=\"CM\""); - HTM_SELECT_Begin (HTM_DONT_SUBMIT_ON_CHANGE, - "name=\"OthInsCod\" class=\"HIE_SEL_NARROW\""); - HTM_OPTION (HTM_Type_STRING,"0",Dpt_EditingDpt->InsCod == 0,false, - "%s",Txt_Another_institution); - for (NumIns = 0; - NumIns < Gbl.Hierarchy.Inss.Num; - NumIns++) - HTM_OPTION (HTM_Type_LONG,&Gbl.Hierarchy.Inss.Lst[NumIns].InsCod, - Gbl.Hierarchy.Inss.Lst[NumIns].InsCod == Dpt_EditingDpt->InsCod,false, - "%s",Gbl.Hierarchy.Inss.Lst[NumIns].ShrtName); - HTM_SELECT_End (); - HTM_TD_End (); + /***** Institution *****/ + HTM_TD_Begin ("class=\"CM\""); + HTM_SELECT_Begin (HTM_DONT_SUBMIT_ON_CHANGE, + "name=\"OthInsCod\" class=\"HIE_SEL_NARROW\""); + HTM_OPTION (HTM_Type_STRING,"0",Dpt_EditingDpt->InsCod == 0,false, + "%s",Txt_Another_institution); + for (NumIns = 0; + NumIns < Gbl.Hierarchy.Inss.Num; + NumIns++) + HTM_OPTION (HTM_Type_LONG,&Gbl.Hierarchy.Inss.Lst[NumIns].InsCod, + Gbl.Hierarchy.Inss.Lst[NumIns].InsCod == Dpt_EditingDpt->InsCod,false, + "%s",Gbl.Hierarchy.Inss.Lst[NumIns].ShrtName); + HTM_SELECT_End (); + HTM_TD_End (); - /***** Department short name *****/ - HTM_TD_Begin ("class=\"CM\""); - HTM_INPUT_TEXT ("ShortName",Cns_HIERARCHY_MAX_CHARS_SHRT_NAME,Dpt_EditingDpt->ShrtName, - HTM_DONT_SUBMIT_ON_CHANGE, - "class=\"INPUT_SHORT_NAME\" required=\"required\""); - HTM_TD_End (); + /***** Department short name *****/ + HTM_TD_Begin ("class=\"CM\""); + HTM_INPUT_TEXT ("ShortName",Cns_HIERARCHY_MAX_CHARS_SHRT_NAME,Dpt_EditingDpt->ShrtName, + HTM_DONT_SUBMIT_ON_CHANGE, + "class=\"INPUT_SHORT_NAME\" required=\"required\""); + HTM_TD_End (); - /***** Department full name *****/ - HTM_TD_Begin ("class=\"CM\""); - HTM_INPUT_TEXT ("FullName",Cns_HIERARCHY_MAX_CHARS_FULL_NAME,Dpt_EditingDpt->FullName, - HTM_DONT_SUBMIT_ON_CHANGE, - "class=\"INPUT_FULL_NAME\" required=\"required\""); - HTM_TD_End (); + /***** Department full name *****/ + HTM_TD_Begin ("class=\"CM\""); + HTM_INPUT_TEXT ("FullName",Cns_HIERARCHY_MAX_CHARS_FULL_NAME,Dpt_EditingDpt->FullName, + HTM_DONT_SUBMIT_ON_CHANGE, + "class=\"INPUT_FULL_NAME\" required=\"required\""); + HTM_TD_End (); - /***** Department WWW *****/ - HTM_TD_Begin ("class=\"CM\""); - HTM_INPUT_URL ("WWW",Dpt_EditingDpt->WWW,HTM_DONT_SUBMIT_ON_CHANGE, - "class=\"INPUT_WWW_NARROW\" required=\"required\""); - HTM_TD_End (); + /***** Department WWW *****/ + HTM_TD_Begin ("class=\"CM\""); + HTM_INPUT_URL ("WWW",Dpt_EditingDpt->WWW,HTM_DONT_SUBMIT_ON_CHANGE, + "class=\"INPUT_WWW_NARROW\" required=\"required\""); + HTM_TD_End (); - HTM_TR_End (); + HTM_TR_End (); - /***** End table, send button and end box *****/ - Box_BoxTableWithButtonEnd (Btn_CREATE_BUTTON,Txt_Create_department); + /***** End table, send button and end box *****/ + Box_BoxTableWithButtonEnd (Btn_CREATE_BUTTON,Txt_Create_department); /***** End form *****/ Frm_EndForm (); @@ -1010,13 +962,13 @@ static void Dpt_PutHeadDepartments (void) HTM_TR_Begin (NULL); - HTM_TH_Empty (1); - HTM_TH (1,1,"RM",Txt_Code); - HTM_TH (1,1,"LM",Txt_Institution); - HTM_TH (1,1,"LM",Txt_Short_name); - HTM_TH (1,1,"LM",Txt_Full_name); - HTM_TH (1,1,"LM",Txt_WWW); - HTM_TH (1,1,"RM",Txt_ROLES_PLURAL_BRIEF_Abc[Rol_TCH]); + HTM_TH_Empty (1); + HTM_TH (1,1,"RM",Txt_Code); + HTM_TH (1,1,"LM",Txt_Institution); + HTM_TH (1,1,"LM",Txt_Short_name); + HTM_TH (1,1,"LM",Txt_Full_name); + HTM_TH (1,1,"LM",Txt_WWW); + HTM_TH (1,1,"RM",Txt_ROLES_PLURAL_BRIEF_Abc[Rol_TCH]); HTM_TR_End (); } @@ -1054,17 +1006,17 @@ void Dpt_ReceiveFormNewDpt (void) if (Dpt_EditingDpt->WWW[0]) { /***** If name of department was in database... *****/ - if (Dpt_CheckIfDepartmentNameExists ("ShortName",Dpt_EditingDpt->ShrtName,-1L)) + if (Dpt_DB_CheckIfDepartmentNameExists ("ShortName",Dpt_EditingDpt->ShrtName,-1L)) Ale_CreateAlert (Ale_WARNING,NULL, Txt_The_department_X_already_exists, Dpt_EditingDpt->ShrtName); - else if (Dpt_CheckIfDepartmentNameExists ("FullName",Dpt_EditingDpt->FullName,-1L)) + else if (Dpt_DB_CheckIfDepartmentNameExists ("FullName",Dpt_EditingDpt->FullName,-1L)) Ale_CreateAlert (Ale_WARNING,NULL, Txt_The_department_X_already_exists, Dpt_EditingDpt->FullName); else // Add new department to database { - Dpt_CreateDepartment (Dpt_EditingDpt); + Dpt_DB_CreateDepartment (Dpt_EditingDpt); Ale_CreateAlert (Ale_SUCCESS,NULL, Txt_Created_new_department_X, Dpt_EditingDpt->FullName); @@ -1079,24 +1031,6 @@ void Dpt_ReceiveFormNewDpt (void) Txt_You_must_specify_the_short_name_and_the_full_name_of_the_new_department); } -/*****************************************************************************/ -/************************** Create a new department **************************/ -/*****************************************************************************/ - -static void Dpt_CreateDepartment (struct Dpt_Department *Dpt) - { - /***** Create a new department *****/ - DB_QueryINSERT ("can not create a new department", - "INSERT INTO dpt_departments" - " (InsCod,ShortName,FullName,WWW)" - " VALUES" - " (%ld,'%s','%s','%s')", - Dpt->InsCod, - Dpt->ShrtName, - Dpt->FullName, - Dpt->WWW); - } - /*****************************************************************************/ /************************** Get number of departments ************************/ /*****************************************************************************/ @@ -1165,32 +1099,32 @@ void Dpt_WriteSelectorDepartment (long InsCod,long DptCod, "id=\"%s\" name=\"%s\" class=\"%s\"", Dpt_PARAM_DPT_COD_NAME,Dpt_PARAM_DPT_COD_NAME,SelectClass); - if (FirstOption <= 0) - { - /* Option when no department selected */ - if (FirstOption < 0) + if (FirstOption <= 0) { - NoDptSelectable = false; - if (TextWhenNoDptSelected) - if (TextWhenNoDptSelected[0]) - NoDptSelectable = true; + /* Option when no department selected */ + if (FirstOption < 0) + { + NoDptSelectable = false; + if (TextWhenNoDptSelected) + if (TextWhenNoDptSelected[0]) + NoDptSelectable = true; - HTM_OPTION (HTM_Type_STRING,"-1",DptCod < 0,!NoDptSelectable, - "%s",TextWhenNoDptSelected); + HTM_OPTION (HTM_Type_STRING,"-1",DptCod < 0,!NoDptSelectable, + "%s",TextWhenNoDptSelected); + } + + /* Another department selected (different to all departments listed) */ + HTM_OPTION (HTM_Type_STRING,"0",DptCod == 0,false, + "%s",Txt_Another_department); } - /* Another department selected (different to all departments listed) */ - HTM_OPTION (HTM_Type_STRING,"0",DptCod == 0,false, - "%s",Txt_Another_department); - } - - /* List all departments */ - for (NumDpt = 0; - NumDpt < Departments.Num; - NumDpt++) - HTM_OPTION (HTM_Type_LONG,&Departments.Lst[NumDpt].DptCod, - Departments.Lst[NumDpt].DptCod == DptCod,false, - "%s",Departments.Lst[NumDpt].FullName); + /* List all departments */ + for (NumDpt = 0; + NumDpt < Departments.Num; + NumDpt++) + HTM_OPTION (HTM_Type_LONG,&Departments.Lst[NumDpt].DptCod, + Departments.Lst[NumDpt].DptCod == DptCod,false, + "%s",Departments.Lst[NumDpt].FullName); /* End selector */ HTM_SELECT_End (); diff --git a/swad_department.h b/swad_department.h index c7679745..8c80b6e1 100644 --- a/swad_department.h +++ b/swad_department.h @@ -67,7 +67,6 @@ struct Dpt_Departments void Dpt_SeeDepts (void); void Dpt_EditDepartments (void); void Dpt_FreeListDepartments (struct Dpt_Departments *Departments); -unsigned Dpt_GetNumDepartmentsInInstitution (long InsCod); void Dpt_GetDataOfDepartmentByCod (struct Dpt_Department *Dpt); long Dpt_GetAndCheckParamDptCod (long MinCodAllowed); void Dpt_RemoveDepartment (void); diff --git a/swad_department_database.c b/swad_department_database.c new file mode 100644 index 00000000..3a572382 --- /dev/null +++ b/swad_department_database.c @@ -0,0 +1,131 @@ +// swad_department_database.c: departments 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 boolean type +// #include // For NULL +// #include // For calloc +// #include // For string functions + +// #include "swad_box.h" +// #include "swad_constant.h" +#include "swad_database.h" +#include "swad_department.h" +#include "swad_department_database.h" +// #include "swad_error.h" +// #include "swad_form.h" +#include "swad_global.h" +// #include "swad_HTML.h" +// #include "swad_institution.h" +// #include "swad_language.h" +// #include "swad_parameter.h" +// #include "swad_string.h" + +/*****************************************************************************/ +/************** External global variables from others modules ****************/ +/*****************************************************************************/ + +extern struct Globals Gbl; + +/*****************************************************************************/ +/***************************** Private constants *****************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/******************************* Private types *******************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/***************************** Private variables *****************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/***************************** Private prototypes ****************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/************************** Create a new department **************************/ +/*****************************************************************************/ + +void Dpt_DB_CreateDepartment (const struct Dpt_Department *Dpt) + { + /***** Create a new department *****/ + DB_QueryINSERT ("can not create a new department", + "INSERT INTO dpt_departments" + " (InsCod,ShortName,FullName,WWW)" + " VALUES" + " (%ld,'%s','%s','%s')", + Dpt->InsCod, + Dpt->ShrtName, + Dpt->FullName, + Dpt->WWW); + } + +/*****************************************************************************/ +/******************* Check if the name of department exists ******************/ +/*****************************************************************************/ + +bool Dpt_DB_CheckIfDepartmentNameExists (const char *FieldName,const char *Name,long DptCod) + { + /***** Get number of departments with a name from database *****/ + return (DB_QueryCOUNT ("can not check if the department name already existed", + "SELECT COUNT(*)" + " FROM dpt_departments" + " WHERE %s='%s'" + " AND DptCod<>%ld", + FieldName,Name, + DptCod) != 0); + } + +/*****************************************************************************/ +/************** Get number of departments in an institution ******************/ +/*****************************************************************************/ + +unsigned Dpt_DB_GetNumDepartmentsInInstitution (long InsCod) + { + /***** Get number of departments in an institution from database *****/ + return (unsigned) + DB_QueryCOUNT ("can not get number of departments in an institution", + "SELECT COUNT(*)" + " FROM dpt_departments" + " WHERE InsCod=%ld", + InsCod); + } + +/*****************************************************************************/ +/************* Update department name in table of departments ****************/ +/*****************************************************************************/ + +void Dpt_DB_UpdateDegName (long DptCod,const char *FieldName,const char *NewDptName) + { + /***** Update department changing old name by new name *****/ + DB_QueryUPDATE ("can not update the name of a department", + "UPDATE dpt_departments" + " SET %s='%s'" + " WHERE DptCod=%ld", + FieldName,NewDptName, + DptCod); + } diff --git a/swad_department_database.h b/swad_department_database.h new file mode 100644 index 00000000..87112ee7 --- /dev/null +++ b/swad_department_database.h @@ -0,0 +1,48 @@ +// swad_department_database.h: departments operations with database + +#ifndef _SWAD_DPT_DB +#define _SWAD_DPT_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_constant.h" +#include "swad_department.h" + +/*****************************************************************************/ +/************************** Public types and constants ***********************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/***************************** Public prototypes *****************************/ +/*****************************************************************************/ + +void Dpt_DB_CreateDepartment (const struct Dpt_Department *Dpt); + +bool Dpt_DB_CheckIfDepartmentNameExists (const char *FieldName,const char *Name,long DptCod); +unsigned Dpt_DB_GetNumDepartmentsInInstitution (long InsCod); + +void Dpt_DB_UpdateDegName (long DptCod,const char *FieldName,const char *NewDptName); + +#endif diff --git a/swad_institution_config.c b/swad_institution_config.c index 72dd2084..e310f74a 100644 --- a/swad_institution_config.c +++ b/swad_institution_config.c @@ -32,7 +32,7 @@ #include // For free #include "swad_database.h" -#include "swad_department.h" +#include "swad_department_database.h" #include "swad_error.h" #include "swad_figure_cache.h" #include "swad_form.h" @@ -535,7 +535,7 @@ static void InsCfg_NumDpts (void) /* Data */ HTM_TD_Begin ("class=\"DAT LB\""); - HTM_Unsigned (Dpt_GetNumDepartmentsInInstitution (Gbl.Hierarchy.Ins.InsCod)); + HTM_Unsigned (Dpt_DB_GetNumDepartmentsInInstitution (Gbl.Hierarchy.Ins.InsCod)); HTM_TD_End (); HTM_TR_End ();