From 42d013dcd354dae4779b73146e70cb88b0a849c1 Mon Sep 17 00:00:00 2001 From: acanas Date: Mon, 18 Oct 2021 23:16:56 +0200 Subject: [PATCH] Version 21.36: Oct 18, 2021 New module swad_setting_database for database queries related to user's settings / preferences. --- Makefile | 5 +- swad_account.c | 1 + swad_browser.c | 1 + swad_calendar.c | 1 + swad_changelog.h | 3 +- swad_course.c | 1 + swad_date.c | 1 + swad_enrolment.c | 1 + swad_file_database.c | 15 -- swad_file_database.h | 3 - swad_icon.c | 1 + swad_language.c | 1 + swad_layout.c | 3 +- swad_menu.c | 1 + swad_notification.c | 1 + swad_photo.c | 1 + swad_profile.c | 1 + swad_session_database.c | 10 -- swad_session_database.h | 1 - swad_setting.c | 389 +++++----------------------------------- swad_setting.h | 19 -- swad_setting_database.c | 357 ++++++++++++++++++++++++++++++++++++ swad_setting_database.h | 71 ++++++++ 23 files changed, 495 insertions(+), 393 deletions(-) create mode 100644 swad_setting_database.c create mode 100644 swad_setting_database.h diff --git a/Makefile b/Makefile index 4f69feb0..acc4cdf6 100644 --- a/Makefile +++ b/Makefile @@ -79,9 +79,8 @@ OBJS = swad_account.o swad_account_database.o swad_action.o swad_admin.o \ swad_report_database.o swad_role.o swad_role_database.o swad_room.o \ swad_room_database.o swad_RSS.o \ swad_scope.o swad_search.o swad_session.o swad_session_database.o \ - swad_setting.o swad_statistic.o swad_string.o swad_survey.o \ - swad_syllabus.o \ - swad_system_config.o \ + swad_setting.o swad_setting_database.o swad_statistic.o swad_string.o \ + swad_survey.o swad_syllabus.o swad_system_config.o \ swad_tab.o swad_tag.o swad_test.o swad_test_config.o \ swad_test_import.o swad_test_print.o swad_test_visibility.o \ swad_theme.o \ diff --git a/swad_account.c b/swad_account.c index d5f0ca52..c81c9f40 100644 --- a/swad_account.c +++ b/swad_account.c @@ -68,6 +68,7 @@ #include "swad_report.h" #include "swad_session_database.h" #include "swad_setting.h" +#include "swad_setting_database.h" #include "swad_test_print.h" #include "swad_timeline.h" diff --git a/swad_browser.c b/swad_browser.c index ce1129ca..99b1b1fe 100644 --- a/swad_browser.c +++ b/swad_browser.c @@ -68,6 +68,7 @@ #include "swad_project_database.h" #include "swad_role.h" #include "swad_setting.h" +#include "swad_setting_database.h" #include "swad_string.h" #include "swad_timeline.h" #include "swad_timeline_note.h" diff --git a/swad_calendar.c b/swad_calendar.c index dc7c3297..3368f9c4 100644 --- a/swad_calendar.c +++ b/swad_calendar.c @@ -39,6 +39,7 @@ #include "swad_HTML.h" #include "swad_parameter.h" #include "swad_setting.h" +#include "swad_setting_database.h" /*****************************************************************************/ /************** External global variables from others modules ****************/ diff --git a/swad_changelog.h b/swad_changelog.h index 8786599c..9aeb2efa 100644 --- a/swad_changelog.h +++ b/swad_changelog.h @@ -602,13 +602,14 @@ TODO: FIX BUG, URGENT! En las fechas como par TODO: En las encuestas, que los estudiantes no puedan ver los resultados hasta que no finalice el plazo. */ -#define Log_PLATFORM_VERSION "SWAD 21.35.1 (2021-10-18)" +#define Log_PLATFORM_VERSION "SWAD 21.36 (2021-10-18)" #define CSS_FILE "swad20.45.css" #define JS_FILE "swad20.69.1.js" /* TODO: Rename CENTRE to CENTER in help wiki. TODO: Rename ASSESSMENT.Announcements to ASSESSMENT.Calls_for_exams + Version 21.36: Oct 18, 2021 New module swad_setting_database for database queries related to user's settings / preferences. (320253 lines) Version 21.35.1: Oct 18, 2021 Queries moved to module swad_file_database. (320175 lines) Version 21.35: Oct 18, 2021 New module swad_file_database for database queries related to files. (320171 lines) Version 21.34: Oct 15, 2021 New module swad_session_database for database queries related to sessions. (320081 lines) diff --git a/swad_course.c b/swad_course.c index 4c4affd4..cb670667 100644 --- a/swad_course.c +++ b/swad_course.c @@ -60,6 +60,7 @@ #include "swad_record_database.h" #include "swad_search.h" #include "swad_setting.h" +#include "swad_setting_database.h" #include "swad_survey.h" #include "swad_test.h" diff --git a/swad_date.c b/swad_date.c index a4d7d8e9..d4922b54 100644 --- a/swad_date.c +++ b/swad_date.c @@ -43,6 +43,7 @@ #include "swad_HTML.h" #include "swad_parameter.h" #include "swad_setting.h" +#include "swad_setting_database.h" /*****************************************************************************/ /************** External global variables from others modules ****************/ diff --git a/swad_enrolment.c b/swad_enrolment.c index f4f92f20..cbd8e507 100644 --- a/swad_enrolment.c +++ b/swad_enrolment.c @@ -61,6 +61,7 @@ #include "swad_role.h" #include "swad_role_database.h" #include "swad_setting.h" +#include "swad_setting_database.h" #include "swad_test_print.h" #include "swad_user.h" diff --git a/swad_file_database.c b/swad_file_database.c index 8d3f9b1b..07e85c65 100644 --- a/swad_file_database.c +++ b/swad_file_database.c @@ -25,24 +25,9 @@ /********************************* Headers ***********************************/ /*****************************************************************************/ -// #include // For isprint, isspace, etc. -// #include // For scandir, etc. -// #include // For errno -// #include // For PATH_MAX -// #include // For NULL -// #include // For FILE,fprintf -// #include // For exit, system, free, etc. -// #include // For string functions -// #include // For mkdir -// #include // For mkdir -// #include // For unlink - -// #include "swad_config.h" #include "swad_database.h" -// #include "swad_error.h" #include "swad_global.h" #include "swad_file_database.h" -// #include "swad_string.h" /*****************************************************************************/ /************** External global variables from others modules ****************/ diff --git a/swad_file_database.h b/swad_file_database.h index c816947e..41085dab 100644 --- a/swad_file_database.h +++ b/swad_file_database.h @@ -28,9 +28,6 @@ /*****************************************************************************/ #include // For PATH_MAX -// #include // For boolean type -// #include // For FILE -// #include // For time_t /*****************************************************************************/ /************************** Public types and constants ***********************/ diff --git a/swad_icon.c b/swad_icon.c index 8a754681..c00eb6ec 100644 --- a/swad_icon.c +++ b/swad_icon.c @@ -38,6 +38,7 @@ #include "swad_layout.h" #include "swad_parameter.h" #include "swad_setting.h" +#include "swad_setting_database.h" /*****************************************************************************/ /************** External global variables from others modules ****************/ diff --git a/swad_language.c b/swad_language.c index 876c0a85..95f29ecd 100644 --- a/swad_language.c +++ b/swad_language.c @@ -33,6 +33,7 @@ #include "swad_HTML.h" #include "swad_language.h" #include "swad_setting.h" +#include "swad_setting_database.h" /*****************************************************************************/ /*************** External global variables from others modules ***************/ diff --git a/swad_layout.c b/swad_layout.c index cf646f53..1538beaa 100644 --- a/swad_layout.c +++ b/swad_layout.c @@ -61,6 +61,7 @@ #include "swad_notification.h" #include "swad_parameter.h" #include "swad_setting.h" +#include "swad_setting_database.h" #include "swad_tab.h" #include "swad_theme.h" #include "swad_timeline.h" @@ -1448,7 +1449,7 @@ void Lay_RefreshNotifsAndConnected (void) else if (!(Gbl.PID % 101)) Brw_DB_RemoveExpiredExpandedFolders (); // Remove old expanded folders (from all users) else if (!(Gbl.PID % 103)) - Set_RemoveOldSettingsFromIP (); // Remove old settings from IP + Set_DB_RemoveOldSettingsFromIP (); // Remove old settings from IP else if (!(Gbl.PID % 107)) Log_DB_RemoveOldEntriesRecentLog (); // Remove old entries in recent log table, it's a slow query else if (!(Gbl.PID % 109)) diff --git a/swad_menu.c b/swad_menu.c index 01f0a51a..17bf3de0 100644 --- a/swad_menu.c +++ b/swad_menu.c @@ -35,6 +35,7 @@ #include "swad_menu.h" #include "swad_parameter.h" #include "swad_setting.h" +#include "swad_setting_database.h" #include "swad_tab.h" /*****************************************************************************/ diff --git a/swad_notification.c b/swad_notification.c index ddf418b7..c8b2cd69 100644 --- a/swad_notification.c +++ b/swad_notification.c @@ -61,6 +61,7 @@ #include "swad_notification_database.h" #include "swad_parameter.h" #include "swad_setting.h" +#include "swad_setting_database.h" #include "swad_survey.h" #include "swad_timeline.h" #include "swad_timeline_database.h" diff --git a/swad_photo.c b/swad_photo.c index 015ce5bf..b5998c00 100644 --- a/swad_photo.c +++ b/swad_photo.c @@ -55,6 +55,7 @@ #include "swad_photo_database.h" #include "swad_privacy.h" #include "swad_setting.h" +#include "swad_setting_database.h" #include "swad_statistic.h" #include "swad_theme.h" #include "swad_user.h" diff --git a/swad_profile.c b/swad_profile.c index 29f39fff..c02070cb 100644 --- a/swad_profile.c +++ b/swad_profile.c @@ -59,6 +59,7 @@ #include "swad_role.h" #include "swad_role_type.h" #include "swad_setting.h" +#include "swad_setting_database.h" #include "swad_theme.h" #include "swad_timeline.h" #include "swad_timeline_database.h" diff --git a/swad_session_database.c b/swad_session_database.c index 684ca266..496a782a 100644 --- a/swad_session_database.c +++ b/swad_session_database.c @@ -25,20 +25,10 @@ /************************************ Headers ********************************/ /*****************************************************************************/ -// #include // To access MySQL databases -// #include // For NULL -// #include // For sprintf -// #include // For string functions - #include "swad_config.h" -// #include "swad_connected_database.h" #include "swad_database.h" -// #include "swad_error.h" #include "swad_global.h" #include "swad_pagination.h" -// #include "swad_parameter.h" -// #include "swad_session_database.h" -// #include "swad_timeline_database.h" /*****************************************************************************/ /***************************** Private constants *****************************/ diff --git a/swad_session_database.h b/swad_session_database.h index 9ad0ff22..0f5195e6 100644 --- a/swad_session_database.h +++ b/swad_session_database.h @@ -27,7 +27,6 @@ /********************************* Headers ***********************************/ /*****************************************************************************/ -// #include "swad_action.h" #include "swad_pagination.h" /*****************************************************************************/ diff --git a/swad_setting.c b/swad_setting.c index f71e5601..b1364a64 100644 --- a/swad_setting.c +++ b/swad_setting.c @@ -47,6 +47,7 @@ #include "swad_parameter.h" #include "swad_privacy.h" #include "swad_setting.h" +#include "swad_setting_database.h" /*****************************************************************************/ /*************** External global variables from others modules ***************/ @@ -64,7 +65,6 @@ extern struct Globals Gbl; static void Set_PutIconsToSelectSideCols (void); static void Set_PutIconsSideColumns (__attribute__((unused)) void *Args); -static void Set_UpdateSideColsOnUsrDataTable (void); /*****************************************************************************/ /***************************** Edit settings ******************************/ @@ -138,56 +138,41 @@ void Set_EditSettings (void) void Set_GetSettingsFromIP (void) { - unsigned NumRows; MYSQL_RES *mysql_res; MYSQL_ROW row; if (Gbl.IP[0]) { /***** Get settings from database *****/ - NumRows = (unsigned) - DB_QuerySELECT (&mysql_res,"can not get settings", - "SELECT FirstDayOfWeek," // row[0] - "DateFormat," // row[1] - "Theme," // row[2] - "IconSet," // row[3] - "Menu," // row[4] - "SideCols" // row[5] - " FROM set_ip_settings" - " WHERE IP='%s'", - Gbl.IP); - if (NumRows) + if (Set_DB_GetSettingsFromIP (&mysql_res) != 1) + Err_ShowErrorAndExit ("Internal error while getting settings."); + + /***** Get settings from database *****/ + row = mysql_fetch_row (mysql_res); + + /* Get first day of week (row[0]) */ + Gbl.Prefs.FirstDayOfWeek = Cal_GetFirstDayOfWeekFromStr (row[0]); + + /* Get date format (row[1]) */ + Gbl.Prefs.DateFormat = Dat_GetDateFormatFromStr (row[1]); + + /* Get theme (row[2]) */ + Gbl.Prefs.Theme = The_GetThemeFromStr (row[2]); + + /* Get icon set (row[3]) */ + Gbl.Prefs.IconSet = Ico_GetIconSetFromStr (row[3]); + + /* Get menu (row[4]) */ + Gbl.Prefs.Menu = Mnu_GetMenuFromStr (row[4]); + + /* Get if user wants to show side columns (row[5]) */ + if (sscanf (row[5],"%u",&Gbl.Prefs.SideCols) == 1) { - if (NumRows != 1) - Err_ShowErrorAndExit ("Internal error while getting settings."); - - /***** Get settings from database *****/ - row = mysql_fetch_row (mysql_res); - - /* Get first day of week (row[0]) */ - Gbl.Prefs.FirstDayOfWeek = Cal_GetFirstDayOfWeekFromStr (row[0]); - - /* Get date format (row[1]) */ - Gbl.Prefs.DateFormat = Dat_GetDateFormatFromStr (row[1]); - - /* Get theme (row[2]) */ - Gbl.Prefs.Theme = The_GetThemeFromStr (row[2]); - - /* Get icon set (row[3]) */ - Gbl.Prefs.IconSet = Ico_GetIconSetFromStr (row[3]); - - /* Get menu (row[4]) */ - Gbl.Prefs.Menu = Mnu_GetMenuFromStr (row[4]); - - /* Get if user wants to show side columns (row[5]) */ - if (sscanf (row[5],"%u",&Gbl.Prefs.SideCols) == 1) - { - if (Gbl.Prefs.SideCols > Lay_SHOW_BOTH_COLUMNS) - Gbl.Prefs.SideCols = Cfg_DEFAULT_COLUMNS; - } - else + if (Gbl.Prefs.SideCols > Lay_SHOW_BOTH_COLUMNS) Gbl.Prefs.SideCols = Cfg_DEFAULT_COLUMNS; } + else + Gbl.Prefs.SideCols = Cfg_DEFAULT_COLUMNS; } } @@ -197,57 +182,12 @@ void Set_GetSettingsFromIP (void) void Set_SetSettingsFromIP (void) { - extern const char *The_ThemeId[The_NUM_THEMES]; - extern const char *Ico_IconSetId[Ico_NUM_ICON_SETS]; - /***** Update settings from current IP in database *****/ - DB_QueryREPLACE ("can not store settings from current IP address", - "REPLACE INTO set_ip_settings" - " (IP,UsrCod,LastChange," - "FirstDayOfWeek,DateFormat,Theme,IconSet,Menu,SideCols)" - " VALUES" - " ('%s',%ld,NOW()," - "%u,%u,'%s','%s',%u,%u)", - Gbl.IP, - Gbl.Usrs.Me.UsrDat.UsrCod, - Gbl.Prefs.FirstDayOfWeek, - (unsigned) Gbl.Prefs.DateFormat, - The_ThemeId[Gbl.Prefs.Theme], - Ico_IconSetId[Gbl.Prefs.IconSet], - (unsigned) Gbl.Prefs.Menu, - Gbl.Prefs.SideCols); + Set_DB_UpdateSettingsFromIP (); - /***** If a user is logged, update its settings in database for all its IP's *****/ + /***** If I am logged, update my settings in database for all my IP's *****/ if (Gbl.Usrs.Me.Logged) - DB_QueryUPDATE ("can not update your settings", - "UPDATE set_ip_settings" - " SET FirstDayOfWeek=%u," - "DateFormat=%u," - "Theme='%s'," - "IconSet='%s'," - "Menu=%u," - "SideCols=%u" - " WHERE UsrCod=%ld", - Gbl.Prefs.FirstDayOfWeek, - (unsigned) Gbl.Prefs.DateFormat, - The_ThemeId[Gbl.Prefs.Theme], - Ico_IconSetId[Gbl.Prefs.IconSet], - (unsigned) Gbl.Prefs.Menu, - Gbl.Prefs.SideCols, - Gbl.Usrs.Me.UsrDat.UsrCod); - } - -/*****************************************************************************/ -/*********************** Remove old settings from IP *************************/ -/*****************************************************************************/ - -void Set_RemoveOldSettingsFromIP (void) - { - /***** Remove old settings *****/ - DB_QueryDELETE ("can not remove old settings", - "DELETE LOW_PRIORITY FROM set_ip_settings" - " WHERE LastChange> 1,SideCols & 1); - Ico_PutSettingIconLink (Icon,Txt_LAYOUT_SIDE_COLUMNS[SideCols]); - Frm_EndForm (); - HTM_DIV_End (); - } - Set_EndOneSettingSelector (); - Set_EndSettingsHead (); + Set_BeginSettingsHead (); + Set_BeginOneSettingSelector (); + for (SideCols = 0; + SideCols <= Lay_SHOW_BOTH_COLUMNS; + SideCols++) + { + HTM_DIV_Begin ("class=\"%s\"",SideCols == Gbl.Prefs.SideCols ? "PREF_ON" : + "PREF_OFF"); + Frm_BeginForm (ActChgCol); + Par_PutHiddenParamUnsigned (NULL,"SideCols",SideCols); + snprintf (Icon,sizeof (Icon),"layout%u%u_32x20.gif", + SideCols >> 1,SideCols & 1); + Ico_PutSettingIconLink (Icon,Txt_LAYOUT_SIDE_COLUMNS[SideCols]); + Frm_EndForm (); + HTM_DIV_End (); + } + Set_EndOneSettingSelector (); + Set_EndSettingsHead (); Box_BoxEnd (); } @@ -307,42 +247,12 @@ void Set_ChangeSideCols (void) /***** Store side colums in database *****/ if (Gbl.Usrs.Me.Logged) - Set_UpdateSideColsOnUsrDataTable (); + Set_DB_UpdateMySettingsAboutSideCols (); /***** Set settings from current IP *****/ Set_SetSettingsFromIP (); } -/*****************************************************************************/ -/**************** Update my language to the current language *****************/ -/*****************************************************************************/ - -void Set_DB_UpdateMySettingsAboutLanguage (void) - { - extern const char *Lan_STR_LANG_ID[1 + Lan_NUM_LANGUAGES]; - - DB_QueryUPDATE ("can not update your language", - "UPDATE usr_data" - " SET Language='%s'" - " WHERE UsrCod=%ld", - Lan_STR_LANG_ID[Gbl.Prefs.Language], - Gbl.Usrs.Me.UsrDat.UsrCod); - } - -/*****************************************************************************/ -/************** Update layout of side colums on user data table **************/ -/*****************************************************************************/ - -static void Set_UpdateSideColsOnUsrDataTable (void) - { - DB_QueryUPDATE ("can not update your setting about side columns", - "UPDATE usr_data" - " SET SideCols=%u" - " WHERE UsrCod=%ld", - Gbl.Prefs.SideCols, - Gbl.Usrs.Me.UsrDat.UsrCod); - } - /*****************************************************************************/ /************** Get parameter used to show/hide side columns *****************/ /*****************************************************************************/ @@ -355,124 +265,6 @@ unsigned Set_GetParamSideCols (void) Cfg_DEFAULT_COLUMNS); } -/*****************************************************************************/ -/******************** Update my settings about date format *******************/ -/*****************************************************************************/ - -void Set_DB_UpdateMySettingsAboutDateFormat (Dat_Format_t DateFormat) - { - DB_QueryUPDATE ("can not update your setting about date format", - "UPDATE usr_data" - " SET DateFormat=%u" - " WHERE UsrCod=%ld", - (unsigned) DateFormat, - Gbl.Usrs.Me.UsrDat.UsrCod); - } - -/*****************************************************************************/ -/***************** Update my settings about first day of week ****************/ -/*****************************************************************************/ - -void Set_DB_UpdateMySettingsAboutFirstDayOfWeek (unsigned FirstDayOfWeek) - { - DB_QueryUPDATE ("can not update your setting about first day of week", - "UPDATE usr_data" - " SET FirstDayOfWeek=%u" - " WHERE UsrCod=%ld", - FirstDayOfWeek, - Gbl.Usrs.Me.UsrDat.UsrCod); - } - -/*****************************************************************************/ -/********************* Update my settings about icon set *********************/ -/*****************************************************************************/ - -void Set_DB_UpdateMySettingsAboutIconSet (const char *IconSetId) - { - DB_QueryUPDATE ("can not update your setting about icon set", - "UPDATE usr_data" - " SET IconSet='%s'" - " WHERE UsrCod=%ld", - IconSetId, - Gbl.Usrs.Me.UsrDat.UsrCod); - } - -/*****************************************************************************/ -/***************** Update my settings about first day of week ****************/ -/*****************************************************************************/ - -void Set_DB_UpdateMySettingsAboutMenu (Mnu_Menu_t Menu) - { - DB_QueryUPDATE ("can not update your setting about menu", - "UPDATE usr_data" - " SET Menu=%u" - " WHERE UsrCod=%ld", - (unsigned) Menu, - Gbl.Usrs.Me.UsrDat.UsrCod); - } - -/*****************************************************************************/ -/***************** Update my settings about photo visibility *****************/ -/*****************************************************************************/ - -void Set_DB_UpdateMySettingsAboutPhoto (void) - { - extern const char *Pri_VisibilityDB[Pri_NUM_OPTIONS_PRIVACY]; - - DB_QueryUPDATE ("can not update user's settings", - "UPDATE usr_data" - " SET PhotoVisibility='%s'" - " WHERE UsrCod=%ld", - Pri_VisibilityDB[Gbl.Usrs.Me.UsrDat.PhotoVisibility], - Gbl.Usrs.Me.UsrDat.UsrCod); - } - -/*****************************************************************************/ -/************ Update my settings about public profile visibility *************/ -/*****************************************************************************/ - -void Set_DB_UpdateMySettingsAboutBasicProfile (void) - { - extern const char *Pri_VisibilityDB[Pri_NUM_OPTIONS_PRIVACY]; - - DB_QueryUPDATE ("can not update your setting" - " about public profile visibility", - "UPDATE usr_data" - " SET BaPrfVisibility='%s'" - " WHERE UsrCod=%ld", - Pri_VisibilityDB[Gbl.Usrs.Me.UsrDat.BaPrfVisibility], - Gbl.Usrs.Me.UsrDat.UsrCod); - } - -void Set_DB_UpdateMySettingsAboutExtendedProfile (void) - { - extern const char *Pri_VisibilityDB[Pri_NUM_OPTIONS_PRIVACY]; - - DB_QueryUPDATE ("can not update your setting" - " about public profile visibility", - "UPDATE usr_data" - " SET ExPrfVisibility='%s'" - " WHERE UsrCod=%ld", - Pri_VisibilityDB[Gbl.Usrs.Me.UsrDat.ExPrfVisibility], - Gbl.Usrs.Me.UsrDat.UsrCod); - } - -/*****************************************************************************/ -/******************* Update my settings about notify events ******************/ -/*****************************************************************************/ - -void Set_DB_UpdateMySettingsAboutNotifyEvents (void) - { - DB_QueryUPDATE ("can not update user's settings", - "UPDATE usr_data" - " SET NotifNtfEvents=%u," - "EmailNtfEvents=%u" - " WHERE UsrCod=%ld", - Gbl.Usrs.Me.UsrDat.NtfEvents.CreateNotif, - Gbl.Usrs.Me.UsrDat.NtfEvents.SendEmail, - Gbl.Usrs.Me.UsrDat.UsrCod); - } - /*****************************************************************************/ /*********** Head to select one or several settings using icons **************/ /*****************************************************************************/ @@ -505,86 +297,3 @@ void Set_EndOneSettingSelector (void) { HTM_DIV_End (); } - -/*****************************************************************************/ -/************ Register last prefs in current course in database **************/ -/*****************************************************************************/ - -void Set_DB_InsertUsrInCrsSettings (long UsrCod,long CrsCod) - { - extern const char *Usr_StringsUsrListTypeInDB[Usr_NUM_USR_LIST_TYPES]; - - DB_QueryINSERT ("can not register user in course", - "INSERT INTO crs_user_settings" - " (UsrCod,CrsCod," - "LastDowGrpCod,LastComGrpCod,LastAssGrpCod," - "NumAccTst,LastAccTst,NumQstsLastTst," - "UsrListType,ColsClassPhoto,ListWithPhotos)" - " VALUES" - " (%ld,%ld," - "-1,-1,-1," - "0,FROM_UNIXTIME(%ld),0," - "'%s',%u,'%c')", - UsrCod, - CrsCod, - (long) (time_t) 0, // The user never accessed to tests in this course - Usr_StringsUsrListTypeInDB[Usr_SHOW_USRS_TYPE_DEFAULT], - Usr_CLASS_PHOTO_COLS_DEF, - Usr_LIST_WITH_PHOTOS_DEF ? 'Y' : - 'N'); - } - -/*****************************************************************************/ -/******** Update the group of my last access to a file browser zone **********/ -/*****************************************************************************/ - -void Set_DB_UpdateGrpLastAccZone (const char *FieldNameDB,long GrpCod) - { - DB_QueryUPDATE ("can not update the group of the last access to a file browser", - "UPDATE crs_user_settings" - " SET %s=%ld" - " WHERE UsrCod=%ld" - " AND CrsCod=%ld", - FieldNameDB,GrpCod, - Gbl.Usrs.Me.UsrDat.UsrCod, - Gbl.Hierarchy.Crs.CrsCod); - } - -/*****************************************************************************/ -/****************** Remove a user from a courses setting *********************/ -/*****************************************************************************/ - -void Set_DB_RemUsrFromCrsSettings (long UsrCod,long CrsCod) - { - DB_QueryDELETE ("can not remove a user from a course", - "DELETE FROM crs_user_settings" - " WHERE UsrCod=%ld" - " AND CrsCod=%ld", - UsrCod, - CrsCod); - } - -/*****************************************************************************/ -/***************** Remove a user from all courses settings *******************/ -/*****************************************************************************/ - -void Set_DB_RemUsrFromAllCrssSettings (long UsrCod) - { - DB_QueryDELETE ("can not remove a user from all courses", - "DELETE FROM crs_user_settings" - " WHERE UsrCod=%ld", - UsrCod); - } - -/*****************************************************************************/ -/*************** Remove all users from settings in a course ******************/ -/*****************************************************************************/ - -void Set_DB_RemAllUsrsFromCrsSettings (long CrsCod) - { - DB_QueryDELETE ("can not remove users from a course settings", - "DELETE FROM crs_user_settings" - " WHERE CrsCod=%ld", - CrsCod); - } - diff --git a/swad_setting.h b/swad_setting.h index cd3edbbe..b9dc7a4d 100644 --- a/swad_setting.h +++ b/swad_setting.h @@ -45,32 +45,13 @@ void Set_EditSettings (void); void Set_GetSettingsFromIP (void); void Set_SetSettingsFromIP (void); -void Set_RemoveOldSettingsFromIP (void); void Set_ChangeSideCols (void); unsigned Set_GetParamSideCols (void); -void Set_DB_UpdateMySettingsAboutLanguage (void); -void Set_DB_UpdateMySettingsAboutDateFormat (Dat_Format_t DateFormat); -void Set_DB_UpdateMySettingsAboutFirstDayOfWeek (unsigned FirstDayOfWeek); -void Set_DB_UpdateMySettingsAboutIconSet (const char *IconSetId); -void Set_DB_UpdateMySettingsAboutMenu (Mnu_Menu_t Menu); -void Set_DB_UpdateMySettingsAboutPhoto (void); -void Set_DB_UpdateMySettingsAboutBasicProfile (void); -void Set_DB_UpdateMySettingsAboutExtendedProfile (void); -void Set_DB_UpdateMySettingsAboutNotifyEvents (void); - void Set_BeginSettingsHead (void); void Set_EndSettingsHead (void); void Set_BeginOneSettingSelector (void); void Set_EndOneSettingSelector (void); -void Set_DB_InsertUsrInCrsSettings (long UsrCod,long CrsCod); - -void Set_DB_UpdateGrpLastAccZone (const char *FieldNameDB,long GrpCod); - -void Set_DB_RemUsrFromCrsSettings (long UsrCod,long CrsCod); -void Set_DB_RemUsrFromAllCrssSettings (long UsrCod); -void Set_DB_RemAllUsrsFromCrsSettings (long CrsCod); - #endif diff --git a/swad_setting_database.c b/swad_setting_database.c new file mode 100644 index 00000000..e96788c9 --- /dev/null +++ b/swad_setting_database.c @@ -0,0 +1,357 @@ +// swad_setting.c: user's settings / preferences, operations with database + +/* + SWAD (Shared Workspace At a Distance), + is a web platform developed at the University of Granada (Spain), + and used to support university teaching. + + This file is part of SWAD core. + Copyright (C) 1999-2021 Antonio Caņas Vargas + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ +/*****************************************************************************/ +/********************************** Headers **********************************/ +/*****************************************************************************/ + +#include "swad_database.h" +#include "swad_global.h" + +/*****************************************************************************/ +/*************** External global variables from others modules ***************/ +/*****************************************************************************/ + +extern struct Globals Gbl; + +/*****************************************************************************/ +/***************************** Private constants *****************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/***************************** Private prototypes ****************************/ +/*****************************************************************************/ +/*****************************************************************************/ +/**************** Update my language to the current language *****************/ +/*****************************************************************************/ + +void Set_DB_UpdateMySettingsAboutLanguage (void) + { + extern const char *Lan_STR_LANG_ID[1 + Lan_NUM_LANGUAGES]; + + DB_QueryUPDATE ("can not update your language", + "UPDATE usr_data" + " SET Language='%s'" + " WHERE UsrCod=%ld", + Lan_STR_LANG_ID[Gbl.Prefs.Language], + Gbl.Usrs.Me.UsrDat.UsrCod); + } + +/*****************************************************************************/ +/************** Update layout of side colums on user data table **************/ +/*****************************************************************************/ + +void Set_DB_UpdateMySettingsAboutSideCols (void) + { + DB_QueryUPDATE ("can not update your setting about side columns", + "UPDATE usr_data" + " SET SideCols=%u" + " WHERE UsrCod=%ld", + Gbl.Prefs.SideCols, + Gbl.Usrs.Me.UsrDat.UsrCod); + } + +/*****************************************************************************/ +/******************** Update my settings about date format *******************/ +/*****************************************************************************/ + +void Set_DB_UpdateMySettingsAboutDateFormat (Dat_Format_t DateFormat) + { + DB_QueryUPDATE ("can not update your setting about date format", + "UPDATE usr_data" + " SET DateFormat=%u" + " WHERE UsrCod=%ld", + (unsigned) DateFormat, + Gbl.Usrs.Me.UsrDat.UsrCod); + } + +/*****************************************************************************/ +/***************** Update my settings about first day of week ****************/ +/*****************************************************************************/ + +void Set_DB_UpdateMySettingsAboutFirstDayOfWeek (unsigned FirstDayOfWeek) + { + DB_QueryUPDATE ("can not update your setting about first day of week", + "UPDATE usr_data" + " SET FirstDayOfWeek=%u" + " WHERE UsrCod=%ld", + FirstDayOfWeek, + Gbl.Usrs.Me.UsrDat.UsrCod); + } + +/*****************************************************************************/ +/********************* Update my settings about icon set *********************/ +/*****************************************************************************/ + +void Set_DB_UpdateMySettingsAboutIconSet (const char *IconSetId) + { + DB_QueryUPDATE ("can not update your setting about icon set", + "UPDATE usr_data" + " SET IconSet='%s'" + " WHERE UsrCod=%ld", + IconSetId, + Gbl.Usrs.Me.UsrDat.UsrCod); + } + +/*****************************************************************************/ +/***************** Update my settings about first day of week ****************/ +/*****************************************************************************/ + +void Set_DB_UpdateMySettingsAboutMenu (Mnu_Menu_t Menu) + { + DB_QueryUPDATE ("can not update your setting about menu", + "UPDATE usr_data" + " SET Menu=%u" + " WHERE UsrCod=%ld", + (unsigned) Menu, + Gbl.Usrs.Me.UsrDat.UsrCod); + } + +/*****************************************************************************/ +/***************** Update my settings about photo visibility *****************/ +/*****************************************************************************/ + +void Set_DB_UpdateMySettingsAboutPhoto (void) + { + extern const char *Pri_VisibilityDB[Pri_NUM_OPTIONS_PRIVACY]; + + DB_QueryUPDATE ("can not update user's settings", + "UPDATE usr_data" + " SET PhotoVisibility='%s'" + " WHERE UsrCod=%ld", + Pri_VisibilityDB[Gbl.Usrs.Me.UsrDat.PhotoVisibility], + Gbl.Usrs.Me.UsrDat.UsrCod); + } + +/*****************************************************************************/ +/************ Update my settings about public profile visibility *************/ +/*****************************************************************************/ + +void Set_DB_UpdateMySettingsAboutBasicProfile (void) + { + extern const char *Pri_VisibilityDB[Pri_NUM_OPTIONS_PRIVACY]; + + DB_QueryUPDATE ("can not update your setting" + " about public profile visibility", + "UPDATE usr_data" + " SET BaPrfVisibility='%s'" + " WHERE UsrCod=%ld", + Pri_VisibilityDB[Gbl.Usrs.Me.UsrDat.BaPrfVisibility], + Gbl.Usrs.Me.UsrDat.UsrCod); + } + +void Set_DB_UpdateMySettingsAboutExtendedProfile (void) + { + extern const char *Pri_VisibilityDB[Pri_NUM_OPTIONS_PRIVACY]; + + DB_QueryUPDATE ("can not update your setting" + " about public profile visibility", + "UPDATE usr_data" + " SET ExPrfVisibility='%s'" + " WHERE UsrCod=%ld", + Pri_VisibilityDB[Gbl.Usrs.Me.UsrDat.ExPrfVisibility], + Gbl.Usrs.Me.UsrDat.UsrCod); + } + +/*****************************************************************************/ +/******************* Update my settings about notify events ******************/ +/*****************************************************************************/ + +void Set_DB_UpdateMySettingsAboutNotifyEvents (void) + { + DB_QueryUPDATE ("can not update user's settings", + "UPDATE usr_data" + " SET NotifNtfEvents=%u," + "EmailNtfEvents=%u" + " WHERE UsrCod=%ld", + Gbl.Usrs.Me.UsrDat.NtfEvents.CreateNotif, + Gbl.Usrs.Me.UsrDat.NtfEvents.SendEmail, + Gbl.Usrs.Me.UsrDat.UsrCod); + } + +/*****************************************************************************/ +/************ Register last prefs in current course in database **************/ +/*****************************************************************************/ + +void Set_DB_InsertUsrInCrsSettings (long UsrCod,long CrsCod) + { + extern const char *Usr_StringsUsrListTypeInDB[Usr_NUM_USR_LIST_TYPES]; + + DB_QueryINSERT ("can not register user in course", + "INSERT INTO crs_user_settings" + " (UsrCod,CrsCod," + "LastDowGrpCod,LastComGrpCod,LastAssGrpCod," + "NumAccTst,LastAccTst,NumQstsLastTst," + "UsrListType,ColsClassPhoto,ListWithPhotos)" + " VALUES" + " (%ld,%ld," + "-1,-1,-1," + "0,FROM_UNIXTIME(%ld),0," + "'%s',%u,'%c')", + UsrCod, + CrsCod, + (long) (time_t) 0, // The user never accessed to tests in this course + Usr_StringsUsrListTypeInDB[Usr_SHOW_USRS_TYPE_DEFAULT], + Usr_CLASS_PHOTO_COLS_DEF, + Usr_LIST_WITH_PHOTOS_DEF ? 'Y' : + 'N'); + } + +/*****************************************************************************/ +/******** Update the group of my last access to a file browser zone **********/ +/*****************************************************************************/ + +void Set_DB_UpdateGrpLastAccZone (const char *FieldNameDB,long GrpCod) + { + DB_QueryUPDATE ("can not update the group of the last access to a file browser", + "UPDATE crs_user_settings" + " SET %s=%ld" + " WHERE UsrCod=%ld" + " AND CrsCod=%ld", + FieldNameDB,GrpCod, + Gbl.Usrs.Me.UsrDat.UsrCod, + Gbl.Hierarchy.Crs.CrsCod); + } + +/*****************************************************************************/ +/****************** Remove a user from a courses setting *********************/ +/*****************************************************************************/ + +void Set_DB_RemUsrFromCrsSettings (long UsrCod,long CrsCod) + { + DB_QueryDELETE ("can not remove a user from a course", + "DELETE FROM crs_user_settings" + " WHERE UsrCod=%ld" + " AND CrsCod=%ld", + UsrCod, + CrsCod); + } + +/*****************************************************************************/ +/***************** Remove a user from all courses settings *******************/ +/*****************************************************************************/ + +void Set_DB_RemUsrFromAllCrssSettings (long UsrCod) + { + DB_QueryDELETE ("can not remove a user from all courses", + "DELETE FROM crs_user_settings" + " WHERE UsrCod=%ld", + UsrCod); + } + +/*****************************************************************************/ +/*************** Remove all users from settings in a course ******************/ +/*****************************************************************************/ + +void Set_DB_RemAllUsrsFromCrsSettings (long CrsCod) + { + DB_QueryDELETE ("can not remove users from a course settings", + "DELETE FROM crs_user_settings" + " WHERE CrsCod=%ld", + CrsCod); + } + +/*****************************************************************************/ +/**************** Update settings from current IP in database ****************/ +/*****************************************************************************/ + +void Set_DB_UpdateSettingsFromIP (void) + { + extern const char *The_ThemeId[The_NUM_THEMES]; + extern const char *Ico_IconSetId[Ico_NUM_ICON_SETS]; + + DB_QueryREPLACE ("can not store settings from current IP address", + "REPLACE INTO set_ip_settings" + " (IP,UsrCod,LastChange," + "FirstDayOfWeek,DateFormat,Theme,IconSet,Menu,SideCols)" + " VALUES" + " ('%s',%ld,NOW()," + "%u,%u,'%s','%s',%u,%u)", + Gbl.IP, + Gbl.Usrs.Me.UsrDat.UsrCod, + Gbl.Prefs.FirstDayOfWeek, + (unsigned) Gbl.Prefs.DateFormat, + The_ThemeId[Gbl.Prefs.Theme], + Ico_IconSetId[Gbl.Prefs.IconSet], + (unsigned) Gbl.Prefs.Menu, + Gbl.Prefs.SideCols); + } + +/*****************************************************************************/ +/*************** Update my settings from current IP in database **************/ +/*****************************************************************************/ + +void Set_DB_UpdateMySettingsFromIP (void) + { + extern const char *The_ThemeId[The_NUM_THEMES]; + extern const char *Ico_IconSetId[Ico_NUM_ICON_SETS]; + + DB_QueryUPDATE ("can not update your settings", + "UPDATE set_ip_settings" + " SET FirstDayOfWeek=%u," + "DateFormat=%u," + "Theme='%s'," + "IconSet='%s'," + "Menu=%u," + "SideCols=%u" + " WHERE UsrCod=%ld", + Gbl.Prefs.FirstDayOfWeek, + (unsigned) Gbl.Prefs.DateFormat, + The_ThemeId[Gbl.Prefs.Theme], + Ico_IconSetId[Gbl.Prefs.IconSet], + (unsigned) Gbl.Prefs.Menu, + Gbl.Prefs.SideCols, + Gbl.Usrs.Me.UsrDat.UsrCod); + } + +/*****************************************************************************/ +/******************** Get settings changed from current IP *******************/ +/*****************************************************************************/ + +unsigned Set_DB_GetSettingsFromIP (MYSQL_RES **mysql_res) + { + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get settings", + "SELECT FirstDayOfWeek," // row[0] + "DateFormat," // row[1] + "Theme," // row[2] + "IconSet," // row[3] + "Menu," // row[4] + "SideCols" // row[5] + " FROM set_ip_settings" + " WHERE IP='%s'", + Gbl.IP); + } + +/*****************************************************************************/ +/*********************** Remove old settings from IP *************************/ +/*****************************************************************************/ + +void Set_DB_RemoveOldSettingsFromIP (void) + { + /***** Remove old settings *****/ + DB_QueryDELETE ("can not remove old settings", + "DELETE LOW_PRIORITY FROM set_ip_settings" + " WHERE LastChange. +*/ +/*****************************************************************************/ +/********************************* Headers ***********************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/***************************** Public constants ******************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/******************************* Public types ********************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/***************************** Public prototypes *****************************/ +/*****************************************************************************/ + +//--------------------------- Global user settings ---------------------------- +void Set_DB_UpdateMySettingsAboutLanguage (void); +void Set_DB_UpdateMySettingsAboutSideCols (void); +void Set_DB_UpdateMySettingsAboutDateFormat (Dat_Format_t DateFormat); +void Set_DB_UpdateMySettingsAboutFirstDayOfWeek (unsigned FirstDayOfWeek); +void Set_DB_UpdateMySettingsAboutIconSet (const char *IconSetId); +void Set_DB_UpdateMySettingsAboutMenu (Mnu_Menu_t Menu); +void Set_DB_UpdateMySettingsAboutPhoto (void); +void Set_DB_UpdateMySettingsAboutBasicProfile (void); +void Set_DB_UpdateMySettingsAboutExtendedProfile (void); +void Set_DB_UpdateMySettingsAboutNotifyEvents (void); + +//-------------------- User settings in the current course -------------------- +void Set_DB_InsertUsrInCrsSettings (long UsrCod,long CrsCod); + +void Set_DB_UpdateGrpLastAccZone (const char *FieldNameDB,long GrpCod); + +void Set_DB_RemUsrFromCrsSettings (long UsrCod,long CrsCod); +void Set_DB_RemUsrFromAllCrssSettings (long UsrCod); +void Set_DB_RemAllUsrsFromCrsSettings (long CrsCod); + +//------------------- IP settings (when no user logged) ----------------------- +void Set_DB_UpdateSettingsFromIP (void); +void Set_DB_UpdateMySettingsFromIP (void); + +unsigned Set_DB_GetSettingsFromIP (MYSQL_RES **mysql_res); + +void Set_DB_RemoveOldSettingsFromIP (void); + +#endif