swad-core/swad_program.c

2243 lines
75 KiB
C
Raw Normal View History

2020-02-20 23:48:48 +01:00
// swad_program.c: course program
/*
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-2020 Antonio Ca<EFBFBD>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 <http://www.gnu.org/licenses/>.
*/
/*****************************************************************************/
/********************************* Headers ***********************************/
/*****************************************************************************/
#define _GNU_SOURCE // For asprintf
#include <linux/limits.h> // For PATH_MAX
#include <stddef.h> // For NULL
#include <stdio.h> // For asprintf
#include <stdlib.h> // For calloc
#include <string.h> // For string functions
#include "swad_box.h"
#include "swad_database.h"
#include "swad_figure.h"
#include "swad_form.h"
#include "swad_global.h"
#include "swad_HTML.h"
#include "swad_pagination.h"
#include "swad_parameter.h"
#include "swad_photo.h"
#include "swad_program.h"
#include "swad_role.h"
#include "swad_setting.h"
#include "swad_string.h"
/*****************************************************************************/
/************** External global variables from others modules ****************/
/*****************************************************************************/
extern struct Globals Gbl;
/*****************************************************************************/
/***************************** Private constants *****************************/
/*****************************************************************************/
/*****************************************************************************/
/******************************* Private types *******************************/
/*****************************************************************************/
2020-03-03 12:00:36 +01:00
typedef enum
{
Prg_DONT_PUT_FORM_ITEM,
Prg_PUT_FORM_CREATE_ITEM,
Prg_PUT_FORM_CHANGE_ITEM,
} Prg_CreateOrChangeItem_t;
2020-03-05 01:37:48 +01:00
#define Prg_NUM_MOVEMENTS_UP_DOWN 2
typedef enum
{
Prg_MOVE_UP,
Prg_MOVE_DOWN,
} Prg_MoveUpDown_t;
#define Prg_NUM_MOVEMENTS_LEFT_RIGHT 2
typedef enum
{
Prg_MOVE_LEFT,
Prg_MOVE_RIGHT,
} Prg_MoveLeftRight_t;
2020-03-04 15:37:05 +01:00
struct Subtree
{
unsigned Begin;
unsigned End;
};
2020-02-20 23:48:48 +01:00
/*****************************************************************************/
/***************************** Private variables *****************************/
/*****************************************************************************/
2020-02-29 13:52:29 +01:00
static long Prg_CurrentItmCod; // Used as parameter in contextual links
static unsigned Prg_MaxLevel; // Maximum level of items
static unsigned *Prg_NumItem = NULL; // Numbers for each level from 1 to maximum level
2020-02-26 00:26:07 +01:00
2020-02-20 23:48:48 +01:00
/*****************************************************************************/
/***************************** Private prototypes ****************************/
/*****************************************************************************/
2020-03-05 01:37:48 +01:00
static void Prg_ShowCourseProgramHighlightingItem (const struct Subtree *ToHighlight);
2020-03-03 12:00:36 +01:00
static void Prg_ShowAllItems (Prg_CreateOrChangeItem_t CreateOrChangeItem,
2020-03-05 01:37:48 +01:00
const struct Subtree *ToHighlight,
2020-03-03 19:28:38 +01:00
long ParentItmCod,long ItmCodBeforeForm,unsigned FormLevel);
2020-02-26 00:26:07 +01:00
static bool Prg_CheckIfICanCreateItems (void);
static void Prg_PutIconsListItems (void);
static void Prg_PutIconToCreateNewItem (void);
static void Prg_PutButtonToCreateNewItem (void);
2020-03-04 00:33:48 +01:00
static void Prg_ShowOneItem (unsigned NumItem,const struct ProgramItem *Item,
2020-03-04 15:37:05 +01:00
const struct Subtree *ToHighlight,bool PrintView);
2020-03-03 19:28:38 +01:00
static void Prg_ShowItemForm (Prg_CreateOrChangeItem_t CreateOrChangeItem,
long ParamItmCod,unsigned FormLevel);
2020-02-29 13:52:29 +01:00
2020-03-05 01:37:48 +01:00
static unsigned Prg_GetMaxItemLevel (void);
2020-02-29 13:52:29 +01:00
static void Prg_CreateNumbers (unsigned MaxLevel);
static void Prg_FreeNumbers (void);
2020-03-02 14:41:52 +01:00
static void Prg_IncreaseNumItem (unsigned Level);
static unsigned Prg_GetNumItem (unsigned Level);
2020-02-27 23:44:21 +01:00
static void Prg_WriteNumItem (unsigned Level);
2020-03-03 17:36:24 +01:00
static void Prg_WriteNumNewItem (unsigned Level);
2020-02-20 23:48:48 +01:00
2020-03-03 14:35:45 +01:00
static void Prg_PutFormsToRemEditOnePrgItem (unsigned NumItem,
2020-03-04 01:26:49 +01:00
const struct ProgramItem *Item);
2020-03-02 14:41:52 +01:00
static bool Prg_CheckIfMoveUpIsAllowed (unsigned NumItem);
static bool Prg_CheckIfMoveDownIsAllowed (unsigned NumItem);
static bool Prg_CheckIfMoveLeftIsAllowed (unsigned NumItem);
static bool Prg_CheckIfMoveRightIsAllowed (unsigned NumItem);
2020-02-26 00:26:07 +01:00
static void Prg_SetCurrentItmCod (long ItmCod);
static long Prg_GetCurrentItmCod (void);
2020-02-20 23:48:48 +01:00
static void Prg_PutParams (void);
2020-02-26 00:26:07 +01:00
2020-03-02 14:41:52 +01:00
static void Prg_GetListPrgItems (void);
2020-03-03 00:56:22 +01:00
static void Prg_GetDataOfItemByCod (struct ProgramItem *Item);
2020-02-26 00:26:07 +01:00
static void Prg_GetDataOfItem (struct ProgramItem *Item,
MYSQL_RES **mysql_res,
unsigned long NumRows);
static void Prg_ResetItem (struct ProgramItem *Item);
2020-03-03 00:56:22 +01:00
static void Prg_FreeListItems (void);
2020-02-26 00:26:07 +01:00
static void Prg_GetPrgItemTxtFromDB (long ItmCod,char Txt[Cns_MAX_BYTES_TEXT + 1]);
static void Prg_PutParamItmCod (long ItmCod);
2020-03-02 14:41:52 +01:00
static long Prg_GetParamItmCod (void);
2020-03-04 15:37:05 +01:00
static unsigned Prg_GetNumItemFromItmCod (long ItmCod);
2020-02-26 00:26:07 +01:00
2020-03-05 01:37:48 +01:00
static void Prg_HideUnhidePrgItem (char YN);
static void Prg_MoveUpDownPrgItem (Prg_MoveUpDown_t UpDown);
static bool Prg_ExchangeSubtrees (int NumItemTop,int NumItemBottom);
2020-03-02 14:41:52 +01:00
static int Prg_GetPrevBrother (int NumItem);
static int Prg_GetNextBrother (int NumItem);
2020-03-05 01:37:48 +01:00
static void Prg_MoveLeftRightPrgItem (Prg_MoveLeftRight_t LeftRight);
static void Prg_SetSubtreeEmpty (struct Subtree *Subtree);
2020-03-05 02:03:32 +01:00
static void Prg_SetSubtreeOnlyItem (unsigned Index,struct Subtree *Subtree);
2020-03-05 01:37:48 +01:00
static void Prg_SetSubtreeWithAllChildren (unsigned NumItem,struct Subtree *Subtree);
static unsigned Prg_GetLastChild (int NumItem);
2020-02-26 00:26:07 +01:00
2020-03-04 00:33:48 +01:00
static void Prg_ShowFormToCreateItem (long ParentItmCod);
static void Prg_ShowFormToChangeItem (long ItmCod);
static void Prg_ShowFormItem (const struct ProgramItem *Item,
const Dat_SetHMS SetHMS[Dat_NUM_START_END_TIME],
const char *Txt);
static void Prg_InsertItem (const struct ProgramItem *ParentItem,
struct ProgramItem *Item,const char *Txt);
static long Prg_InsertItemIntoDB (struct ProgramItem *Item,const char *Txt);
static void Prg_UpdateItem (struct ProgramItem *Item,const char *Txt);
2020-02-20 23:48:48 +01:00
/*****************************************************************************/
/************************ List all the program items *************************/
/*****************************************************************************/
2020-03-04 00:33:48 +01:00
void Prg_ShowCourseProgram (void)
{
2020-03-05 01:37:48 +01:00
struct Subtree ToHighlight;
/***** Get list of program items *****/
Prg_GetListPrgItems ();
/***** Show course program without highlighting any item *****/
Prg_SetSubtreeEmpty (&ToHighlight);
Prg_ShowCourseProgramHighlightingItem (&ToHighlight);
/***** Free list of program items *****/
Prg_FreeListItems ();
2020-03-04 00:33:48 +01:00
}
2020-03-05 01:37:48 +01:00
static void Prg_ShowCourseProgramHighlightingItem (const struct Subtree *ToHighlight)
2020-02-20 23:48:48 +01:00
{
/***** Show all the program items *****/
2020-03-05 01:37:48 +01:00
Prg_ShowAllItems (Prg_DONT_PUT_FORM_ITEM,ToHighlight,-1L,-1L,0);
2020-02-20 23:48:48 +01:00
}
/*****************************************************************************/
/*********************** Show all the program items **************************/
/*****************************************************************************/
2020-03-03 12:00:36 +01:00
static void Prg_ShowAllItems (Prg_CreateOrChangeItem_t CreateOrChangeItem,
2020-03-05 01:37:48 +01:00
const struct Subtree *ToHighlight,
2020-03-03 19:28:38 +01:00
long ParentItmCod,long ItmCodBeforeForm,unsigned FormLevel)
2020-02-20 23:48:48 +01:00
{
extern const char *Hlp_COURSE_Program;
extern const char *Txt_Course_program;
2020-02-25 00:22:05 +01:00
unsigned NumItem;
2020-03-03 14:35:45 +01:00
struct ProgramItem Item;
2020-02-20 23:48:48 +01:00
2020-03-05 01:37:48 +01:00
/***** Create numbers *****/
2020-03-03 14:35:45 +01:00
Prg_MaxLevel = Prg_GetMaxItemLevel ();
Prg_CreateNumbers (Prg_MaxLevel);
2020-02-20 23:48:48 +01:00
/***** Begin box *****/
2020-02-26 00:26:07 +01:00
Box_BoxBegin ("100%",Txt_Course_program,Prg_PutIconsListItems,
2020-02-20 23:48:48 +01:00
Hlp_COURSE_Program,Box_NOT_CLOSABLE);
2020-03-04 01:26:49 +01:00
/***** Table head *****/
HTM_TABLE_BeginWideMarginPadding (2);
/***** Write all the program items *****/
for (NumItem = 0;
NumItem < Gbl.Prg.Num;
NumItem++)
2020-02-20 23:48:48 +01:00
{
2020-03-04 01:26:49 +01:00
/* Get data of this program item */
Item.Hierarchy.ItmCod = Gbl.Prg.LstItems[NumItem].ItmCod;
Prg_GetDataOfItemByCod (&Item);
/* Show item */
2020-03-05 01:37:48 +01:00
Prg_ShowOneItem (NumItem,&Item,ToHighlight,
2020-03-04 01:26:49 +01:00
false); // Not print view
/* Show form to create/change item */
if (ItmCodBeforeForm == Item.Hierarchy.ItmCod)
switch (CreateOrChangeItem)
{
case Prg_DONT_PUT_FORM_ITEM:
break;
case Prg_PUT_FORM_CREATE_ITEM:
Prg_ShowItemForm (Prg_PUT_FORM_CREATE_ITEM,ParentItmCod,FormLevel);
break;
case Prg_PUT_FORM_CHANGE_ITEM:
Prg_ShowItemForm (Prg_PUT_FORM_CHANGE_ITEM,ItmCodBeforeForm,FormLevel);
break;
}
2020-02-20 23:48:48 +01:00
2020-03-04 01:26:49 +01:00
Gbl.RowEvenOdd = 1 - Gbl.RowEvenOdd;
2020-02-20 23:48:48 +01:00
}
2020-03-04 01:26:49 +01:00
/***** Create item at the end? *****/
if (ItmCodBeforeForm <= 0 && CreateOrChangeItem == Prg_PUT_FORM_CREATE_ITEM)
Prg_ShowItemForm (Prg_PUT_FORM_CREATE_ITEM,-1L,1);
/***** End table *****/
HTM_TABLE_End ();
2020-02-20 23:48:48 +01:00
/***** Button to create a new program item *****/
2020-02-26 00:26:07 +01:00
if (Prg_CheckIfICanCreateItems ())
Prg_PutButtonToCreateNewItem ();
2020-02-20 23:48:48 +01:00
/***** End box *****/
Box_BoxEnd ();
2020-03-05 01:37:48 +01:00
/***** Free numbers *****/
2020-03-03 14:35:45 +01:00
Prg_FreeNumbers ();
2020-02-20 23:48:48 +01:00
}
/*****************************************************************************/
/******************* Check if I can create program items *********************/
/*****************************************************************************/
2020-02-26 00:26:07 +01:00
static bool Prg_CheckIfICanCreateItems (void)
2020-02-20 23:48:48 +01:00
{
return (bool) (Gbl.Usrs.Me.Role.Logged == Rol_TCH ||
Gbl.Usrs.Me.Role.Logged == Rol_SYS_ADM);
}
/*****************************************************************************/
/************** Put contextual icons in list of program items ****************/
/*****************************************************************************/
2020-02-26 00:26:07 +01:00
static void Prg_PutIconsListItems (void)
2020-02-20 23:48:48 +01:00
{
/***** Put icon to create a new program item *****/
2020-02-26 00:26:07 +01:00
if (Prg_CheckIfICanCreateItems ())
Prg_PutIconToCreateNewItem ();
2020-02-20 23:48:48 +01:00
/***** Put icon to show a figure *****/
2020-02-24 22:47:34 +01:00
Gbl.Figures.FigureType = Fig_COURSE_PROGRAMS;
2020-02-20 23:48:48 +01:00
Fig_PutIconToShowFigure ();
}
/*****************************************************************************/
/****************** Put icon to create a new program item ********************/
/*****************************************************************************/
2020-02-26 00:26:07 +01:00
static void Prg_PutIconToCreateNewItem (void)
2020-02-20 23:48:48 +01:00
{
extern const char *Txt_New_item;
/***** Put form to create a new program item *****/
2020-02-26 00:26:07 +01:00
Prg_SetCurrentItmCod (-1L);
2020-02-26 19:35:33 +01:00
Ico_PutContextualIconToAdd (ActFrmNewPrgItm,NULL,Prg_PutParams,
2020-02-20 23:48:48 +01:00
Txt_New_item);
}
/*****************************************************************************/
/***************** Put button to create a new program item *******************/
/*****************************************************************************/
2020-02-26 00:26:07 +01:00
static void Prg_PutButtonToCreateNewItem (void)
2020-02-20 23:48:48 +01:00
{
extern const char *Txt_New_item;
2020-02-26 00:26:07 +01:00
Prg_SetCurrentItmCod (-1L);
2020-02-26 19:35:33 +01:00
Frm_StartForm (ActFrmNewPrgItm);
2020-02-20 23:48:48 +01:00
Prg_PutParams ();
Btn_PutConfirmButton (Txt_New_item);
Frm_EndForm ();
}
/*****************************************************************************/
/************************** Show one program item ****************************/
/*****************************************************************************/
2020-02-27 23:44:21 +01:00
#define Prg_WIDTH_NUM_ITEM 20
2020-03-04 00:33:48 +01:00
static void Prg_ShowOneItem (unsigned NumItem,const struct ProgramItem *Item,
2020-03-04 15:37:05 +01:00
const struct Subtree *ToHighlight,bool PrintView)
2020-02-20 23:48:48 +01:00
{
static unsigned UniqueId = 0;
2020-03-04 15:37:05 +01:00
static bool FirstTBodyOpen = false;
2020-02-20 23:48:48 +01:00
char *Id;
2020-02-27 23:44:21 +01:00
unsigned ColSpan;
2020-03-03 17:36:24 +01:00
unsigned NumCol;
2020-02-20 23:48:48 +01:00
Dat_StartEndTime_t StartEndTime;
char Txt[Cns_MAX_BYTES_TEXT + 1];
2020-03-03 14:35:45 +01:00
/***** Increase number of item *****/
Prg_IncreaseNumItem (Item->Hierarchy.Level);
2020-02-20 23:48:48 +01:00
2020-03-04 15:37:05 +01:00
/***** In general, the table is divided into three bodys:
1. Rows before highlighted: <tbody></tbody>
2. Rows highlighted: <tbody id="highlighted_item"></tbody>
3. Rows after highlighted: <tbody></tbody> */
if (Item->Hierarchy.Index == ToHighlight->Begin) // Begin of the highlighted subtree
{
if (FirstTBodyOpen)
{
HTM_TBODY_End (); // 1st tbody end
FirstTBodyOpen = false;
}
HTM_TBODY_Begin ("id=\"highlighted_item\" class=\"PRG_HIGHLIGHTED_ITEM\""); // Highlighted tbody start
}
else if (NumItem == 0) // First item
{
HTM_TBODY_Begin (NULL); // 1st tbody start
FirstTBodyOpen = true;
}
2020-02-27 23:44:21 +01:00
/***** Start row *****/
2020-03-04 15:37:05 +01:00
HTM_TR_Begin (NULL);
2020-02-20 23:48:48 +01:00
2020-02-27 23:44:21 +01:00
/***** Forms to remove/edit this program item *****/
2020-02-26 22:05:20 +01:00
if (!PrintView)
2020-02-20 23:48:48 +01:00
{
2020-02-27 23:44:21 +01:00
HTM_TD_Begin ("class=\"PRG_COL1 LT COLOR%u\"",Gbl.RowEvenOdd);
2020-03-04 01:26:49 +01:00
Prg_PutFormsToRemEditOnePrgItem (NumItem,Item);
2020-02-26 22:05:20 +01:00
HTM_TD_End ();
2020-02-20 23:48:48 +01:00
}
2020-02-27 23:44:21 +01:00
/***** Indent depending on the level *****/
2020-03-03 17:36:24 +01:00
for (NumCol = 1;
NumCol < Item->Hierarchy.Level;
NumCol++)
2020-02-27 23:44:21 +01:00
{
2020-03-03 17:36:24 +01:00
HTM_TD_Begin ("class=\"COLOR%u\"",Gbl.RowEvenOdd);
2020-02-27 23:44:21 +01:00
HTM_TD_End ();
}
/***** Item number *****/
HTM_TD_Begin ("class=\"%s RT COLOR%u\" style=\"width:%dpx;\"",
2020-03-03 14:35:45 +01:00
Item->Hierarchy.Hidden ? "ASG_TITLE_LIGHT" :
"ASG_TITLE",
2020-02-27 23:44:21 +01:00
Gbl.RowEvenOdd,
2020-03-03 14:35:45 +01:00
Item->Hierarchy.Level * Prg_WIDTH_NUM_ITEM);
Prg_WriteNumItem (Item->Hierarchy.Level);
2020-02-27 23:44:21 +01:00
HTM_TD_End ();
2020-03-03 09:23:15 +01:00
/***** Title and text *****/
/* Begin title and text */
2020-03-03 17:36:24 +01:00
ColSpan = (Prg_MaxLevel + 2) - Item->Hierarchy.Level;
2020-02-24 23:51:48 +01:00
if (PrintView)
2020-02-27 23:44:21 +01:00
HTM_TD_Begin ("colspan=\"%u\" class=\"LT\"",
ColSpan);
2020-02-24 23:51:48 +01:00
else
2020-02-27 23:44:21 +01:00
HTM_TD_Begin ("colspan=\"%u\" class=\"LT COLOR%u\"",
ColSpan,Gbl.RowEvenOdd);
/* Title */
HTM_DIV_Begin ("class=\"%s\"",
2020-03-03 14:35:45 +01:00
Item->Hierarchy.Hidden ? "ASG_TITLE_LIGHT" :
"ASG_TITLE");
HTM_Txt (Item->Title);
2020-02-27 23:44:21 +01:00
HTM_DIV_End ();
/* Text */
2020-03-03 14:35:45 +01:00
Prg_GetPrgItemTxtFromDB (Item->Hierarchy.ItmCod,Txt);
2020-02-27 23:44:21 +01:00
Str_ChangeFormat (Str_FROM_HTML,Str_TO_RIGOROUS_HTML,
Txt,Cns_MAX_BYTES_TEXT,false); // Convert from HTML to recpectful HTML
Str_InsertLinks (Txt,Cns_MAX_BYTES_TEXT,60); // Insert links
2020-03-03 14:35:45 +01:00
HTM_DIV_Begin ("class=\"PAR %s\"",Item->Hierarchy.Hidden ? "DAT_LIGHT" :
"DAT");
2020-02-27 23:44:21 +01:00
HTM_Txt (Txt);
HTM_DIV_End ();
2020-03-03 09:23:15 +01:00
/* End title and text */
2020-02-24 23:51:48 +01:00
HTM_TD_End ();
2020-02-27 23:44:21 +01:00
/***** Start/end date/time *****/
2020-02-20 23:48:48 +01:00
UniqueId++;
for (StartEndTime = (Dat_StartEndTime_t) 0;
StartEndTime <= (Dat_StartEndTime_t) (Dat_NUM_START_END_TIME - 1);
StartEndTime++)
{
if (asprintf (&Id,"scd_date_%u_%u",(unsigned) StartEndTime,UniqueId) < 0)
Lay_NotEnoughMemoryExit ();
if (PrintView)
2020-02-27 23:44:21 +01:00
HTM_TD_Begin ("id=\"%s\" class=\"%s LT\"",
2020-02-20 23:48:48 +01:00
Id,
2020-03-03 14:35:45 +01:00
Item->Hierarchy.Hidden ? (Item->Open ? "DATE_GREEN_LIGHT" :
"DATE_RED_LIGHT") :
(Item->Open ? "DATE_GREEN" :
"DATE_RED"));
2020-02-20 23:48:48 +01:00
else
2020-02-27 23:44:21 +01:00
HTM_TD_Begin ("id=\"%s\" class=\"%s LT COLOR%u\"",
2020-02-20 23:48:48 +01:00
Id,
2020-03-03 14:35:45 +01:00
Item->Hierarchy.Hidden ? (Item->Open ? "DATE_GREEN_LIGHT" :
"DATE_RED_LIGHT") :
(Item->Open ? "DATE_GREEN" :
"DATE_RED"),
2020-02-20 23:48:48 +01:00
Gbl.RowEvenOdd);
2020-03-03 14:35:45 +01:00
Dat_WriteLocalDateHMSFromUTC (Id,Item->TimeUTC[StartEndTime],
2020-02-20 23:48:48 +01:00
Gbl.Prefs.DateFormat,Dat_SEPARATOR_BREAK,
true,true,true,0x7);
HTM_TD_End ();
free (Id);
}
2020-03-03 12:00:36 +01:00
/***** End row *****/
2020-02-20 23:48:48 +01:00
HTM_TR_End ();
2020-03-04 15:37:05 +01:00
/***** End tbodies *****/
if (Item->Hierarchy.Index == ToHighlight->End) // End of the highlighted subtree
{
HTM_TBODY_End (); // Highlighted tbody end
if (NumItem < Gbl.Prg.Num - 1) // Not the last item
HTM_TBODY_Begin (NULL); // 3rd tbody begin
}
else if (NumItem == Gbl.Prg.Num - 1) // Last item
HTM_TBODY_End (); // 3rd tbody end
2020-03-03 14:35:45 +01:00
}
/*****************************************************************************/
/**************************** Show item form *********************************/
/*****************************************************************************/
2020-03-03 12:00:36 +01:00
2020-03-03 19:28:38 +01:00
static void Prg_ShowItemForm (Prg_CreateOrChangeItem_t CreateOrChangeItem,
long ParamItmCod,unsigned FormLevel)
2020-03-03 14:35:45 +01:00
{
unsigned ColSpan;
2020-03-03 17:36:24 +01:00
unsigned NumCol;
2020-03-03 12:00:36 +01:00
2020-03-03 14:35:45 +01:00
/***** Trivial check *****/
if (CreateOrChangeItem == Prg_DONT_PUT_FORM_ITEM)
return;
2020-03-03 12:00:36 +01:00
2020-03-03 14:35:45 +01:00
/***** Change color row? *****/
if (CreateOrChangeItem == Prg_PUT_FORM_CREATE_ITEM)
Gbl.RowEvenOdd = 1 - Gbl.RowEvenOdd;
2020-03-03 12:00:36 +01:00
2020-03-03 14:35:45 +01:00
/***** Start row *****/
HTM_TR_Begin (NULL);
/***** Column under icons *****/
HTM_TD_Begin ("class=\"PRG_COL1 LT COLOR%u\"",Gbl.RowEvenOdd);
HTM_TD_End ();
/***** Indent depending on the level *****/
2020-03-03 17:36:24 +01:00
for (NumCol = 1;
NumCol < FormLevel;
NumCol++)
2020-03-03 14:35:45 +01:00
{
2020-03-03 17:36:24 +01:00
HTM_TD_Begin ("class=\"COLOR%u\"",Gbl.RowEvenOdd);
2020-03-03 14:35:45 +01:00
HTM_TD_End ();
2020-03-03 12:00:36 +01:00
}
2020-03-03 17:36:24 +01:00
/***** Item number *****/
HTM_TD_Begin ("class=\"ASG_TITLE RT COLOR%u\" style=\"width:%dpx;\"",
Gbl.RowEvenOdd,
FormLevel * Prg_WIDTH_NUM_ITEM);
if (CreateOrChangeItem == Prg_PUT_FORM_CREATE_ITEM)
Prg_WriteNumNewItem (FormLevel);
else
Prg_WriteNumItem (FormLevel);
HTM_TD_End ();
2020-03-03 14:35:45 +01:00
/***** Show form to create new item as child *****/
2020-03-03 17:36:24 +01:00
ColSpan = (Prg_MaxLevel + 4) - FormLevel;
2020-03-03 14:35:45 +01:00
HTM_TD_Begin ("colspan=\"%u\" class=\"LT COLOR%u\"",
ColSpan,Gbl.RowEvenOdd);
HTM_ARTICLE_Begin ("item_form");
if (CreateOrChangeItem == Prg_PUT_FORM_CREATE_ITEM)
2020-03-04 00:33:48 +01:00
Prg_ShowFormToCreateItem (ParamItmCod);
2020-03-03 14:35:45 +01:00
else
2020-03-04 00:33:48 +01:00
Prg_ShowFormToChangeItem (ParamItmCod);
2020-03-03 14:35:45 +01:00
HTM_ARTICLE_End ();
HTM_TD_End ();
2020-02-20 23:48:48 +01:00
2020-03-03 14:35:45 +01:00
/***** End row *****/
HTM_TR_End ();
2020-02-20 23:48:48 +01:00
}
2020-03-05 01:37:48 +01:00
/*****************************************************************************/
/******************* Get maximum level in a course program *******************/
/*****************************************************************************/
// Return 0 if no items
static unsigned Prg_GetMaxItemLevel (void)
{
unsigned NumItem;
unsigned MaxLevel = 0;
/***** Compute maximum level of all program items *****/
for (NumItem = 0;
NumItem < Gbl.Prg.Num;
NumItem++)
if (Gbl.Prg.LstItems[NumItem].Level > MaxLevel)
MaxLevel = Gbl.Prg.LstItems[NumItem].Level;
return MaxLevel;
}
2020-02-29 13:52:29 +01:00
/*****************************************************************************/
/********************* Allocate memory for item numbers **********************/
/*****************************************************************************/
static void Prg_CreateNumbers (unsigned MaxLevel)
{
2020-03-03 14:35:45 +01:00
if (MaxLevel)
{
/***** Allocate memory for item numbers and initialize to 0 *****/
2020-03-03 17:36:24 +01:00
/*
Example: 2.5.2.1
MaxLevel = 4
Level Number
----- ------
0 <--- Not used
1 2
2 5
3 2
4 1
5 0 <--- Used to create a new item
*/
if ((Prg_NumItem = (unsigned *) calloc (1 + MaxLevel + 1,sizeof (unsigned))) == NULL)
2020-03-03 14:35:45 +01:00
Lay_NotEnoughMemoryExit ();
}
else
Prg_NumItem = NULL;
2020-02-29 13:52:29 +01:00
}
/*****************************************************************************/
/*********************** Free memory for item numbers ************************/
/*****************************************************************************/
static void Prg_FreeNumbers (void)
{
2020-03-03 14:35:45 +01:00
if (Prg_MaxLevel && Prg_NumItem)
2020-02-29 13:52:29 +01:00
{
2020-03-03 14:35:45 +01:00
/***** Free allocated memory for item numbers *****/
2020-02-29 13:52:29 +01:00
free (Prg_NumItem);
Prg_NumItem = NULL;
}
}
2020-03-02 14:41:52 +01:00
/*****************************************************************************/
/**************************** Increase number of item ************************/
/*****************************************************************************/
static void Prg_IncreaseNumItem (unsigned Level)
{
/***** Increase number for this level *****/
Prg_NumItem[Level]++;
/***** Reset number for next level (children) *****/
Prg_NumItem[Level + 1] = 0;
}
/*****************************************************************************/
/*********************** Get number of item in a level ***********************/
/*****************************************************************************/
static unsigned Prg_GetNumItem (unsigned Level)
{
if (Prg_NumItem)
return Prg_NumItem[Level];
return 0;
}
2020-02-27 23:44:21 +01:00
/*****************************************************************************/
/******************** Write number of item in legal style ********************/
/*****************************************************************************/
static void Prg_WriteNumItem (unsigned Level)
{
2020-02-29 13:52:29 +01:00
unsigned i;
2020-02-27 23:44:21 +01:00
2020-03-02 14:41:52 +01:00
/***** Write number in legal style *****/
2020-03-03 17:36:24 +01:00
for (i = 1;
2020-02-29 13:52:29 +01:00
i <= Level;
i++)
2020-02-27 23:44:21 +01:00
{
2020-03-03 17:36:24 +01:00
if (i > 1)
HTM_Txt (".");
2020-03-02 14:41:52 +01:00
HTM_Unsigned (Prg_GetNumItem (i));
2020-02-27 23:44:21 +01:00
}
}
2020-03-03 17:36:24 +01:00
static void Prg_WriteNumNewItem (unsigned Level)
{
unsigned i;
/***** Write number in legal style *****/
for (i = 1;
i <= Level;
i++)
{
if (i > 1)
HTM_Txt (".");
if (i < Level)
HTM_Unsigned (Prg_GetNumItem (i));
else
HTM_Unsigned (Prg_GetNumItem (i) + 1);
}
}
2020-02-20 23:48:48 +01:00
/*****************************************************************************/
/**************** Put a link (form) to edit one program item *****************/
/*****************************************************************************/
2020-03-03 14:35:45 +01:00
static void Prg_PutFormsToRemEditOnePrgItem (unsigned NumItem,
2020-03-04 01:26:49 +01:00
const struct ProgramItem *Item)
2020-02-20 23:48:48 +01:00
{
2020-03-03 00:56:22 +01:00
extern const char *Txt_New_item;
2020-02-25 00:22:05 +01:00
extern const char *Txt_Move_up_X;
extern const char *Txt_Move_down_X;
2020-02-27 00:19:55 +01:00
extern const char *Txt_Increase_level_of_X;
extern const char *Txt_Decrease_level_of_X;
2020-02-25 00:22:05 +01:00
extern const char *Txt_Movement_not_allowed;
char StrItemIndex[Cns_MAX_DECIMAL_DIGITS_UINT + 1];
2020-03-03 14:35:45 +01:00
Prg_SetCurrentItmCod (Item->Hierarchy.ItmCod); // Used as parameter in contextual links
2020-02-20 23:48:48 +01:00
2020-02-25 00:22:05 +01:00
/***** Initialize item index string *****/
snprintf (StrItemIndex,sizeof (StrItemIndex),
"%u",
2020-03-03 14:35:45 +01:00
Item->Hierarchy.Index);
2020-02-25 00:22:05 +01:00
2020-02-20 23:48:48 +01:00
switch (Gbl.Usrs.Me.Role.Logged)
{
case Rol_TCH:
case Rol_SYS_ADM:
/***** Put form to remove program item *****/
2020-02-26 19:35:33 +01:00
Ico_PutContextualIconToRemove (ActReqRemPrgItm,Prg_PutParams);
2020-02-20 23:48:48 +01:00
/***** Put form to hide/show program item *****/
2020-03-03 14:35:45 +01:00
if (Item->Hierarchy.Hidden)
2020-03-04 00:33:48 +01:00
Ico_PutContextualIconToUnhide (ActShoPrgItm,"highlighted_item",Prg_PutParams);
2020-02-25 00:22:05 +01:00
else
2020-03-04 00:33:48 +01:00
Ico_PutContextualIconToHide (ActHidPrgItm,"highlighted_item",Prg_PutParams);
2020-02-25 00:22:05 +01:00
2020-02-26 22:05:20 +01:00
/***** Put form to edit program item *****/
2020-03-03 14:35:45 +01:00
Ico_PutContextualIconToEdit (ActFrmChgPrgItm,"item_form",Prg_PutParams);
2020-03-03 00:56:22 +01:00
/***** Put form to add a new child item inside this item *****/
2020-03-03 14:35:45 +01:00
Ico_PutContextualIconToAdd (ActFrmNewPrgItm,"item_form",Prg_PutParams,Txt_New_item);
2020-02-26 22:05:20 +01:00
2020-02-27 00:19:55 +01:00
HTM_BR ();
2020-02-26 00:26:07 +01:00
/***** Put icon to move up the item *****/
2020-03-02 14:41:52 +01:00
if (Prg_CheckIfMoveUpIsAllowed (NumItem))
2020-02-25 00:22:05 +01:00
{
2020-03-04 00:33:48 +01:00
Lay_PutContextualLinkOnlyIcon (ActUp_PrgItm,"highlighted_item",Prg_PutParams,
2020-02-25 00:22:05 +01:00
"arrow-up.svg",
Str_BuildStringStr (Txt_Move_up_X,
StrItemIndex));
Str_FreeString ();
}
2020-02-20 23:48:48 +01:00
else
2020-02-25 00:22:05 +01:00
Ico_PutIconOff ("arrow-up.svg",Txt_Movement_not_allowed);
2020-02-26 00:26:07 +01:00
/***** Put icon to move down the item *****/
2020-03-02 14:41:52 +01:00
if (Prg_CheckIfMoveDownIsAllowed (NumItem))
2020-02-25 00:22:05 +01:00
{
2020-03-04 00:33:48 +01:00
Lay_PutContextualLinkOnlyIcon (ActDwnPrgItm,"highlighted_item",Prg_PutParams,
2020-02-25 00:22:05 +01:00
"arrow-down.svg",
Str_BuildStringStr (Txt_Move_down_X,
StrItemIndex));
Str_FreeString ();
}
else
Ico_PutIconOff ("arrow-down.svg",Txt_Movement_not_allowed);
2020-02-27 00:19:55 +01:00
2020-02-29 12:11:07 +01:00
/***** Icon to move left item (increase level) *****/
2020-03-02 14:41:52 +01:00
if (Prg_CheckIfMoveLeftIsAllowed (NumItem))
2020-02-27 00:19:55 +01:00
{
2020-03-04 00:33:48 +01:00
Lay_PutContextualLinkOnlyIcon (ActLftPrgItm,"highlighted_item",Prg_PutParams,
2020-02-27 00:19:55 +01:00
"arrow-left.svg",
Str_BuildStringStr (Txt_Increase_level_of_X,
StrItemIndex));
Str_FreeString ();
}
else
Ico_PutIconOff ("arrow-left.svg",Txt_Movement_not_allowed);
2020-02-29 12:11:07 +01:00
/***** Icon to move right item (indent, decrease level) *****/
2020-03-02 14:41:52 +01:00
if (Prg_CheckIfMoveRightIsAllowed (NumItem))
2020-02-27 00:19:55 +01:00
{
2020-03-04 00:33:48 +01:00
Lay_PutContextualLinkOnlyIcon (ActRgtPrgItm,"highlighted_item",Prg_PutParams,
2020-02-27 00:19:55 +01:00
"arrow-right.svg",
Str_BuildStringStr (Txt_Decrease_level_of_X,
StrItemIndex));
Str_FreeString ();
}
else
Ico_PutIconOff ("arrow-right.svg",Txt_Movement_not_allowed);
2020-02-25 00:22:05 +01:00
break;
2020-02-20 23:48:48 +01:00
case Rol_STD:
case Rol_NET:
break;
default:
break;
}
}
2020-03-02 14:41:52 +01:00
/*****************************************************************************/
/*********************** Check if item can be moved up ***********************/
/*****************************************************************************/
static bool Prg_CheckIfMoveUpIsAllowed (unsigned NumItem)
{
/***** Trivial check: if item is the first one, move up is not allowed *****/
if (NumItem == 0)
return false;
/***** Move up is allowed if the item has brothers before it *****/
// NumItem >= 2
return Gbl.Prg.LstItems[NumItem - 1].Level >=
Gbl.Prg.LstItems[NumItem ].Level;
}
/*****************************************************************************/
/********************** Check if item can be moved down **********************/
/*****************************************************************************/
static bool Prg_CheckIfMoveDownIsAllowed (unsigned NumItem)
{
unsigned i;
unsigned Level;
/***** Trivial check: if item is the last one, move up is not allowed *****/
if (NumItem >= Gbl.Prg.Num - 1)
return false;
/***** Move down is allowed if the item has brothers after it *****/
// NumItem + 1 < Gbl.Prg.Num
Level = Gbl.Prg.LstItems[NumItem].Level;
for (i = NumItem + 1;
i < Gbl.Prg.Num;
i++)
{
if (Gbl.Prg.LstItems[i].Level == Level)
return true; // Next brother found
if (Gbl.Prg.LstItems[i].Level < Level)
return false; // Next lower level found ==> there are no more brothers
}
return false; // End reached ==> there are no more brothers
}
/*****************************************************************************/
/******************* Check if item can be moved to the left ******************/
/*****************************************************************************/
static bool Prg_CheckIfMoveLeftIsAllowed (unsigned NumItem)
{
/***** Move left is allowed if the item has parent *****/
return Gbl.Prg.LstItems[NumItem].Level > 1;
}
/*****************************************************************************/
/****************** Check if item can be moved to the right ******************/
/*****************************************************************************/
static bool Prg_CheckIfMoveRightIsAllowed (unsigned NumItem)
{
/***** If item is the first, move right is not allowed *****/
if (NumItem == 0)
return false;
/***** Move right is allowed if the item has brothers before it *****/
// NumItem >= 2
return Gbl.Prg.LstItems[NumItem - 1].Level >=
Gbl.Prg.LstItems[NumItem ].Level;
}
2020-02-26 00:26:07 +01:00
/*****************************************************************************/
/**************** Access to variables used to pass parameter *****************/
/*****************************************************************************/
static void Prg_SetCurrentItmCod (long ItmCod)
{
Prg_CurrentItmCod = ItmCod;
}
static long Prg_GetCurrentItmCod (void)
{
return Prg_CurrentItmCod;
}
2020-02-20 23:48:48 +01:00
/*****************************************************************************/
/******************** Params used to edit a program item *********************/
/*****************************************************************************/
static void Prg_PutParams (void)
{
2020-02-26 00:26:07 +01:00
long CurrentItmCod = Prg_GetCurrentItmCod ();
if (CurrentItmCod > 0)
Prg_PutParamItmCod (CurrentItmCod);
2020-02-20 23:48:48 +01:00
}
/*****************************************************************************/
/*********************** List all the program items **************************/
/*****************************************************************************/
2020-03-02 14:41:52 +01:00
static void Prg_GetListPrgItems (void)
2020-02-20 23:48:48 +01:00
{
static const char *HiddenSubQuery[Rol_NUM_ROLES] =
{
[Rol_UNK ] = " AND Hidden='N'",
[Rol_GST ] = " AND Hidden='N'",
[Rol_USR ] = " AND Hidden='N'",
[Rol_STD ] = " AND Hidden='N'",
[Rol_NET ] = " AND Hidden='N'",
[Rol_TCH ] = "",
[Rol_DEG_ADM] = " AND Hidden='N'",
[Rol_CTR_ADM] = " AND Hidden='N'",
[Rol_INS_ADM] = " AND Hidden='N'",
[Rol_SYS_ADM] = "",
};
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned long NumRows;
2020-02-26 19:35:33 +01:00
unsigned NumItem;
2020-02-20 23:48:48 +01:00
if (Gbl.Prg.LstIsRead)
2020-02-26 00:26:07 +01:00
Prg_FreeListItems ();
2020-02-20 23:48:48 +01:00
/***** Get list of program items from database *****/
2020-03-03 09:23:15 +01:00
NumRows = DB_QuerySELECT (&mysql_res,"can not get program items",
"SELECT ItmCod," // row[0]
"ItmInd," // row[1]
"Level," // row[2]
"Hidden" // row[3]
" FROM prg_items"
" WHERE CrsCod=%ld%s"
" ORDER BY ItmInd",
Gbl.Hierarchy.Crs.CrsCod,
HiddenSubQuery[Gbl.Usrs.Me.Role.Logged]);
2020-02-20 23:48:48 +01:00
2020-02-26 00:26:07 +01:00
if (NumRows) // Items found...
2020-02-20 23:48:48 +01:00
{
Gbl.Prg.Num = (unsigned) NumRows;
/***** Create list of program items *****/
2020-03-02 14:41:52 +01:00
if ((Gbl.Prg.LstItems =
(struct ProgramItemHierarchy *) calloc (NumRows,
sizeof (struct ProgramItemHierarchy))) == NULL)
2020-02-20 23:48:48 +01:00
Lay_NotEnoughMemoryExit ();
/***** Get the program items codes *****/
2020-02-26 19:35:33 +01:00
for (NumItem = 0;
NumItem < Gbl.Prg.Num;
NumItem++)
2020-02-20 23:48:48 +01:00
{
2020-03-02 14:41:52 +01:00
/* Get row */
2020-02-20 23:48:48 +01:00
row = mysql_fetch_row (mysql_res);
2020-03-02 14:41:52 +01:00
/* Get code of the program item (row[0]) */
if ((Gbl.Prg.LstItems[NumItem].ItmCod = Str_ConvertStrCodToLongCod (row[0])) < 0)
2020-02-20 23:48:48 +01:00
Lay_ShowErrorAndExit ("Error: wrong program item code.");
2020-03-02 14:41:52 +01:00
/* Get index of the program item (row[1]) */
Gbl.Prg.LstItems[NumItem].Index = Str_ConvertStrToUnsigned (row[1]);
/* Get level of the program item (row[2]) */
Gbl.Prg.LstItems[NumItem].Level = Str_ConvertStrToUnsigned (row[2]);
/* Get whether the program item is hidden or not (row[3]) */
Gbl.Prg.LstItems[NumItem].Hidden = (row[3][0] == 'Y');
2020-02-20 23:48:48 +01:00
}
}
else
Gbl.Prg.Num = 0;
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
Gbl.Prg.LstIsRead = true;
}
/*****************************************************************************/
/****************** Get program item data using its code *********************/
/*****************************************************************************/
2020-03-03 00:56:22 +01:00
static void Prg_GetDataOfItemByCod (struct ProgramItem *Item)
2020-02-20 23:48:48 +01:00
{
MYSQL_RES *mysql_res;
unsigned long NumRows;
2020-03-02 14:41:52 +01:00
if (Item->Hierarchy.ItmCod > 0)
2020-02-20 23:48:48 +01:00
{
/***** Build query *****/
NumRows = DB_QuerySELECT (&mysql_res,"can not get program item data",
2020-02-26 00:26:07 +01:00
"SELECT ItmCod," // row[0]
"ItmInd," // row[1]
2020-02-27 00:19:55 +01:00
"Level," // row[2]
"Hidden," // row[3]
"UsrCod," // row[4]
"UNIX_TIMESTAMP(StartTime)," // row[5]
"UNIX_TIMESTAMP(EndTime)," // row[6]
"NOW() BETWEEN StartTime AND EndTime," // row[7]
"Title" // row[8]
2020-02-20 23:48:48 +01:00
" FROM prg_items"
2020-02-27 23:44:21 +01:00
" WHERE ItmCod=%ld"
" AND CrsCod=%ld", // Extra check
2020-03-02 14:41:52 +01:00
Item->Hierarchy.ItmCod,Gbl.Hierarchy.Crs.CrsCod);
2020-02-20 23:48:48 +01:00
/***** Get data of program item *****/
2020-02-26 00:26:07 +01:00
Prg_GetDataOfItem (Item,&mysql_res,NumRows);
2020-02-20 23:48:48 +01:00
}
else
/***** Clear all program item data *****/
2020-02-26 00:26:07 +01:00
Prg_ResetItem (Item);
2020-02-20 23:48:48 +01:00
}
/*****************************************************************************/
/************************* Get program item data *****************************/
/*****************************************************************************/
2020-02-26 00:26:07 +01:00
static void Prg_GetDataOfItem (struct ProgramItem *Item,
MYSQL_RES **mysql_res,
unsigned long NumRows)
2020-02-20 23:48:48 +01:00
{
MYSQL_ROW row;
/***** Clear all program item data *****/
2020-02-26 00:26:07 +01:00
Prg_ResetItem (Item);
2020-02-20 23:48:48 +01:00
/***** Get data of program item from database *****/
2020-03-03 00:56:22 +01:00
if (NumRows) // Item found...
2020-02-20 23:48:48 +01:00
{
/* Get row */
row = mysql_fetch_row (*mysql_res);
2020-02-26 00:26:07 +01:00
/*
ItmCod row[0]
ItmInd row[1]
2020-02-27 00:19:55 +01:00
Level row[2]
Hidden row[3]
UsrCod row[4]
UNIX_TIMESTAMP(StartTime) row[5]
UNIX_TIMESTAMP(EndTime) row[6]
NOW() BETWEEN StartTime AND EndTime row[7]
Title row[8]
2020-02-26 00:26:07 +01:00
*/
2020-02-20 23:48:48 +01:00
/* Get code of the program item (row[0]) */
2020-03-02 14:41:52 +01:00
Item->Hierarchy.ItmCod = Str_ConvertStrCodToLongCod (row[0]);
2020-02-26 00:26:07 +01:00
/* Get index of the program item (row[1]) */
2020-03-02 14:41:52 +01:00
Item->Hierarchy.Index = Str_ConvertStrToUnsigned (row[1]);
2020-02-20 23:48:48 +01:00
2020-02-27 00:19:55 +01:00
/* Get level of the program item (row[2]) */
2020-03-02 14:41:52 +01:00
Item->Hierarchy.Level = Str_ConvertStrToUnsigned (row[2]);
2020-02-20 23:48:48 +01:00
2020-02-27 00:19:55 +01:00
/* Get whether the program item is hidden or not (row[3]) */
2020-03-03 14:35:45 +01:00
Item->Hierarchy.Hidden = (row[3][0] == 'Y');
2020-02-20 23:48:48 +01:00
2020-02-27 00:19:55 +01:00
/* Get author of the program item (row[4]) */
Item->UsrCod = Str_ConvertStrCodToLongCod (row[4]);
2020-02-20 23:48:48 +01:00
2020-02-27 00:19:55 +01:00
/* Get start date (row[5] holds the start UTC time) */
Item->TimeUTC[Dat_START_TIME] = Dat_GetUNIXTimeFromStr (row[5]);
2020-02-20 23:48:48 +01:00
2020-02-27 00:19:55 +01:00
/* Get end date (row[6] holds the end UTC time) */
Item->TimeUTC[Dat_END_TIME ] = Dat_GetUNIXTimeFromStr (row[6]);
2020-02-20 23:48:48 +01:00
2020-02-27 00:19:55 +01:00
/* Get whether the program item is open or closed (row(7)) */
Item->Open = (row[7][0] == '1');
/* Get the title of the program item (row[8]) */
Str_Copy (Item->Title,row[8],
2020-02-20 23:48:48 +01:00
Prg_MAX_BYTES_PROGRAM_ITEM_TITLE);
}
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (mysql_res);
}
/*****************************************************************************/
/************************ Clear all program item data ************************/
/*****************************************************************************/
2020-02-26 00:26:07 +01:00
static void Prg_ResetItem (struct ProgramItem *Item)
2020-02-20 23:48:48 +01:00
{
2020-03-02 14:41:52 +01:00
Item->Hierarchy.ItmCod = -1L;
Item->Hierarchy.Index = 0;
2020-03-03 00:56:22 +01:00
Item->Hierarchy.Level = 0;
2020-03-03 14:35:45 +01:00
Item->Hierarchy.Hidden = false;
2020-02-26 00:26:07 +01:00
Item->UsrCod = -1L;
Item->TimeUTC[Dat_START_TIME] =
Item->TimeUTC[Dat_END_TIME ] = (time_t) 0;
Item->Open = false;
Item->Title[0] = '\0';
2020-02-20 23:48:48 +01:00
}
/*****************************************************************************/
/************************ Free list of program items *************************/
/*****************************************************************************/
2020-03-03 00:56:22 +01:00
static void Prg_FreeListItems (void)
2020-02-20 23:48:48 +01:00
{
2020-03-02 14:41:52 +01:00
if (Gbl.Prg.LstIsRead && Gbl.Prg.LstItems)
2020-02-20 23:48:48 +01:00
{
/***** Free memory used by the list of program items *****/
2020-03-02 14:41:52 +01:00
free (Gbl.Prg.LstItems);
Gbl.Prg.LstItems = NULL;
2020-02-20 23:48:48 +01:00
Gbl.Prg.Num = 0;
Gbl.Prg.LstIsRead = false;
}
}
/*****************************************************************************/
/******************* Get program item text from database *********************/
/*****************************************************************************/
2020-02-26 00:26:07 +01:00
static void Prg_GetPrgItemTxtFromDB (long ItmCod,char Txt[Cns_MAX_BYTES_TEXT + 1])
2020-02-20 23:48:48 +01:00
{
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned long NumRows;
/***** Get text of program item from database *****/
NumRows = DB_QuerySELECT (&mysql_res,"can not get program item text",
"SELECT Txt FROM prg_items"
2020-02-27 23:44:21 +01:00
" WHERE ItmCod=%ld"
" AND CrsCod=%ld", // Extra check
2020-02-26 00:26:07 +01:00
ItmCod,Gbl.Hierarchy.Crs.CrsCod);
2020-02-20 23:48:48 +01:00
/***** The result of the query must have one row or none *****/
if (NumRows == 1)
{
/* Get info text */
row = mysql_fetch_row (mysql_res);
Str_Copy (Txt,row[0],
Cns_MAX_BYTES_TEXT);
}
else
Txt[0] = '\0';
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
if (NumRows > 1)
Lay_ShowErrorAndExit ("Error when getting program item text.");
}
/*****************************************************************************/
/**************** Write parameter with code of program item ******************/
/*****************************************************************************/
2020-02-26 00:26:07 +01:00
static void Prg_PutParamItmCod (long ItmCod)
2020-02-20 23:48:48 +01:00
{
2020-02-26 00:26:07 +01:00
Par_PutHiddenParamLong (NULL,"ItmCod",ItmCod);
2020-02-20 23:48:48 +01:00
}
/*****************************************************************************/
/***************** Get parameter with code of program item *******************/
/*****************************************************************************/
2020-03-02 14:41:52 +01:00
static long Prg_GetParamItmCod (void)
2020-02-20 23:48:48 +01:00
{
/***** Get code of program item *****/
2020-02-26 00:26:07 +01:00
return Par_GetParToLong ("ItmCod");
2020-02-20 23:48:48 +01:00
}
2020-03-02 14:41:52 +01:00
/*****************************************************************************/
/**************** Get number of item in list from item code ******************/
/*****************************************************************************/
2020-03-04 15:37:05 +01:00
static unsigned Prg_GetNumItemFromItmCod (long ItmCod)
2020-03-02 14:41:52 +01:00
{
unsigned NumItem;
2020-03-05 01:37:48 +01:00
/***** List of items must be filled *****/
if (!Gbl.Prg.LstIsRead || Gbl.Prg.LstItems == NULL)
Lay_ShowErrorAndExit ("Wrong list of items.");
/***** Find item code in list *****/
2020-03-02 14:41:52 +01:00
for (NumItem = 0;
NumItem < Gbl.Prg.Num;
NumItem++)
2020-03-05 01:37:48 +01:00
if (Gbl.Prg.LstItems[NumItem].ItmCod == ItmCod) // Found!
2020-03-02 14:41:52 +01:00
return NumItem;
2020-03-05 01:37:48 +01:00
/***** Not found *****/
2020-03-02 14:41:52 +01:00
Lay_ShowErrorAndExit ("Wrong item code.");
return 0; // Not reached
}
2020-02-20 23:48:48 +01:00
/*****************************************************************************/
/************* Ask for confirmation of removing a program item ***************/
/*****************************************************************************/
void Prg_ReqRemPrgItem (void)
{
extern const char *Txt_Do_you_really_want_to_remove_the_item_X;
extern const char *Txt_Remove_item;
2020-02-26 00:26:07 +01:00
struct ProgramItem Item;
2020-03-05 01:37:48 +01:00
struct Subtree ToHighlight;
2020-02-20 23:48:48 +01:00
2020-03-05 01:37:48 +01:00
/***** Get list of program items *****/
Prg_GetListPrgItems ();
2020-02-20 23:48:48 +01:00
/***** Get data of the program item from database *****/
2020-03-05 01:37:48 +01:00
Item.Hierarchy.ItmCod = Prg_GetParamItmCod ();
2020-02-26 00:26:07 +01:00
Prg_GetDataOfItemByCod (&Item);
2020-03-05 01:37:48 +01:00
if (Item.Hierarchy.ItmCod <= 0)
Lay_ShowErrorAndExit ("Wrong item code.");
2020-02-20 23:48:48 +01:00
/***** Show question and button to remove the program item *****/
2020-03-02 14:41:52 +01:00
Prg_SetCurrentItmCod (Item.Hierarchy.ItmCod);
2020-02-26 19:35:33 +01:00
Ale_ShowAlertAndButton (ActRemPrgItm,NULL,NULL,Prg_PutParams,
2020-02-20 23:48:48 +01:00
Btn_REMOVE_BUTTON,Txt_Remove_item,
Ale_QUESTION,Txt_Do_you_really_want_to_remove_the_item_X,
2020-02-26 00:26:07 +01:00
Item.Title);
2020-02-20 23:48:48 +01:00
2020-03-05 01:37:48 +01:00
/***** Show program items highlighting subtree *****/
2020-03-05 02:03:32 +01:00
Prg_SetSubtreeWithAllChildren (Prg_GetNumItemFromItmCod (Item.Hierarchy.ItmCod),
&ToHighlight);
2020-03-05 01:37:48 +01:00
Prg_ShowCourseProgramHighlightingItem (&ToHighlight);
/***** Free list of program items *****/
Prg_FreeListItems ();
2020-02-20 23:48:48 +01:00
}
/*****************************************************************************/
2020-03-03 19:00:49 +01:00
/******************* Remove a program item and its children ******************/
2020-02-20 23:48:48 +01:00
/*****************************************************************************/
void Prg_RemovePrgItem (void)
{
2020-02-26 00:26:07 +01:00
extern const char *Txt_Item_X_removed;
struct ProgramItem Item;
2020-03-04 15:37:05 +01:00
struct Subtree ToRemove;
2020-03-05 01:37:48 +01:00
struct Subtree ToHighlight;
2020-02-20 23:48:48 +01:00
2020-03-03 19:00:49 +01:00
/***** Get list of program items *****/
Prg_GetListPrgItems ();
2020-03-05 01:37:48 +01:00
/***** Get data of the program item from database *****/
Item.Hierarchy.ItmCod = Prg_GetParamItmCod ();
Prg_GetDataOfItemByCod (&Item);
if (Item.Hierarchy.ItmCod <= 0)
Lay_ShowErrorAndExit ("Wrong item code.");
2020-03-03 19:00:49 +01:00
/***** Indexes of items *****/
2020-03-05 02:03:32 +01:00
Prg_SetSubtreeWithAllChildren (Prg_GetNumItemFromItmCod (Item.Hierarchy.ItmCod),
&ToRemove);
2020-03-03 19:00:49 +01:00
/***** Remove program items *****/
2020-02-20 23:48:48 +01:00
DB_QueryDELETE ("can not remove program item",
2020-03-03 19:00:49 +01:00
"DELETE FROM prg_items"
" WHERE CrsCod=%ld AND"
" ItmInd>=%u AND ItmInd<=%u",
Gbl.Hierarchy.Crs.CrsCod,
2020-03-04 15:37:05 +01:00
ToRemove.Begin,ToRemove.End);
2020-02-20 23:48:48 +01:00
/***** Write message to show the change made *****/
2020-02-26 00:26:07 +01:00
Ale_ShowAlert (Ale_SUCCESS,Txt_Item_X_removed,Item.Title);
2020-02-20 23:48:48 +01:00
2020-03-05 01:37:48 +01:00
/***** Update list of program items *****/
2020-03-03 19:00:49 +01:00
Prg_FreeListItems ();
2020-03-05 01:37:48 +01:00
Prg_GetListPrgItems ();
2020-03-03 19:00:49 +01:00
2020-03-05 01:37:48 +01:00
/***** Show course program without highlighting any item *****/
Prg_SetSubtreeEmpty (&ToHighlight);
Prg_ShowCourseProgramHighlightingItem (&ToHighlight);
/***** Free list of program items *****/
Prg_FreeListItems ();
2020-02-20 23:48:48 +01:00
}
/*****************************************************************************/
/***************************** Hide a program item ***************************/
/*****************************************************************************/
void Prg_HidePrgItem (void)
{
2020-03-05 01:37:48 +01:00
Prg_HideUnhidePrgItem ('Y');
2020-02-20 23:48:48 +01:00
}
2020-03-05 01:37:48 +01:00
void Prg_UnhidePrgItem (void)
{
Prg_HideUnhidePrgItem ('N');
}
2020-02-20 23:48:48 +01:00
2020-03-05 01:37:48 +01:00
static void Prg_HideUnhidePrgItem (char YN)
2020-02-20 23:48:48 +01:00
{
2020-02-26 00:26:07 +01:00
struct ProgramItem Item;
2020-03-05 01:37:48 +01:00
struct Subtree ToHighlight;
2020-02-20 23:48:48 +01:00
2020-03-05 01:37:48 +01:00
/***** Get list of program items *****/
Prg_GetListPrgItems ();
2020-02-20 23:48:48 +01:00
2020-03-05 01:37:48 +01:00
/***** Get data of the item from database *****/
Item.Hierarchy.ItmCod = Prg_GetParamItmCod ();
2020-02-26 00:26:07 +01:00
Prg_GetDataOfItemByCod (&Item);
2020-03-05 01:37:48 +01:00
if (Item.Hierarchy.ItmCod <= 0)
Lay_ShowErrorAndExit ("Wrong item code.");
2020-02-20 23:48:48 +01:00
2020-03-05 01:37:48 +01:00
/***** Hide/unhide program item *****/
DB_QueryUPDATE ("can not change program item",
"UPDATE prg_items SET Hidden='%c'"
2020-02-27 23:44:21 +01:00
" WHERE ItmCod=%ld"
" AND CrsCod=%ld", // Extra check
2020-03-05 01:37:48 +01:00
YN,
2020-03-02 14:41:52 +01:00
Item.Hierarchy.ItmCod,Gbl.Hierarchy.Crs.CrsCod);
2020-02-20 23:48:48 +01:00
2020-03-05 01:37:48 +01:00
/***** Show program items highlighting subtree *****/
2020-03-05 02:03:32 +01:00
Prg_SetSubtreeWithAllChildren (Prg_GetNumItemFromItmCod (Item.Hierarchy.ItmCod),
&ToHighlight);
2020-03-05 01:37:48 +01:00
Prg_ShowCourseProgramHighlightingItem (&ToHighlight);
/***** Free list of program items *****/
Prg_FreeListItems ();
2020-02-20 23:48:48 +01:00
}
2020-02-26 00:26:07 +01:00
/*****************************************************************************/
2020-03-05 01:37:48 +01:00
/********** Move up/down position of a subtree in a course program ***********/
2020-02-26 00:26:07 +01:00
/*****************************************************************************/
void Prg_MoveUpPrgItem (void)
{
2020-03-05 01:37:48 +01:00
Prg_MoveUpDownPrgItem (Prg_MOVE_UP);
2020-02-26 00:26:07 +01:00
}
void Prg_MoveDownPrgItem (void)
2020-03-05 01:37:48 +01:00
{
Prg_MoveUpDownPrgItem (Prg_MOVE_DOWN);
}
static void Prg_MoveUpDownPrgItem (Prg_MoveUpDown_t UpDown)
2020-02-26 00:26:07 +01:00
{
extern const char *Txt_Movement_not_allowed;
2020-03-05 01:37:48 +01:00
struct ProgramItem Item;
2020-03-02 14:41:52 +01:00
unsigned NumItem;
2020-03-05 01:37:48 +01:00
bool Success = false;
struct Subtree ToHighlight;
static bool (*CheckIfAllowed[Prg_NUM_MOVEMENTS_UP_DOWN])(unsigned NumItem) =
{
[Prg_MOVE_UP ] = Prg_CheckIfMoveUpIsAllowed,
[Prg_MOVE_DOWN] = Prg_CheckIfMoveDownIsAllowed,
};
2020-02-26 00:26:07 +01:00
2020-03-02 14:41:52 +01:00
/***** Get list of program items *****/
Prg_GetListPrgItems ();
2020-02-29 22:08:03 +01:00
2020-03-05 01:37:48 +01:00
/***** Get data of the item from database *****/
Item.Hierarchy.ItmCod = Prg_GetParamItmCod ();
Prg_GetDataOfItemByCod (&Item);
if (Item.Hierarchy.ItmCod <= 0)
Lay_ShowErrorAndExit ("Wrong item code.");
/***** Move up/down item *****/
2020-03-05 02:03:32 +01:00
NumItem = Prg_GetNumItemFromItmCod (Item.Hierarchy.ItmCod);
2020-03-05 01:37:48 +01:00
if (CheckIfAllowed[UpDown](NumItem))
{
2020-03-02 14:41:52 +01:00
/* Exchange subtrees */
2020-03-05 01:37:48 +01:00
switch (UpDown)
{
case Prg_MOVE_UP:
Success = Prg_ExchangeSubtrees (Prg_GetPrevBrother (NumItem),NumItem);
break;
case Prg_MOVE_DOWN:
Success = Prg_ExchangeSubtrees (NumItem,Prg_GetNextBrother (NumItem));
break;
}
}
2020-03-05 02:03:32 +01:00
if (Success)
2020-03-05 01:37:48 +01:00
{
/* Update list of program items */
Prg_FreeListItems ();
Prg_GetListPrgItems ();
2020-03-05 02:03:32 +01:00
Prg_SetSubtreeWithAllChildren (Prg_GetNumItemFromItmCod (Item.Hierarchy.ItmCod),
&ToHighlight);
2020-03-05 01:37:48 +01:00
}
2020-03-02 14:41:52 +01:00
else
2020-03-05 01:37:48 +01:00
{
2020-03-02 14:41:52 +01:00
Ale_ShowAlert (Ale_WARNING,Txt_Movement_not_allowed);
2020-03-05 01:37:48 +01:00
Prg_SetSubtreeEmpty (&ToHighlight);
}
2020-02-29 22:39:06 +01:00
2020-03-05 01:37:48 +01:00
/***** Show program items highlighting subtree *****/
Prg_ShowCourseProgramHighlightingItem (&ToHighlight);
2020-03-02 14:41:52 +01:00
/***** Free list of program items *****/
Prg_FreeListItems ();
2020-02-27 00:19:55 +01:00
}
/*****************************************************************************/
2020-03-05 01:37:48 +01:00
/**** Exchange the order of two consecutive subtrees in a course program *****/
2020-02-27 00:19:55 +01:00
/*****************************************************************************/
2020-03-05 01:37:48 +01:00
// Return true if success
2020-02-27 00:19:55 +01:00
2020-03-05 01:37:48 +01:00
static bool Prg_ExchangeSubtrees (int NumItemTop,int NumItemBottom)
2020-02-27 00:19:55 +01:00
{
2020-03-05 01:37:48 +01:00
struct Subtree Top;
struct Subtree Bottom;
unsigned DiffBegin;
unsigned DiffEnd;
2020-03-02 14:41:52 +01:00
2020-03-05 01:37:48 +01:00
if (NumItemTop >= 0 &&
NumItemBottom >= 0)
{
Prg_SetSubtreeWithAllChildren (NumItemTop ,&Top );
Prg_SetSubtreeWithAllChildren (NumItemBottom,&Bottom);
DiffBegin = Bottom.Begin - Top.Begin;
DiffEnd = Bottom.End - Top.End;
2020-02-27 00:19:55 +01:00
2020-03-05 01:37:48 +01:00
/***** Lock table to make the move atomic *****/
DB_Query ("can not lock tables to move program item",
"LOCK TABLES prg_items WRITE");
Gbl.DB.LockedTables = true;
2020-02-26 00:26:07 +01:00
2020-03-05 01:37:48 +01:00
/***** Exchange indexes of items *****/
// Although indexes are always continuous...
// ...this implementation works even with non continuous indexes
/*
Example:
Top.Begin = 5
= 10
Top.End = 17
Bottom.Begin = 28
Bottom.End = 49
2020-03-04 15:37:05 +01:00
2020-03-05 01:37:48 +01:00
DiffBegin = 28 - 5 = 23;
DiffEnd = 49 - 17 = 32;
2020-03-04 15:37:05 +01:00
2020-03-05 01:37:48 +01:00
Step 1 Step 2 Step 3 (Equivalent to)
+------+------+ +------+------+ +------+------+ +------+------+ +------+------+
|ItmInd|ItmCod| |ItmInd|ItmCod| |ItmInd|ItmCod| |ItmInd|ItmCod| |ItmInd|ItmCod|
+------+------+ +------+------+ +------+------+ +------+------+ +------+------+
Top.Begin: | 5| 218|-->|--> -5| 218|-->|--> 37| 218| | 37| 218| | 5| 221|
| 10| 219|-->|-->-10| 219|-->|--> 42| 219| | 42| 219| | 26| 222|
Top.End: | 17| 220|-->|-->-17| 220|-->|--> 49| 220| | 49| 220| | 37| 218|
Bottom.Begin: | 28| 221|-->|-->-28| 221| | -28| 221|-->|--> 5| 221| | 42| 219|
Bottom.End: | 49| 222|-->|-->-49| 222| | -49| 222|-->|--> 26| 222| | 49| 220|
+------+------+ +------+------+ +------+------+ +------+------+ +------+------+
*/
/* Step 1: Change all indexes involved to negative,
necessary to preserve unique index (CrsCod,ItmInd) */
DB_QueryUPDATE ("can not exchange indexes of items",
"UPDATE prg_items SET ItmInd=-ItmInd"
" WHERE CrsCod=%ld"
" AND ItmInd>=%u AND ItmInd<=%u",
Gbl.Hierarchy.Crs.CrsCod,
Top.Begin,Bottom.End); // All indexes in both parts
2020-03-04 15:37:05 +01:00
2020-03-05 01:37:48 +01:00
/* Step 2: Increase top indexes */
DB_QueryUPDATE ("can not exchange indexes of items",
"UPDATE prg_items SET ItmInd=-ItmInd+%u"
" WHERE CrsCod=%ld"
" AND ItmInd>=-%u AND ItmInd<=-%u",
DiffEnd,
Gbl.Hierarchy.Crs.CrsCod,
Top.End,Top.Begin); // All indexes in top part
2020-03-04 15:37:05 +01:00
2020-03-05 01:37:48 +01:00
/* Step 3: Decrease bottom indexes */
DB_QueryUPDATE ("can not exchange indexes of items",
"UPDATE prg_items SET ItmInd=-ItmInd-%u"
" WHERE CrsCod=%ld"
" AND ItmInd>=-%u AND ItmInd<=-%u",
DiffBegin,
Gbl.Hierarchy.Crs.CrsCod,
Bottom.End,Bottom.Begin); // All indexes in bottom part
2020-02-27 23:44:21 +01:00
2020-03-05 01:37:48 +01:00
/***** Unlock table *****/
Gbl.DB.LockedTables = false; // Set to false before the following unlock...
// ...to not retry the unlock if error in unlocking
DB_Query ("can not unlock tables after moving items",
"UNLOCK TABLES");
2020-02-27 23:44:21 +01:00
2020-03-05 01:37:48 +01:00
return true; // Success
}
2020-02-27 23:44:21 +01:00
2020-03-05 01:37:48 +01:00
return false; // No success
2020-02-27 23:44:21 +01:00
}
2020-02-26 00:26:07 +01:00
/*****************************************************************************/
2020-03-02 14:41:52 +01:00
/******** Get previous brother item to a given item in current course ********/
2020-02-26 00:26:07 +01:00
/*****************************************************************************/
2020-03-02 14:41:52 +01:00
// Return -1 if no previous brother
2020-02-26 00:26:07 +01:00
2020-03-02 14:41:52 +01:00
static int Prg_GetPrevBrother (int NumItem)
2020-02-26 00:26:07 +01:00
{
2020-03-02 14:41:52 +01:00
unsigned Level;
int i;
/***** Trivial check: if item is the first one, there is no previous brother *****/
if (NumItem <= 0 ||
NumItem >= (int) Gbl.Prg.Num)
return -1;
/***** Get previous brother before item *****/
// 1 <= NumItem < Gbl.Prg.Num
Level = Gbl.Prg.LstItems[NumItem].Level;
for (i = NumItem - 1;
i >= 0;
i--)
2020-02-29 22:39:06 +01:00
{
2020-03-02 14:41:52 +01:00
if (Gbl.Prg.LstItems[i].Level == Level)
return i; // Previous brother before item found
if (Gbl.Prg.LstItems[i].Level < Level)
return -1; // Previous lower level found ==> there are no brothers before item
2020-02-29 22:39:06 +01:00
}
2020-03-02 14:41:52 +01:00
return -1; // Start reached ==> there are no brothers before item
2020-02-29 22:08:03 +01:00
}
/*****************************************************************************/
2020-03-02 14:41:52 +01:00
/********** Get next brother item to a given item in current course **********/
2020-02-29 22:08:03 +01:00
/*****************************************************************************/
2020-03-02 14:41:52 +01:00
// Return -1 if no next brother
2020-02-29 22:08:03 +01:00
2020-03-02 14:41:52 +01:00
static int Prg_GetNextBrother (int NumItem)
2020-02-29 22:08:03 +01:00
{
2020-03-02 14:41:52 +01:00
unsigned Level;
int i;
/***** Trivial check: if item is the last one, there is no next brother *****/
if (NumItem < 0 ||
NumItem >= (int) Gbl.Prg.Num - 1)
return -1;
/***** Get next brother after item *****/
// 0 <= NumItem < Gbl.Prg.Num - 1
Level = Gbl.Prg.LstItems[NumItem].Level;
for (i = NumItem + 1;
i < (int) Gbl.Prg.Num;
i++)
2020-02-29 22:08:03 +01:00
{
2020-03-02 14:41:52 +01:00
if (Gbl.Prg.LstItems[i].Level == Level)
return i; // Next brother found
if (Gbl.Prg.LstItems[i].Level < Level)
return -1; // Next lower level found ==> there are no brothers after item
2020-02-29 22:39:06 +01:00
}
2020-03-02 14:41:52 +01:00
return -1; // End reached ==> there are no brothers after item
2020-02-29 22:08:03 +01:00
}
/*****************************************************************************/
2020-03-05 01:37:48 +01:00
/************** Move a subtree to left/right in a course program *************/
2020-02-29 22:08:03 +01:00
/*****************************************************************************/
2020-03-05 01:37:48 +01:00
void Prg_MoveLeftPrgItem (void)
2020-02-29 22:08:03 +01:00
{
2020-03-05 01:37:48 +01:00
Prg_MoveLeftRightPrgItem (Prg_MOVE_LEFT);
2020-02-27 23:44:21 +01:00
}
2020-03-05 01:37:48 +01:00
void Prg_MoveRightPrgItem (void)
{
Prg_MoveLeftRightPrgItem (Prg_MOVE_RIGHT);
}
2020-02-27 23:44:21 +01:00
2020-03-05 01:37:48 +01:00
static void Prg_MoveLeftRightPrgItem (Prg_MoveLeftRight_t LeftRight)
2020-02-27 23:44:21 +01:00
{
2020-02-29 22:08:03 +01:00
extern const char *Txt_Movement_not_allowed;
2020-03-05 01:37:48 +01:00
struct ProgramItem Item;
unsigned NumItem;
struct Subtree ToMove;
struct Subtree ToHighlight;
static bool (*CheckIfAllowed[Prg_NUM_MOVEMENTS_LEFT_RIGHT])(unsigned NumItem) =
2020-03-02 14:41:52 +01:00
{
2020-03-05 01:37:48 +01:00
[Prg_MOVE_LEFT ] = Prg_CheckIfMoveLeftIsAllowed,
[Prg_MOVE_RIGHT] = Prg_CheckIfMoveRightIsAllowed,
};
static const char IncDec[Prg_NUM_MOVEMENTS_LEFT_RIGHT] =
{
[Prg_MOVE_LEFT ] = '-',
[Prg_MOVE_RIGHT] = '+',
};
2020-02-29 22:08:03 +01:00
2020-03-05 01:37:48 +01:00
/***** Get list of program items *****/
Prg_GetListPrgItems ();
2020-02-29 22:08:03 +01:00
2020-03-05 01:37:48 +01:00
/***** Get data of the item from database *****/
Item.Hierarchy.ItmCod = Prg_GetParamItmCod ();
Prg_GetDataOfItemByCod (&Item);
if (Item.Hierarchy.ItmCod <= 0)
Lay_ShowErrorAndExit ("Wrong item code.");
2020-02-29 22:08:03 +01:00
2020-03-05 01:37:48 +01:00
/***** Move up/down item *****/
2020-03-05 02:03:32 +01:00
NumItem = Prg_GetNumItemFromItmCod (Item.Hierarchy.ItmCod);
2020-03-05 01:37:48 +01:00
if (CheckIfAllowed[LeftRight](NumItem))
2020-03-02 14:41:52 +01:00
{
/* Indexes of items */
2020-03-05 01:37:48 +01:00
Prg_SetSubtreeWithAllChildren (NumItem,&ToMove);
2020-02-29 22:08:03 +01:00
/* Move item and its children to left or right */
2020-02-29 12:11:07 +01:00
DB_QueryUPDATE ("can not move items",
2020-03-05 01:37:48 +01:00
"UPDATE prg_items SET Level=Level%c1"
2020-02-29 11:38:50 +01:00
" WHERE CrsCod=%ld"
2020-02-29 22:08:03 +01:00
" AND ItmInd>=%u AND ItmInd<=%u",
2020-03-05 01:37:48 +01:00
IncDec[LeftRight],
2020-02-29 11:38:50 +01:00
Gbl.Hierarchy.Crs.CrsCod,
2020-03-05 01:37:48 +01:00
ToMove.Begin,ToMove.End);
/* Update list of program items */
Prg_FreeListItems ();
Prg_GetListPrgItems ();
2020-03-05 02:03:32 +01:00
Prg_SetSubtreeWithAllChildren (Prg_GetNumItemFromItmCod (Item.Hierarchy.ItmCod),
&ToHighlight);
2020-03-02 14:41:52 +01:00
}
2020-02-29 22:08:03 +01:00
else
2020-03-05 01:37:48 +01:00
{
2020-02-29 22:08:03 +01:00
Ale_ShowAlert (Ale_WARNING,Txt_Movement_not_allowed);
2020-03-05 01:37:48 +01:00
Prg_SetSubtreeEmpty (&ToHighlight);
}
/***** Show program items highlighting subtree *****/
Prg_ShowCourseProgramHighlightingItem (&ToHighlight);
/***** Free list of program items *****/
Prg_FreeListItems ();
2020-02-27 23:44:21 +01:00
}
/*****************************************************************************/
2020-03-05 02:03:32 +01:00
/****** Set subtree begin and end from number of item in course program ******/
2020-02-27 23:44:21 +01:00
/*****************************************************************************/
2020-03-05 01:37:48 +01:00
static void Prg_SetSubtreeEmpty (struct Subtree *Subtree)
2020-02-27 23:44:21 +01:00
{
2020-03-05 02:03:32 +01:00
/***** Range is empty *****/
2020-03-05 01:37:48 +01:00
Subtree->Begin =
Subtree->End = Gbl.Prg.Num;
}
2020-02-29 22:08:03 +01:00
2020-03-05 02:03:32 +01:00
static void Prg_SetSubtreeOnlyItem (unsigned Index,struct Subtree *Subtree)
2020-03-05 01:37:48 +01:00
{
/***** Range includes only this item *****/
Subtree->Begin =
2020-03-05 02:03:32 +01:00
Subtree->End = Index;
2020-03-05 01:37:48 +01:00
}
2020-02-29 22:08:03 +01:00
2020-03-05 01:37:48 +01:00
static void Prg_SetSubtreeWithAllChildren (unsigned NumItem,struct Subtree *Subtree)
{
/***** List of items must be filled *****/
if (!Gbl.Prg.LstIsRead || Gbl.Prg.LstItems == NULL)
Lay_ShowErrorAndExit ("Wrong list of items.");
2020-02-29 22:08:03 +01:00
2020-03-05 01:37:48 +01:00
/***** Number of item must be in the correct range *****/
if (NumItem >= Gbl.Prg.Num)
Lay_ShowErrorAndExit ("Wrong item number.");
2020-02-29 22:08:03 +01:00
2020-03-05 01:37:48 +01:00
/***** Range includes this item and all its children *****/
Subtree->Begin = Gbl.Prg.LstItems[NumItem ].Index;
Subtree->End = Gbl.Prg.LstItems[Prg_GetLastChild (NumItem)].Index;
2020-02-26 00:26:07 +01:00
}
2020-02-20 23:48:48 +01:00
/*****************************************************************************/
2020-03-05 01:37:48 +01:00
/********************** Get last child in current course *********************/
2020-02-20 23:48:48 +01:00
/*****************************************************************************/
2020-03-05 01:37:48 +01:00
static unsigned Prg_GetLastChild (int NumItem)
2020-02-20 23:48:48 +01:00
{
2020-03-05 01:37:48 +01:00
unsigned Level;
int i;
/***** Trivial check: if item is wrong, there are no children *****/
if (NumItem < 0 ||
NumItem >= (int) Gbl.Prg.Num)
Lay_ShowErrorAndExit ("Wrong number of item.");
/***** Get next brother after item *****/
// 0 <= NumItem < Gbl.Prg.Num
Level = Gbl.Prg.LstItems[NumItem].Level;
for (i = NumItem + 1;
i < (int) Gbl.Prg.Num;
i++)
{
if (Gbl.Prg.LstItems[i].Level <= Level)
return i - 1; // Last child found
}
return Gbl.Prg.Num - 1; // End reached ==> all items after the given item are its children
2020-02-20 23:48:48 +01:00
}
2020-03-03 00:56:22 +01:00
/*****************************************************************************/
/******* Put a form to create/edit program item and show current items *******/
/*****************************************************************************/
void Prg_RequestCreatePrgItem (void)
{
2020-03-03 12:00:36 +01:00
long ParentItmCod;
2020-03-05 02:03:32 +01:00
unsigned NumItem;
2020-03-03 14:35:45 +01:00
long ItmCodBeforeForm;
unsigned FormLevel;
2020-03-05 01:37:48 +01:00
struct Subtree ToHighlight;
/***** Get list of program items *****/
Prg_GetListPrgItems ();
2020-03-03 12:00:36 +01:00
/***** Get the code of the parent program item *****/
ParentItmCod = Prg_GetParamItmCod ();
2020-03-03 14:35:45 +01:00
if (ParentItmCod > 0)
{
2020-03-05 02:03:32 +01:00
NumItem = Prg_GetNumItemFromItmCod (ParentItmCod);
ItmCodBeforeForm = Gbl.Prg.LstItems[Prg_GetLastChild (NumItem)].ItmCod;
FormLevel = Gbl.Prg.LstItems[NumItem].Level + 1;
2020-03-03 14:35:45 +01:00
}
else
{
2020-03-04 01:26:49 +01:00
ParentItmCod = -1L;
2020-03-03 14:35:45 +01:00
ItmCodBeforeForm = -1L;
FormLevel = 0;
}
2020-03-03 00:56:22 +01:00
/***** Show current program items, if any *****/
2020-03-05 01:37:48 +01:00
Prg_SetSubtreeEmpty (&ToHighlight);
2020-03-03 19:28:38 +01:00
Prg_ShowAllItems (Prg_PUT_FORM_CREATE_ITEM,
2020-03-05 01:37:48 +01:00
&ToHighlight,ParentItmCod,ItmCodBeforeForm,FormLevel);
/***** Free list of program items *****/
Prg_FreeListItems ();
2020-03-03 00:56:22 +01:00
}
void Prg_RequestChangePrgItem (void)
{
2020-03-03 14:35:45 +01:00
long ItmCodBeforeForm;
unsigned FormLevel;
2020-03-05 01:37:48 +01:00
struct Subtree ToHighlight;
/***** Get list of program items *****/
Prg_GetListPrgItems ();
2020-03-03 12:00:36 +01:00
/***** Get the code of the program item *****/
2020-03-03 14:35:45 +01:00
ItmCodBeforeForm = Prg_GetParamItmCod ();
if (ItmCodBeforeForm > 0)
2020-03-05 02:03:32 +01:00
FormLevel = Gbl.Prg.LstItems[Prg_GetNumItemFromItmCod (ItmCodBeforeForm)].Level;
2020-03-03 14:35:45 +01:00
else
2020-03-05 02:03:32 +01:00
FormLevel = 0;
2020-03-03 00:56:22 +01:00
/***** Show current program items, if any *****/
2020-03-05 01:37:48 +01:00
Prg_SetSubtreeEmpty (&ToHighlight);
2020-03-03 19:28:38 +01:00
Prg_ShowAllItems (Prg_PUT_FORM_CHANGE_ITEM,
2020-03-05 01:37:48 +01:00
&ToHighlight,-1L,ItmCodBeforeForm,FormLevel);
/***** Free list of program items *****/
Prg_FreeListItems ();
2020-03-03 00:56:22 +01:00
}
2020-02-20 23:48:48 +01:00
/*****************************************************************************/
/***************** Put a form to create a new program item *******************/
/*****************************************************************************/
2020-03-04 00:33:48 +01:00
static void Prg_ShowFormToCreateItem (long ParentItmCod)
2020-02-20 23:48:48 +01:00
{
extern const char *Hlp_COURSE_Program_new_item;
extern const char *Txt_New_item;
extern const char *Txt_Create_item;
2020-03-03 00:56:22 +01:00
struct ProgramItem ParentItem; // Parent item
2020-02-26 00:26:07 +01:00
struct ProgramItem Item;
2020-02-26 19:35:33 +01:00
static const Dat_SetHMS SetHMS[Dat_NUM_START_END_TIME] =
{
Dat_HMS_TO_000000,
Dat_HMS_TO_235959
};
2020-02-20 23:48:48 +01:00
2020-03-03 12:00:36 +01:00
/***** Get data of the parent program item from database *****/
ParentItem.Hierarchy.ItmCod = ParentItmCod;
2020-03-03 00:56:22 +01:00
Prg_GetDataOfItemByCod (&ParentItem);
/***** Initialize to empty program item *****/
Prg_ResetItem (&Item);
Item.TimeUTC[Dat_START_TIME] = Gbl.StartExecutionTimeUTC;
Item.TimeUTC[Dat_END_TIME ] = Gbl.StartExecutionTimeUTC + (2 * 60 * 60); // +2 hours
Item.Open = true;
2020-03-05 01:37:48 +01:00
/***** Show pending alerts */
Ale_ShowAlerts (NULL);
2020-03-03 00:56:22 +01:00
/***** Begin form *****/
2020-03-04 00:33:48 +01:00
Frm_StartFormAnchor (ActNewPrgItm,"highlighted_item");
2020-03-03 19:28:38 +01:00
Prg_PutParamItmCod (ParentItem.Hierarchy.ItmCod);
2020-03-03 00:56:22 +01:00
/***** Begin box and table *****/
2020-03-03 12:00:36 +01:00
Box_BoxTableBegin ("100%",Txt_New_item,NULL,
2020-03-03 00:56:22 +01:00
Hlp_COURSE_Program_new_item,Box_NOT_CLOSABLE,2);
/***** Show form *****/
2020-03-04 00:33:48 +01:00
Prg_ShowFormItem (&Item,SetHMS,NULL);
2020-03-03 00:56:22 +01:00
/***** End table, send button and end box *****/
Box_BoxTableWithButtonEnd (Btn_CREATE_BUTTON,Txt_Create_item);
/***** End form *****/
Frm_EndForm ();
}
/*****************************************************************************/
2020-03-05 01:37:48 +01:00
/***************** Put a form to change program item *******************/
2020-03-03 00:56:22 +01:00
/*****************************************************************************/
2020-03-04 00:33:48 +01:00
static void Prg_ShowFormToChangeItem (long ItmCod)
2020-03-03 00:56:22 +01:00
{
extern const char *Hlp_COURSE_Program_edit_item;
extern const char *Txt_Edit_item;
extern const char *Txt_Save_changes;
struct ProgramItem Item;
char Txt[Cns_MAX_BYTES_TEXT + 1];
static const Dat_SetHMS SetHMS[Dat_NUM_START_END_TIME] =
{
Dat_HMS_DO_NOT_SET,
Dat_HMS_DO_NOT_SET
};
2020-03-03 12:00:36 +01:00
/***** Get data of the program item from database *****/
Item.Hierarchy.ItmCod = ItmCod;
2020-03-03 00:56:22 +01:00
Prg_GetDataOfItemByCod (&Item);
Prg_GetPrgItemTxtFromDB (Item.Hierarchy.ItmCod,Txt);
2020-02-20 23:48:48 +01:00
2020-03-05 01:37:48 +01:00
/***** Show pending alerts */
Ale_ShowAlerts (NULL);
2020-02-20 23:48:48 +01:00
/***** Begin form *****/
2020-03-04 00:33:48 +01:00
Frm_StartFormAnchor (ActChgPrgItm,"highlighted_item");
2020-03-03 19:28:38 +01:00
Prg_PutParamItmCod (Item.Hierarchy.ItmCod);
2020-02-20 23:48:48 +01:00
/***** Begin box and table *****/
2020-03-03 12:00:36 +01:00
Box_BoxTableBegin ("100%",
2020-03-03 00:56:22 +01:00
Item.Title[0] ? Item.Title :
Txt_Edit_item,
NULL,
Hlp_COURSE_Program_edit_item,Box_NOT_CLOSABLE,2);
/***** Show form *****/
2020-03-04 00:33:48 +01:00
Prg_ShowFormItem (&Item,SetHMS,Txt);
2020-03-03 00:56:22 +01:00
/***** End table, send button and end box *****/
Box_BoxTableWithButtonEnd (Btn_CONFIRM_BUTTON,Txt_Save_changes);
/***** End form *****/
Frm_EndForm ();
}
/*****************************************************************************/
/***************** Put a form to create a new program item *******************/
/*****************************************************************************/
2020-02-20 23:48:48 +01:00
2020-03-04 00:33:48 +01:00
static void Prg_ShowFormItem (const struct ProgramItem *Item,
const Dat_SetHMS SetHMS[Dat_NUM_START_END_TIME],
const char *Txt)
2020-03-03 00:56:22 +01:00
{
extern const char *Txt_Title;
extern const char *Txt_Description;
2020-02-20 23:48:48 +01:00
2020-03-03 00:56:22 +01:00
/***** Item title *****/
2020-02-20 23:48:48 +01:00
HTM_TR_Begin (NULL);
/* Label */
Frm_LabelColumn ("RM","Title",Txt_Title);
/* Data */
HTM_TD_Begin ("class=\"LM\"");
2020-03-03 00:56:22 +01:00
HTM_INPUT_TEXT ("Title",Prg_MAX_CHARS_PROGRAM_ITEM_TITLE,Item->Title,false,
2020-02-20 23:48:48 +01:00
"id=\"Title\" required=\"required\""
2020-03-03 12:00:36 +01:00
" class=\"PRG_TITLE_DESCRIPTION_WIDTH\"");
2020-02-20 23:48:48 +01:00
HTM_TD_End ();
HTM_TR_End ();
2020-02-26 19:35:33 +01:00
/***** Program item start and end dates *****/
2020-03-03 00:56:22 +01:00
Dat_PutFormStartEndClientLocalDateTimes (Item->TimeUTC,
2020-02-26 19:35:33 +01:00
Dat_FORM_SECONDS_ON,
SetHMS);
2020-02-20 23:48:48 +01:00
2020-02-26 19:35:33 +01:00
/***** Program item text *****/
2020-02-20 23:48:48 +01:00
HTM_TR_Begin (NULL);
/* Label */
Frm_LabelColumn ("RT","Txt",Txt_Description);
/* Data */
HTM_TD_Begin ("class=\"LT\"");
HTM_TEXTAREA_Begin ("id=\"Txt\" name=\"Txt\" rows=\"10\""
2020-03-03 12:00:36 +01:00
" class=\"PRG_TITLE_DESCRIPTION_WIDTH\"");
2020-03-03 00:56:22 +01:00
if (Txt)
if (Txt[0])
HTM_Txt (Txt);
2020-02-20 23:48:48 +01:00
HTM_TEXTAREA_End ();
HTM_TD_End ();
HTM_TR_End ();
}
/*****************************************************************************/
/***************** Receive form to create a new program item *****************/
/*****************************************************************************/
2020-03-03 00:56:22 +01:00
void Prg_RecFormNewPrgItem (void)
2020-02-20 23:48:48 +01:00
{
2020-03-03 00:56:22 +01:00
struct ProgramItem ParentItem; // Parent item
struct ProgramItem NewItem; // Item data received from form
2020-02-20 23:48:48 +01:00
char Description[Cns_MAX_BYTES_TEXT + 1];
2020-03-05 01:37:48 +01:00
struct Subtree ToHighlight;
2020-02-20 23:48:48 +01:00
2020-03-05 01:37:48 +01:00
/***** Get list of program items *****/
Prg_GetListPrgItems ();
/***** Get data of the program item from database *****/
2020-03-03 00:56:22 +01:00
ParentItem.Hierarchy.ItmCod = Prg_GetParamItmCod ();
Prg_GetDataOfItemByCod (&ParentItem);
2020-03-05 01:37:48 +01:00
if (ParentItem.Hierarchy.ItmCod <= 0)
Lay_ShowErrorAndExit ("Wrong item code.");
2020-02-20 23:48:48 +01:00
2020-03-03 00:56:22 +01:00
/***** Set new item code *****/
NewItem.Hierarchy.ItmCod = -1L;
NewItem.Hierarchy.Level = ParentItem.Hierarchy.Level + 1; // Create as child
2020-02-20 23:48:48 +01:00
/***** Get start/end date-times *****/
2020-02-26 00:26:07 +01:00
NewItem.TimeUTC[Dat_START_TIME] = Dat_GetTimeUTCFromForm ("StartTimeUTC");
NewItem.TimeUTC[Dat_END_TIME ] = Dat_GetTimeUTCFromForm ("EndTimeUTC" );
2020-02-20 23:48:48 +01:00
/***** Get program item title *****/
2020-02-26 00:26:07 +01:00
Par_GetParToText ("Title",NewItem.Title,Prg_MAX_BYTES_PROGRAM_ITEM_TITLE);
2020-02-20 23:48:48 +01:00
/***** Get program item text *****/
Par_GetParToHTML ("Txt",Description,Cns_MAX_BYTES_TEXT); // Store in HTML format (not rigorous)
/***** Adjust dates *****/
2020-02-26 00:26:07 +01:00
if (NewItem.TimeUTC[Dat_START_TIME] == 0)
NewItem.TimeUTC[Dat_START_TIME] = Gbl.StartExecutionTimeUTC;
if (NewItem.TimeUTC[Dat_END_TIME] == 0)
NewItem.TimeUTC[Dat_END_TIME] = NewItem.TimeUTC[Dat_START_TIME] + 2 * 60 * 60; // +2 hours
2020-02-20 23:48:48 +01:00
2020-03-05 01:37:48 +01:00
/***** Create a new program item *****/
Prg_InsertItem (&ParentItem,&NewItem,Description);
2020-02-20 23:48:48 +01:00
2020-03-05 01:37:48 +01:00
/* Update list of program items */
Prg_FreeListItems ();
Prg_GetListPrgItems ();
2020-02-20 23:48:48 +01:00
2020-03-05 01:37:48 +01:00
/***** Show program items highlighting subtree *****/
2020-03-05 02:03:32 +01:00
Prg_SetSubtreeOnlyItem (NewItem.Hierarchy.Index,&ToHighlight);
2020-03-05 01:37:48 +01:00
Prg_ShowCourseProgramHighlightingItem (&ToHighlight);
2020-02-20 23:48:48 +01:00
2020-03-05 01:37:48 +01:00
/***** Free list of program items *****/
Prg_FreeListItems ();
2020-03-03 00:56:22 +01:00
}
/*****************************************************************************/
/************* Receive form to change an existing program item ***************/
/*****************************************************************************/
void Prg_RecFormChgPrgItem (void)
{
struct ProgramItem OldItem; // Current program item data in database
struct ProgramItem NewItem; // Item data received from form
char Description[Cns_MAX_BYTES_TEXT + 1];
2020-03-05 01:37:48 +01:00
struct Subtree ToHighlight;
2020-03-03 00:56:22 +01:00
2020-03-05 01:37:48 +01:00
/***** Get list of program items *****/
Prg_GetListPrgItems ();
/***** Get data of the item from database *****/
2020-03-03 00:56:22 +01:00
NewItem.Hierarchy.ItmCod = Prg_GetParamItmCod ();
2020-03-05 01:37:48 +01:00
Prg_GetDataOfItemByCod (&NewItem);
if (NewItem.Hierarchy.ItmCod <= 0)
Lay_ShowErrorAndExit ("Wrong item code.");
2020-03-03 00:56:22 +01:00
/***** Get data of the old (current) program item from database *****/
OldItem.Hierarchy.ItmCod = NewItem.Hierarchy.ItmCod;
Prg_GetDataOfItemByCod (&OldItem);
/***** Get start/end date-times *****/
NewItem.TimeUTC[Dat_START_TIME] = Dat_GetTimeUTCFromForm ("StartTimeUTC");
NewItem.TimeUTC[Dat_END_TIME ] = Dat_GetTimeUTCFromForm ("EndTimeUTC" );
/***** Get program item title *****/
Par_GetParToText ("Title",NewItem.Title,Prg_MAX_BYTES_PROGRAM_ITEM_TITLE);
/***** Get program item text *****/
Par_GetParToHTML ("Txt",Description,Cns_MAX_BYTES_TEXT); // Store in HTML format (not rigorous)
/***** Adjust dates *****/
if (NewItem.TimeUTC[Dat_START_TIME] == 0)
NewItem.TimeUTC[Dat_START_TIME] = Gbl.StartExecutionTimeUTC;
if (NewItem.TimeUTC[Dat_END_TIME] == 0)
NewItem.TimeUTC[Dat_END_TIME] = NewItem.TimeUTC[Dat_START_TIME] + 2 * 60 * 60; // +2 hours
/***** Update existing item *****/
2020-03-05 01:37:48 +01:00
Prg_UpdateItem (&NewItem,Description);
2020-03-03 00:56:22 +01:00
2020-03-05 01:37:48 +01:00
/***** Show program items highlighting subtree *****/
2020-03-05 02:03:32 +01:00
Prg_SetSubtreeOnlyItem (NewItem.Hierarchy.Index,&ToHighlight);
2020-03-05 01:37:48 +01:00
Prg_ShowCourseProgramHighlightingItem (&ToHighlight);
/***** Free list of program items *****/
Prg_FreeListItems ();
2020-02-20 23:48:48 +01:00
}
/*****************************************************************************/
2020-03-03 00:56:22 +01:00
/*********** Insert a new program item as a child of a parent item ***********/
2020-02-20 23:48:48 +01:00
/*****************************************************************************/
2020-03-04 00:33:48 +01:00
static void Prg_InsertItem (const struct ProgramItem *ParentItem,
struct ProgramItem *Item,const char *Txt)
2020-02-20 23:48:48 +01:00
{
2020-03-03 00:56:22 +01:00
unsigned NumItemLastChild;
2020-02-26 19:35:33 +01:00
/***** Lock table to create program item *****/
DB_Query ("can not lock tables to create program item",
"LOCK TABLES prg_items WRITE");
Gbl.DB.LockedTables = true;
2020-03-03 00:56:22 +01:00
/***** Get list of program items *****/
Prg_GetListPrgItems ();
if (Gbl.Prg.Num) // There are items
{
if (ParentItem->Hierarchy.ItmCod > 0) // Parent specified
{
/***** Calculate where to insert *****/
2020-03-05 02:03:32 +01:00
NumItemLastChild = Prg_GetLastChild (Prg_GetNumItemFromItmCod (ParentItem->Hierarchy.ItmCod));
2020-03-03 00:56:22 +01:00
if (NumItemLastChild < Gbl.Prg.Num - 1)
{
/***** New program item will be inserted after last child of parent *****/
Item->Hierarchy.Index = Gbl.Prg.LstItems[NumItemLastChild + 1].Index;
/***** Move down all indexes of after last child of parent *****/
DB_QueryUPDATE ("can not move down items",
"UPDATE prg_items SET ItmInd=ItmInd+1"
" WHERE CrsCod=%ld"
" AND ItmInd>=%u"
" ORDER BY ItmInd DESC", // Necessary to not create duplicate key (CrsCod,ItmInd)
Gbl.Hierarchy.Crs.CrsCod,
Item->Hierarchy.Index);
}
else
/***** New program item will be inserted at the end *****/
Item->Hierarchy.Index = Gbl.Prg.LstItems[Gbl.Prg.Num - 1].Index + 1;
/***** Child ==> parent level + 1 *****/
Item->Hierarchy.Level = ParentItem->Hierarchy.Level + 1;
}
else // No parent specified
{
/***** New program item will be inserted at the end *****/
Item->Hierarchy.Index = Gbl.Prg.LstItems[Gbl.Prg.Num - 1].Index + 1;
/***** First level *****/
Item->Hierarchy.Level = 1;
}
}
else // There are no items
{
/***** New program item will be inserted as the first one *****/
Item->Hierarchy.Index = 1;
/***** First level *****/
Item->Hierarchy.Level = 1;
}
/***** Insert new program item *****/
2020-03-04 00:33:48 +01:00
Item->Hierarchy.ItmCod = Prg_InsertItemIntoDB (Item,Txt);
2020-02-20 23:48:48 +01:00
2020-02-26 19:35:33 +01:00
/***** Unlock table *****/
Gbl.DB.LockedTables = false; // Set to false before the following unlock...
// ...to not retry the unlock if error in unlocking
DB_Query ("can not unlock tables after moving items",
"UNLOCK TABLES");
2020-03-03 00:56:22 +01:00
/***** Free list items *****/
Prg_FreeListItems ();
2020-02-20 23:48:48 +01:00
}
2020-03-03 00:56:22 +01:00
/*****************************************************************************/
/***************** Create a new program item into database *******************/
/*****************************************************************************/
2020-03-04 00:33:48 +01:00
static long Prg_InsertItemIntoDB (struct ProgramItem *Item,const char *Txt)
2020-03-03 00:56:22 +01:00
{
return DB_QueryINSERTandReturnCode ("can not create new program item",
"INSERT INTO prg_items"
" (CrsCod,ItmInd,Level,UsrCod,StartTime,EndTime,Title,Txt)"
" VALUES"
" (%ld,%u,%u,%ld,FROM_UNIXTIME(%ld),FROM_UNIXTIME(%ld),"
"'%s','%s')",
Gbl.Hierarchy.Crs.CrsCod,
Item->Hierarchy.Index,
Item->Hierarchy.Level,
Gbl.Usrs.Me.UsrDat.UsrCod,
Item->TimeUTC[Dat_START_TIME],
Item->TimeUTC[Dat_END_TIME ],
Item->Title,
Txt);
}
2020-02-20 23:48:48 +01:00
/*****************************************************************************/
/******************** Update an existing program item ************************/
/*****************************************************************************/
2020-03-04 00:33:48 +01:00
static void Prg_UpdateItem (struct ProgramItem *Item,const char *Txt)
2020-02-20 23:48:48 +01:00
{
/***** Update the data of the program item *****/
DB_QueryUPDATE ("can not update program item",
"UPDATE prg_items SET "
"StartTime=FROM_UNIXTIME(%ld),"
"EndTime=FROM_UNIXTIME(%ld),"
"Title='%s',Txt='%s'"
2020-02-27 23:44:21 +01:00
" WHERE ItmCod=%ld"
" AND CrsCod=%ld", // Extra check
2020-02-26 00:26:07 +01:00
Item->TimeUTC[Dat_START_TIME],
Item->TimeUTC[Dat_END_TIME ],
Item->Title,
2020-02-20 23:48:48 +01:00
Txt,
2020-03-02 14:41:52 +01:00
Item->Hierarchy.ItmCod,Gbl.Hierarchy.Crs.CrsCod);
2020-02-20 23:48:48 +01:00
}
/*****************************************************************************/
/***************** Remove all the program items of a course ******************/
/*****************************************************************************/
2020-02-26 00:26:07 +01:00
void Prg_RemoveCrsItems (long CrsCod)
2020-02-20 23:48:48 +01:00
{
/***** Remove program items *****/
DB_QueryDELETE ("can not remove all the program items of a course",
"DELETE FROM prg_items WHERE CrsCod=%ld",
CrsCod);
}
/*****************************************************************************/
2020-02-26 00:26:07 +01:00
/***************** Get number of items in a course program *******************/
2020-02-20 23:48:48 +01:00
/*****************************************************************************/
2020-02-26 00:26:07 +01:00
unsigned Prg_GetNumItemsInCrsProgram (long CrsCod)
2020-02-20 23:48:48 +01:00
{
2020-02-26 00:26:07 +01:00
/***** Get number of items in a course program from database *****/
2020-02-20 23:48:48 +01:00
return
2020-02-26 00:26:07 +01:00
(unsigned) DB_QueryCOUNT ("can not get number of items in course program",
2020-02-20 23:48:48 +01:00
"SELECT COUNT(*) FROM prg_items"
" WHERE CrsCod=%ld",
CrsCod);
}
/*****************************************************************************/
/****************** Get number of courses with program items *****************/
/*****************************************************************************/
// Returns the number of courses with program items
// in this location (all the platform, current degree or current course)
2020-02-26 00:26:07 +01:00
unsigned Prg_GetNumCoursesWithItems (Hie_Level_t Scope)
2020-02-20 23:48:48 +01:00
{
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned NumCourses;
/***** Get number of courses with program items from database *****/
switch (Scope)
{
case Hie_SYS:
DB_QuerySELECT (&mysql_res,"can not get number of courses with program items",
"SELECT COUNT(DISTINCT CrsCod)"
" FROM prg_items"
" WHERE CrsCod>0");
break;
case Hie_CTY:
DB_QuerySELECT (&mysql_res,"can not get number of courses with program items",
"SELECT COUNT(DISTINCT prg_items.CrsCod)"
" FROM institutions,centres,degrees,courses,prg_items"
" WHERE institutions.CtyCod=%ld"
" AND institutions.InsCod=centres.InsCod"
" AND centres.CtrCod=degrees.CtrCod"
" AND degrees.DegCod=courses.DegCod"
" AND courses.Status=0"
" AND courses.CrsCod=prg_items.CrsCod",
Gbl.Hierarchy.Cty.CtyCod);
break;
case Hie_INS:
DB_QuerySELECT (&mysql_res,"can not get number of courses with program items",
"SELECT COUNT(DISTINCT prg_items.CrsCod)"
" FROM centres,degrees,courses,prg_items"
" WHERE centres.InsCod=%ld"
" AND centres.CtrCod=degrees.CtrCod"
" AND degrees.DegCod=courses.DegCod"
" AND courses.Status=0"
" AND courses.CrsCod=prg_items.CrsCod",
Gbl.Hierarchy.Ins.InsCod);
break;
case Hie_CTR:
DB_QuerySELECT (&mysql_res,"can not get number of courses with program items",
"SELECT COUNT(DISTINCT prg_items.CrsCod)"
" FROM degrees,courses,prg_items"
" WHERE degrees.CtrCod=%ld"
" AND degrees.DegCod=courses.DegCod"
" AND courses.Status=0"
" AND courses.CrsCod=prg_items.CrsCod",
Gbl.Hierarchy.Ctr.CtrCod);
break;
case Hie_DEG:
DB_QuerySELECT (&mysql_res,"can not get number of courses with program items",
"SELECT COUNT(DISTINCT prg_items.CrsCod)"
" FROM courses,prg_items"
" WHERE courses.DegCod=%ld"
" AND courses.Status=0"
" AND courses.CrsCod=prg_items.CrsCod",
Gbl.Hierarchy.Deg.DegCod);
break;
case Hie_CRS:
DB_QuerySELECT (&mysql_res,"can not get number of courses with program items",
"SELECT COUNT(DISTINCT CrsCod)"
" FROM prg_items"
" WHERE CrsCod=%ld",
Gbl.Hierarchy.Crs.CrsCod);
break;
default:
Lay_WrongScopeExit ();
break;
}
/***** Get number of courses *****/
row = mysql_fetch_row (mysql_res);
if (sscanf (row[0],"%u",&NumCourses) != 1)
Lay_ShowErrorAndExit ("Error when getting number of courses with program items.");
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
return NumCourses;
}
/*****************************************************************************/
/************************ Get number of program items ************************/
/*****************************************************************************/
2020-02-24 22:47:34 +01:00
// Returns the number of program items in a hierarchy scope
2020-02-20 23:48:48 +01:00
2020-02-26 00:26:07 +01:00
unsigned Prg_GetNumItems (Hie_Level_t Scope)
2020-02-20 23:48:48 +01:00
{
MYSQL_RES *mysql_res;
MYSQL_ROW row;
2020-02-26 00:26:07 +01:00
unsigned NumItems;
2020-02-20 23:48:48 +01:00
/***** Get number of program items from database *****/
switch (Scope)
{
case Hie_SYS:
DB_QuerySELECT (&mysql_res,"can not get number of program items",
2020-02-24 22:47:34 +01:00
"SELECT COUNT(*)"
2020-02-20 23:48:48 +01:00
" FROM prg_items"
" WHERE CrsCod>0");
break;
case Hie_CTY:
DB_QuerySELECT (&mysql_res,"can not get number of program items",
2020-02-24 22:47:34 +01:00
"SELECT COUNT(*)"
2020-02-20 23:48:48 +01:00
" FROM institutions,centres,degrees,courses,prg_items"
" WHERE institutions.CtyCod=%ld"
" AND institutions.InsCod=centres.InsCod"
" AND centres.CtrCod=degrees.CtrCod"
" AND degrees.DegCod=courses.DegCod"
" AND courses.CrsCod=prg_items.CrsCod",
Gbl.Hierarchy.Cty.CtyCod);
break;
case Hie_INS:
DB_QuerySELECT (&mysql_res,"can not get number of program items",
2020-02-24 22:47:34 +01:00
"SELECT COUNT(*)"
2020-02-20 23:48:48 +01:00
" FROM centres,degrees,courses,prg_items"
" WHERE centres.InsCod=%ld"
" AND centres.CtrCod=degrees.CtrCod"
" AND degrees.DegCod=courses.DegCod"
" AND courses.CrsCod=prg_items.CrsCod",
Gbl.Hierarchy.Ins.InsCod);
break;
case Hie_CTR:
DB_QuerySELECT (&mysql_res,"can not get number of program items",
2020-02-24 22:47:34 +01:00
"SELECT COUNT(*)"
2020-02-20 23:48:48 +01:00
" FROM degrees,courses,prg_items"
" WHERE degrees.CtrCod=%ld"
" AND degrees.DegCod=courses.DegCod"
" AND courses.CrsCod=prg_items.CrsCod",
Gbl.Hierarchy.Ctr.CtrCod);
break;
case Hie_DEG:
DB_QuerySELECT (&mysql_res,"can not get number of program items",
2020-02-24 22:47:34 +01:00
"SELECT COUNT(*)"
2020-02-20 23:48:48 +01:00
" FROM courses,prg_items"
" WHERE courses.DegCod=%ld"
" AND courses.CrsCod=prg_items.CrsCod",
Gbl.Hierarchy.Deg.DegCod);
break;
case Hie_CRS:
DB_QuerySELECT (&mysql_res,"can not get number of program items",
2020-02-24 22:47:34 +01:00
"SELECT COUNT(*)"
2020-02-20 23:48:48 +01:00
" FROM prg_items"
" WHERE CrsCod=%ld",
Gbl.Hierarchy.Crs.CrsCod);
break;
default:
Lay_WrongScopeExit ();
break;
}
/***** Get number of program items *****/
row = mysql_fetch_row (mysql_res);
2020-02-26 00:26:07 +01:00
if (sscanf (row[0],"%u",&NumItems) != 1)
2020-02-20 23:48:48 +01:00
Lay_ShowErrorAndExit ("Error when getting number of program items.");
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
2020-02-26 00:26:07 +01:00
return NumItems;
2020-02-20 23:48:48 +01:00
}