swad-core/swad_report.c

879 lines
30 KiB
C
Raw Normal View History

2016-09-08 18:04:30 +02:00
// swad_report.c: report on my use of the platform
/*
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-2016 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 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 *********************************/
/*****************************************************************************/
// #include <stdbool.h> // For boolean type
// #include <stdio.h> // For sprintf
// #include <string.h> // For string functions
2016-09-11 21:25:14 +02:00
#include "swad_database.h"
2016-09-08 19:21:25 +02:00
#include "swad_global.h"
2016-09-11 12:46:52 +02:00
#include "swad_ID.h"
2016-09-08 19:21:25 +02:00
#include "swad_profile.h"
2016-09-08 18:04:30 +02:00
/*****************************************************************************/
/****************************** Public constants *****************************/
/*****************************************************************************/
/*****************************************************************************/
/***************************** Private constants *****************************/
/*****************************************************************************/
2016-09-29 01:48:52 +02:00
#define Rep_MAX_BAR_WIDTH 90 // Maximum width of graphic bar
2016-09-12 10:02:17 +02:00
// #define Rep_BLOCK "&boxH;" // HTML code for a block in graphic bar
// #define Rep_BLOCK "&blk12;" // HTML code for a block in graphic bar
// #define Rep_BLOCK "&block;" // HTML code for a block in graphic bar
// #define Rep_BLOCK "&equiv;" // HTML code for a block in graphic bar
// #define Rep_BLOCK "&bull;" // HTML code for a block in graphic bar
#define Rep_BLOCK "&squf;" // HTML code for a block in graphic bar
2016-09-08 18:04:30 +02:00
/*****************************************************************************/
/****************************** Internal types *******************************/
/*****************************************************************************/
2016-09-09 00:36:44 +02:00
typedef enum
{
Rep_SEE,
Rep_PRINT,
} Rep_SeeOrPrint_t;
2016-09-08 18:04:30 +02:00
/*****************************************************************************/
/************** External global variables from others modules ****************/
/*****************************************************************************/
2016-09-08 19:21:25 +02:00
extern struct Globals Gbl;
2016-09-08 18:04:30 +02:00
/*****************************************************************************/
/************************* Internal global variables *************************/
/*****************************************************************************/
/*****************************************************************************/
/***************************** Private prototypes ****************************/
/*****************************************************************************/
2016-09-09 00:36:44 +02:00
static void Rep_ShowOrPrintMyUsageReport (Rep_SeeOrPrint_t SeeOrPrint);
static void Rep_PutIconToPrintMyUsageReport (void);
2016-10-02 19:42:47 +02:00
static unsigned long Rep_GetMaxHitsPerYear (const char *BrowserTimeZone,
time_t FirstClickTimeUTC);
2016-09-29 01:34:17 +02:00
static void Rep_GetAndWriteCurrentCrssOfAUsr (const struct UsrData *UsrDat,Rol_Role_t Role,
2016-10-02 17:56:05 +02:00
const char *BrowserTimeZone,
2016-09-29 01:34:17 +02:00
time_t FirstClickTimeUTC,
2016-09-29 01:48:52 +02:00
struct tm *tm_FirstClickTime,
2016-10-02 17:56:05 +02:00
unsigned long MaxHitsPerYear);
2016-10-02 19:42:47 +02:00
static void Rep_GetAndWriteHistoricCrssOfAUsr (const struct UsrData *UsrDat,Rol_Role_t Role,
const char *BrowserTimeZone,
time_t FirstClickTimeUTC,
struct tm *tm_FirstClickTime,
unsigned long MaxHitsPerYear);
static void Rep_WriteRowCrsData (long CrsCod,Rol_Role_t Role,
2016-10-02 17:56:05 +02:00
const char *BrowserTimeZone,
2016-09-29 01:34:17 +02:00
time_t FirstClickTimeUTC,
2016-09-29 01:48:52 +02:00
struct tm *tm_FirstClickTime,
2016-10-02 17:56:05 +02:00
unsigned long MaxHitsPerYear);
2016-10-02 19:42:47 +02:00
static void Rep_ShowMyHitsPerYear (bool AnyCourse,long CrsCod,Rol_Role_t Role,
2016-10-02 17:56:05 +02:00
const char *BrowserTimeZone,
time_t FirstClickTimeUTC,
struct tm *tm_FirstClickTime,
unsigned long MaxHitsPerYear);
2016-09-12 17:43:23 +02:00
// static void Rep_ShowMyHitsPerMonth (time_t FirstClickTimeUTC,struct tm *tm_FirstClickTime);
2016-09-12 10:02:17 +02:00
static void Rep_DrawBarNumHits (float HitsNum,float HitsMax,
unsigned MaxBarWidth);
2016-09-12 00:50:24 +02:00
2016-09-08 18:04:30 +02:00
/*****************************************************************************/
/********* Show my usage report (report on my use of the platform) ***********/
/*****************************************************************************/
void Rep_ShowMyUsageReport (void)
2016-09-09 00:36:44 +02:00
{
Rep_ShowOrPrintMyUsageReport (false);
}
void Rep_PrintMyUsageReport (void)
{
Rep_ShowOrPrintMyUsageReport (true);
}
static void Rep_ShowOrPrintMyUsageReport (Rep_SeeOrPrint_t SeeOrPrint)
2016-09-08 18:04:30 +02:00
{
2016-09-08 23:47:54 +02:00
extern const char *Txt_Report_of_use_of_the_platform;
2016-09-11 22:45:53 +02:00
extern const char *Txt_Personal_information;
2016-09-11 12:46:52 +02:00
extern const char *Txt_User[Usr_NUM_SEXS];
extern const char *Txt_ID;
extern const char *Txt_Email;
extern const char *Txt_Country;
extern const char *Txt_Institution;
2016-09-11 22:45:53 +02:00
extern const char *Txt_Figures;
2016-09-11 14:28:32 +02:00
extern const char *Txt_From_TIME;
extern const char *Txt_day;
extern const char *Txt_days;
2016-09-11 14:37:35 +02:00
extern const char *Txt_Clicks;
2016-09-11 14:28:32 +02:00
extern const char *Txt_Files_uploaded;
extern const char *Txt_file;
extern const char *Txt_files;
extern const char *Txt_public_FILES;
2016-09-11 14:41:34 +02:00
extern const char *Txt_Downloads;
2016-09-11 14:48:12 +02:00
extern const char *Txt_download;
extern const char *Txt_downloads;
extern const char *Txt_Forum_posts;
extern const char *Txt_post;
extern const char *Txt_posts;
2016-09-11 14:55:35 +02:00
extern const char *Txt_Messages_sent;
extern const char *Txt_message;
extern const char *Txt_messages;
2016-09-11 22:45:53 +02:00
extern const char *Txt_Courses;
2016-09-12 00:50:24 +02:00
extern const char *Txt_Hits;
2016-10-02 17:56:05 +02:00
char BrowserTimeZone[Dat_MAX_BYTES_TIME_ZONE+1];
2016-09-11 12:46:52 +02:00
unsigned NumID;
char CtyName[Cty_MAX_BYTES_COUNTRY_NAME+1];
struct Institution Ins;
2016-09-11 14:28:32 +02:00
struct UsrFigures UsrFigures;
2016-09-12 00:50:24 +02:00
struct tm tm_FirstClickTime;
2016-09-11 14:28:32 +02:00
unsigned NumFiles;
unsigned NumPublicFiles;
2016-09-11 18:31:11 +02:00
Rol_Role_t Role;
2016-10-02 17:56:05 +02:00
unsigned long MaxHitsPerYear;
/***** Get client time zone *****/
Dat_GetBrowserTimeZone (BrowserTimeZone);
2016-09-08 23:47:54 +02:00
2016-09-11 22:45:53 +02:00
/***** Start frame *****/
2016-09-11 21:45:29 +02:00
if (SeeOrPrint == Rep_SEE)
2016-09-11 22:45:53 +02:00
Lay_StartRoundFrame (NULL,Txt_Report_of_use_of_the_platform,
2016-09-11 21:45:29 +02:00
Rep_PutIconToPrintMyUsageReport);
2016-09-11 22:45:53 +02:00
fprintf (Gbl.F.Out,"<div class=\"LEFT_TOP\" style=\"margin:10px;\">");
/***** Personal information *****/
fprintf (Gbl.F.Out,"<h2>%s</h2>"
"<ul>",
Txt_Personal_information);
2016-09-11 12:46:52 +02:00
/***** User's name *****/
2016-09-11 18:31:11 +02:00
fprintf (Gbl.F.Out,"<li>%s: <strong>%s</strong></li>",
2016-09-11 12:46:52 +02:00
Txt_User[Gbl.Usrs.Me.UsrDat.Sex],
Gbl.Usrs.Me.UsrDat.FullName);
/***** User's ID *****/
fprintf (Gbl.F.Out,"<li>%s:",
Txt_ID);
for (NumID = 0;
NumID < Gbl.Usrs.Me.UsrDat.IDs.Num;
NumID++)
{
if (NumID)
fprintf (Gbl.F.Out,",");
2016-10-02 17:56:05 +02:00
fprintf (Gbl.F.Out," %s",Gbl.Usrs.Me.UsrDat.IDs.List[NumID].ID);
2016-09-11 12:46:52 +02:00
}
fprintf (Gbl.F.Out,"</li>");
/***** User's e-mail *****/
fprintf (Gbl.F.Out,"<li>%s: %s</li>",
Txt_Email,
Gbl.Usrs.Me.UsrDat.Email);
/***** User's country *****/
Cty_GetCountryName (Gbl.Usrs.Me.UsrDat.CtyCod,CtyName);
fprintf (Gbl.F.Out,"<li>%s: %s</li>",
Txt_Country,
CtyName);
/***** User's institution *****/
Ins.InsCod = Gbl.Usrs.Me.UsrDat.InsCod;
Ins_GetDataOfInstitutionByCod (&Ins,Ins_GET_BASIC_DATA);
fprintf (Gbl.F.Out,"<li>%s: %s</li>",
Txt_Institution,
Ins.FullName);
2016-09-11 22:45:53 +02:00
fprintf (Gbl.F.Out,"</ul>");
/***** Figures *****/
fprintf (Gbl.F.Out,"<h2>%s</h2>"
"<ul>",
Txt_Figures);
2016-09-11 14:28:32 +02:00
/***** Get figures *****/
Prf_GetUsrFigures (Gbl.Usrs.Me.UsrDat.UsrCod,&UsrFigures);
/***** Time since first click *****/
fprintf (Gbl.F.Out,"<li>%s: ",Txt_From_TIME);
if (UsrFigures.FirstClickTimeUTC)
{
2016-09-12 00:50:24 +02:00
if ((gmtime_r (&UsrFigures.FirstClickTimeUTC,&tm_FirstClickTime)) != NULL)
2016-09-11 14:28:32 +02:00
{
fprintf (Gbl.F.Out,"%04d-%02d-%02d %02d:%02d:%02d UTC",
2016-09-12 00:50:24 +02:00
1900 + tm_FirstClickTime.tm_year, // year
1 + tm_FirstClickTime.tm_mon, // month
tm_FirstClickTime.tm_mday, // day of the month
tm_FirstClickTime.tm_hour, // hours
tm_FirstClickTime.tm_min, // minutes
tm_FirstClickTime.tm_sec); // seconds
2016-09-11 14:28:32 +02:00
if (UsrFigures.NumDays > 0)
fprintf (Gbl.F.Out," (%d %s)",
UsrFigures.NumDays,
(UsrFigures.NumDays == 1) ? Txt_day :
Txt_days);
}
}
2016-09-11 14:37:35 +02:00
else // Time of first click is unknown
fprintf (Gbl.F.Out,"?");
fprintf (Gbl.F.Out,"</li>");
/***** Number of clicks *****/
fprintf (Gbl.F.Out,"<li>%s: ",Txt_Clicks);
if (UsrFigures.NumClicks >= 0)
{
fprintf (Gbl.F.Out,"%ld",UsrFigures.NumClicks);
if (UsrFigures.NumDays > 0)
{
fprintf (Gbl.F.Out," (");
Str_WriteFloatNum ((float) UsrFigures.NumClicks /
(float) UsrFigures.NumDays);
2016-09-11 18:31:11 +02:00
fprintf (Gbl.F.Out," / %s)",Txt_day);
2016-09-11 14:37:35 +02:00
}
}
else // Number of clicks is unknown
2016-09-11 14:28:32 +02:00
fprintf (Gbl.F.Out,"?");
fprintf (Gbl.F.Out,"</li>");
/***** Number of files currently published *****/
if ((NumFiles = Brw_GetNumFilesUsr (Gbl.Usrs.Me.UsrDat.UsrCod)))
NumPublicFiles = Brw_GetNumPublicFilesUsr (Gbl.Usrs.Me.UsrDat.UsrCod);
else
NumPublicFiles = 0;
fprintf (Gbl.F.Out,"<li>"
"%s: %u %s (%u %s)"
"</li>",
Txt_Files_uploaded,
NumFiles,
(NumFiles == 1) ? Txt_file :
Txt_files,
NumPublicFiles,Txt_public_FILES);
2016-09-11 14:41:34 +02:00
/***** Number of file views *****/
fprintf (Gbl.F.Out,"<li>%s: ",Txt_Downloads);
if (UsrFigures.NumFileViews >= 0)
{
2016-09-11 14:48:12 +02:00
fprintf (Gbl.F.Out,"%ld %s",
UsrFigures.NumFileViews,
(UsrFigures.NumFileViews == 1) ? Txt_download :
Txt_downloads);
2016-09-11 14:41:34 +02:00
if (UsrFigures.NumDays > 0)
{
fprintf (Gbl.F.Out," (");
Str_WriteFloatNum ((float) UsrFigures.NumFileViews /
(float) UsrFigures.NumDays);
2016-09-11 18:31:11 +02:00
fprintf (Gbl.F.Out," / %s)",Txt_day);
2016-09-11 14:41:34 +02:00
}
}
else // Number of file views is unknown
fprintf (Gbl.F.Out,"?");
fprintf (Gbl.F.Out,"</li>");
2016-09-11 14:48:12 +02:00
/***** Number of posts in forums *****/
fprintf (Gbl.F.Out,"<li>%s: ",Txt_Forum_posts);
if (UsrFigures.NumForPst >= 0)
{
fprintf (Gbl.F.Out,"%ld %s",
UsrFigures.NumForPst,
(UsrFigures.NumForPst == 1) ? Txt_post :
Txt_posts);
if (UsrFigures.NumDays > 0)
{
fprintf (Gbl.F.Out," (");
Str_WriteFloatNum ((float) UsrFigures.NumForPst /
(float) UsrFigures.NumDays);
2016-09-11 18:31:11 +02:00
fprintf (Gbl.F.Out," / %s)",Txt_day);
2016-09-11 14:48:12 +02:00
}
}
else // Number of forum posts is unknown
fprintf (Gbl.F.Out,"?");
fprintf (Gbl.F.Out,"</li>");
2016-09-11 14:55:35 +02:00
/***** Number of messages sent *****/
fprintf (Gbl.F.Out,"<li>%s: ",Txt_Messages_sent);
if (UsrFigures.NumMsgSnt >= 0)
{
fprintf (Gbl.F.Out,"%ld %s",
UsrFigures.NumMsgSnt,
(UsrFigures.NumMsgSnt == 1) ? Txt_message :
Txt_messages);
if (UsrFigures.NumDays > 0)
{
fprintf (Gbl.F.Out," (");
Str_WriteFloatNum ((float) UsrFigures.NumMsgSnt /
(float) UsrFigures.NumDays);
2016-09-11 18:31:11 +02:00
fprintf (Gbl.F.Out," / %s)",Txt_day);
2016-09-11 14:55:35 +02:00
}
}
else // Number of messages sent is unknown
fprintf (Gbl.F.Out,"?");
fprintf (Gbl.F.Out,"</li>");
2016-09-11 22:45:53 +02:00
fprintf (Gbl.F.Out,"</ul>");
2016-10-02 19:42:47 +02:00
/***** Current courses *****/
fprintf (Gbl.F.Out,"<h2>%s (actuales)</h2>" // TODO: Need translation!!!
2016-09-11 22:45:53 +02:00
"<ul>",
Txt_Courses);
2016-10-02 19:42:47 +02:00
/* Number of courses in which the user is student/teacher */
2016-10-02 17:56:05 +02:00
MaxHitsPerYear = Rep_GetMaxHitsPerYear (BrowserTimeZone,UsrFigures.FirstClickTimeUTC);
2016-09-11 18:31:11 +02:00
for (Role = Rol_STUDENT;
Role <= Rol_TEACHER;
Role++)
2016-10-02 19:42:47 +02:00
/* List my courses with this role */
Rep_GetAndWriteCurrentCrssOfAUsr (&Gbl.Usrs.Me.UsrDat,Role,
BrowserTimeZone,
UsrFigures.FirstClickTimeUTC,&tm_FirstClickTime,
MaxHitsPerYear);
2016-09-11 18:31:11 +02:00
2016-09-11 12:46:52 +02:00
fprintf (Gbl.F.Out,"</ul>");
2016-09-11 22:45:53 +02:00
2016-10-02 19:42:47 +02:00
/***** Historic courses *****/
fprintf (Gbl.F.Out,"<h2>%s (hist&oacute;rico)</h2>" // TODO: Need translation!!!
"<ul>",
Txt_Courses);
/* Number of courses in which the user clicked as student/teacher */
for (Role = Rol_STUDENT;
Role <= Rol_TEACHER;
Role++)
/* List my courses with this role */
Rep_GetAndWriteHistoricCrssOfAUsr (&Gbl.Usrs.Me.UsrDat,Role,
BrowserTimeZone,
UsrFigures.FirstClickTimeUTC,&tm_FirstClickTime,
MaxHitsPerYear);
fprintf (Gbl.F.Out,"</ul>");
2016-10-02 17:56:05 +02:00
/***** Global hits *****/
fprintf (Gbl.F.Out,"<h2>%s</h2>",Txt_Hits);
2016-10-02 19:42:47 +02:00
Rep_ShowMyHitsPerYear (true,-1L,Rol_UNKNOWN,
2016-10-02 17:56:05 +02:00
BrowserTimeZone,
UsrFigures.FirstClickTimeUTC,
&tm_FirstClickTime,
0);
// fprintf (Gbl.F.Out,"<br />");
// Rep_ShowMyHitsPerMonth (UsrFigures.FirstClickTimeUTC,&tm_FirstClickTime);
2016-09-11 22:45:53 +02:00
/***** End frame *****/
fprintf (Gbl.F.Out,"</div>");
2016-09-11 21:45:29 +02:00
if (SeeOrPrint == Rep_SEE)
Lay_EndRoundFrame ();
2016-09-08 18:04:30 +02:00
}
2016-09-09 00:36:44 +02:00
/*****************************************************************************/
/********************* Put icon to print my usage report *********************/
/*****************************************************************************/
static void Rep_PutIconToPrintMyUsageReport (void)
{
extern const char *Txt_Print;
Lay_PutContextualLink (ActPrnMyUsgRep,NULL,
"print64x64.png",
Txt_Print,NULL,
NULL);
}
2016-09-11 21:25:14 +02:00
2016-10-02 19:42:47 +02:00
/*****************************************************************************/
/************ Get the maximum number of hits per course-year-role ************/
/*****************************************************************************/
// Return the maximum number of hits per year
static unsigned long Rep_GetMaxHitsPerYear (const char *BrowserTimeZone,
time_t FirstClickTimeUTC)
{
char Query[1024];
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned long MaxHitsPerYear = 0;
sprintf (Query,"SELECT MAX(N) FROM (SELECT "
"CrsCod,"
"YEAR(CONVERT_TZ(ClickTime,@@session.time_zone,'%s')) AS Year,"
"Role,"
"COUNT(*) AS N"
" FROM log_full"
" WHERE ClickTime>=FROM_UNIXTIME('%ld')"
" AND UsrCod='%ld'"
" GROUP BY CrsCod,Year,Role)"
" AS hits_per_crs_year",
BrowserTimeZone,
(long) FirstClickTimeUTC,
Gbl.Usrs.Me.UsrDat.UsrCod);
DB_QuerySELECT (Query,&mysql_res,"can not get last question index");
/***** Get number of users *****/
row = mysql_fetch_row (mysql_res);
if (row[0]) // There are questions
if (sscanf (row[0],"%lu",&MaxHitsPerYear) != 1)
Lay_ShowErrorAndExit ("Error when getting maximum hits.");
return MaxHitsPerYear;
}
2016-09-11 21:25:14 +02:00
/*****************************************************************************/
/************************** Write courses of a user **************************/
/*****************************************************************************/
2016-09-29 01:34:17 +02:00
static void Rep_GetAndWriteCurrentCrssOfAUsr (const struct UsrData *UsrDat,Rol_Role_t Role,
2016-10-02 17:56:05 +02:00
const char *BrowserTimeZone,
2016-09-29 01:34:17 +02:00
time_t FirstClickTimeUTC,
2016-09-29 01:48:52 +02:00
struct tm *tm_FirstClickTime,
2016-10-02 17:56:05 +02:00
unsigned long MaxHitsPerYear)
2016-09-11 21:25:14 +02:00
{
2016-10-02 19:42:47 +02:00
extern const char *Txt_USER_in_COURSE;
extern const char *Txt_ROLES_SINGUL_Abc[Rol_NUM_ROLES][Usr_NUM_SEXS];
extern const char *Txt_course;
extern const char *Txt_courses;
extern const char *Txt_teachers_ABBREVIATION;
extern const char *Txt_students_ABBREVIATION;
char Query[1024];
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned NumCrss;
unsigned NumCrs;
long CrsCod;
NumCrss = Usr_GetNumCrssOfUsrWithARole (UsrDat->UsrCod,Role);
sprintf (Gbl.Title,Txt_USER_in_COURSE,Txt_ROLES_SINGUL_Abc[Role][Gbl.Usrs.Me.UsrDat.Sex]);
fprintf (Gbl.F.Out,"<li>%s %u %s",
Gbl.Title,
NumCrss,
NumCrss == 1 ? Txt_course :
Txt_courses);
if (NumCrss)
{
fprintf (Gbl.F.Out," (%u %s / %u %s)",
Usr_GetNumUsrsInCrssOfAUsr (Gbl.Usrs.Me.UsrDat.UsrCod,Role,Rol_TEACHER),
Txt_teachers_ABBREVIATION,
Usr_GetNumUsrsInCrssOfAUsr (Gbl.Usrs.Me.UsrDat.UsrCod,Role,Rol_STUDENT),
Txt_students_ABBREVIATION);
/***** Get courses of a user from database *****/
sprintf (Query,"SELECT courses.CrsCod,degrees.FullName,courses.Year,courses.FullName"
" FROM crs_usr,courses,degrees"
" WHERE crs_usr.UsrCod='%ld'"
" AND crs_usr.Role='%u'"
" AND crs_usr.CrsCod=courses.CrsCod"
" AND courses.DegCod=degrees.DegCod"
" ORDER BY degrees.FullName,courses.Year,courses.FullName",
UsrDat->UsrCod,(unsigned) Role);
/***** List the courses (one row per course) *****/
if ((NumCrss = (unsigned) DB_QuerySELECT (Query,&mysql_res,"can not get courses of a user")))
{
/* Heading row */
fprintf (Gbl.F.Out,"<ol>");
/* Write courses */
for (NumCrs = 1;
NumCrs <= NumCrss;
NumCrs++)
{
/* Get next course */
row = mysql_fetch_row (mysql_res);
/* Get course code (row[0]) */
CrsCod = Str_ConvertStrCodToLongCod (row[0]);
/* Write data of this course */
Rep_WriteRowCrsData (CrsCod,Role,
BrowserTimeZone,FirstClickTimeUTC,tm_FirstClickTime,
MaxHitsPerYear);
}
/* End table */
fprintf (Gbl.F.Out,"</ol>");
}
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
}
fprintf (Gbl.F.Out,"</li>");
}
/*****************************************************************************/
/************************** Write courses of a user **************************/
/*****************************************************************************/
static void Rep_GetAndWriteHistoricCrssOfAUsr (const struct UsrData *UsrDat,Rol_Role_t Role,
const char *BrowserTimeZone,
time_t FirstClickTimeUTC,
struct tm *tm_FirstClickTime,
unsigned long MaxHitsPerYear)
{
extern const char *Txt_ROLES_SINGUL_abc[Rol_NUM_ROLES][Usr_NUM_SEXS];
2016-09-11 21:25:14 +02:00
char Query[1024];
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned NumCrss;
unsigned NumCrs;
2016-10-02 19:42:47 +02:00
long CrsCod;
2016-09-11 21:25:14 +02:00
/***** Get courses of a user from database *****/
2016-10-02 19:42:47 +02:00
sprintf (Query,"SELECT CrsCod,"
"COUNT(*) AS N"
" FROM log_full"
" WHERE UsrCod='%ld' AND Role='%u'"
" GROUP BY CrsCod ORDER BY N DESC",
2016-09-11 21:25:14 +02:00
UsrDat->UsrCod,(unsigned) Role);
/***** List the courses (one row per course) *****/
if ((NumCrss = (unsigned) DB_QuerySELECT (Query,&mysql_res,"can not get courses of a user")))
{
/* Heading row */
2016-10-02 19:42:47 +02:00
sprintf (Gbl.Title,"Accesos como %s", // TODO: Need translation
Txt_ROLES_SINGUL_abc[Role][Gbl.Usrs.Me.UsrDat.Sex]);
fprintf (Gbl.F.Out,"<li>%s:"
"<ol>",
Gbl.Title);
2016-09-11 21:25:14 +02:00
/* Write courses */
for (NumCrs = 1;
NumCrs <= NumCrss;
NumCrs++)
{
/* Get next course */
row = mysql_fetch_row (mysql_res);
2016-10-02 19:42:47 +02:00
/* Get course code (row[0]) */
CrsCod = Str_ConvertStrCodToLongCod (row[0]);
2016-09-11 21:25:14 +02:00
/* Write data of this course */
2016-10-02 19:42:47 +02:00
Rep_WriteRowCrsData (CrsCod,Role,
2016-10-02 17:56:05 +02:00
BrowserTimeZone,FirstClickTimeUTC,tm_FirstClickTime,
MaxHitsPerYear);
2016-09-11 21:25:14 +02:00
}
2016-10-02 19:42:47 +02:00
/* End of list */
fprintf (Gbl.F.Out,"</ol>"
"</li>");
2016-09-11 21:25:14 +02:00
}
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
}
/*****************************************************************************/
/************** Write the data of a course (result of a query) ***************/
/*****************************************************************************/
2016-10-02 19:42:47 +02:00
static void Rep_WriteRowCrsData (long CrsCod,Rol_Role_t Role,
2016-10-02 17:56:05 +02:00
const char *BrowserTimeZone,
2016-09-29 01:34:17 +02:00
time_t FirstClickTimeUTC,
2016-09-29 01:48:52 +02:00
struct tm *tm_FirstClickTime,
2016-10-02 17:56:05 +02:00
unsigned long MaxHitsPerYear)
2016-09-11 21:25:14 +02:00
{
extern const char *Txt_YEAR_OF_DEGREE[1+Deg_MAX_YEARS_PER_DEGREE];
extern const char *Txt_teachers_ABBREVIATION;
extern const char *Txt_students_ABBREVIATION;
2016-10-02 20:03:21 +02:00
extern const char *Txt_unknown_removed_course;
2016-10-02 19:55:12 +02:00
extern const char *Txt_no_course_selected;
2016-10-02 19:42:47 +02:00
struct Course Crs;
2016-09-11 21:25:14 +02:00
struct Degree Deg;
2016-10-02 19:42:47 +02:00
/***** Get course data *****/
Crs.CrsCod = CrsCod;
Crs_GetDataOfCourseByCod (&Crs);
/***** Get degree data *****/
Deg.DegCod = Crs.DegCod;
Deg_GetDataOfDegreeByCod (&Deg);
2016-09-11 21:25:14 +02:00
/***** Start row *****/
fprintf (Gbl.F.Out,"<li>");
2016-10-02 19:42:47 +02:00
if (CrsCod > 0) // CrsCod > 0 in log ==> course selected
{
if (Crs.CrsCod > 0) // Course exists
{
/***** Write course full name *****/
fprintf (Gbl.F.Out,"<strong>%s</strong> -",Crs.FullName);
2016-09-11 21:45:29 +02:00
2016-10-02 19:42:47 +02:00
/***** Write year *****/
if (Crs.Year)
fprintf (Gbl.F.Out," %s",Txt_YEAR_OF_DEGREE[Crs.Year]);
2016-09-11 21:45:29 +02:00
2016-10-02 19:42:47 +02:00
/***** Write degree full name *****/
fprintf (Gbl.F.Out," %s",Deg.FullName);
2016-09-11 21:25:14 +02:00
2016-10-02 19:42:47 +02:00
/***** Write number of teachers / students in course *****/
fprintf (Gbl.F.Out," (%u %s / %u %s)",
Usr_GetNumUsrsInCrs (Rol_TEACHER,Crs.CrsCod),Txt_teachers_ABBREVIATION,
Usr_GetNumUsrsInCrs (Rol_STUDENT,Crs.CrsCod),Txt_students_ABBREVIATION);
}
else
2016-10-02 20:03:21 +02:00
fprintf (Gbl.F.Out,"(%s)",Txt_unknown_removed_course);
2016-10-02 19:42:47 +02:00
}
else // CrsCod <= 0 in log ==> no course selected
2016-10-02 19:55:12 +02:00
fprintf (Gbl.F.Out,"(%s)",Txt_no_course_selected);
2016-09-29 01:34:17 +02:00
/***** Write hits per year for this course *****/
2016-10-02 19:42:47 +02:00
fprintf (Gbl.F.Out,"<br />");
Rep_ShowMyHitsPerYear (false,CrsCod,Role,
2016-10-02 17:56:05 +02:00
BrowserTimeZone,FirstClickTimeUTC,tm_FirstClickTime,
MaxHitsPerYear);
2016-09-29 01:34:17 +02:00
fprintf (Gbl.F.Out,"</li>");
2016-09-11 21:25:14 +02:00
}
2016-09-12 00:50:24 +02:00
2016-10-02 17:56:05 +02:00
/*****************************************************************************/
/********************** Write my hits grouped by years ***********************/
/*****************************************************************************/
2016-10-02 19:42:47 +02:00
static void Rep_ShowMyHitsPerYear (bool AnyCourse,long CrsCod,Rol_Role_t Role,
2016-10-02 17:56:05 +02:00
const char *BrowserTimeZone,
time_t FirstClickTimeUTC,
struct tm *tm_FirstClickTime,
unsigned long MaxHitsPerYear)
{
char Query[1024];
char SubQueryCrs[128];
2016-10-02 19:42:47 +02:00
char SubQueryRol[128];
2016-09-12 17:43:23 +02:00
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned long NumRows;
unsigned long NumRow;
unsigned ReadYear;
unsigned LastYear;
unsigned Year;
struct Sta_Hits Hits;
/***** Make the query *****/
2016-10-02 19:42:47 +02:00
if (AnyCourse)
SubQueryCrs[0] = '\0';
else
2016-10-02 17:56:05 +02:00
sprintf (SubQueryCrs," AND CrsCod='%ld'",CrsCod);
2016-10-02 19:42:47 +02:00
if (Role == Rol_UNKNOWN) // Here Rol_UNKNOWN means any role
SubQueryRol[0] = '\0';
2016-09-29 01:34:17 +02:00
else
2016-10-02 19:42:47 +02:00
sprintf (SubQueryRol," AND Role='%u'",(unsigned) Role);
2016-10-02 17:56:05 +02:00
2016-09-12 17:43:23 +02:00
sprintf (Query,"SELECT SQL_NO_CACHE "
"YEAR(CONVERT_TZ(ClickTime,@@session.time_zone,'%s')) AS Year,"
"COUNT(*) FROM log_full"
" WHERE ClickTime>=FROM_UNIXTIME('%ld')"
2016-10-02 19:42:47 +02:00
" AND UsrCod='%ld'%s%s"
2016-09-12 17:43:23 +02:00
" GROUP BY Year DESC",
BrowserTimeZone,
(long) FirstClickTimeUTC,
2016-09-29 01:34:17 +02:00
Gbl.Usrs.Me.UsrDat.UsrCod,
2016-10-02 19:42:47 +02:00
SubQueryCrs,
SubQueryRol);
2016-09-12 17:43:23 +02:00
NumRows = DB_QuerySELECT (Query,&mysql_res,"can not get clicks");
/***** Initialize first year *****/
Gbl.DateRange.DateIni.Date.Year = 1900 + tm_FirstClickTime->tm_year;
/***** Initialize LastYear *****/
LastYear = Gbl.Now.Date.Year;
2016-10-02 17:56:05 +02:00
/***** Set maximum number of hits per year *****/
if (MaxHitsPerYear)
/* Set maximum number of hits per year from parameter */
Hits.Max = (float) MaxHitsPerYear;
2016-09-29 01:48:52 +02:00
else
2016-10-02 17:56:05 +02:00
{
/* Compute maximum number of hits per year */
2016-09-29 01:48:52 +02:00
Sta_ComputeMaxAndTotalHits (&Hits,NumRows,mysql_res,1,1);
2016-10-02 17:56:05 +02:00
mysql_data_seek (mysql_res,0);
}
2016-09-12 17:43:23 +02:00
/***** Write rows *****/
for (NumRow = 1;
NumRow <= NumRows;
NumRow++)
{
row = mysql_fetch_row (mysql_res);
/* Get the year (in row[0] is the date in YYYY format) */
if (sscanf (row[0],"%04u",&ReadYear) != 1)
Lay_ShowErrorAndExit ("Wrong date.");
/* Get number hits (in row[1]) */
Hits.Num = Str_GetFloatNumFromStr (row[1]);
for (Year = LastYear;
Year >= ReadYear;
Year--)
{
/* Write the year */
fprintf (Gbl.F.Out,"%04u ",Year);
/* Draw bar proportional to number of hits */
Rep_DrawBarNumHits (Year == LastYear ? Hits.Num :
0.0,
Hits.Max,Rep_MAX_BAR_WIDTH);
}
LastYear = Year;
}
/***** Finally, show the oldest years without clicks *****/
for (Year = LastYear;
Year >= Gbl.DateRange.DateIni.Date.Year;
Year--)
{
/* Write the year */
fprintf (Gbl.F.Out,"%04u ",Year);
/* Draw bar proportional to number of hits */
Rep_DrawBarNumHits (0.0,Hits.Max,Rep_MAX_BAR_WIDTH);
}
}
/*****************************************************************************/
/********************* Write my hits grouped by months ***********************/
/*****************************************************************************/
/*
static void Rep_ShowMyHitsPerMonth (time_t FirstClickTimeUTC,struct tm *tm_FirstClickTime)
2016-09-12 00:50:24 +02:00
{
char BrowserTimeZone[Dat_MAX_BYTES_TIME_ZONE+1];
char Query[1024];
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned long NumRows;
unsigned long NumRow;
struct Date ReadDate;
struct Date LastDate;
struct Date Date;
unsigned M;
unsigned NumMonthsBetweenLastDateAndCurrentDate;
struct Sta_Hits Hits;
2016-09-12 17:43:23 +02:00
***** Get client time zone *****
2016-09-12 00:50:24 +02:00
Dat_GetBrowserTimeZone (BrowserTimeZone);
2016-09-12 17:43:23 +02:00
***** Make the query *****
2016-09-12 00:50:24 +02:00
sprintf (Query,"SELECT SQL_NO_CACHE "
"DATE_FORMAT(CONVERT_TZ(ClickTime,@@session.time_zone,'%s'),'%%Y%%m') AS Month,"
"COUNT(*) FROM log_full"
" WHERE ClickTime>=FROM_UNIXTIME('%ld')"
" AND UsrCod='%ld'"
" GROUP BY Month DESC",
BrowserTimeZone,
(long) FirstClickTimeUTC,
Gbl.Usrs.Me.UsrDat.UsrCod);
NumRows = DB_QuerySELECT (Query,&mysql_res,"can not get clicks");
2016-09-12 17:43:23 +02:00
***** Initialize first date *****
2016-09-12 00:50:24 +02:00
Gbl.DateRange.DateIni.Date.Year = 1900 + tm_FirstClickTime->tm_year;
Gbl.DateRange.DateIni.Date.Month = 1 + tm_FirstClickTime->tm_mon;
Gbl.DateRange.DateIni.Date.Day = tm_FirstClickTime->tm_mday;
2016-09-12 17:43:23 +02:00
***** Initialize LastDate *****
2016-09-12 00:50:24 +02:00
Dat_AssignDate (&LastDate,&Gbl.Now.Date);
2016-09-12 17:43:23 +02:00
***** Compute maximum number of hits per month *****
2016-09-12 00:50:24 +02:00
Sta_ComputeMaxAndTotalHits (&Hits,NumRows,mysql_res,1,1);
2016-09-12 17:43:23 +02:00
***** Write rows *****
2016-09-12 00:50:24 +02:00
mysql_data_seek (mysql_res,0);
for (NumRow = 1;
NumRow <= NumRows;
NumRow++)
{
row = mysql_fetch_row (mysql_res);
2016-09-12 17:43:23 +02:00
* Get the year and the month (in row[0] is the date in YYYYMM format) *
2016-09-12 00:50:24 +02:00
if (sscanf (row[0],"%04u%02u",&ReadDate.Year,&ReadDate.Month) != 2)
Lay_ShowErrorAndExit ("Wrong date.");
2016-09-12 17:43:23 +02:00
* Get number hits (in row[1]) *
2016-09-12 00:50:24 +02:00
Hits.Num = Str_GetFloatNumFromStr (row[1]);
Dat_AssignDate (&Date,&LastDate);
NumMonthsBetweenLastDateAndCurrentDate = Dat_GetNumMonthsBetweenDates (&ReadDate,&LastDate);
for (M = 1;
M <= NumMonthsBetweenLastDateAndCurrentDate;
M++)
{
2016-09-12 17:43:23 +02:00
* Write the month *
2016-09-12 10:02:17 +02:00
fprintf (Gbl.F.Out,"%04u-%02u ",Date.Year,Date.Month);
2016-09-12 17:43:23 +02:00
* Draw bar proportional to number of hits *
2016-09-12 10:02:17 +02:00
Rep_DrawBarNumHits (M == NumMonthsBetweenLastDateAndCurrentDate ? Hits.Num :
2016-09-12 00:50:24 +02:00
0.0,
2016-09-12 10:02:17 +02:00
Hits.Max,Rep_MAX_BAR_WIDTH);
2016-09-12 00:50:24 +02:00
2016-09-12 17:43:23 +02:00
* Decrease month *
2016-09-12 00:50:24 +02:00
Dat_GetMonthBefore (&Date,&Date);
}
Dat_AssignDate (&LastDate,&Date);
}
2016-09-12 17:43:23 +02:00
***** Finally, show the oldest months without clicks *****
2016-09-12 00:50:24 +02:00
NumMonthsBetweenLastDateAndCurrentDate = Dat_GetNumMonthsBetweenDates (&Gbl.DateRange.DateIni.Date,&LastDate);
for (M = 1;
M <= NumMonthsBetweenLastDateAndCurrentDate;
M++)
{
2016-09-12 17:43:23 +02:00
* Write the month *
2016-09-12 10:02:17 +02:00
fprintf (Gbl.F.Out,"%04u-%02u ",Date.Year,Date.Month);
2016-09-12 00:50:24 +02:00
2016-09-12 17:43:23 +02:00
* Draw bar proportional to number of hits *
2016-09-12 10:02:17 +02:00
Rep_DrawBarNumHits (0.0,Hits.Max,Rep_MAX_BAR_WIDTH);
2016-09-12 00:50:24 +02:00
2016-09-12 17:43:23 +02:00
* Decrease month *
2016-09-12 00:50:24 +02:00
Dat_GetMonthBefore (&Date,&Date);
}
}
2016-09-12 17:43:23 +02:00
*/
2016-09-12 01:06:36 +02:00
/*****************************************************************************/
/********************* Draw a bar with the number of hits ********************/
/*****************************************************************************/
2016-09-12 10:02:17 +02:00
static void Rep_DrawBarNumHits (float HitsNum,float HitsMax,
unsigned MaxBarWidth)
2016-09-12 01:06:36 +02:00
{
unsigned BarWidth;
2016-09-12 10:02:17 +02:00
unsigned i;
2016-09-12 01:06:36 +02:00
if (HitsNum != 0.0)
{
/***** Draw bar with a with proportional to the number of hits *****/
BarWidth = (unsigned) (((HitsNum * (float) MaxBarWidth) / HitsMax) + 0.5);
2016-09-12 10:02:17 +02:00
for (i = 0;
i < BarWidth;
i++)
fprintf (Gbl.F.Out,Rep_BLOCK);
2016-09-12 01:06:36 +02:00
/***** Write the number of hits *****/
2016-09-12 10:02:17 +02:00
fprintf (Gbl.F.Out," ");
2016-09-12 01:06:36 +02:00
Str_WriteFloatNum (HitsNum);
}
else
/***** Write the number of clicks *****/
fprintf (Gbl.F.Out,"0");
2016-09-12 10:02:17 +02:00
fprintf (Gbl.F.Out,"<br />");
2016-09-12 01:06:36 +02:00
}