swad-core/swad_degree_type.c

1011 lines
36 KiB
C
Raw Normal View History

2016-03-20 16:30:52 +01:00
// swad_degree_type.c: degree types
/*
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.
2017-01-13 01:51:23 +01:00
Copyright (C) 1999-2017 Antonio Ca<EFBFBD>as Vargas
2016-03-20 16:30:52 +01: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 <ctype.h> // For isprint, isspace, etc.
#include <linux/stddef.h> // For NULL
#include <stdbool.h> // For boolean type
#include <stdio.h> // For fprintf, etc.
#include <stdlib.h> // For exit, system, calloc, free, etc.
#include <string.h> // For string functions
#include <mysql/mysql.h> // To access MySQL databases
#include "swad_config.h"
#include "swad_database.h"
#include "swad_degree.h"
#include "swad_degree_type.h"
#include "swad_global.h"
#include "swad_parameter.h"
/*****************************************************************************/
/************** External global variables from others modules ****************/
/*****************************************************************************/
extern struct Globals Gbl;
/*****************************************************************************/
/*************************** Public constants ********************************/
/*****************************************************************************/
/*****************************************************************************/
/***************************** Private types *********************************/
/*****************************************************************************/
/*****************************************************************************/
/**************************** Private constants ******************************/
/*****************************************************************************/
/*****************************************************************************/
/**************************** Private prototypes *****************************/
/*****************************************************************************/
2017-03-24 13:29:52 +01:00
static void DT_SeeDegreeTypes (Act_Action_t NextAction,Sco_Scope_t Scope,
DT_Order_t DefaultOrder);
2017-03-24 13:22:54 +01:00
static DT_Order_t DT_GetParamDegTypOrder (DT_Order_t DefaultOrder);
2017-03-24 01:09:27 +01:00
2017-03-24 13:22:54 +01:00
static void DT_ListDegreeTypes (Act_Action_t NextAction,DT_Order_t SelectedOrder);
2016-03-20 22:31:57 +01:00
static void DT_EditDegreeTypes (void);
static void DT_ListDegreeTypesForSeeing (void);
2017-03-24 13:58:35 +01:00
static void DT_PutIconsListDegTypes (void);
2016-03-20 22:31:57 +01:00
static void DT_PutIconToEditDegTypes (void);
static void DT_ListDegreeTypesForEdition (void);
2016-03-20 16:30:52 +01:00
2016-03-20 22:31:57 +01:00
static void DT_PutFormToCreateDegreeType (void);
2017-03-24 13:22:54 +01:00
static void DT_PutHeadDegreeTypesForSeeing (Act_Action_t NextAction,DT_Order_t SelectedOrder);
2016-03-20 22:31:57 +01:00
static void DT_PutHeadDegreeTypesForEdition (void);
static void DT_CreateDegreeType (struct DegreeType *DegTyp);
2016-03-20 16:30:52 +01:00
2016-03-20 22:31:57 +01:00
static void DT_PutParamOtherDegTypCod (long DegTypCod);
2016-03-20 16:30:52 +01:00
2016-03-20 22:31:57 +01:00
static unsigned DT_CountNumDegsOfType (long DegTypCod);
static void DT_RemoveDegreeTypeCompletely (long DegTypCod);
static bool DT_CheckIfDegreeTypeNameExists (const char *DegTypName,long DegTypCod);
2016-03-20 16:30:52 +01:00
2017-03-26 16:47:02 +02:00
/*****************************************************************************/
/******************* Put link (form) to view degree types ********************/
/*****************************************************************************/
void DT_PutIconToViewDegreeTypes (void)
{
extern const char *Txt_Types_of_degree;
Lay_PutContextualLink (ActSeeDegTyp,NULL,
"hierarchy64x64.png",
Txt_Types_of_degree,NULL,
NULL);
}
2016-03-20 16:30:52 +01:00
/*****************************************************************************/
/************** Show selector of degree types for statistics *****************/
/*****************************************************************************/
2016-03-20 22:31:57 +01:00
void DT_WriteSelectorDegreeTypes (void)
2016-03-20 16:30:52 +01:00
{
extern const char *Txt_Any_type_of_degree;
unsigned NumDegTyp;
2016-06-09 17:56:57 +02:00
/***** Form to select degree types *****/
/* Get list of degree types */
2017-03-24 13:22:54 +01:00
DT_GetListDegreeTypes (Sco_SCOPE_SYS,DT_ORDER_BY_DEGREE_TYPE);
2016-03-20 16:30:52 +01:00
2016-06-09 17:56:57 +02:00
/* List degree types */
2016-12-20 02:18:50 +01:00
fprintf (Gbl.F.Out,"<select id=\"OthDegTypCod\" name=\"OthDegTypCod\""
2016-06-09 17:56:57 +02:00
" onchange=\"document.getElementById('%s').submit();\">",
Gbl.Form.Id);
fprintf (Gbl.F.Out,"<option value=\"-1\"");
2016-03-20 16:30:52 +01:00
if (Gbl.Stat.DegTypCod == -1L)
fprintf (Gbl.F.Out," selected=\"selected\"");
fprintf (Gbl.F.Out,">%s</option>",Txt_Any_type_of_degree);
2016-06-09 17:56:57 +02:00
2016-03-20 16:30:52 +01:00
for (NumDegTyp = 0;
NumDegTyp < Gbl.Degs.DegTypes.Num;
NumDegTyp++)
{
fprintf (Gbl.F.Out,"<option value=\"%ld\"",Gbl.Degs.DegTypes.Lst[NumDegTyp].DegTypCod );
if (Gbl.Degs.DegTypes.Lst[NumDegTyp].DegTypCod == Gbl.Stat.DegTypCod)
fprintf (Gbl.F.Out," selected=\"selected\"");
fprintf (Gbl.F.Out,">%s</option>",Gbl.Degs.DegTypes.Lst[NumDegTyp].DegTypName);
}
2016-06-09 17:56:57 +02:00
2016-03-20 16:30:52 +01:00
fprintf (Gbl.F.Out,"</select>");
/***** Free list of degree types *****/
2016-03-20 22:31:57 +01:00
DT_FreeListDegreeTypes ();
2016-03-20 16:30:52 +01:00
}
/*****************************************************************************/
/***************************** Show degree types *****************************/
/*****************************************************************************/
2017-03-24 12:23:42 +01:00
void DT_SeeDegreeTypesInSysTab (void)
{
2017-03-24 13:29:52 +01:00
DT_SeeDegreeTypes (ActSeeDegTyp,Sco_SCOPE_SYS,
2017-03-24 12:23:42 +01:00
DT_ORDER_BY_DEGREE_TYPE); // Default order if not specified
}
void DT_SeeDegreeTypesInStaTab (void)
{
2017-03-24 13:29:52 +01:00
DT_SeeDegreeTypes (ActSeeUseGbl,Gbl.Scope.Current,
2017-03-24 12:23:42 +01:00
DT_ORDER_BY_NUM_DEGREES); // Default order if not specified
}
2017-03-24 13:29:52 +01:00
static void DT_SeeDegreeTypes (Act_Action_t NextAction,Sco_Scope_t Scope,
DT_Order_t DefaultOrder)
2016-03-20 16:30:52 +01:00
{
2017-03-24 13:22:54 +01:00
DT_Order_t SelectedOrder;
2017-03-24 01:09:27 +01:00
/***** Get parameter with the type of order in the list of degree types *****/
2017-03-24 13:22:54 +01:00
SelectedOrder = DT_GetParamDegTypOrder (DefaultOrder);
2017-03-24 01:09:27 +01:00
2016-03-20 16:30:52 +01:00
/***** Get list of degree types *****/
2017-03-24 13:29:52 +01:00
DT_GetListDegreeTypes (Scope,SelectedOrder);
2016-03-20 16:30:52 +01:00
/***** List degree types *****/
2017-03-24 13:22:54 +01:00
DT_ListDegreeTypes (NextAction,SelectedOrder);
2016-03-20 16:30:52 +01:00
/***** Free list of degree types *****/
2016-03-20 22:31:57 +01:00
DT_FreeListDegreeTypes ();
2016-03-20 16:30:52 +01:00
}
2017-03-24 01:09:27 +01:00
/*****************************************************************************/
/******* Get parameter with the type or order in list of degree types ********/
/*****************************************************************************/
2017-03-24 13:22:54 +01:00
static DT_Order_t DT_GetParamDegTypOrder (DT_Order_t DefaultOrder)
2017-03-24 01:09:27 +01:00
{
2017-03-24 13:22:54 +01:00
return (DT_Order_t) Par_GetParToUnsignedLong ("Order",
0,
DT_NUM_ORDERS - 1,
(unsigned long) DefaultOrder);
2017-03-24 01:09:27 +01:00
}
2016-03-20 16:30:52 +01:00
/*****************************************************************************/
/********************** Request edition of degree types **********************/
/*****************************************************************************/
2016-03-20 22:31:57 +01:00
void DT_ReqEditDegreeTypes (void)
2016-03-20 16:30:52 +01:00
{
/***** Get list of degree types *****/
2017-03-24 13:22:54 +01:00
DT_GetListDegreeTypes (Sco_SCOPE_SYS,DT_ORDER_BY_DEGREE_TYPE);
2016-03-20 16:30:52 +01:00
/***** Put form to edit degree types *****/
2016-03-20 22:31:57 +01:00
DT_EditDegreeTypes ();
2016-03-20 16:30:52 +01:00
/***** Free list of degree types *****/
2016-03-20 22:31:57 +01:00
DT_FreeListDegreeTypes ();
2016-03-20 16:30:52 +01:00
}
/*****************************************************************************/
/***************************** List degree types *****************************/
/*****************************************************************************/
2017-03-24 12:23:42 +01:00
// This function can be called from:
// - system tab ==> NextAction = ActSeeDegTyp
// - statistic tab ==> NextAction = ActSeeUseGbl
2016-03-20 16:30:52 +01:00
2017-03-24 13:22:54 +01:00
static void DT_ListDegreeTypes (Act_Action_t NextAction,DT_Order_t SelectedOrder)
2016-03-20 16:30:52 +01:00
{
2017-03-24 12:23:42 +01:00
extern const char *Hlp_SYSTEM_Studies;
extern const char *Hlp_STATS_Figures_types_of_degree;
extern const char *Txt_Types_of_degree;
2016-03-20 16:30:52 +01:00
extern const char *Txt_There_are_no_types_of_degree;
if (Gbl.Degs.DegTypes.Num)
2017-03-24 12:23:42 +01:00
{
/***** Write heading *****/
2017-03-24 13:58:35 +01:00
switch (NextAction)
{
case ActSeeDegTyp:
Lay_StartRoundFrameTable (NULL,Txt_Types_of_degree,
DT_PutIconsListDegTypes,
Hlp_SYSTEM_Studies,
2);
break;
case ActSeeUseGbl:
Lay_StartRoundFrameTable (NULL,Txt_Types_of_degree,
DT_PutIconToEditDegTypes,
Hlp_STATS_Figures_types_of_degree,
2);
break;
default: // Bad call
return;
}
2017-03-24 13:22:54 +01:00
DT_PutHeadDegreeTypesForSeeing (NextAction,SelectedOrder);
2017-03-24 12:23:42 +01:00
/***** List current degree types for seeing *****/
2016-03-20 22:31:57 +01:00
DT_ListDegreeTypesForSeeing ();
2017-03-24 12:23:42 +01:00
/***** End table and frame *****/
Lay_EndRoundFrameTable ();
}
2016-03-20 16:30:52 +01:00
else
Lay_ShowAlert (Lay_INFO,Txt_There_are_no_types_of_degree);
}
/*****************************************************************************/
/************************ Put forms to edit degree types *********************/
/*****************************************************************************/
2016-03-20 22:31:57 +01:00
static void DT_EditDegreeTypes (void)
2016-03-20 16:30:52 +01:00
{
extern const char *Txt_There_are_no_types_of_degree;
if (!Gbl.Degs.DegTypes.Num)
/***** Help message *****/
Lay_ShowAlert (Lay_INFO,Txt_There_are_no_types_of_degree);
/***** Put a form to create a new degree type *****/
2016-03-20 22:31:57 +01:00
DT_PutFormToCreateDegreeType ();
2016-03-20 16:30:52 +01:00
/***** Forms to edit current degree types *****/
if (Gbl.Degs.DegTypes.Num)
2016-03-20 22:31:57 +01:00
DT_ListDegreeTypesForEdition ();
2016-03-20 16:30:52 +01:00
}
/*****************************************************************************/
/******************* List current degree types for seeing ********************/
/*****************************************************************************/
2016-03-20 22:31:57 +01:00
static void DT_ListDegreeTypesForSeeing (void)
2016-03-20 16:30:52 +01:00
{
unsigned NumDegTyp;
const char *BgColor;
/***** List degree types with forms for edition *****/
for (NumDegTyp = 0;
NumDegTyp < Gbl.Degs.DegTypes.Num;
NumDegTyp++)
{
BgColor = (Gbl.Degs.DegTypes.Lst[NumDegTyp].DegTypCod ==
Gbl.CurrentDegTyp.DegTyp.DegTypCod) ? "LIGHT_BLUE" :
Gbl.ColorRows[Gbl.RowEvenOdd];
2017-03-24 01:09:27 +01:00
/* Number of degree type in this list */
2017-03-23 20:16:19 +01:00
fprintf (Gbl.F.Out,"<tr>"
2017-03-24 01:09:27 +01:00
"<td class=\"DAT_N RIGHT_MIDDLE %s\">"
"%u"
"</td>",
BgColor,NumDegTyp + 1);
/* Name of degree type */
fprintf (Gbl.F.Out,"<td class=\"DAT_N LEFT_MIDDLE %s\">"
2016-03-20 16:30:52 +01:00
"%s"
"</td>",
BgColor,Gbl.Degs.DegTypes.Lst[NumDegTyp].DegTypName);
/* Number of degrees of this type */
2017-03-24 01:09:27 +01:00
fprintf (Gbl.F.Out,"<td class=\"DAT_N RIGHT_MIDDLE %s\">"
2016-03-20 16:30:52 +01:00
"%u"
"</td>"
"</tr>",
BgColor,Gbl.Degs.DegTypes.Lst[NumDegTyp].NumDegs);
Gbl.RowEvenOdd = 1 - Gbl.RowEvenOdd;
}
}
2017-03-24 13:58:35 +01:00
/*****************************************************************************/
/************** Put contextual icons in list of degree types *****************/
/*****************************************************************************/
static void DT_PutIconsListDegTypes (void)
{
/***** Put icon to edit degree types *****/
DT_PutIconToEditDegTypes ();
/***** Put icon to show a figure *****/
Gbl.Stat.FigureType = Sta_DEGREE_TYPES;
Sta_PutIconToShowFigure ();
}
2016-03-20 16:30:52 +01:00
/*****************************************************************************/
/******************* Put link (form) to edit degree types ********************/
/*****************************************************************************/
2016-03-20 22:31:57 +01:00
static void DT_PutIconToEditDegTypes (void)
2016-03-20 16:30:52 +01:00
{
extern const char *Txt_Edit;
2017-03-24 13:58:35 +01:00
if (Gbl.Usrs.Me.LoggedRole == Rol_SYS_ADM)
Lay_PutContextualLink (ActEdiDegTyp,NULL,
"edit64x64.png",
Txt_Edit,NULL,
NULL);
2016-03-20 16:30:52 +01:00
}
/*****************************************************************************/
/******************* List current degree types for edition *******************/
/*****************************************************************************/
2016-03-20 22:31:57 +01:00
static void DT_ListDegreeTypesForEdition (void)
2016-03-20 16:30:52 +01:00
{
2016-11-28 02:22:32 +01:00
extern const char *Hlp_SYSTEM_Studies_edit;
2016-03-20 16:30:52 +01:00
extern const char *Txt_Types_of_degree;
unsigned NumDegTyp;
/***** Write heading *****/
2016-11-28 02:22:32 +01:00
Lay_StartRoundFrameTable (NULL,Txt_Types_of_degree,
NULL,Hlp_SYSTEM_Studies_edit,2);
2016-03-20 22:31:57 +01:00
DT_PutHeadDegreeTypesForEdition ();
2016-03-20 16:30:52 +01:00
/***** List degree types with forms for edition *****/
for (NumDegTyp = 0;
NumDegTyp < Gbl.Degs.DegTypes.Num;
NumDegTyp++)
{
/* Put icon to remove degree type */
fprintf (Gbl.F.Out,"<tr>"
"<td class=\"BM\">");
if (Gbl.Degs.DegTypes.Lst[NumDegTyp].NumDegs) // Degree type has degrees ==> deletion forbidden
Lay_PutIconRemovalNotAllowed ();
else
{
Act_FormStart (ActRemDegTyp);
2016-03-20 22:31:57 +01:00
DT_PutParamOtherDegTypCod (Gbl.Degs.DegTypes.Lst[NumDegTyp].DegTypCod);
2016-03-20 16:30:52 +01:00
Lay_PutIconRemove ();
Act_FormEnd ();
}
/* Degree type code */
fprintf (Gbl.F.Out,"</td>"
"<td class=\"DAT CENTER_MIDDLE\">"
"%ld"
"</td>",
Gbl.Degs.DegTypes.Lst[NumDegTyp].DegTypCod);
/* Name of degree type */
fprintf (Gbl.F.Out,"<td class=\"LEFT_MIDDLE\">");
Act_FormStart (ActRenDegTyp);
2016-03-20 22:31:57 +01:00
DT_PutParamOtherDegTypCod (Gbl.Degs.DegTypes.Lst[NumDegTyp].DegTypCod);
2016-03-20 16:30:52 +01:00
fprintf (Gbl.F.Out,"<input type=\"text\" name=\"DegTypName\""
" size=\"25\" maxlength=\"%u\" value=\"%s\""
" onchange=\"document.getElementById('%s').submit();\" />",
2017-03-07 11:03:05 +01:00
Deg_MAX_CHARS_DEGREE_TYPE_NAME,
2016-03-20 16:30:52 +01:00
Gbl.Degs.DegTypes.Lst[NumDegTyp].DegTypName,
Gbl.Form.Id);
Act_FormEnd ();
fprintf (Gbl.F.Out,"</td>");
/* Number of degrees of this type */
2016-03-20 16:55:34 +01:00
fprintf (Gbl.F.Out,"<td class=\"DAT RIGHT_MIDDLE\">"
2016-03-20 16:30:52 +01:00
"%u"
"</td>"
"</tr>",
Gbl.Degs.DegTypes.Lst[NumDegTyp].NumDegs);
}
Lay_EndRoundFrameTable ();
}
/*****************************************************************************/
/******************** Put a form to create a new degree type *****************/
/*****************************************************************************/
2016-03-20 22:31:57 +01:00
static void DT_PutFormToCreateDegreeType (void)
2016-03-20 16:30:52 +01:00
{
2016-11-28 02:22:32 +01:00
extern const char *Hlp_SYSTEM_Studies_edit;
2016-03-20 16:30:52 +01:00
extern const char *Txt_New_type_of_degree;
2016-03-20 16:55:34 +01:00
extern const char *Txt_Type_of_degree;
2016-03-20 16:30:52 +01:00
extern const char *Txt_Create_type_of_degree;
/***** Start form *****/
Act_FormStart (ActNewDegTyp);
/***** Start of frame *****/
2016-11-28 02:22:32 +01:00
Lay_StartRoundFrameTable (NULL,Txt_New_type_of_degree,
NULL,Hlp_SYSTEM_Studies_edit,2);
2016-03-20 16:30:52 +01:00
/***** Write heading *****/
fprintf (Gbl.F.Out,"<tr>"
"<th class=\"CENTER_MIDDLE\">"
"%s"
"</th>"
"</tr>",
2016-03-20 16:55:34 +01:00
Txt_Type_of_degree);
2016-03-20 16:30:52 +01:00
/***** Degree type name *****/
fprintf (Gbl.F.Out,"<tr>"
"<td class=\"LEFT_MIDDLE\">"
"<input type=\"text\" name=\"DegTypName\""
2016-11-19 20:12:13 +01:00
" size=\"25\" maxlength=\"%u\" value=\"%s\""
" required=\"required\" />"
2016-03-20 16:30:52 +01:00
"</td>",
2017-03-07 11:03:05 +01:00
Deg_MAX_CHARS_DEGREE_TYPE_NAME,Gbl.Degs.EditingDegTyp.DegTypName);
2016-03-20 16:30:52 +01:00
/***** Send button and end frame *****/
Lay_EndRoundFrameTableWithButton (Lay_CREATE_BUTTON,Txt_Create_type_of_degree);
/***** End form *****/
Act_FormEnd ();
}
/*****************************************************************************/
/***************** Write header with fields of a degree type *****************/
/*****************************************************************************/
2017-03-24 13:22:54 +01:00
static void DT_PutHeadDegreeTypesForSeeing (Act_Action_t NextAction,DT_Order_t SelectedOrder)
2016-03-20 16:30:52 +01:00
{
2017-03-24 01:09:27 +01:00
extern const char *Txt_DEGREE_TYPES_HELP_ORDER[DT_NUM_ORDERS];
extern const char *Txt_DEGREE_TYPES_ORDER[DT_NUM_ORDERS];
DT_Order_t Order;
2016-03-20 16:30:52 +01:00
fprintf (Gbl.F.Out,"<tr>"
2017-03-24 01:09:27 +01:00
"<th></th>");
for (Order = DT_ORDER_BY_DEGREE_TYPE;
Order <= DT_ORDER_BY_NUM_DEGREES;
Order++)
{
fprintf (Gbl.F.Out,"<th class=\"%s\">",
Order == DT_ORDER_BY_DEGREE_TYPE ? "LEFT_MIDDLE" :
"RIGHT_MIDDLE");
2017-03-24 12:23:42 +01:00
/* Start form to change order */
Act_FormStart (NextAction);
if (NextAction == ActSeeUseGbl)
Sta_PutHiddenParamFigures ();
2017-03-24 01:09:27 +01:00
Par_PutHiddenParamUnsigned ("Order",(unsigned) Order);
2017-03-24 12:23:42 +01:00
/* Link with the head of this column */
2017-03-24 01:09:27 +01:00
Act_LinkFormSubmit (Txt_DEGREE_TYPES_HELP_ORDER[Order],"TIT_TBL",NULL);
2017-03-24 13:22:54 +01:00
if (Order == SelectedOrder)
2017-03-24 01:09:27 +01:00
fprintf (Gbl.F.Out,"<u>");
fprintf (Gbl.F.Out,"%s",Txt_DEGREE_TYPES_ORDER[Order]);
2017-03-24 13:22:54 +01:00
if (Order == SelectedOrder)
2017-03-24 01:09:27 +01:00
fprintf (Gbl.F.Out,"</u>");
fprintf (Gbl.F.Out,"</a>");
2017-03-24 12:23:42 +01:00
/* End form */
2017-03-24 01:09:27 +01:00
Act_FormEnd ();
fprintf (Gbl.F.Out,"</th>");
}
2016-03-20 16:30:52 +01:00
}
/*****************************************************************************/
/***************** Write header with fields of a degree type *****************/
/*****************************************************************************/
2016-03-20 22:31:57 +01:00
static void DT_PutHeadDegreeTypesForEdition (void)
2016-03-20 16:30:52 +01:00
{
extern const char *Txt_Code;
2016-03-20 16:55:34 +01:00
extern const char *Txt_Type_of_degree;
2016-03-20 16:30:52 +01:00
extern const char *Txt_Degrees;
fprintf (Gbl.F.Out,"<tr>"
"<th class=\"BM\"></th>"
"<th class=\"CENTER_MIDDLE\">"
"%s"
"</th>"
"<th class=\"CENTER_MIDDLE\">"
"%s"
"</th>"
"<th class=\"RIGHT_MIDDLE\">"
"%s"
"</th>"
"</tr>",
Txt_Code,
2016-03-20 16:55:34 +01:00
Txt_Type_of_degree,
2016-03-20 16:30:52 +01:00
Txt_Degrees);
}
/*****************************************************************************/
/************************** Create a new degree type *************************/
/*****************************************************************************/
2016-03-20 22:31:57 +01:00
static void DT_CreateDegreeType (struct DegreeType *DegTyp)
2016-03-20 16:30:52 +01:00
{
extern const char *Txt_Created_new_type_of_degree_X;
2017-03-07 11:03:05 +01:00
char Query[128 + Deg_MAX_BYTES_DEGREE_TYPE_NAME];
2016-03-20 16:30:52 +01:00
/***** Create a new degree type *****/
sprintf (Query,"INSERT INTO deg_types SET DegTypName='%s'",
DegTyp->DegTypName);
DB_QueryINSERT (Query,"can not create a new type of degree");
/***** Write success message *****/
sprintf (Gbl.Message,Txt_Created_new_type_of_degree_X,
DegTyp->DegTypName);
Lay_ShowAlert (Lay_SUCCESS,Gbl.Message);
}
/*****************************************************************************/
/**************** Create a list with all the degree types ********************/
/*****************************************************************************/
2017-03-24 13:22:54 +01:00
void DT_GetListDegreeTypes (Sco_Scope_t Scope,DT_Order_t Order)
2016-03-20 16:30:52 +01:00
{
2017-03-24 01:09:27 +01:00
static const char *OrderBySubQuery[DT_NUM_ORDERS] =
{
"DegTypName", // DT_ORDER_BY_DEGREE_TYPE
"NumDegs DESC,DegTypName", // DT_ORDER_BY_NUM_DEGREES
};
2016-03-20 16:30:52 +01:00
char Query[1024];
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned long NumRow;
/***** Get types of degree from database *****/
2017-03-24 13:22:54 +01:00
switch (Scope)
2017-03-24 13:01:42 +01:00
{
case Sco_SCOPE_SYS:
/* Get
all degree types with degrees
union with
all degree types without any degree */
sprintf (Query,"(SELECT deg_types.DegTypCod,deg_types.DegTypName,"
"COUNT(degrees.DegCod) AS NumDegs"
" FROM degrees,deg_types"
" WHERE degrees.DegTypCod=deg_types.DegTypCod"
" GROUP BY degrees.DegTypCod)"
" UNION "
"(SELECT DegTypCod,DegTypName,0 AS NumDegs" // Do not use '0' because NumDegs will be casted to string and order will be wrong
" FROM deg_types"
" WHERE DegTypCod NOT IN"
" (SELECT DegTypCod FROM degrees))"
" ORDER BY %s",
2017-03-24 13:22:54 +01:00
OrderBySubQuery[Order]);
2017-03-24 13:01:42 +01:00
break;
case Sco_SCOPE_CTY:
/* Get only degree types with degrees in the current country */
sprintf (Query,"SELECT deg_types.DegTypCod,deg_types.DegTypName,"
"COUNT(degrees.DegCod) AS NumDegs"
" FROM institutions,centres,degrees,deg_types"
" WHERE institutions.CtyCod=%ld"
" AND institutions.InsCod=centres.InsCod"
" AND centres.CtrCod=degrees.CtrCod"
" AND degrees.DegTypCod=deg_types.DegTypCod"
" GROUP BY degrees.DegTypCod"
" ORDER BY %s",
Gbl.CurrentCty.Cty.CtyCod,
2017-03-24 13:22:54 +01:00
OrderBySubQuery[Order]);
2017-03-24 13:01:42 +01:00
break;
case Sco_SCOPE_INS:
/* Get only degree types with degrees in the current institution */
sprintf (Query,"SELECT deg_types.DegTypCod,deg_types.DegTypName,"
"COUNT(degrees.DegCod) AS NumDegs"
" FROM centres,degrees,deg_types"
" WHERE centres.InsCod=%ld"
" AND centres.CtrCod=degrees.CtrCod"
" AND degrees.DegTypCod=deg_types.DegTypCod"
" GROUP BY degrees.DegTypCod"
" ORDER BY %s",
Gbl.CurrentIns.Ins.InsCod,
2017-03-24 13:22:54 +01:00
OrderBySubQuery[Order]);
2017-03-24 13:01:42 +01:00
break;
case Sco_SCOPE_CTR:
/* Get only degree types with degrees in the current centre */
sprintf (Query,"SELECT deg_types.DegTypCod,deg_types.DegTypName,"
"COUNT(degrees.DegCod) AS NumDegs"
" FROM degrees,deg_types"
" WHERE degrees.CtrCod=%ld"
" AND degrees.DegTypCod=deg_types.DegTypCod"
" GROUP BY degrees.DegTypCod"
" ORDER BY %s",
Gbl.CurrentCtr.Ctr.CtrCod,
2017-03-24 13:22:54 +01:00
OrderBySubQuery[Order]);
2017-03-24 13:01:42 +01:00
break;
case Sco_SCOPE_DEG:
case Sco_SCOPE_CRS:
/* Get only degree types with degrees in the current degree */
sprintf (Query,"SELECT deg_types.DegTypCod,deg_types.DegTypName,"
"COUNT(degrees.DegCod) AS NumDegs"
" FROM degrees,deg_types"
" WHERE degrees.DegCod=%ld"
" AND degrees.DegTypCod=deg_types.DegTypCod"
" GROUP BY degrees.DegTypCod"
" ORDER BY %s",
Gbl.CurrentDeg.Deg.DegCod,
2017-03-24 13:22:54 +01:00
OrderBySubQuery[Order]);
2017-03-24 13:01:42 +01:00
break;
default:
Lay_ShowErrorAndExit ("Wrong scope.");
break;
}
2016-03-20 16:30:52 +01:00
Gbl.Degs.DegTypes.Num = (unsigned) DB_QuerySELECT (Query,&mysql_res,"can not get types of degree");
/***** Get degree types *****/
if (Gbl.Degs.DegTypes.Num)
{
/***** Create a list of degree types *****/
if ((Gbl.Degs.DegTypes.Lst = (struct DegreeType *) calloc (Gbl.Degs.DegTypes.Num,sizeof (struct DegreeType))) == NULL)
Lay_ShowErrorAndExit ("Not enough memory to store types of degree.");
/***** Get degree types *****/
for (NumRow = 0;
NumRow < Gbl.Degs.DegTypes.Num;
NumRow++)
{
/* Get next degree type */
row = mysql_fetch_row (mysql_res);
/* Get degree type code (row[0]) */
if ((Gbl.Degs.DegTypes.Lst[NumRow].DegTypCod = Str_ConvertStrCodToLongCod (row[0])) < 0)
Lay_ShowErrorAndExit ("Wrong code of type of degree.");
/* Get degree type name (row[1]) */
2017-01-15 18:02:52 +01:00
Str_Copy (Gbl.Degs.DegTypes.Lst[NumRow].DegTypName,row[1],
2017-03-07 11:03:05 +01:00
Deg_MAX_BYTES_DEGREE_TYPE_NAME);
2016-03-20 16:30:52 +01:00
/* Number of degrees of this type (row[2]) */
if (sscanf (row[2],"%u",&Gbl.Degs.DegTypes.Lst[NumRow].NumDegs) != 1)
Lay_ShowErrorAndExit ("Error when getting number of degrees of a type");
}
}
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
}
/*****************************************************************************/
/********* Free list of degree types and list of degrees of each type ********/
/*****************************************************************************/
2016-03-20 22:31:57 +01:00
void DT_FreeListDegreeTypes (void)
2016-03-20 16:30:52 +01:00
{
/***** Free memory used by the list of degree types *****/
if (Gbl.Degs.DegTypes.Lst)
{
free ((void *) Gbl.Degs.DegTypes.Lst);
Gbl.Degs.DegTypes.Lst = NULL;
Gbl.Degs.DegTypes.Num = 0;
}
}
/*****************************************************************************/
/***************** Receive form to create a new degree type ******************/
/*****************************************************************************/
2016-03-20 22:31:57 +01:00
void DT_RecFormNewDegreeType (void)
2016-03-20 16:30:52 +01:00
{
extern const char *Txt_The_type_of_degree_X_already_exists;
extern const char *Txt_You_must_specify_the_name_of_the_new_type_of_degree;
struct DegreeType *DegTyp;
DegTyp = &Gbl.Degs.EditingDegTyp;
/***** Get parameters from form *****/
/* Get the name of degree type */
2017-03-07 11:03:05 +01:00
Par_GetParToText ("DegTypName",DegTyp->DegTypName,Deg_MAX_BYTES_DEGREE_TYPE_NAME);
2016-03-20 16:30:52 +01:00
if (DegTyp->DegTypName[0]) // If there's a degree type name
{
/***** If name of degree type was in database... *****/
2016-03-20 22:31:57 +01:00
if (DT_CheckIfDegreeTypeNameExists (DegTyp->DegTypName,-1L))
2016-03-20 16:30:52 +01:00
{
sprintf (Gbl.Message,Txt_The_type_of_degree_X_already_exists,
DegTyp->DegTypName);
Lay_ShowAlert (Lay_WARNING,Gbl.Message);
}
else // Add new degree type to database
2016-03-20 22:31:57 +01:00
DT_CreateDegreeType (DegTyp);
2016-03-20 16:30:52 +01:00
}
else // If there is not a degree type name
{
sprintf (Gbl.Message,"%s",Txt_You_must_specify_the_name_of_the_new_type_of_degree);
Lay_ShowAlert (Lay_WARNING,Gbl.Message);
}
/***** Show the form again *****/
2016-03-20 22:31:57 +01:00
DT_ReqEditDegreeTypes ();
2016-03-20 16:30:52 +01:00
}
/*****************************************************************************/
/**************************** Remove a degree type ***************************/
/*****************************************************************************/
2016-03-20 22:31:57 +01:00
void DT_RemoveDegreeType (void)
2016-03-20 16:30:52 +01:00
{
extern const char *Txt_To_remove_a_type_of_degree_you_must_first_remove_all_degrees_of_that_type;
extern const char *Txt_Type_of_degree_X_removed;
struct DegreeType DegTyp;
/***** Get the code of the degree type *****/
2016-03-20 22:31:57 +01:00
if ((DegTyp.DegTypCod = DT_GetParamOtherDegTypCod ()) == -1L)
2016-03-20 16:30:52 +01:00
Lay_ShowErrorAndExit ("Code of type of degree is missing.");
/***** Get data of the degree type from database *****/
2016-03-20 22:31:57 +01:00
if (!DT_GetDataOfDegreeTypeByCod (&DegTyp))
2016-03-20 16:30:52 +01:00
Lay_ShowErrorAndExit ("Code of type of degree not found.");
/***** Check if this degree type has degrees *****/
if (DegTyp.NumDegs) // Degree type has degrees ==> don't remove
Lay_ShowAlert (Lay_WARNING,Txt_To_remove_a_type_of_degree_you_must_first_remove_all_degrees_of_that_type);
else // Degree type has no degrees ==> remove it
{
/***** Remove degree type *****/
2016-03-20 22:31:57 +01:00
DT_RemoveDegreeTypeCompletely (DegTyp.DegTypCod);
2016-03-20 16:30:52 +01:00
/***** Write message to show the change made *****/
sprintf (Gbl.Message,Txt_Type_of_degree_X_removed,
DegTyp.DegTypName);
Lay_ShowAlert (Lay_SUCCESS,Gbl.Message);
}
/***** Show the form again *****/
2016-03-20 22:31:57 +01:00
DT_ReqEditDegreeTypes ();
2016-03-20 16:30:52 +01:00
}
/*****************************************************************************/
/***************** Write parameter with code of degree type ******************/
/*****************************************************************************/
2016-03-20 22:31:57 +01:00
static void DT_PutParamOtherDegTypCod (long DegTypCod)
2016-03-20 16:30:52 +01:00
{
Par_PutHiddenParamLong ("OthDegTypCod",DegTypCod);
}
/*****************************************************************************/
/******************* Get parameter with code of degree type ******************/
/*****************************************************************************/
2016-03-20 22:31:57 +01:00
long DT_GetParamOtherDegTypCod (void)
2016-03-20 16:30:52 +01:00
{
2017-01-28 20:32:50 +01:00
/***** Get code of degree type *****/
return Par_GetParToLong ("OthDegTypCod");
2016-03-20 16:30:52 +01:00
}
/*****************************************************************************/
/**************** Count number of degrees in a degree type ******************/
/*****************************************************************************/
2016-03-20 22:31:57 +01:00
static unsigned DT_CountNumDegsOfType (long DegTypCod)
2016-03-20 16:30:52 +01:00
{
2017-03-11 20:36:55 +01:00
char Query[128];
2016-03-20 16:30:52 +01:00
/***** Get number of degrees of a type from database *****/
2017-03-24 01:09:27 +01:00
sprintf (Query,"SELECT COUNT(*) FROM degrees WHERE DegTypCod=%ld",
2016-03-20 16:30:52 +01:00
DegTypCod);
return (unsigned) DB_QueryCOUNT (Query,"can not get number of degrees of a type");
}
/*****************************************************************************/
/****************** Get data of a degree type from its code ******************/
/*****************************************************************************/
2016-03-20 22:31:57 +01:00
bool DT_GetDataOfDegreeTypeByCod (struct DegreeType *DegTyp)
2016-03-20 16:30:52 +01:00
{
2017-03-11 20:36:55 +01:00
char Query[128];
2016-03-20 16:30:52 +01:00
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned long NumRows;
bool DegTypFound = false;
if (DegTyp->DegTypCod <= 0)
{
DegTyp->DegTypCod = -1L;
DegTyp->DegTypName[0] = '\0';
DegTyp->NumDegs = 0;
return false;
}
/***** Get the name of a type of degree from database *****/
2017-03-24 01:09:27 +01:00
sprintf (Query,"SELECT DegTypName FROM deg_types WHERE DegTypCod=%ld",
2016-03-20 16:30:52 +01:00
DegTyp->DegTypCod);
NumRows = DB_QuerySELECT (Query,&mysql_res,"can not get the name of a type of degree");
if (NumRows == 1)
{
/***** Get data of degree type *****/
row = mysql_fetch_row (mysql_res);
/* Get the name of the degree type (row[0]) */
2017-01-17 03:10:43 +01:00
Str_Copy (DegTyp->DegTypName,row[0],
2017-03-07 11:03:05 +01:00
Deg_MAX_BYTES_DEGREE_TYPE_NAME);
2016-03-20 16:30:52 +01:00
/* Count number of degrees of this type */
2016-03-20 22:31:57 +01:00
DegTyp->NumDegs = DT_CountNumDegsOfType (DegTyp->DegTypCod);
2016-03-20 16:30:52 +01:00
/* Set return value */
DegTypFound = true;
}
else if (NumRows == 0)
{
DegTyp->DegTypCod = -1L;
DegTyp->DegTypName[0] = '\0';
DegTyp->NumDegs = 0;
return false;
}
else // NumRows > 1
Lay_ShowErrorAndExit ("Type of degree repeated in database.");
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
return DegTypFound;
}
/*****************************************************************************/
/******************** Remove a degree type and its degrees *******************/
/*****************************************************************************/
2016-03-20 22:31:57 +01:00
static void DT_RemoveDegreeTypeCompletely (long DegTypCod)
2016-03-20 16:30:52 +01:00
{
2017-03-11 20:36:55 +01:00
char Query[128];
2016-03-20 16:30:52 +01:00
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned long NumRow,NumRows;
long DegCod;
/***** Get degrees of a type from database *****/
2017-03-24 01:09:27 +01:00
sprintf (Query,"SELECT DegCod FROM degrees WHERE DegTypCod=%ld",
2016-03-20 16:30:52 +01:00
DegTypCod);
NumRows = DB_QuerySELECT (Query,&mysql_res,"can not get degrees of a type");
/* Get degrees of this type */
for (NumRow = 0;
NumRow < NumRows;
NumRow++)
{
/* Get next degree */
row = mysql_fetch_row (mysql_res);
/* Get degree code (row[0]) */
if ((DegCod = Str_ConvertStrCodToLongCod (row[0])) < 0)
Lay_ShowErrorAndExit ("Wrong code of degree.");
/* Remove degree */
Deg_RemoveDegreeCompletely (DegCod);
}
/* Free structure that stores the query result */
DB_FreeMySQLResult (&mysql_res);
/***** Remove the degree type *****/
2017-03-24 01:09:27 +01:00
sprintf (Query,"DELETE FROM deg_types WHERE DegTypCod=%ld",DegTypCod);
2016-03-20 16:30:52 +01:00
DB_QueryDELETE (Query,"can not remove a type of degree");
}
/*****************************************************************************/
/**************************** Rename a degree type ***************************/
/*****************************************************************************/
2016-03-20 22:31:57 +01:00
void DT_RenameDegreeType (void)
2016-03-20 16:30:52 +01:00
{
extern const char *Txt_You_can_not_leave_the_name_of_the_type_of_degree_X_empty;
extern const char *Txt_The_type_of_degree_X_already_exists;
extern const char *Txt_The_type_of_degree_X_has_been_renamed_as_Y;
extern const char *Txt_The_name_of_the_type_of_degree_X_has_not_changed;
struct DegreeType *DegTyp;
2017-03-11 20:36:55 +01:00
char Query[128 + Deg_MAX_BYTES_DEGREE_TYPE_NAME];
2017-03-07 11:03:05 +01:00
char NewNameDegTyp[Deg_MAX_BYTES_DEGREE_TYPE_NAME + 1];
2016-03-20 16:30:52 +01:00
DegTyp = &Gbl.Degs.EditingDegTyp;
/***** Get parameters from form *****/
/* Get the code of the degree type */
2016-03-20 22:31:57 +01:00
if ((DegTyp->DegTypCod = DT_GetParamOtherDegTypCod ()) == -1L)
2016-03-20 16:30:52 +01:00
Lay_ShowErrorAndExit ("Code of type of degree is missing.");
/* Get the new name for the degree type */
2017-03-07 11:03:05 +01:00
Par_GetParToText ("DegTypName",NewNameDegTyp,Deg_MAX_BYTES_DEGREE_TYPE_NAME);
2016-03-20 16:30:52 +01:00
/***** Get from the database the old name of the degree type *****/
2016-03-20 22:31:57 +01:00
if (!DT_GetDataOfDegreeTypeByCod (DegTyp))
2016-03-20 16:30:52 +01:00
Lay_ShowErrorAndExit ("Code of type of degree not found.");
/***** Check if new name is empty *****/
if (!NewNameDegTyp[0])
{
sprintf (Gbl.Message,Txt_You_can_not_leave_the_name_of_the_type_of_degree_X_empty,
DegTyp->DegTypName);
Lay_ShowAlert (Lay_WARNING,Gbl.Message);
}
else
{
/***** Check if old and new names are the same (this happens when user press enter with no changes in the form) *****/
if (strcmp (DegTyp->DegTypName,NewNameDegTyp)) // Different names
{
/***** If degree type was in database... *****/
2016-03-20 22:31:57 +01:00
if (DT_CheckIfDegreeTypeNameExists (NewNameDegTyp,DegTyp->DegTypCod))
2016-03-20 16:30:52 +01:00
{
sprintf (Gbl.Message,Txt_The_type_of_degree_X_already_exists,
NewNameDegTyp);
Lay_ShowAlert (Lay_WARNING,Gbl.Message);
}
else
{
/* Update the table changing old name by new name */
sprintf (Query,"UPDATE deg_types SET DegTypName='%s'"
2017-03-24 01:09:27 +01:00
" WHERE DegTypCod=%ld",
2016-03-20 16:30:52 +01:00
NewNameDegTyp,DegTyp->DegTypCod);
DB_QueryUPDATE (Query,"can not update the type of a degree");
/* Write message to show the change made */
sprintf (Gbl.Message,Txt_The_type_of_degree_X_has_been_renamed_as_Y,
DegTyp->DegTypName,NewNameDegTyp);
Lay_ShowAlert (Lay_SUCCESS,Gbl.Message);
}
}
else // The same name
{
sprintf (Gbl.Message,Txt_The_name_of_the_type_of_degree_X_has_not_changed,
NewNameDegTyp);
Lay_ShowAlert (Lay_INFO,Gbl.Message);
}
}
/***** Show the form again *****/
2017-01-17 03:10:43 +01:00
Str_Copy (DegTyp->DegTypName,NewNameDegTyp,
2017-03-07 11:03:05 +01:00
Deg_MAX_BYTES_DEGREE_TYPE_NAME);
2016-03-20 22:31:57 +01:00
DT_ReqEditDegreeTypes ();
2016-03-20 16:30:52 +01:00
}
/*****************************************************************************/
/****************** Check if name of degree type exists **********************/
/*****************************************************************************/
2016-03-20 22:31:57 +01:00
static bool DT_CheckIfDegreeTypeNameExists (const char *DegTypName,long DegTypCod)
2016-03-20 16:30:52 +01:00
{
2017-03-11 20:36:55 +01:00
char Query[256 + Deg_MAX_BYTES_DEGREE_TYPE_NAME];
2016-03-20 16:30:52 +01:00
/***** Get number of degree types with a name from database *****/
sprintf (Query,"SELECT COUNT(*) FROM deg_types"
2017-03-24 01:09:27 +01:00
" WHERE DegTypName='%s' AND DegTypCod<>%ld",
2016-03-20 16:30:52 +01:00
DegTypName,DegTypCod);
return (DB_QueryCOUNT (Query,"can not check if the name of a type of degree already existed") != 0);
}
/*****************************************************************************/
/************************ Change the type of a degree ************************/
/*****************************************************************************/
2016-03-20 22:31:57 +01:00
void DT_ChangeDegreeType (void)
2016-03-20 16:30:52 +01:00
{
extern const char *Txt_The_type_of_degree_of_the_degree_X_has_changed;
struct Degree *Deg;
long NewDegTypCod;
2017-03-11 20:36:55 +01:00
char Query[128];
2016-03-20 16:30:52 +01:00
Deg = &Gbl.Degs.EditingDeg;
/***** Get parameters from form *****/
/* Get degree code */
2016-10-23 19:40:14 +02:00
Deg->DegCod = Deg_GetAndCheckParamOtherDegCod ();
2016-03-20 16:30:52 +01:00
/* Get the new degree type */
2016-03-20 22:31:57 +01:00
NewDegTypCod = DT_GetParamOtherDegTypCod ();
2016-03-20 16:30:52 +01:00
/***** Get data of degree *****/
Deg_GetDataOfDegreeByCod (Deg);
/***** Update the table of degrees changing old type by new type *****/
2017-03-24 01:09:27 +01:00
sprintf (Query,"UPDATE degrees SET DegTypCod=%ld WHERE DegCod=%ld",
2016-03-20 16:30:52 +01:00
NewDegTypCod,Deg->DegCod);
DB_QueryUPDATE (Query,"can not update the type of a degree");
/***** Write message to show the change made *****/
sprintf (Gbl.Message,Txt_The_type_of_degree_of_the_degree_X_has_changed,
Deg->FullName);
Lay_ShowAlert (Lay_SUCCESS,Gbl.Message);
/***** Put button to go to degree changed *****/
Deg_PutButtonToGoToDeg (Deg);
/***** Show the form again *****/
Gbl.Degs.EditingDegTyp.DegTypCod = NewDegTypCod;
Deg_EditDegrees ();
}