swad-core/swad_test_visibility.c

217 lines
8.0 KiB
C
Raw Permalink Normal View History

// swad_test_visibility.c: visibility of test prints
2020-02-18 09:19:33 +01:00
/*
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-2024 Antonio Ca<EFBFBD>as Vargas
2020-02-18 09:19:33 +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 *********************************/
/*****************************************************************************/
2020-02-19 01:55:32 +01:00
#define _GNU_SOURCE // For asprintf
#include <stdio.h> // For asprintf
2020-02-19 21:54:23 +01:00
#include <stdlib.h> // For malloc, free
2020-02-19 01:55:32 +01:00
#include "swad_error.h"
#include "swad_global.h"
#include "swad_hidden_visible.h"
2020-02-18 09:19:33 +01:00
#include "swad_HTML.h"
#include "swad_parameter.h"
#include "swad_test_visibility.h"
/*****************************************************************************/
2020-02-19 01:55:32 +01:00
/******************************* Show visibility *****************************/
/*****************************************************************************/
void TstVis_ShowVisibilityIcons (unsigned SelectedVisibility,
HidVis_HiddenOrVisible_t HiddenOrVisible)
2020-02-19 01:55:32 +01:00
{
2020-04-02 03:28:08 +02:00
extern const char *Txt_TST_STR_VISIBILITY[TstVis_NUM_ITEMS_VISIBILITY];
extern const char *Txt_TST_HIDDEN_VISIBLE[HidVis_NUM_HIDDEN_VISIBLE];
static const char *Icons[TstVis_NUM_ITEMS_VISIBILITY] =
2020-02-19 09:40:28 +01:00
{
[TstVis_VISIBLE_QST_ANS_TXT ] = "file-alt.svg",
[TstVis_VISIBLE_FEEDBACK_TXT ] = "file-signature.svg",
[TstVis_VISIBLE_CORRECT_ANSWER] = "spell-check.svg",
[TstVis_VISIBLE_EACH_QST_SCORE] = "tasks.svg",
[TstVis_VISIBLE_TOTAL_SCORE ] = "check-circle-regular.svg",
2020-02-19 09:40:28 +01:00
};
static Ico_Color_t Color[HidVis_NUM_HIDDEN_VISIBLE] =
{
[HidVis_HIDDEN ] = Ico_RED,
[HidVis_VISIBLE] = Ico_GREEN,
};
static void (*Ico_PutIcon[HidVis_NUM_HIDDEN_VISIBLE]) (const char *Icon,
Ico_Color_t Color,
const char *Title) =
{
[HidVis_HIDDEN ] = Ico_PutIconOff,
[HidVis_VISIBLE] = Ico_PutIconOn,
};
2020-04-02 03:28:08 +02:00
TstVis_Visibility_t Visibility;
HidVis_HiddenOrVisible_t ItemHiddenOrVisible;
2020-02-19 01:55:32 +01:00
char *Title;
2020-04-02 03:28:08 +02:00
for (Visibility = (TstVis_Visibility_t) 0;
Visibility <= (TstVis_Visibility_t) (TstVis_NUM_ITEMS_VISIBILITY - 1);
2020-02-19 01:55:32 +01:00
Visibility++)
{
ItemHiddenOrVisible = ((SelectedVisibility & (1 << Visibility)) != 0) ? HidVis_VISIBLE :
HidVis_HIDDEN;
2020-02-19 01:55:32 +01:00
if (asprintf (&Title,"%s: %s",
Txt_TST_STR_VISIBILITY[Visibility],
Txt_TST_HIDDEN_VISIBLE[ItemHiddenOrVisible]) < 0)
Err_NotEnoughMemoryExit ();
Ico_PutIcon[HiddenOrVisible] (Icons[Visibility],Color[ItemHiddenOrVisible],Title);
2020-02-19 01:55:32 +01:00
free (Title);
}
}
2020-02-18 15:40:04 +01:00
/*****************************************************************************/
2020-04-02 03:28:08 +02:00
/************* Put checkboxes in form to select exam visibility **************/
2020-02-18 15:40:04 +01:00
/*****************************************************************************/
2020-04-02 03:28:08 +02:00
void TstVis_PutVisibilityCheckboxes (unsigned SelectedVisibility)
2020-02-18 15:40:04 +01:00
{
2020-04-02 03:28:08 +02:00
extern const char *Txt_TST_STR_VISIBILITY[TstVis_NUM_ITEMS_VISIBILITY];
static const char *Icons[TstVis_NUM_ITEMS_VISIBILITY] =
2020-02-19 09:40:28 +01:00
{
2020-04-02 03:28:08 +02:00
[TstVis_VISIBLE_QST_ANS_TXT ] = "file-alt.svg",
[TstVis_VISIBLE_FEEDBACK_TXT ] = "file-signature.svg",
[TstVis_VISIBLE_CORRECT_ANSWER] = "spell-check.svg",
[TstVis_VISIBLE_EACH_QST_SCORE] = "tasks.svg",
[TstVis_VISIBLE_TOTAL_SCORE ] = "check-circle-regular.svg",
2020-02-19 09:40:28 +01:00
};
2020-04-02 03:28:08 +02:00
TstVis_Visibility_t Visibility;
2020-02-19 09:40:28 +01:00
bool ItemVisible;
2020-02-18 15:40:04 +01:00
/***** Write list of checkboxes for visibility *****/
2020-04-02 03:28:08 +02:00
for (Visibility = (TstVis_Visibility_t) 0;
Visibility <= (TstVis_Visibility_t) (TstVis_NUM_ITEMS_VISIBILITY - 1);
2020-02-18 15:40:04 +01:00
Visibility++)
{
/* Begin label */
HTM_LABEL_Begin ("class=\"DAT_%s\"",The_GetSuffix ());
/* Checkbox with icon and text */
ItemVisible = (SelectedVisibility & (1 << Visibility)) != 0;
HTM_INPUT_CHECKBOX ("Visibility",HTM_DONT_SUBMIT_ON_CHANGE,
"value=\"%u\"%s",
(unsigned) Visibility,
ItemVisible ? " checked=\"checked\"" :
"");
Ico_PutIconOn (Icons[Visibility],Ico_BLACK,
Txt_TST_STR_VISIBILITY[Visibility]);
HTM_Txt (Txt_TST_STR_VISIBILITY[Visibility]);
/* End label */
2020-02-18 15:40:04 +01:00
HTM_LABEL_End ();
HTM_BR ();
}
}
/*****************************************************************************/
/************************** Get visibility from form *************************/
2020-02-18 09:19:33 +01:00
/*****************************************************************************/
2020-04-02 03:28:08 +02:00
unsigned TstVis_GetVisibilityFromForm (void)
2020-02-18 09:19:33 +01:00
{
size_t MaxSizeListVisibilitySelected;
char *StrVisibilitySelected;
const char *Ptr;
char UnsignedStr[Cns_MAX_DECIMAL_DIGITS_UINT + 1];
unsigned UnsignedNum;
2020-04-02 03:28:08 +02:00
TstVis_Visibility_t VisibilityItem;
2020-02-18 09:19:33 +01:00
unsigned Visibility = 0; // Nothing selected
/***** Allocate memory for list of attendance events selected *****/
2020-04-02 03:28:08 +02:00
MaxSizeListVisibilitySelected = TstVis_NUM_ITEMS_VISIBILITY * (Cns_MAX_DECIMAL_DIGITS_UINT + 1);
if ((StrVisibilitySelected = malloc (MaxSizeListVisibilitySelected + 1)) == NULL)
Err_NotEnoughMemoryExit ();
2020-02-18 09:19:33 +01:00
/***** Get parameter multiple with list of visibility items selected *****/
Par_GetParMultiToText ("Visibility",StrVisibilitySelected,MaxSizeListVisibilitySelected);
/***** Set which attendance events will be shown as selected (checkboxes on) *****/
if (StrVisibilitySelected[0]) // There are events selected
for (Ptr = StrVisibilitySelected;
*Ptr;
)
{
/* Get next visibility item selected */
Par_GetNextStrUntilSeparParMult (&Ptr,UnsignedStr,Cns_MAX_DECIMAL_DIGITS_UINT);
2020-02-18 09:19:33 +01:00
if (sscanf (UnsignedStr,"%u",&UnsignedNum) == 1)
2020-04-02 03:28:08 +02:00
if (UnsignedNum < TstVis_NUM_ITEMS_VISIBILITY)
2020-02-18 09:19:33 +01:00
{
2020-04-02 03:28:08 +02:00
VisibilityItem = (TstVis_Visibility_t) UnsignedNum;
2020-02-18 09:19:33 +01:00
Visibility |= (1 << VisibilityItem);
}
}
return Visibility;
}
/*****************************************************************************/
2020-02-18 15:40:04 +01:00
/************************** Get visibility from string *************************/
2020-02-18 09:19:33 +01:00
/*****************************************************************************/
2020-04-02 03:28:08 +02:00
unsigned TstVis_GetVisibilityFromStr (const char *Str)
2020-02-18 09:19:33 +01:00
{
2020-02-18 15:40:04 +01:00
unsigned UnsignedNum;
2020-04-02 03:28:08 +02:00
unsigned Visibility = TstVis_MIN_VISIBILITY; // In nothing is read, return minimum visibility
2020-02-18 09:19:33 +01:00
2020-02-18 15:40:04 +01:00
/***** Get visibility from string *****/
if (Str)
if (Str[0])
if (sscanf (Str,"%u",&UnsignedNum) == 1)
2020-04-02 03:28:08 +02:00
Visibility = UnsignedNum & TstVis_MAX_VISIBILITY;
2020-02-18 15:40:04 +01:00
return Visibility;
2020-02-18 09:19:33 +01:00
}
/*****************************************************************************/
/***************************** Get visibility items **************************/
/*****************************************************************************/
2020-04-02 03:28:08 +02:00
bool TstVis_IsVisibleQstAndAnsTxt (unsigned Visibility)
2020-02-18 09:19:33 +01:00
{
2020-04-02 03:28:08 +02:00
return (Visibility & (1 << TstVis_VISIBLE_QST_ANS_TXT)) != 0;
2020-02-18 09:19:33 +01:00
}
2020-04-02 03:28:08 +02:00
bool TstVis_IsVisibleFeedbackTxt (unsigned Visibility)
2020-02-18 09:19:33 +01:00
{
2020-04-02 03:28:08 +02:00
return (Visibility & (1 << TstVis_VISIBLE_FEEDBACK_TXT)) != 0;
2020-02-18 09:19:33 +01:00
}
2020-04-02 03:28:08 +02:00
bool TstVis_IsVisibleCorrectAns (unsigned Visibility)
2020-02-18 09:19:33 +01:00
{
2020-04-02 03:28:08 +02:00
return (Visibility & (1 << TstVis_VISIBLE_CORRECT_ANSWER)) != 0;
2020-02-18 09:19:33 +01:00
}
2020-04-02 03:28:08 +02:00
bool TstVis_IsVisibleEachQstScore (unsigned Visibility)
2020-02-18 09:19:33 +01:00
{
2020-04-02 03:28:08 +02:00
return (Visibility & (1 << TstVis_VISIBLE_EACH_QST_SCORE)) != 0;
2020-02-18 09:19:33 +01:00
}
2020-04-02 03:28:08 +02:00
bool TstVis_IsVisibleTotalScore (unsigned Visibility)
2020-02-18 09:19:33 +01:00
{
2020-04-02 03:28:08 +02:00
return (Visibility & (1 << TstVis_VISIBLE_TOTAL_SCORE)) != 0;
2020-02-18 09:19:33 +01:00
}