From 364b133b284546d8ded207b25eb773f44c199759 Mon Sep 17 00:00:00 2001 From: acanas Date: Tue, 6 Apr 2021 22:34:23 +0200 Subject: [PATCH] Version 20.58: Apr 06, 2021 Optimizations in database selects. --- swad_changelog.h | 3 +- swad_database.c | 36 ++++ swad_database.h | 2 + swad_exam.c | 165 +++++++--------- swad_figure.c | 196 +++++++++--------- swad_game.c | 165 +++++++--------- swad_survey.c | 193 ++++++++---------- swad_user.c | 502 ++++++++++++++++++++++------------------------- 8 files changed, 597 insertions(+), 665 deletions(-) diff --git a/swad_changelog.h b/swad_changelog.h index 6b74859be..b6f6cc98d 100644 --- a/swad_changelog.h +++ b/swad_changelog.h @@ -600,13 +600,14 @@ TODO: Salvador Romero Cort TODO: FIX BUG, URGENT! En las fechas como parámetro Dat_WriteParamsIniEndDates(), por ejemplo al cambiar el color de la gráfica de accesos por día y hora, no se respeta la zona horaria. */ -#define Log_PLATFORM_VERSION "SWAD 20.57 (2021-04-05)" +#define Log_PLATFORM_VERSION "SWAD 20.58 (2021-04-06)" #define CSS_FILE "swad20.45.css" #define JS_FILE "swad20.6.2.js" /* TODO: Rename CENTRE to CENTER in help wiki. TODO: Rename ASSESSMENT.Announcements to ASSESSMENT.Calls_for_exams + Version 20.58: Apr 06, 2021 Optimizations in database selects. (308989 lines) Version 20.57: Apr 05, 2021 Optimizations in database selects. (309041 lines) Version 20.56.1: Apr 04, 2021 Optimizations in database selects. (309252 lines) Version 20.56: Apr 04, 2021 Optimizations in database selects. (309296 lines) diff --git a/swad_database.c b/swad_database.c index bcacd628f..4ca00aa79 100644 --- a/swad_database.c +++ b/swad_database.c @@ -3836,6 +3836,42 @@ long DB_QuerySELECTCode (const char *MsgError, return Cod; } +/*****************************************************************************/ +/**** Make a SELECT query for a unique row with one double from database *****/ +/*****************************************************************************/ + +double DB_QuerySELECTDouble (const char *MsgError, + const char *fmt,...) + { + MYSQL_RES *mysql_res; + MYSQL_ROW row; + va_list ap; + int NumBytesPrinted; + char *Query; + double DoubleNum; + + /***** Create query string *****/ + va_start (ap,fmt); + NumBytesPrinted = vasprintf (&Query,fmt,ap); + va_end (ap); + if (NumBytesPrinted < 0) // -1 if no memory or any other error + Lay_NotEnoughMemoryExit (); + + /***** Do SELECT query *****/ + if (DB_QuerySELECTusingQueryStr (Query,&mysql_res,MsgError)) // Row found + { + row = mysql_fetch_row (mysql_res); + DoubleNum = Str_GetDoubleFromStr (row[0]); + } + else + DoubleNum = 0.0; + + /***** Free structure that stores the query result *****/ + DB_FreeMySQLResult (&mysql_res); + + return DoubleNum; + } + /*****************************************************************************/ /*********** Make a SELECT query from database using query string ************/ /*****************************************************************************/ diff --git a/swad_database.h b/swad_database.h index 78a29d1c7..d76b2e50c 100644 --- a/swad_database.h +++ b/swad_database.h @@ -43,6 +43,8 @@ unsigned long DB_QuerySELECT (MYSQL_RES **mysql_res,const char *MsgError, const char *fmt,...); long DB_QuerySELECTCode (const char *MsgError, const char *fmt,...); +double DB_QuerySELECTDouble (const char *MsgError, + const char *fmt,...); long DB_GetNextCode (MYSQL_RES *mysql_res); unsigned long DB_GetNumRowsTable (const char *Table); unsigned long DB_QueryCOUNT (const char *MsgError,const char *fmt,...); diff --git a/swad_exam.c b/swad_exam.c index 40bcfebf8..4bfd8bfc6 100644 --- a/swad_exam.c +++ b/swad_exam.c @@ -2040,110 +2040,91 @@ unsigned Exa_GetNumExams (Hie_Lvl_Level_t Scope) double Exa_GetNumQstsPerCrsExam (Hie_Lvl_Level_t Scope) { - MYSQL_RES *mysql_res; - MYSQL_ROW row; - double NumQstsPerExam; - /***** Get number of questions per exam from database *****/ switch (Scope) { case Hie_Lvl_SYS: - DB_QuerySELECT (&mysql_res,"can not get number of questions per exam", - "SELECT AVG(NumQsts)" // row[0] - " FROM (SELECT COUNT(exa_set_questions.QstCod) AS NumQsts" - " FROM exa_exams," - "exa_set_questions" - " WHERE exa_exams.ExaCod=exa_set_questions.ExaCod" - " GROUP BY exa_set_questions.ExaCod) AS NumQstsTable"); - break; + return DB_QuerySELECTDouble ("can not get number of questions per exam", + "SELECT AVG(NumQsts)" + " FROM (SELECT COUNT(exa_set_questions.QstCod) AS NumQsts" + " FROM exa_exams," + "exa_set_questions" + " WHERE exa_exams.ExaCod=exa_set_questions.ExaCod" + " GROUP BY exa_set_questions.ExaCod) AS NumQstsTable"); case Hie_Lvl_CTY: - DB_QuerySELECT (&mysql_res,"can not get number of questions per exam", - "SELECT AVG(NumQsts)" // row[0] - " FROM (SELECT COUNT(exa_set_questions.QstCod) AS NumQsts" - " FROM ins_instits," - "ctr_centers," - "deg_degrees," - "crs_courses," - "exa_exams," - "exa_set_questions" - " WHERE ins_instits.CtyCod=%ld" - " AND ins_instits.InsCod=ctr_centers.InsCod" - " AND ctr_centers.CtrCod=deg_degrees.CtrCod" - " AND deg_degrees.DegCod=crs_courses.DegCod" - " AND crs_courses.CrsCod=exa_exams.CrsCod" - " AND exa_exams.ExaCod=exa_set_questions.ExaCod" - " GROUP BY exa_set_questions.ExaCod) AS NumQstsTable", - Gbl.Hierarchy.Cty.CtyCod); - break; + return DB_QuerySELECTDouble ("can not get number of questions per exam", + "SELECT AVG(NumQsts)" + " FROM (SELECT COUNT(exa_set_questions.QstCod) AS NumQsts" + " FROM ins_instits," + "ctr_centers," + "deg_degrees," + "crs_courses," + "exa_exams," + "exa_set_questions" + " WHERE ins_instits.CtyCod=%ld" + " AND ins_instits.InsCod=ctr_centers.InsCod" + " AND ctr_centers.CtrCod=deg_degrees.CtrCod" + " AND deg_degrees.DegCod=crs_courses.DegCod" + " AND crs_courses.CrsCod=exa_exams.CrsCod" + " AND exa_exams.ExaCod=exa_set_questions.ExaCod" + " GROUP BY exa_set_questions.ExaCod) AS NumQstsTable", + Gbl.Hierarchy.Cty.CtyCod); case Hie_Lvl_INS: - DB_QuerySELECT (&mysql_res,"can not get number of questions per exam", - "SELECT AVG(NumQsts)" // row[0] - " FROM (SELECT COUNT(exa_set_questions.QstCod) AS NumQsts" - " FROM ctr_centers," - "deg_degrees," - "crs_courses," - "exa_exams," - "exa_set_questions" - " WHERE ctr_centers.InsCod=%ld" - " AND ctr_centers.CtrCod=deg_degrees.CtrCod" - " AND deg_degrees.DegCod=crs_courses.DegCod" - " AND crs_courses.CrsCod=exa_exams.CrsCod" - " AND exa_exams.ExaCod=exa_set_questions.ExaCod" - " GROUP BY exa_set_questions.ExaCod) AS NumQstsTable", - Gbl.Hierarchy.Ins.InsCod); - break; + return DB_QuerySELECTDouble ("can not get number of questions per exam", + "SELECT AVG(NumQsts)" + " FROM (SELECT COUNT(exa_set_questions.QstCod) AS NumQsts" + " FROM ctr_centers," + "deg_degrees," + "crs_courses," + "exa_exams," + "exa_set_questions" + " WHERE ctr_centers.InsCod=%ld" + " AND ctr_centers.CtrCod=deg_degrees.CtrCod" + " AND deg_degrees.DegCod=crs_courses.DegCod" + " AND crs_courses.CrsCod=exa_exams.CrsCod" + " AND exa_exams.ExaCod=exa_set_questions.ExaCod" + " GROUP BY exa_set_questions.ExaCod) AS NumQstsTable", + Gbl.Hierarchy.Ins.InsCod); case Hie_Lvl_CTR: - DB_QuerySELECT (&mysql_res,"can not get number of questions per exam", - "SELECT AVG(NumQsts)" // row[0] - " FROM (SELECT COUNT(exa_set_questions.QstCod) AS NumQsts" - " FROM deg_degrees," - "crs_courses," - "exa_exams," - "exa_set_questions" - " WHERE deg_degrees.CtrCod=%ld" - " AND deg_degrees.DegCod=crs_courses.DegCod" - " AND crs_courses.CrsCod=exa_exams.CrsCod" - " AND exa_exams.ExaCod=exa_set_questions.ExaCod" - " GROUP BY exa_set_questions.ExaCod) AS NumQstsTable", - Gbl.Hierarchy.Ctr.CtrCod); - break; + return DB_QuerySELECTDouble ("can not get number of questions per exam", + "SELECT AVG(NumQsts)" + " FROM (SELECT COUNT(exa_set_questions.QstCod) AS NumQsts" + " FROM deg_degrees," + "crs_courses," + "exa_exams," + "exa_set_questions" + " WHERE deg_degrees.CtrCod=%ld" + " AND deg_degrees.DegCod=crs_courses.DegCod" + " AND crs_courses.CrsCod=exa_exams.CrsCod" + " AND exa_exams.ExaCod=exa_set_questions.ExaCod" + " GROUP BY exa_set_questions.ExaCod) AS NumQstsTable", + Gbl.Hierarchy.Ctr.CtrCod); case Hie_Lvl_DEG: - DB_QuerySELECT (&mysql_res,"can not get number of questions per exam", - "SELECT AVG(NumQsts)" // row[0] - " FROM (SELECT COUNT(exa_set_questions.QstCod) AS NumQsts" - " FROM crs_courses," - "exa_exams," - "exa_set_questions" - " WHERE crs_courses.DegCod=%ld" - " AND crs_courses.CrsCod=exa_exams.CrsCod" - " AND exa_exams.ExaCod=exa_set_questions.ExaCod" - " GROUP BY exa_set_questions.ExaCod) AS NumQstsTable", - Gbl.Hierarchy.Deg.DegCod); - break; + return DB_QuerySELECTDouble ("can not get number of questions per exam", + "SELECT AVG(NumQsts)" + " FROM (SELECT COUNT(exa_set_questions.QstCod) AS NumQsts" + " FROM crs_courses," + "exa_exams," + "exa_set_questions" + " WHERE crs_courses.DegCod=%ld" + " AND crs_courses.CrsCod=exa_exams.CrsCod" + " AND exa_exams.ExaCod=exa_set_questions.ExaCod" + " GROUP BY exa_set_questions.ExaCod) AS NumQstsTable", + Gbl.Hierarchy.Deg.DegCod); case Hie_Lvl_CRS: - DB_QuerySELECT (&mysql_res,"can not get number of questions per exam", - "SELECT AVG(NumQsts)" // row[0] - " FROM (SELECT COUNT(exa_set_questions.QstCod) AS NumQsts" - " FROM exa_exams," - "exa_set_questions" - " WHERE exa_exams.Cod=%ld" - " AND exa_exams.ExaCod=exa_set_questions.ExaCod" - " GROUP BY exa_set_questions.ExaCod) AS NumQstsTable", - Gbl.Hierarchy.Crs.CrsCod); - break; + return DB_QuerySELECTDouble ("can not get number of questions per exam", + "SELECT AVG(NumQsts)" + " FROM (SELECT COUNT(exa_set_questions.QstCod) AS NumQsts" + " FROM exa_exams," + "exa_set_questions" + " WHERE exa_exams.Cod=%ld" + " AND exa_exams.ExaCod=exa_set_questions.ExaCod" + " GROUP BY exa_set_questions.ExaCod) AS NumQstsTable", + Gbl.Hierarchy.Crs.CrsCod); default: Lay_WrongScopeExit (); - break; + return 0.0; // Not reached } - - /***** Get average number of questions per exam *****/ - row = mysql_fetch_row (mysql_res); - NumQstsPerExam = Str_GetDoubleFromStr (row[0]); - - /***** Free structure that stores the query result *****/ - DB_FreeMySQLResult (&mysql_res); - - return NumQstsPerExam; } /*****************************************************************************/ diff --git a/swad_figure.c b/swad_figure.c index 21ae0e6d9..affb3859c 100644 --- a/swad_figure.c +++ b/swad_figure.c @@ -3895,8 +3895,6 @@ static void Fig_GetAndShowFollowStats (void) "FollowedCod", "FollowerCod" }; - MYSQL_RES *mysql_res; - MYSQL_ROW row; unsigned Fol; unsigned NumUsrsTotal; unsigned NumUsrs; @@ -4048,122 +4046,116 @@ static void Fig_GetAndShowFollowStats (void) switch (Gbl.Scope.Current) { case Hie_Lvl_SYS: - DB_QuerySELECT (&mysql_res,"can not get number of questions" - " per survey", - "SELECT AVG(N)" // row[0] - " FROM (SELECT COUNT(%s) AS N" - " FROM usr_follow" - " GROUP BY %s) AS F", - FieldDB[Fol], - FieldDB[1 - Fol]); + Average = DB_QuerySELECTDouble ("can not get number of questions" + " per survey", + "SELECT AVG(N)" // row[0] + " FROM (SELECT COUNT(%s) AS N" + " FROM usr_follow" + " GROUP BY %s) AS F", + FieldDB[Fol], + FieldDB[1 - Fol]); break; case Hie_Lvl_CTY: - DB_QuerySELECT (&mysql_res,"can not get number of questions" - " per survey", - "SELECT AVG(N)" // row[0] - " FROM (SELECT COUNT(DISTINCT usr_follow.%s) AS N" - " FROM ins_instits," - "ctr_centers," - "deg_degrees," - "crs_courses," - "crs_users," - "usr_follow" - " WHERE ins_instits.CtyCod=%ld" - " AND ins_instits.InsCod=ctr_centers.InsCod" - " AND ctr_centers.CtrCod=deg_degrees.CtrCod" - " AND deg_degrees.DegCod=crs_courses.DegCod" - " AND crs_courses.CrsCod=crs_users.CrsCod" - " AND crs_users.UsrCod=usr_follow.%s" - " GROUP BY %s) AS F", - FieldDB[Fol], - Gbl.Hierarchy.Cty.CtyCod, - FieldDB[Fol], - FieldDB[1 - Fol]); + Average = DB_QuerySELECTDouble ("can not get number of questions" + " per survey", + "SELECT AVG(N)" // row[0] + " FROM (SELECT COUNT(DISTINCT usr_follow.%s) AS N" + " FROM ins_instits," + "ctr_centers," + "deg_degrees," + "crs_courses," + "crs_users," + "usr_follow" + " WHERE ins_instits.CtyCod=%ld" + " AND ins_instits.InsCod=ctr_centers.InsCod" + " AND ctr_centers.CtrCod=deg_degrees.CtrCod" + " AND deg_degrees.DegCod=crs_courses.DegCod" + " AND crs_courses.CrsCod=crs_users.CrsCod" + " AND crs_users.UsrCod=usr_follow.%s" + " GROUP BY %s) AS F", + FieldDB[Fol], + Gbl.Hierarchy.Cty.CtyCod, + FieldDB[Fol], + FieldDB[1 - Fol]); break; case Hie_Lvl_INS: - DB_QuerySELECT (&mysql_res,"can not get number of questions" - " per survey", - "SELECT AVG(N)" // row[0] - " FROM (SELECT COUNT(DISTINCT usr_follow.%s) AS N" - " FROM ctr_centers," - "deg_degrees," - "crs_courses," - "crs_users," - "usr_follow" - " WHERE ctr_centers.InsCod=%ld" - " AND ctr_centers.CtrCod=deg_degrees.CtrCod" - " AND deg_degrees.DegCod=crs_courses.DegCod" - " AND crs_courses.CrsCod=crs_users.CrsCod" - " AND crs_users.UsrCod=usr_follow.%s" - " GROUP BY %s) AS F", - FieldDB[Fol], - Gbl.Hierarchy.Ins.InsCod, - FieldDB[Fol], - FieldDB[1 - Fol]); + Average = DB_QuerySELECTDouble ("can not get number of questions" + " per survey", + "SELECT AVG(N)" // row[0] + " FROM (SELECT COUNT(DISTINCT usr_follow.%s) AS N" + " FROM ctr_centers," + "deg_degrees," + "crs_courses," + "crs_users," + "usr_follow" + " WHERE ctr_centers.InsCod=%ld" + " AND ctr_centers.CtrCod=deg_degrees.CtrCod" + " AND deg_degrees.DegCod=crs_courses.DegCod" + " AND crs_courses.CrsCod=crs_users.CrsCod" + " AND crs_users.UsrCod=usr_follow.%s" + " GROUP BY %s) AS F", + FieldDB[Fol], + Gbl.Hierarchy.Ins.InsCod, + FieldDB[Fol], + FieldDB[1 - Fol]); break; case Hie_Lvl_CTR: - DB_QuerySELECT (&mysql_res,"can not get number of questions" - " per survey", - "SELECT AVG(N)" // row[0] - " FROM (SELECT COUNT(DISTINCT usr_follow.%s) AS N" - " FROM deg_degrees," - "crs_courses," - "crs_users," - "usr_follow" - " WHERE deg_degrees.CtrCod=%ld" - " AND deg_degrees.DegCod=crs_courses.DegCod" - " AND crs_courses.CrsCod=crs_users.CrsCod" - " AND crs_users.UsrCod=usr_follow.%s" - " GROUP BY %s) AS F", - FieldDB[Fol], - Gbl.Hierarchy.Ctr.CtrCod, - FieldDB[Fol], - FieldDB[1 - Fol]); + Average = DB_QuerySELECTDouble ("can not get number of questions" + " per survey", + "SELECT AVG(N)" // row[0] + " FROM (SELECT COUNT(DISTINCT usr_follow.%s) AS N" + " FROM deg_degrees," + "crs_courses," + "crs_users," + "usr_follow" + " WHERE deg_degrees.CtrCod=%ld" + " AND deg_degrees.DegCod=crs_courses.DegCod" + " AND crs_courses.CrsCod=crs_users.CrsCod" + " AND crs_users.UsrCod=usr_follow.%s" + " GROUP BY %s) AS F", + FieldDB[Fol], + Gbl.Hierarchy.Ctr.CtrCod, + FieldDB[Fol], + FieldDB[1 - Fol]); break; case Hie_Lvl_DEG: - DB_QuerySELECT (&mysql_res,"can not get number of questions" - " per survey", - "SELECT AVG(N)" // row[0] - " FROM (SELECT COUNT(DISTINCT usr_follow.%s) AS N" - " FROM crs_courses," - "crs_users," - "usr_follow" - " WHERE crs_courses.DegCod=%ld" - " AND crs_courses.CrsCod=crs_users.CrsCod" - " AND crs_users.UsrCod=usr_follow.%s" - " GROUP BY %s) AS F", - FieldDB[Fol], - Gbl.Hierarchy.Deg.DegCod, - FieldDB[Fol], - FieldDB[1 - Fol]); + Average = DB_QuerySELECTDouble ("can not get number of questions" + " per survey", + "SELECT AVG(N)" // row[0] + " FROM (SELECT COUNT(DISTINCT usr_follow.%s) AS N" + " FROM crs_courses," + "crs_users," + "usr_follow" + " WHERE crs_courses.DegCod=%ld" + " AND crs_courses.CrsCod=crs_users.CrsCod" + " AND crs_users.UsrCod=usr_follow.%s" + " GROUP BY %s) AS F", + FieldDB[Fol], + Gbl.Hierarchy.Deg.DegCod, + FieldDB[Fol], + FieldDB[1 - Fol]); break; case Hie_Lvl_CRS: - DB_QuerySELECT (&mysql_res,"can not get number of questions" - " per survey", - "SELECT AVG(N)" // row[0] - " FROM (SELECT COUNT(DISTINCT usr_follow.%s) AS N" - " FROM crs_users," - "usr_follow" - " WHERE crs_users.CrsCod=%ld" - " AND crs_users.UsrCod=usr_follow.%s" - " GROUP BY %s) AS F", - FieldDB[Fol], - Gbl.Hierarchy.Crs.CrsCod, - FieldDB[Fol], - FieldDB[1 - Fol]); + Average = DB_QuerySELECTDouble ("can not get number of questions" + " per survey", + "SELECT AVG(N)" // row[0] + " FROM (SELECT COUNT(DISTINCT usr_follow.%s) AS N" + " FROM crs_users," + "usr_follow" + " WHERE crs_users.CrsCod=%ld" + " AND crs_users.UsrCod=usr_follow.%s" + " GROUP BY %s) AS F", + FieldDB[Fol], + Gbl.Hierarchy.Crs.CrsCod, + FieldDB[Fol], + FieldDB[1 - Fol]); break; default: Lay_WrongScopeExit (); + Average = 0.0; // Not reached break; } - /***** Get average *****/ - row = mysql_fetch_row (mysql_res); - Average = Str_GetDoubleFromStr (row[0]); - - /***** Free structure that stores the query result *****/ - DB_FreeMySQLResult (&mysql_res); - /***** Write number of followed per follower *****/ HTM_TR_Begin (NULL); diff --git a/swad_game.c b/swad_game.c index 74fc4a1d0..8416cd67f 100644 --- a/swad_game.c +++ b/swad_game.c @@ -2817,110 +2817,91 @@ unsigned Gam_GetNumGames (Hie_Lvl_Level_t Scope) double Gam_GetNumQstsPerCrsGame (Hie_Lvl_Level_t Scope) { - MYSQL_RES *mysql_res; - MYSQL_ROW row; - double NumQstsPerGame; - /***** Get number of questions per game from database *****/ switch (Scope) { case Hie_Lvl_SYS: - DB_QuerySELECT (&mysql_res,"can not get number of questions per game", - "SELECT AVG(NumQsts)" // row[0] - " FROM (SELECT COUNT(gam_questions.QstCod) AS NumQsts" - " FROM gam_games," - "gam_questions" - " WHERE gam_games.GamCod=gam_questions.GamCod" - " GROUP BY gam_questions.GamCod) AS NumQstsTable"); - break; + return DB_QuerySELECTDouble ("can not get number of questions per game", + "SELECT AVG(NumQsts)" + " FROM (SELECT COUNT(gam_questions.QstCod) AS NumQsts" + " FROM gam_games," + "gam_questions" + " WHERE gam_games.GamCod=gam_questions.GamCod" + " GROUP BY gam_questions.GamCod) AS NumQstsTable"); case Hie_Lvl_CTY: - DB_QuerySELECT (&mysql_res,"can not get number of questions per game", - "SELECT AVG(NumQsts)" // row[0] - " FROM (SELECT COUNT(gam_questions.QstCod) AS NumQsts" - " FROM ins_instits," - "ctr_centers," - "deg_degrees," - "crs_courses," - "gam_games," - "gam_questions" - " WHERE ins_instits.CtyCod=%ld" - " AND ins_instits.InsCod=ctr_centers.InsCod" - " AND ctr_centers.CtrCod=deg_degrees.CtrCod" - " AND deg_degrees.DegCod=crs_courses.DegCod" - " AND crs_courses.CrsCod=gam_games.CrsCod" - " AND gam_games.GamCod=gam_questions.GamCod" - " GROUP BY gam_questions.GamCod) AS NumQstsTable", - Gbl.Hierarchy.Cty.CtyCod); - break; + return DB_QuerySELECTDouble ("can not get number of questions per game", + "SELECT AVG(NumQsts)" + " FROM (SELECT COUNT(gam_questions.QstCod) AS NumQsts" + " FROM ins_instits," + "ctr_centers," + "deg_degrees," + "crs_courses," + "gam_games," + "gam_questions" + " WHERE ins_instits.CtyCod=%ld" + " AND ins_instits.InsCod=ctr_centers.InsCod" + " AND ctr_centers.CtrCod=deg_degrees.CtrCod" + " AND deg_degrees.DegCod=crs_courses.DegCod" + " AND crs_courses.CrsCod=gam_games.CrsCod" + " AND gam_games.GamCod=gam_questions.GamCod" + " GROUP BY gam_questions.GamCod) AS NumQstsTable", + Gbl.Hierarchy.Cty.CtyCod); case Hie_Lvl_INS: - DB_QuerySELECT (&mysql_res,"can not get number of questions per game", - "SELECT AVG(NumQsts)" // row[0] - " FROM (SELECT COUNT(gam_questions.QstCod) AS NumQsts" - " FROM ctr_centers," - "deg_degrees," - "crs_courses," - "gam_games," - "gam_questions" - " WHERE ctr_centers.InsCod=%ld" - " AND ctr_centers.CtrCod=deg_degrees.CtrCod" - " AND deg_degrees.DegCod=crs_courses.DegCod" - " AND crs_courses.CrsCod=gam_games.CrsCod" - " AND gam_games.GamCod=gam_questions.GamCod" - " GROUP BY gam_questions.GamCod) AS NumQstsTable", - Gbl.Hierarchy.Ins.InsCod); - break; + return DB_QuerySELECTDouble ("can not get number of questions per game", + "SELECT AVG(NumQsts)" + " FROM (SELECT COUNT(gam_questions.QstCod) AS NumQsts" + " FROM ctr_centers," + "deg_degrees," + "crs_courses," + "gam_games," + "gam_questions" + " WHERE ctr_centers.InsCod=%ld" + " AND ctr_centers.CtrCod=deg_degrees.CtrCod" + " AND deg_degrees.DegCod=crs_courses.DegCod" + " AND crs_courses.CrsCod=gam_games.CrsCod" + " AND gam_games.GamCod=gam_questions.GamCod" + " GROUP BY gam_questions.GamCod) AS NumQstsTable", + Gbl.Hierarchy.Ins.InsCod); case Hie_Lvl_CTR: - DB_QuerySELECT (&mysql_res,"can not get number of questions per game", - "SELECT AVG(NumQsts)" // row[0] - " FROM (SELECT COUNT(gam_questions.QstCod) AS NumQsts" - " FROM deg_degrees," - "crs_courses," - "gam_games," - "gam_questions" - " WHERE deg_degrees.CtrCod=%ld" - " AND deg_degrees.DegCod=crs_courses.DegCod" - " AND crs_courses.CrsCod=gam_games.CrsCod" - " AND gam_games.GamCod=gam_questions.GamCod" - " GROUP BY gam_questions.GamCod) AS NumQstsTable", - Gbl.Hierarchy.Ctr.CtrCod); - break; + return DB_QuerySELECTDouble ("can not get number of questions per game", + "SELECT AVG(NumQsts)" + " FROM (SELECT COUNT(gam_questions.QstCod) AS NumQsts" + " FROM deg_degrees," + "crs_courses," + "gam_games," + "gam_questions" + " WHERE deg_degrees.CtrCod=%ld" + " AND deg_degrees.DegCod=crs_courses.DegCod" + " AND crs_courses.CrsCod=gam_games.CrsCod" + " AND gam_games.GamCod=gam_questions.GamCod" + " GROUP BY gam_questions.GamCod) AS NumQstsTable", + Gbl.Hierarchy.Ctr.CtrCod); case Hie_Lvl_DEG: - DB_QuerySELECT (&mysql_res,"can not get number of questions per game", - "SELECT AVG(NumQsts)" // row[0] - " FROM (SELECT COUNT(gam_questions.QstCod) AS NumQsts" - " FROM crs_courses," - "gam_games," - "gam_questions" - " WHERE crs_courses.DegCod=%ld" - " AND crs_courses.CrsCod=gam_games.CrsCod" - " AND gam_games.GamCod=gam_questions.GamCod" - " GROUP BY gam_questions.GamCod) AS NumQstsTable", - Gbl.Hierarchy.Deg.DegCod); - break; + return DB_QuerySELECTDouble ("can not get number of questions per game", + "SELECT AVG(NumQsts)" + " FROM (SELECT COUNT(gam_questions.QstCod) AS NumQsts" + " FROM crs_courses," + "gam_games," + "gam_questions" + " WHERE crs_courses.DegCod=%ld" + " AND crs_courses.CrsCod=gam_games.CrsCod" + " AND gam_games.GamCod=gam_questions.GamCod" + " GROUP BY gam_questions.GamCod) AS NumQstsTable", + Gbl.Hierarchy.Deg.DegCod); case Hie_Lvl_CRS: - DB_QuerySELECT (&mysql_res,"can not get number of questions per game", - "SELECT AVG(NumQsts)" // row[0] - " FROM (SELECT COUNT(gam_questions.QstCod) AS NumQsts" - " FROM gam_games," - "gam_questions" - " WHERE gam_games.Cod=%ld" - " AND gam_games.GamCod=gam_questions.GamCod" - " GROUP BY gam_questions.GamCod) AS NumQstsTable", - Gbl.Hierarchy.Crs.CrsCod); - break; + return DB_QuerySELECTDouble ("can not get number of questions per game", + "SELECT AVG(NumQsts)" + " FROM (SELECT COUNT(gam_questions.QstCod) AS NumQsts" + " FROM gam_games," + "gam_questions" + " WHERE gam_games.Cod=%ld" + " AND gam_games.GamCod=gam_questions.GamCod" + " GROUP BY gam_questions.GamCod) AS NumQstsTable", + Gbl.Hierarchy.Crs.CrsCod); default: Lay_WrongScopeExit (); - break; + return 0.0; // Not reached } - - /***** Get average number of questions per game *****/ - row = mysql_fetch_row (mysql_res); - NumQstsPerGame = Str_GetDoubleFromStr (row[0]); - - /***** Free structure that stores the query result *****/ - DB_FreeMySQLResult (&mysql_res); - - return NumQstsPerGame; } /*****************************************************************************/ diff --git a/swad_survey.c b/swad_survey.c index 7c9746d28..07793d2f9 100644 --- a/swad_survey.c +++ b/swad_survey.c @@ -4131,125 +4131,100 @@ unsigned Svy_GetNumCrsSurveys (Hie_Lvl_Level_t Scope,unsigned *NumNotif) double Svy_GetNumQstsPerCrsSurvey (Hie_Lvl_Level_t Scope) { - MYSQL_RES *mysql_res; - MYSQL_ROW row; - double NumQstsPerSurvey; - /***** Get number of questions per survey from database *****/ switch (Scope) { case Hie_Lvl_SYS: - DB_QuerySELECT (&mysql_res,"can not get number of questions" - " per survey", - "SELECT AVG(NumQsts)" // row[0] - " FROM (SELECT COUNT(svy_questions.QstCod) AS NumQsts" - " FROM svy_surveys," - "svy_questions" - " WHERE svy_surveys.Scope='%s'" - " AND svy_surveys.SvyCod=svy_questions.SvyCod" - " GROUP BY svy_questions.SvyCod) AS NumQstsTable", - Sco_GetDBStrFromScope (Hie_Lvl_CRS)); - break; + return DB_QuerySELECTDouble ("can not get number of questions per survey", + "SELECT AVG(NumQsts)" + " FROM (SELECT COUNT(svy_questions.QstCod) AS NumQsts" + " FROM svy_surveys," + "svy_questions" + " WHERE svy_surveys.Scope='%s'" + " AND svy_surveys.SvyCod=svy_questions.SvyCod" + " GROUP BY svy_questions.SvyCod) AS NumQstsTable", + Sco_GetDBStrFromScope (Hie_Lvl_CRS)); case Hie_Lvl_CTY: - DB_QuerySELECT (&mysql_res,"can not get number of questions" - " per survey", - "SELECT AVG(NumQsts)" // row[0] - " FROM (SELECT COUNT(svy_questions.QstCod) AS NumQsts" - " FROM ins_instits," - "ctr_centers," - "deg_degrees," - "crs_courses," - "svy_surveys," - "svy_questions" - " WHERE ins_instits.CtyCod=%ld" - " AND ins_instits.InsCod=ctr_centers.InsCod" - " AND ctr_centers.CtrCod=deg_degrees.CtrCod" - " AND deg_degrees.DegCod=crs_courses.DegCod" - " AND crs_courses.CrsCod=svy_surveys.Cod" - " AND svy_surveys.Scope='%s'" - " AND svy_surveys.SvyCod=svy_questions.SvyCod" - " GROUP BY svy_questions.SvyCod) AS NumQstsTable", - Gbl.Hierarchy.Cty.CtyCod, - Sco_GetDBStrFromScope (Hie_Lvl_CRS)); - break; + return DB_QuerySELECTDouble ("can not get number of questions per survey", + "SELECT AVG(NumQsts)" + " FROM (SELECT COUNT(svy_questions.QstCod) AS NumQsts" + " FROM ins_instits," + "ctr_centers," + "deg_degrees," + "crs_courses," + "svy_surveys," + "svy_questions" + " WHERE ins_instits.CtyCod=%ld" + " AND ins_instits.InsCod=ctr_centers.InsCod" + " AND ctr_centers.CtrCod=deg_degrees.CtrCod" + " AND deg_degrees.DegCod=crs_courses.DegCod" + " AND crs_courses.CrsCod=svy_surveys.Cod" + " AND svy_surveys.Scope='%s'" + " AND svy_surveys.SvyCod=svy_questions.SvyCod" + " GROUP BY svy_questions.SvyCod) AS NumQstsTable", + Gbl.Hierarchy.Cty.CtyCod, + Sco_GetDBStrFromScope (Hie_Lvl_CRS)); case Hie_Lvl_INS: - DB_QuerySELECT (&mysql_res,"can not get number of questions" - " per survey", - "SELECT AVG(NumQsts)" // row[0] - " FROM (SELECT COUNT(svy_questions.QstCod) AS NumQsts" - " FROM ctr_centers," - "deg_degrees," - "crs_courses," - "svy_surveys," - "svy_questions" - " WHERE ctr_centers.InsCod=%ld" - " AND ctr_centers.CtrCod=deg_degrees.CtrCod" - " AND deg_degrees.DegCod=crs_courses.DegCod" - " AND crs_courses.CrsCod=svy_surveys.Cod" - " AND svy_surveys.Scope='%s'" - " AND svy_surveys.SvyCod=svy_questions.SvyCod" - " GROUP BY svy_questions.SvyCod) AS NumQstsTable", - Gbl.Hierarchy.Ins.InsCod, - Sco_GetDBStrFromScope (Hie_Lvl_CRS)); - break; + return DB_QuerySELECTDouble ("can not get number of questions per survey", + "SELECT AVG(NumQsts)" + " FROM (SELECT COUNT(svy_questions.QstCod) AS NumQsts" + " FROM ctr_centers," + "deg_degrees," + "crs_courses," + "svy_surveys," + "svy_questions" + " WHERE ctr_centers.InsCod=%ld" + " AND ctr_centers.CtrCod=deg_degrees.CtrCod" + " AND deg_degrees.DegCod=crs_courses.DegCod" + " AND crs_courses.CrsCod=svy_surveys.Cod" + " AND svy_surveys.Scope='%s'" + " AND svy_surveys.SvyCod=svy_questions.SvyCod" + " GROUP BY svy_questions.SvyCod) AS NumQstsTable", + Gbl.Hierarchy.Ins.InsCod, + Sco_GetDBStrFromScope (Hie_Lvl_CRS)); case Hie_Lvl_CTR: - DB_QuerySELECT (&mysql_res,"can not get number of questions" - " per survey", - "SELECT AVG(NumQsts)" // row[0] - " FROM (SELECT COUNT(svy_questions.QstCod) AS NumQsts" - " FROM deg_degrees," - "crs_courses," - "svy_surveys," - "svy_questions" - " WHERE deg_degrees.CtrCod=%ld" - " AND deg_degrees.DegCod=crs_courses.DegCod" - " AND crs_courses.CrsCod=svy_surveys.Cod" - " AND svy_surveys.Scope='%s'" - " AND svy_surveys.SvyCod=svy_questions.SvyCod" - " GROUP BY svy_questions.SvyCod) AS NumQstsTable", - Gbl.Hierarchy.Ctr.CtrCod, - Sco_GetDBStrFromScope (Hie_Lvl_CRS)); - break; + return DB_QuerySELECTDouble ("can not get number of questions per survey", + "SELECT AVG(NumQsts)" + " FROM (SELECT COUNT(svy_questions.QstCod) AS NumQsts" + " FROM deg_degrees," + "crs_courses," + "svy_surveys," + "svy_questions" + " WHERE deg_degrees.CtrCod=%ld" + " AND deg_degrees.DegCod=crs_courses.DegCod" + " AND crs_courses.CrsCod=svy_surveys.Cod" + " AND svy_surveys.Scope='%s'" + " AND svy_surveys.SvyCod=svy_questions.SvyCod" + " GROUP BY svy_questions.SvyCod) AS NumQstsTable", + Gbl.Hierarchy.Ctr.CtrCod, + Sco_GetDBStrFromScope (Hie_Lvl_CRS)); case Hie_Lvl_DEG: - DB_QuerySELECT (&mysql_res,"can not get number of questions" - " per survey", - "SELECT AVG(NumQsts)" // row[0] - " FROM (SELECT COUNT(svy_questions.QstCod) AS NumQsts" - " FROM crs_courses," - "svy_surveys," - "svy_questions" - " WHERE crs_courses.DegCod=%ld" - " AND crs_courses.CrsCod=svy_surveys.Cod" - " AND svy_surveys.Scope='%s'" - " AND svy_surveys.SvyCod=svy_questions.SvyCod" - " GROUP BY svy_questions.SvyCod) AS NumQstsTable", - Gbl.Hierarchy.Deg.DegCod, - Sco_GetDBStrFromScope (Hie_Lvl_CRS)); - break; + return DB_QuerySELECTDouble ("can not get number of questions per survey", + "SELECT AVG(NumQsts)" + " FROM (SELECT COUNT(svy_questions.QstCod) AS NumQsts" + " FROM crs_courses," + "svy_surveys," + "svy_questions" + " WHERE crs_courses.DegCod=%ld" + " AND crs_courses.CrsCod=svy_surveys.Cod" + " AND svy_surveys.Scope='%s'" + " AND svy_surveys.SvyCod=svy_questions.SvyCod" + " GROUP BY svy_questions.SvyCod) AS NumQstsTable", + Gbl.Hierarchy.Deg.DegCod, + Sco_GetDBStrFromScope (Hie_Lvl_CRS)); case Hie_Lvl_CRS: - DB_QuerySELECT (&mysql_res,"can not get number of questions" - " per survey", - "SELECT AVG(NumQsts)" // row[0] - " FROM (SELECT COUNT(svy_questions.QstCod) AS NumQsts" - " FROM svy_surveys," - "svy_questions" - " WHERE svy_surveys.Scope='%s'" - " AND svy_surveys.Cod=%ld" - " AND svy_surveys.SvyCod=svy_questions.SvyCod" - " GROUP BY svy_questions.SvyCod) AS NumQstsTable", - Sco_GetDBStrFromScope (Hie_Lvl_CRS),Gbl.Hierarchy.Crs.CrsCod); - break; + return DB_QuerySELECTDouble ("can not get number of questions per survey", + "SELECT AVG(NumQsts)" + " FROM (SELECT COUNT(svy_questions.QstCod) AS NumQsts" + " FROM svy_surveys," + "svy_questions" + " WHERE svy_surveys.Scope='%s'" + " AND svy_surveys.Cod=%ld" + " AND svy_surveys.SvyCod=svy_questions.SvyCod" + " GROUP BY svy_questions.SvyCod) AS NumQstsTable", + Sco_GetDBStrFromScope (Hie_Lvl_CRS),Gbl.Hierarchy.Crs.CrsCod); default: Lay_WrongScopeExit (); - break; + return 0.0; // Not reached } - - /***** Get average number of questions per survey *****/ - row = mysql_fetch_row (mysql_res); - NumQstsPerSurvey = Str_GetDoubleFromStr (row[0]); - - /***** Free structure that stores the query result *****/ - DB_FreeMySQLResult (&mysql_res); - - return NumQstsPerSurvey; } diff --git a/swad_user.c b/swad_user.c index 8d6f3d8bf..aa8e14ceb 100644 --- a/swad_user.c +++ b/swad_user.c @@ -9708,164 +9708,146 @@ unsigned Usr_GetCachedNumUsrsNotBelongingToAnyCrs (void) static double Usr_GetNumCrssPerUsr (Hie_Lvl_Level_t Scope,long Cod,Rol_Role_t Role) { - MYSQL_RES *mysql_res; - MYSQL_ROW row; - double NumCrssPerUsr; - /***** Get number of courses per user from database *****/ switch (Scope) { case Hie_Lvl_SYS: if (Role == Rol_UNK) // Any user - DB_QuerySELECT (&mysql_res,"can not get number of courses per user", - "SELECT AVG(NumCrss)" // row[0] - " FROM (SELECT COUNT(CrsCod) AS NumCrss" - " FROM crs_users" - " GROUP BY UsrCod) AS NumCrssTable"); + return DB_QuerySELECTDouble ("can not get number of courses per user", + "SELECT AVG(NumCrss)" + " FROM (SELECT COUNT(CrsCod) AS NumCrss" + " FROM crs_users" + " GROUP BY UsrCod) AS NumCrssTable"); else - DB_QuerySELECT (&mysql_res,"can not get number of courses per user", - "SELECT AVG(NumCrss)" // row[0] - " FROM (SELECT COUNT(CrsCod) AS NumCrss" - " FROM crs_users" - " WHERE Role=%u" - " GROUP BY UsrCod) AS NumCrssTable", - (unsigned) Role); - break; + return DB_QuerySELECTDouble ("can not get number of courses per user", + "SELECT AVG(NumCrss)" + " FROM (SELECT COUNT(CrsCod) AS NumCrss" + " FROM crs_users" + " WHERE Role=%u" + " GROUP BY UsrCod) AS NumCrssTable", + (unsigned) Role); case Hie_Lvl_CTY: if (Role == Rol_UNK) // Any user - DB_QuerySELECT (&mysql_res,"can not get number of courses per user", - "SELECT AVG(NumCrss)" // row[0] - " FROM (SELECT COUNT(crs_users.CrsCod) AS NumCrss" - " FROM ins_instits," - "ctr_centers," - "deg_degrees," - "crs_courses," - "crs_users" - " WHERE ins_instits.CtyCod=%ld" - " AND ins_instits.InsCod=ctr_centers.InsCod" - " AND ctr_centers.CtrCod=deg_degrees.CtrCod" - " AND deg_degrees.DegCod=crs_courses.DegCod" - " AND crs_courses.CrsCod=crs_users.CrsCod" - " GROUP BY crs_users.UsrCod) AS NumCrssTable", - Cod); + return DB_QuerySELECTDouble ("can not get number of courses per user", + "SELECT AVG(NumCrss)" + " FROM (SELECT COUNT(crs_users.CrsCod) AS NumCrss" + " FROM ins_instits," + "ctr_centers," + "deg_degrees," + "crs_courses," + "crs_users" + " WHERE ins_instits.CtyCod=%ld" + " AND ins_instits.InsCod=ctr_centers.InsCod" + " AND ctr_centers.CtrCod=deg_degrees.CtrCod" + " AND deg_degrees.DegCod=crs_courses.DegCod" + " AND crs_courses.CrsCod=crs_users.CrsCod" + " GROUP BY crs_users.UsrCod) AS NumCrssTable", + Cod); else - DB_QuerySELECT (&mysql_res,"can not get number of courses per user", - "SELECT AVG(NumCrss)" // row[0] - " FROM (SELECT COUNT(crs_users.CrsCod) AS NumCrss" - " FROM ins_instits," - "ctr_centers," - "deg_degrees," - "crs_courses," - "crs_users" - " WHERE ins_instits.CtyCod=%ld" - " AND ins_instits.InsCod=ctr_centers.InsCod" - " AND ctr_centers.CtrCod=deg_degrees.CtrCod" - " AND deg_degrees.DegCod=crs_courses.DegCod" - " AND crs_courses.CrsCod=crs_users.CrsCod" - " AND crs_users.Role=%u" - " GROUP BY crs_users.UsrCod) AS NumCrssTable", - Cod, - (unsigned) Role); - break; + return DB_QuerySELECTDouble ("can not get number of courses per user", + "SELECT AVG(NumCrss)" // row[0] + " FROM (SELECT COUNT(crs_users.CrsCod) AS NumCrss" + " FROM ins_instits," + "ctr_centers," + "deg_degrees," + "crs_courses," + "crs_users" + " WHERE ins_instits.CtyCod=%ld" + " AND ins_instits.InsCod=ctr_centers.InsCod" + " AND ctr_centers.CtrCod=deg_degrees.CtrCod" + " AND deg_degrees.DegCod=crs_courses.DegCod" + " AND crs_courses.CrsCod=crs_users.CrsCod" + " AND crs_users.Role=%u" + " GROUP BY crs_users.UsrCod) AS NumCrssTable", + Cod, + (unsigned) Role); case Hie_Lvl_INS: if (Role == Rol_UNK) // Any user - DB_QuerySELECT (&mysql_res,"can not get number of courses per user", - "SELECT AVG(NumCrss)" // row[0] - " FROM (SELECT COUNT(crs_users.CrsCod) AS NumCrss" - " FROM ctr_centers," - "deg_degrees," - "crs_courses," - "crs_users" - " WHERE ctr_centers.InsCod=%ld" - " AND ctr_centers.CtrCod=deg_degrees.CtrCod" - " AND deg_degrees.DegCod=crs_courses.DegCod" - " AND crs_courses.CrsCod=crs_users.CrsCod" - " GROUP BY crs_users.UsrCod) AS NumCrssTable", - Cod); + return DB_QuerySELECTDouble ("can not get number of courses per user", + "SELECT AVG(NumCrss)" // row[0] + " FROM (SELECT COUNT(crs_users.CrsCod) AS NumCrss" + " FROM ctr_centers," + "deg_degrees," + "crs_courses," + "crs_users" + " WHERE ctr_centers.InsCod=%ld" + " AND ctr_centers.CtrCod=deg_degrees.CtrCod" + " AND deg_degrees.DegCod=crs_courses.DegCod" + " AND crs_courses.CrsCod=crs_users.CrsCod" + " GROUP BY crs_users.UsrCod) AS NumCrssTable", + Cod); else - DB_QuerySELECT (&mysql_res,"can not get number of courses per user", - "SELECT AVG(NumCrss)" // row[0] - " FROM (SELECT COUNT(crs_users.CrsCod) AS NumCrss" - " FROM ctr_centers," - "deg_degrees," - "crs_courses," - "crs_users" - " WHERE ctr_centers.InsCod=%ld" - " AND ctr_centers.CtrCod=deg_degrees.CtrCod" - " AND deg_degrees.DegCod=crs_courses.DegCod" - " AND crs_courses.CrsCod=crs_users.CrsCod" - " AND crs_users.Role=%u" - " GROUP BY crs_users.UsrCod) AS NumCrssTable", - Cod, - (unsigned) Role); - break; + return DB_QuerySELECTDouble ("can not get number of courses per user", + "SELECT AVG(NumCrss)" // row[0] + " FROM (SELECT COUNT(crs_users.CrsCod) AS NumCrss" + " FROM ctr_centers," + "deg_degrees," + "crs_courses," + "crs_users" + " WHERE ctr_centers.InsCod=%ld" + " AND ctr_centers.CtrCod=deg_degrees.CtrCod" + " AND deg_degrees.DegCod=crs_courses.DegCod" + " AND crs_courses.CrsCod=crs_users.CrsCod" + " AND crs_users.Role=%u" + " GROUP BY crs_users.UsrCod) AS NumCrssTable", + Cod, + (unsigned) Role); case Hie_Lvl_CTR: if (Role == Rol_UNK) // Any user - DB_QuerySELECT (&mysql_res,"can not get number of courses per user", - "SELECT AVG(NumCrss)" // row[0] - " FROM (SELECT COUNT(crs_users.CrsCod) AS NumCrss" - " FROM deg_degrees," - "crs_courses," - "crs_users" - " WHERE deg_degrees.CtrCod=%ld" - " AND deg_degrees.DegCod=crs_courses.DegCod" - " AND crs_courses.CrsCod=crs_users.CrsCod" - " GROUP BY crs_users.UsrCod) AS NumCrssTable", - Cod); + return DB_QuerySELECTDouble ("can not get number of courses per user", + "SELECT AVG(NumCrss)" // row[0] + " FROM (SELECT COUNT(crs_users.CrsCod) AS NumCrss" + " FROM deg_degrees," + "crs_courses," + "crs_users" + " WHERE deg_degrees.CtrCod=%ld" + " AND deg_degrees.DegCod=crs_courses.DegCod" + " AND crs_courses.CrsCod=crs_users.CrsCod" + " GROUP BY crs_users.UsrCod) AS NumCrssTable", + Cod); else - DB_QuerySELECT (&mysql_res,"can not get number of courses per user", - "SELECT AVG(NumCrss)" // row[0] - " FROM (SELECT COUNT(crs_users.CrsCod) AS NumCrss" - " FROM deg_degrees," - "crs_courses," - "crs_users" - " WHERE deg_degrees.CtrCod=%ld" - " AND deg_degrees.DegCod=crs_courses.DegCod" - " AND crs_courses.CrsCod=crs_users.CrsCod" - " AND crs_users.Role=%u" - " GROUP BY crs_users.UsrCod) AS NumCrssTable", - Cod, - (unsigned) Role); - break; + return DB_QuerySELECTDouble ("can not get number of courses per user", + "SELECT AVG(NumCrss)" // row[0] + " FROM (SELECT COUNT(crs_users.CrsCod) AS NumCrss" + " FROM deg_degrees," + "crs_courses," + "crs_users" + " WHERE deg_degrees.CtrCod=%ld" + " AND deg_degrees.DegCod=crs_courses.DegCod" + " AND crs_courses.CrsCod=crs_users.CrsCod" + " AND crs_users.Role=%u" + " GROUP BY crs_users.UsrCod) AS NumCrssTable", + Cod, + (unsigned) Role); case Hie_Lvl_DEG: if (Role == Rol_UNK) // Any user - DB_QuerySELECT (&mysql_res,"can not get number of courses per user", - "SELECT AVG(NumCrss)" // row[0] - " FROM (SELECT COUNT(crs_users.CrsCod) AS NumCrss" - " FROM crs_courses," - "crs_users" - " WHERE crs_courses.DegCod=%ld" - " AND crs_courses.CrsCod=crs_users.CrsCod" - " GROUP BY crs_users.UsrCod) AS NumCrssTable", - Cod); + return DB_QuerySELECTDouble ("can not get number of courses per user", + "SELECT AVG(NumCrss)" // row[0] + " FROM (SELECT COUNT(crs_users.CrsCod) AS NumCrss" + " FROM crs_courses," + "crs_users" + " WHERE crs_courses.DegCod=%ld" + " AND crs_courses.CrsCod=crs_users.CrsCod" + " GROUP BY crs_users.UsrCod) AS NumCrssTable", + Cod); else - DB_QuerySELECT (&mysql_res,"can not get number of courses per user", - "SELECT AVG(NumCrss)" // row[0] - " FROM (SELECT COUNT(crs_users.CrsCod) AS NumCrss" - " FROM crs_courses," - "crs_users" - " WHERE crs_courses.DegCod=%ld" - " AND crs_courses.CrsCod=crs_users.CrsCod" - " AND crs_users.Role=%u" - " GROUP BY crs_users.UsrCod) AS NumCrssTable", - Cod, - (unsigned) Role); - break; + return DB_QuerySELECTDouble ("can not get number of courses per user", + "SELECT AVG(NumCrss)" // row[0] + " FROM (SELECT COUNT(crs_users.CrsCod) AS NumCrss" + " FROM crs_courses," + "crs_users" + " WHERE crs_courses.DegCod=%ld" + " AND crs_courses.CrsCod=crs_users.CrsCod" + " AND crs_users.Role=%u" + " GROUP BY crs_users.UsrCod) AS NumCrssTable", + Cod, + (unsigned) Role); case Hie_Lvl_CRS: return 1.0; default: Lay_WrongScopeExit (); - break; + return 0.0; // Not reached } - - /***** Get averga number of courses per user *****/ - row = mysql_fetch_row (mysql_res); - NumCrssPerUsr = Str_GetDoubleFromStr (row[0]); - - /***** Free structure that stores the query result *****/ - DB_FreeMySQLResult (&mysql_res); - - return NumCrssPerUsr; } double Usr_GetCachedNumCrssPerUsr (Hie_Lvl_Level_t Scope,long Cod,Rol_Role_t Role) @@ -9898,148 +9880,139 @@ double Usr_GetCachedNumCrssPerUsr (Hie_Lvl_Level_t Scope,long Cod,Rol_Role_t Rol static double Usr_GetNumUsrsPerCrs (Hie_Lvl_Level_t Scope,long Cod,Rol_Role_t Role) { - MYSQL_RES *mysql_res; - MYSQL_ROW row; - double NumUsrsPerCrs; - /***** Get number of users per course from database *****/ switch (Scope) { case Hie_Lvl_SYS: if (Role == Rol_UNK) // Any user - DB_QuerySELECT (&mysql_res,"can not get number of users per course", - "SELECT AVG(NumUsrs)" // row[0] - " FROM (SELECT COUNT(UsrCod) AS NumUsrs" - " FROM crs_users" - " GROUP BY CrsCod) AS NumUsrsTable"); + return DB_QuerySELECTDouble ("can not get number of users per course", + "SELECT AVG(NumUsrs)" // row[0] + " FROM (SELECT COUNT(UsrCod) AS NumUsrs" + " FROM crs_users" + " GROUP BY CrsCod) AS NumUsrsTable"); else - DB_QuerySELECT (&mysql_res,"can not get number of users per course", - "SELECT AVG(NumUsrs)" // row[0] - " FROM (SELECT COUNT(UsrCod) AS NumUsrs" - " FROM crs_users" - " WHERE Role=%u GROUP BY CrsCod) AS NumUsrsTable", - (unsigned) Role); - break; + return DB_QuerySELECTDouble ("can not get number of users per course", + "SELECT AVG(NumUsrs)" // row[0] + " FROM (SELECT COUNT(UsrCod) AS NumUsrs" + " FROM crs_users" + " WHERE Role=%u GROUP BY CrsCod) AS NumUsrsTable", + (unsigned) Role); case Hie_Lvl_CTY: if (Role == Rol_UNK) // Any user - DB_QuerySELECT (&mysql_res,"can not get number of users per course", - "SELECT AVG(NumUsrs)" // row[0] - " FROM (SELECT COUNT(crs_users.UsrCod) AS NumUsrs" - " FROM ins_instits," - "ctr_centers," - "deg_degrees," - "crs_courses," - "crs_users" - " WHERE ins_instits.CtyCod=%ld" - " AND ins_instits.InsCod=ctr_centers.InsCod" - " AND ctr_centers.CtrCod=deg_degrees.CtrCod" - " AND deg_degrees.DegCod=crs_courses.DegCod" - " AND crs_courses.CrsCod=crs_users.CrsCod" - " GROUP BY crs_users.CrsCod) AS NumUsrsTable", - Cod); + return DB_QuerySELECTDouble ("can not get number of users per course", + "SELECT AVG(NumUsrs)" // row[0] + " FROM (SELECT COUNT(crs_users.UsrCod) AS NumUsrs" + " FROM ins_instits," + "ctr_centers," + "deg_degrees," + "crs_courses," + "crs_users" + " WHERE ins_instits.CtyCod=%ld" + " AND ins_instits.InsCod=ctr_centers.InsCod" + " AND ctr_centers.CtrCod=deg_degrees.CtrCod" + " AND deg_degrees.DegCod=crs_courses.DegCod" + " AND crs_courses.CrsCod=crs_users.CrsCod" + " GROUP BY crs_users.CrsCod) AS NumUsrsTable", + Cod); else - DB_QuerySELECT (&mysql_res,"can not get number of users per course", - "SELECT AVG(NumUsrs)" // row[0] - " FROM (SELECT COUNT(crs_users.UsrCod) AS NumUsrs" - " FROM ins_instits," - "ctr_centers," - "deg_degrees," - "crs_courses," - "crs_users" - " WHERE ins_instits.CtyCod=%ld" - " AND ins_instits.InsCod=ctr_centers.InsCod" - " AND ctr_centers.CtrCod=deg_degrees.CtrCod" - " AND deg_degrees.DegCod=crs_courses.DegCod" - " AND crs_courses.CrsCod=crs_users.CrsCod" - " AND crs_users.Role=%u" - " GROUP BY crs_users.CrsCod) AS NumUsrsTable", - Cod, - (unsigned) Role); - break; + return DB_QuerySELECTDouble ("can not get number of users per course", + "SELECT AVG(NumUsrs)" // row[0] + " FROM (SELECT COUNT(crs_users.UsrCod) AS NumUsrs" + " FROM ins_instits," + "ctr_centers," + "deg_degrees," + "crs_courses," + "crs_users" + " WHERE ins_instits.CtyCod=%ld" + " AND ins_instits.InsCod=ctr_centers.InsCod" + " AND ctr_centers.CtrCod=deg_degrees.CtrCod" + " AND deg_degrees.DegCod=crs_courses.DegCod" + " AND crs_courses.CrsCod=crs_users.CrsCod" + " AND crs_users.Role=%u" + " GROUP BY crs_users.CrsCod) AS NumUsrsTable", + Cod, + (unsigned) Role); case Hie_Lvl_INS: if (Role == Rol_UNK) // Any user - DB_QuerySELECT (&mysql_res,"can not get number of users per course", - "SELECT AVG(NumUsrs)" // row[0] - " FROM (SELECT COUNT(crs_users.UsrCod) AS NumUsrs" - " FROM ctr_centers," - "deg_degrees," - "crs_courses," - "crs_users" - " WHERE ctr_centers.InsCod=%ld" - " AND ctr_centers.CtrCod=deg_degrees.CtrCod" - " AND deg_degrees.DegCod=crs_courses.DegCod" - " AND crs_courses.CrsCod=crs_users.CrsCod" - " GROUP BY crs_users.CrsCod) AS NumUsrsTable", - Cod); + return DB_QuerySELECTDouble ("can not get number of users per course", + "SELECT AVG(NumUsrs)" // row[0] + " FROM (SELECT COUNT(crs_users.UsrCod) AS NumUsrs" + " FROM ctr_centers," + "deg_degrees," + "crs_courses," + "crs_users" + " WHERE ctr_centers.InsCod=%ld" + " AND ctr_centers.CtrCod=deg_degrees.CtrCod" + " AND deg_degrees.DegCod=crs_courses.DegCod" + " AND crs_courses.CrsCod=crs_users.CrsCod" + " GROUP BY crs_users.CrsCod) AS NumUsrsTable", + Cod); else - DB_QuerySELECT (&mysql_res,"can not get number of users per course", - "SELECT AVG(NumUsrs)" // row[0] - " FROM (SELECT COUNT(crs_users.UsrCod) AS NumUsrs" - " FROM ctr_centers," - "deg_degrees," - "crs_courses," - "crs_users" - " WHERE ctr_centers.InsCod=%ld" - " AND ctr_centers.CtrCod=deg_degrees.CtrCod" - " AND deg_degrees.DegCod=crs_courses.DegCod" - " AND crs_courses.CrsCod=crs_users.CrsCod" - " AND crs_users.Role=%u" - " GROUP BY crs_users.CrsCod) AS NumUsrsTable", - Cod, - (unsigned) Role); - break; + return DB_QuerySELECTDouble ("can not get number of users per course", + "SELECT AVG(NumUsrs)" // row[0] + " FROM (SELECT COUNT(crs_users.UsrCod) AS NumUsrs" + " FROM ctr_centers," + "deg_degrees," + "crs_courses," + "crs_users" + " WHERE ctr_centers.InsCod=%ld" + " AND ctr_centers.CtrCod=deg_degrees.CtrCod" + " AND deg_degrees.DegCod=crs_courses.DegCod" + " AND crs_courses.CrsCod=crs_users.CrsCod" + " AND crs_users.Role=%u" + " GROUP BY crs_users.CrsCod) AS NumUsrsTable", + Cod, + (unsigned) Role); case Hie_Lvl_CTR: if (Role == Rol_UNK) // Any user - DB_QuerySELECT (&mysql_res,"can not get number of users per course", - "SELECT AVG(NumUsrs)" // row[0] - " FROM (SELECT COUNT(crs_users.UsrCod) AS NumUsrs" - " FROM deg_degrees," - "crs_courses," - "crs_users" - " WHERE deg_degrees.CtrCod=%ld" - " AND deg_degrees.DegCod=crs_courses.DegCod" - " AND crs_courses.CrsCod=crs_users.CrsCod" - " GROUP BY crs_users.CrsCod) AS NumUsrsTable", - Cod); + return DB_QuerySELECTDouble ("can not get number of users per course", + "SELECT AVG(NumUsrs)" // row[0] + " FROM (SELECT COUNT(crs_users.UsrCod) AS NumUsrs" + " FROM deg_degrees," + "crs_courses," + "crs_users" + " WHERE deg_degrees.CtrCod=%ld" + " AND deg_degrees.DegCod=crs_courses.DegCod" + " AND crs_courses.CrsCod=crs_users.CrsCod" + " GROUP BY crs_users.CrsCod) AS NumUsrsTable", + Cod); else - DB_QuerySELECT (&mysql_res,"can not get number of users per course", - "SELECT AVG(NumUsrs)" // row[0] - " FROM (SELECT COUNT(crs_users.UsrCod) AS NumUsrs" - " FROM deg_degrees," - "crs_courses," - "crs_users" - " WHERE deg_degrees.CtrCod=%ld" - " AND deg_degrees.DegCod=crs_courses.DegCod" - " AND crs_courses.CrsCod=crs_users.CrsCod" - " AND crs_users.Role=%u" - " GROUP BY crs_users.CrsCod) AS NumUsrsTable", - Cod, - (unsigned) Role); - break; + return DB_QuerySELECTDouble ("can not get number of users per course", + "SELECT AVG(NumUsrs)" // row[0] + " FROM (SELECT COUNT(crs_users.UsrCod) AS NumUsrs" + " FROM deg_degrees," + "crs_courses," + "crs_users" + " WHERE deg_degrees.CtrCod=%ld" + " AND deg_degrees.DegCod=crs_courses.DegCod" + " AND crs_courses.CrsCod=crs_users.CrsCod" + " AND crs_users.Role=%u" + " GROUP BY crs_users.CrsCod) AS NumUsrsTable", + Cod, + (unsigned) Role); case Hie_Lvl_DEG: if (Role == Rol_UNK) // Any user - DB_QuerySELECT (&mysql_res,"can not get number of users per course", - "SELECT AVG(NumUsrs)" // row[0] - " FROM (SELECT COUNT(crs_users.UsrCod) AS NumUsrs" - " FROM crs_courses," - "crs_users" - " WHERE crs_courses.DegCod=%ld" - " AND crs_courses.CrsCod=crs_users.CrsCod" - " GROUP BY crs_users.CrsCod) AS NumUsrsTable", - Cod); + return DB_QuerySELECTDouble ("can not get number of users per course", + "SELECT AVG(NumUsrs)" // row[0] + " FROM (SELECT COUNT(crs_users.UsrCod) AS NumUsrs" + " FROM crs_courses," + "crs_users" + " WHERE crs_courses.DegCod=%ld" + " AND crs_courses.CrsCod=crs_users.CrsCod" + " GROUP BY crs_users.CrsCod) AS NumUsrsTable", + Cod); else - DB_QuerySELECT (&mysql_res,"can not get number of users per course", - "SELECT AVG(NumUsrs)" // row[0] - " FROM (SELECT COUNT(crs_users.UsrCod) AS NumUsrs" - " FROM crs_courses," - "crs_users" - " WHERE crs_courses.DegCod=%ld" - " AND crs_courses.CrsCod=crs_users.CrsCod" - " AND crs_users.Role=%u" - " GROUP BY crs_users.CrsCod) AS NumUsrsTable", - Cod, - (unsigned) Role); - break; + return DB_QuerySELECTDouble ("can not get number of users per course", + "SELECT AVG(NumUsrs)" // row[0] + " FROM (SELECT COUNT(crs_users.UsrCod) AS NumUsrs" + " FROM crs_courses," + "crs_users" + " WHERE crs_courses.DegCod=%ld" + " AND crs_courses.CrsCod=crs_users.CrsCod" + " AND crs_users.Role=%u" + " GROUP BY crs_users.CrsCod) AS NumUsrsTable", + Cod, + (unsigned) Role); case Hie_Lvl_CRS: return (double) Usr_GetNumUsrsInCrss (Hie_Lvl_CRS,Cod, Role == Rol_UNK ? 1 << Rol_STD | @@ -10049,17 +10022,8 @@ static double Usr_GetNumUsrsPerCrs (Hie_Lvl_Level_t Scope,long Cod,Rol_Role_t Ro default: Lay_WrongScopeExit (); - break; + return 0.0; // Not reached } - - /***** Get average number of users per course *****/ - row = mysql_fetch_row (mysql_res); - NumUsrsPerCrs = Str_GetDoubleFromStr (row[0]); - - /***** Free structure that stores the query result *****/ - DB_FreeMySQLResult (&mysql_res); - - return NumUsrsPerCrs; } double Usr_GetCachedNumUsrsPerCrs (Hie_Lvl_Level_t Scope,long Cod,Rol_Role_t Role)