swad-core/swad_MFU.c

390 lines
14 KiB
C
Raw Normal View History

2016-08-14 13:25:11 +02:00
// swad_role.c: user's roles
/*
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.
2019-01-07 21:52:19 +01:00
Copyright (C) 1999-2019 Antonio Ca<EFBFBD>as Vargas
2016-08-14 13:25:11 +02:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General 3 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 *********************************/
/*****************************************************************************/
2018-10-23 23:05:16 +02:00
#include <mysql/mysql.h> // To access MySQL databases
#include <stdlib.h> // For malloc and free
#include <string.h> // For string functions
2016-08-14 13:25:11 +02:00
#include "swad_action.h"
2017-06-10 21:38:10 +02:00
#include "swad_box.h"
2016-08-14 13:25:11 +02:00
#include "swad_config.h"
#include "swad_database.h"
2018-11-09 20:47:39 +01:00
#include "swad_form.h"
2016-08-14 13:25:11 +02:00
#include "swad_global.h"
2019-10-23 20:07:56 +02:00
#include "swad_HTML.h"
2016-08-14 13:25:11 +02:00
#include "swad_MFU.h"
#include "swad_tab.h"
#include "swad_theme.h"
/*****************************************************************************/
/****************************** Public constants *****************************/
/*****************************************************************************/
/*****************************************************************************/
/***************************** Private constants *****************************/
/*****************************************************************************/
2017-03-08 14:12:33 +01:00
#define MFU_MAX_CHARS_TAB (128 - 1) // 127
#define MFU_MAX_BYTES_TAB ((MFU_MAX_CHARS_TAB + 1) * Str_MAX_BYTES_PER_CHAR - 1) // 2047
2017-03-07 19:55:29 +01:00
2017-03-08 14:12:33 +01:00
#define MFU_MAX_CHARS_MENU (128 - 1) // 127
#define MFU_MAX_BYTES_MENU ((MFU_MAX_CHARS_MENU + 1) * Str_MAX_BYTES_PER_CHAR - 1) // 2047
2017-01-15 18:02:52 +01:00
2016-08-14 13:25:11 +02:00
/*****************************************************************************/
/****************************** Internal types *******************************/
/*****************************************************************************/
/*****************************************************************************/
/************** External global variables from others modules ****************/
/*****************************************************************************/
extern struct Globals Gbl;
/*****************************************************************************/
/************************* Internal global variables *************************/
/*****************************************************************************/
/*****************************************************************************/
/***************************** Private prototypes ****************************/
/*****************************************************************************/
/*****************************************************************************/
/************** Allocate list of most frequently used actions ****************/
/*****************************************************************************/
void MFU_AllocateMFUActions (struct MFU_ListMFUActions *ListMFUActions,unsigned MaxActionsShown)
{
if ((ListMFUActions->Actions = (Act_Action_t *) malloc (sizeof (Act_Action_t) * MaxActionsShown)) == NULL)
Lay_ShowErrorAndExit ("Can not allocate memory for list of most frequently used actions.");
}
/*****************************************************************************/
/**************** Free list of most frequently used actions ******************/
/*****************************************************************************/
void MFU_FreeMFUActions (struct MFU_ListMFUActions *ListMFUActions)
{
if (ListMFUActions->Actions != NULL)
2019-11-06 19:45:20 +01:00
free (ListMFUActions->Actions);
2016-08-14 13:25:11 +02:00
}
/*****************************************************************************/
/*************** Get and write most frequently used actions ******************/
/*****************************************************************************/
// ListMFUActions->Actions must have space for MaxActionsShown actions
void MFU_GetMFUActions (struct MFU_ListMFUActions *ListMFUActions,unsigned MaxActionsShown)
{
2017-01-28 15:58:46 +01:00
extern Act_Action_t Act_FromActCodToAction[1 + Act_MAX_ACTION_COD];
2016-08-14 13:25:11 +02:00
MYSQL_RES *mysql_res;
MYSQL_ROW row;
2019-11-07 14:34:03 +01:00
unsigned long NumRows;
unsigned long NumRow;
2016-08-14 13:25:11 +02:00
long ActCod;
Act_Action_t Action;
/***** Get most frequently used actions *****/
2018-11-01 12:02:58 +01:00
NumRows = DB_QuerySELECT (&mysql_res,"can not get most frequently used actions",
"SELECT ActCod FROM actions_MFU"
" WHERE UsrCod=%ld ORDER BY Score DESC,LastClick DESC",
Gbl.Usrs.Me.UsrDat.UsrCod);
2016-08-14 13:25:11 +02:00
/***** Write list of frequently used actions *****/
for (NumRow = 0, ListMFUActions->NumActions = 0;
NumRow < NumRows && ListMFUActions->NumActions < MaxActionsShown;
NumRow++)
{
row = mysql_fetch_row (mysql_res);
/* Get action code (row[0]) */
ActCod = Str_ConvertStrCodToLongCod (row[0]);
if (ActCod >= 0 && ActCod <= Act_MAX_ACTION_COD)
if ((Action = Act_FromActCodToAction[ActCod]) >= 0)
2018-04-24 13:21:53 +02:00
if (Act_GetIndexInMenu (Action) >= 0) // MFU actions must be only actions shown on menu (database could contain wrong action numbers)
2016-08-14 13:25:11 +02:00
if (Act_CheckIfIHavePermissionToExecuteAction (Action))
ListMFUActions->Actions[ListMFUActions->NumActions++] = Action;
}
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
}
/*****************************************************************************/
/****************** Get my last action in the current tab ********************/
/*****************************************************************************/
Act_Action_t MFU_GetMyLastActionInCurrentTab (void)
{
2017-01-28 15:58:46 +01:00
extern Act_Action_t Act_FromActCodToAction[1 + Act_MAX_ACTION_COD];
2016-08-14 13:25:11 +02:00
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned NumActions;
unsigned NumAct;
long ActCod;
Act_Action_t Action;
Act_Action_t MoreRecentActionInCurrentTab = ActUnk;
if (Gbl.Usrs.Me.UsrDat.UsrCod > 0)
{
/***** Get my most frequently used actions *****/
2018-11-01 12:02:58 +01:00
NumActions =
(unsigned) DB_QuerySELECT (&mysql_res,"can not get"
" most frequently used actions",
"SELECT ActCod FROM actions_MFU"
" WHERE UsrCod=%ld"
" ORDER BY LastClick DESC,Score DESC",
Gbl.Usrs.Me.UsrDat.UsrCod);
2016-08-14 13:25:11 +02:00
/***** Loop over list of frequently used actions *****/
for (NumAct = 0;
NumAct < NumActions;
NumAct++)
{
row = mysql_fetch_row (mysql_res);
/* Get action code (row[0]) */
ActCod = Str_ConvertStrCodToLongCod (row[0]);
if (ActCod >= 0 && ActCod <= Act_MAX_ACTION_COD)
if ((Action = Act_FromActCodToAction[ActCod]) >= 0)
2018-04-24 13:21:53 +02:00
if (Act_GetTab (Act_GetSuperAction (Action)) == Gbl.Action.Tab)
2016-08-14 13:25:11 +02:00
if (Act_CheckIfIHavePermissionToExecuteAction (Action))
{
MoreRecentActionInCurrentTab = Action;
break;
}
}
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
}
return MoreRecentActionInCurrentTab;
}
/*****************************************************************************/
/************* Show a list of my most frequently used actions ****************/
/*****************************************************************************/
void MFU_ShowMyMFUActions (void)
{
struct MFU_ListMFUActions ListMFUActions;
MFU_AllocateMFUActions (&ListMFUActions,10);
MFU_GetMFUActions (&ListMFUActions,10);
MFU_WriteBigMFUActions (&ListMFUActions);
MFU_FreeMFUActions (&ListMFUActions);
}
/*****************************************************************************/
/*************** Write list of most frequently used actions ******************/
/*****************************************************************************/
void MFU_WriteBigMFUActions (struct MFU_ListMFUActions *ListMFUActions)
{
2017-12-19 18:41:19 +01:00
extern const char *Hlp_ANALYTICS_Frequent;
2019-02-22 21:47:50 +01:00
extern const char *The_ClassFormInBoxNoWrap[The_NUM_THEMES];
2016-11-10 21:16:23 +01:00
extern const char *Txt_My_frequent_actions;
2016-12-28 17:22:25 +01:00
extern const char *Txt_TABS_TXT[Tab_NUM_TABS];
2016-08-14 13:25:11 +02:00
unsigned NumAct;
Act_Action_t Action;
const char *Title;
2017-03-07 19:55:29 +01:00
char TabStr[MFU_MAX_BYTES_TAB + 1];
char MenuStr[MFU_MAX_BYTES_MENU + 1];
char TabMenuStr[MFU_MAX_BYTES_TAB + 6 + MFU_MAX_BYTES_MENU + 1];
2016-08-14 13:25:11 +02:00
2019-10-26 02:19:42 +02:00
/***** Begin box *****/
2019-10-25 22:48:34 +02:00
Box_BoxBegin (NULL,Txt_My_frequent_actions,NULL,
2017-12-19 18:41:19 +01:00
Hlp_ANALYTICS_Frequent,Box_NOT_CLOSABLE);
2019-10-24 00:04:40 +02:00
HTM_DIV_Begin ("id=\"MFU_actions_big\"");
2016-08-14 13:25:11 +02:00
/***** Write list of frequently used actions *****/
2019-10-26 12:25:27 +02:00
HTM_UL_Begin ("class=\"LIST_LEFT\"");
2016-08-14 13:25:11 +02:00
for (NumAct = 0;
NumAct < ListMFUActions->NumActions;
NumAct++)
{
Action = ListMFUActions->Actions[NumAct];
if ((Title = Act_GetTitleAction (Action)) != NULL)
{
/* Action string */
2018-04-24 13:21:53 +02:00
Str_Copy (TabStr,Txt_TABS_TXT[Act_GetTab (Act_GetSuperAction (Action))],
2017-03-07 19:55:29 +01:00
MFU_MAX_BYTES_TAB);
2017-01-17 03:10:43 +01:00
Str_Copy (MenuStr,Title,
2017-03-07 19:55:29 +01:00
MFU_MAX_BYTES_MENU);
2018-10-18 02:02:32 +02:00
snprintf (TabMenuStr,sizeof (TabMenuStr),
"%s &gt; %s",
TabStr,MenuStr);
2016-08-14 13:25:11 +02:00
/* Icon and text */
2019-10-26 22:49:13 +02:00
HTM_LI_Begin ("class=\"ICO_HIGHLIGHT\"");
2018-11-09 20:47:39 +01:00
Frm_StartForm (Action);
2019-02-22 21:47:50 +01:00
Frm_LinkFormSubmit (TabMenuStr,The_ClassFormInBoxNoWrap[Gbl.Prefs.Theme],NULL);
2019-10-30 00:42:01 +01:00
HTM_IMG (Gbl.Prefs.URLIconSet,Act_GetIcon (Action),MenuStr,
2019-10-30 22:31:03 +01:00
NULL);
2019-10-27 22:29:08 +01:00
fprintf (Gbl.F.Out," %s",TabMenuStr);
Frm_LinkFormEnd ();
2018-11-09 20:47:39 +01:00
Frm_EndForm ();
2019-10-26 22:49:13 +02:00
HTM_LI_End ();
2016-08-14 13:25:11 +02:00
}
}
2017-06-12 14:16:33 +02:00
/***** End box *****/
2019-10-23 20:07:56 +02:00
HTM_DIV_End ();
2019-10-25 22:48:34 +02:00
Box_BoxEnd ();
2016-08-14 13:25:11 +02:00
}
/*****************************************************************************/
/*************** Get and write most frequently used actions ******************/
/*****************************************************************************/
void MFU_WriteSmallMFUActions (struct MFU_ListMFUActions *ListMFUActions)
{
2016-11-10 21:16:23 +01:00
extern const char *Txt_My_frequent_actions;
2016-08-14 13:25:11 +02:00
extern const char *Txt_Frequent_ACTIONS;
2016-12-28 17:22:25 +01:00
extern const char *Txt_TABS_TXT[Tab_NUM_TABS];
2016-08-14 13:25:11 +02:00
unsigned NumAct;
Act_Action_t Action;
const char *Title;
2017-03-07 19:55:29 +01:00
char TabStr[MFU_MAX_BYTES_TAB + 1];
char MenuStr[MFU_MAX_BYTES_MENU + 1];
char TabMenuStr[MFU_MAX_BYTES_TAB + 6 + MFU_MAX_BYTES_MENU + 1];
2016-08-14 13:25:11 +02:00
/***** Start div and link *****/
2019-10-24 00:04:40 +02:00
HTM_DIV_Begin ("id=\"MFU_actions\"");
2018-11-09 20:47:39 +01:00
Frm_StartForm (ActMFUAct);
Frm_LinkFormSubmit (Txt_My_frequent_actions,NULL,NULL);
2019-10-27 22:29:08 +01:00
fprintf (Gbl.F.Out," %s",Txt_Frequent_ACTIONS);
Frm_LinkFormEnd ();
2018-11-09 20:47:39 +01:00
Frm_EndForm ();
2016-08-14 13:25:11 +02:00
/***** Write list of frequently used actions *****/
2019-10-26 12:25:27 +02:00
HTM_UL_Begin (NULL);
2016-08-14 13:25:11 +02:00
for (NumAct = 0;
NumAct < ListMFUActions->NumActions;
NumAct++)
{
Action = ListMFUActions->Actions[NumAct];
if ((Title = Act_GetTitleAction (Action)) != NULL)
{
/* Action string */
2018-04-24 13:21:53 +02:00
Str_Copy (TabStr,Txt_TABS_TXT[Act_GetTab (Act_GetSuperAction (Action))],
2017-03-07 19:55:29 +01:00
MFU_MAX_BYTES_TAB);
2017-01-17 03:10:43 +01:00
Str_Copy (MenuStr,Title,
2017-03-07 19:55:29 +01:00
MFU_MAX_BYTES_MENU);
2018-10-18 02:02:32 +02:00
snprintf (TabMenuStr,sizeof (TabMenuStr),
"%s &gt; %s",
TabStr,MenuStr);
2016-08-14 13:25:11 +02:00
/* Icon and text */
2019-10-26 22:49:13 +02:00
HTM_LI_Begin ("class=\"ICO_HIGHLIGHT\"");
2018-11-09 20:47:39 +01:00
Frm_StartForm (Action);
Frm_LinkFormSubmit (TabMenuStr,NULL,NULL);
2019-10-30 00:42:01 +01:00
HTM_IMG (Gbl.Prefs.URLIconSet,Act_GetIcon (Action),MenuStr,
2019-10-30 22:31:03 +01:00
NULL);
2019-10-27 22:29:08 +01:00
fprintf (Gbl.F.Out," %s",MenuStr);
Frm_LinkFormEnd ();
2018-11-09 20:47:39 +01:00
Frm_EndForm ();
2019-10-26 22:49:13 +02:00
HTM_LI_End ();
2016-08-14 13:25:11 +02:00
}
}
2019-10-26 02:19:42 +02:00
HTM_UL_End ();
2016-08-14 13:25:11 +02:00
/***** End div *****/
2019-10-23 20:07:56 +02:00
HTM_DIV_End ();
2016-08-14 13:25:11 +02:00
}
/*****************************************************************************/
/******************** Update most frequently used actions ********************/
/*****************************************************************************/
#define MFU_MIN_SCORE 0.5
#define MFU_MAX_SCORE 100.0
#define MFU_INCREASE_FACTOR 1.2
#define MFU_DECREASE_FACTOR 0.99
void MFU_UpdateMFUActions (void)
{
MYSQL_RES *mysql_res;
MYSQL_ROW row;
float Score;
2018-04-24 13:21:53 +02:00
long ActCod;
Act_Action_t SuperAction;
2016-08-14 13:25:11 +02:00
/***** In some cases, don't register action *****/
if (!Gbl.Usrs.Me.Logged)
return;
2018-04-24 13:21:53 +02:00
if (Act_GetIndexInMenu (Gbl.Action.Act) < 0)
2016-08-14 13:25:11 +02:00
return;
2018-04-24 13:21:53 +02:00
SuperAction = Act_GetSuperAction (Gbl.Action.Act);
if (SuperAction == ActMFUAct)
2016-08-14 13:25:11 +02:00
return;
2018-04-24 13:21:53 +02:00
ActCod = Act_GetActCod (SuperAction);
2016-08-14 13:25:11 +02:00
Str_SetDecimalPointToUS (); // To get the decimal point as a dot
/***** Get current score *****/
2018-11-01 12:02:58 +01:00
if (DB_QuerySELECT (&mysql_res,"can not get score for current action",
"SELECT Score FROM actions_MFU"
" WHERE UsrCod=%ld AND ActCod=%ld",
Gbl.Usrs.Me.UsrDat.UsrCod,ActCod))
2016-08-14 13:25:11 +02:00
{
row = mysql_fetch_row (mysql_res);
if (sscanf (row[0],"%f",&Score) != 1)
Lay_ShowErrorAndExit ("Error when getting score for current action.");
Score *= MFU_INCREASE_FACTOR;
if (Score > MFU_MAX_SCORE)
Score = MFU_MAX_SCORE;
}
else
Score = MFU_MIN_SCORE; // Initial score for a new action not present in MFU table
/* Free structure that stores the query result */
DB_FreeMySQLResult (&mysql_res);
/***** Update score for the current action *****/
2018-11-02 16:39:35 +01:00
DB_QueryREPLACE ("can not update most frequently used actions",
"REPLACE INTO actions_MFU"
" (UsrCod,ActCod,Score,LastClick)"
" VALUES"
" (%ld,%ld,'%f',NOW())",
Gbl.Usrs.Me.UsrDat.UsrCod,ActCod,Score);
2016-08-14 13:25:11 +02:00
/***** Update score for other actions *****/
2018-11-03 12:16:40 +01:00
DB_QueryUPDATE ("can not update most frequently used actions",
"UPDATE actions_MFU SET Score=GREATEST(Score*'%f','%f')"
" WHERE UsrCod=%ld AND ActCod<>%ld",
MFU_DECREASE_FACTOR,MFU_MIN_SCORE,
Gbl.Usrs.Me.UsrDat.UsrCod,ActCod);
2016-08-14 13:25:11 +02:00
Str_SetDecimalPointToLocal (); // Return to local system
}