Version 21.41.1: Oct 24, 2021 Queries moved to module swad_tag_database.

This commit is contained in:
acanas 2021-10-24 20:25:01 +02:00
parent 00302bea7d
commit 4d46a06d38
4 changed files with 108 additions and 54 deletions

View File

@ -602,13 +602,14 @@ TODO: FIX BUG, URGENT! En las fechas como par
TODO: En las encuestas, que los estudiantes no puedan ver los resultados hasta que no finalice el plazo.
*/
#define Log_PLATFORM_VERSION "SWAD 21.41 (2021-10-24)"
#define Log_PLATFORM_VERSION "SWAD 21.41.1 (2021-10-24)"
#define CSS_FILE "swad20.45.css"
#define JS_FILE "swad20.69.1.js"
/*
TODO: Rename CENTRE to CENTER in help wiki.
TODO: Rename ASSESSMENT.Announcements to ASSESSMENT.Calls_for_exams
Version 21.41.1: Oct 24, 2021 Queries moved to module swad_tag_database. (320767 lines)
Version 21.41: Oct 24, 2021 New module swad_tag_database for database queries related to question tags. (320721 lines)
Version 21.40.1: Oct 24, 2021 Queries moved to module swad_center_database. (320621 lines)
Version 21.40: Oct 24, 2021 Queries moved to module swad_survey_database. (320673 lines)

View File

@ -172,8 +172,8 @@ void Tag_RenameTag (void)
extern const char *Txt_The_tag_X_has_not_changed;
char OldTagTxt[Tag_MAX_BYTES_TAG + 1];
char NewTagTxt[Tag_MAX_BYTES_TAG + 1];
long ExistingTagCod;
long OldTagCod;
long ExistingTagCodNewTxt;
long TagCodOldTxt;
bool ComplexRenaming;
/***** Get old and new tags from the form *****/
@ -199,81 +199,43 @@ void Tag_RenameTag (void)
// are not the same (case insensitively)
/* Check if the new tag text is equal to any of the tags
already present in the database */
if ((ExistingTagCod = Tag_DB_GetTagCodFromTagTxt (NewTagTxt)) > 0)
if ((ExistingTagCodNewTxt = Tag_DB_GetTagCodFromTagTxt (NewTagTxt)) > 0)
// The new tag was already in database
ComplexRenaming = true;
if (ComplexRenaming) // Renaming is not easy
{
/***** Complex update made to not repeat tags:
/***** Complex renaming made to not repeat tags:
- If the new tag existed for a question ==>
delete old tag from tst_question_tags;
the new tag will remain
- If the new tag did not exist for a question ==>
change old tag to new tag in tst_question_tags *****/
/* Get tag code of the old tag */
if ((OldTagCod = Tag_DB_GetTagCodFromTagTxt (OldTagTxt)) <= 0)
if ((TagCodOldTxt = Tag_DB_GetTagCodFromTagTxt (OldTagTxt)) <= 0)
Err_WrongTagExit ();
/* Create a temporary table with all the question codes
that had the new tag as one of their tags */
DB_Query ("can not remove temporary table",
"DROP TEMPORARY TABLE IF EXISTS tst_question_tags_tmp");
DB_Query ("can not create temporary table",
"CREATE TEMPORARY TABLE tst_question_tags_tmp"
" ENGINE=MEMORY"
" SELECT QstCod"
" FROM tst_question_tags"
" WHERE TagCod=%ld",
ExistingTagCod);
Tag_DB_DropTmpTableQuestionsWithTag ();
Tag_DB_CreateTmpTableQuestionsWithTag (ExistingTagCodNewTxt);
/* Remove old tag in questions where it would be repeated */
// New tag existed for a question ==> delete old tag
DB_QueryDELETE ("can not remove a tag from some questions",
"DELETE FROM tst_question_tags"
" WHERE TagCod=%ld"
" AND QstCod IN"
" (SELECT QstCod"
" FROM tst_question_tags_tmp)",
OldTagCod);
/* Change old tag to new tag in questions where it would not be repeated */
// New tag existed for a question ==> delete old tag
// New tag did not exist for a question ==> change old tag to new tag
DB_QueryUPDATE ("can not update a tag in some questions",
"UPDATE tst_question_tags"
" SET TagCod=%ld"
" WHERE TagCod=%ld"
" AND QstCod NOT IN"
" (SELECT QstCod"
" FROM tst_question_tags_tmp)",
ExistingTagCod,
OldTagCod);
Tag_DB_ComplexRenameTag (TagCodOldTxt,ExistingTagCodNewTxt);
/* Drop temporary table, no longer necessary */
DB_Query ("can not remove temporary table",
"DROP TEMPORARY TABLE IF EXISTS tst_question_tags_tmp");
Tag_DB_DropTmpTableQuestionsWithTag ();
/***** Delete old tag from tst_tags
because it is not longer used *****/
DB_QueryDELETE ("can not remove old tag",
"DELETE FROM tst_tags"
" WHERE TagCod=%ld",
OldTagCod);
Tag_DB_RemoveTag (TagCodOldTxt);
}
else // Renaming is easy
{
/***** Simple update replacing each instance of the old tag by the new tag *****/
DB_QueryUPDATE ("can not update tag",
"UPDATE tst_tags"
" SET TagTxt='%s',"
"ChangeTime=NOW()"
" WHERE tst_tags.CrsCod=%ld"
" AND tst_tags.TagTxt='%s'",
NewTagTxt,
Gbl.Hierarchy.Crs.CrsCod,
OldTagTxt);
}
Tag_DB_SimplexRenameTag (OldTagTxt,NewTagTxt);
/***** Write message to show the change made *****/
Ale_ShowAlert (Ale_SUCCESS,Txt_The_tag_X_has_been_renamed_as_Y,

View File

@ -35,7 +35,7 @@
// #include "swad_error.h"
// #include "swad_form.h"
#include "swad_global.h"
// #include "swad_tag.h"
#include "swad_tag.h"
#include "swad_tag_database.h"
// #include "swad_theme.h"
@ -98,6 +98,81 @@ void Tag_DB_AddTagToQst (long QstCod,long TagCod,unsigned TagInd)
TagInd);
}
/*****************************************************************************/
/********** Create a temporary table with all the question codes *************/
/********** that had the new tag as one of their tags *************/
/*****************************************************************************/
void Tag_DB_CreateTmpTableQuestionsWithTag (long TagCod)
{
DB_Query ("can not create temporary table",
"CREATE TEMPORARY TABLE tst_question_tags_tmp"
" ENGINE=MEMORY"
" SELECT QstCod"
" FROM tst_question_tags"
" WHERE TagCod=%ld",
TagCod);
}
/*****************************************************************************/
/************* Drop temporary table with all the question codes **************/
/************* that had the new tag as one of their tags **************/
/*****************************************************************************/
void Tag_DB_DropTmpTableQuestionsWithTag (void)
{
DB_Query ("can not remove temporary table",
"DROP TEMPORARY TABLE IF EXISTS tst_question_tags_tmp");
}
/*****************************************************************************/
/************************* Complex renaming of tag ***************************/
/*****************************************************************************/
void Tag_DB_ComplexRenameTag (long TagCodOldTxt,long ExistingTagCodNewTxt)
{
/***** Remove old tag in questions where it would be repeated *****/
// New tag existed for a question ==> delete old tag
DB_QueryDELETE ("can not remove a tag from some questions",
"DELETE FROM tst_question_tags"
" WHERE TagCod=%ld"
" AND QstCod IN"
// Questions that already have a tag with the new name
" (SELECT QstCod"
" FROM tst_question_tags_tmp)",
TagCodOldTxt);
/***** Change old tag to new tag in questions where it would not be repeated *****/
// New tag did not exist for a question ==> change old tag to new tag
DB_QueryUPDATE ("can not update a tag in some questions",
"UPDATE tst_question_tags"
" SET TagCod=%ld"
" WHERE TagCod=%ld"
" AND QstCod NOT IN"
" (SELECT QstCod"
" FROM tst_question_tags_tmp)",
ExistingTagCodNewTxt,
TagCodOldTxt);
}
/*****************************************************************************/
/**** Simple update replacing each instance of the old tag by the new tag ****/
/*****************************************************************************/
void Tag_DB_SimplexRenameTag (const char OldTagTxt[Tag_MAX_BYTES_TAG + 1],
const char NewTagTxt[Tag_MAX_BYTES_TAG + 1])
{
DB_QueryUPDATE ("can not update tag",
"UPDATE tst_tags"
" SET TagTxt='%s',"
"ChangeTime=NOW()"
" WHERE tst_tags.CrsCod=%ld"
" AND tst_tags.TagTxt='%s'",
NewTagTxt,
Gbl.Hierarchy.Crs.CrsCod,
OldTagTxt);
}
/*****************************************************************************/
/********** Change visibility of an existing tag into tst_tags table *********/
/*****************************************************************************/
@ -190,13 +265,24 @@ long Tag_DB_GetTagCodFromTagTxt (const char *TagTxt)
void Tag_DB_RemTagsFromQst (long QstCod)
{
/***** Remove tags *****/
DB_QueryDELETE ("can not remove the tags of a question",
"DELETE FROM tst_question_tags"
" WHERE QstCod=%ld",
QstCod);
}
/*****************************************************************************/
/******************************** Remove a tag *******************************/
/*****************************************************************************/
void Tag_DB_RemoveTag (long TagCod)
{
DB_QueryDELETE ("can not remove tag",
"DELETE FROM tst_tags"
" WHERE TagCod=%ld",
TagCod);
}
/*****************************************************************************/
/********************** Remove unused tags in a course ***********************/
/*****************************************************************************/

View File

@ -45,7 +45,11 @@
long Tag_DB_CreateNewTag (long CrsCod,const char *TagTxt);
void Tag_DB_AddTagToQst (long QstCod,long TagCod,unsigned TagInd);
void Tag_DB_CreateTmpTableQuestionsWithTag (long TagCod);
void Tag_DB_DropTmpTableQuestionsWithTag (void);
void Tag_DB_ComplexRenameTag (long TagCodOldTxt,long ExistingTagCodNewTxt);
void Tag_DB_SimplexRenameTag (const char OldTagTxt[Tag_MAX_BYTES_TAG + 1],
const char NewTagTxt[Tag_MAX_BYTES_TAG + 1]);
void Tag_DB_EnableOrDisableTag (long TagCod,bool TagHidden);
unsigned Tag_DB_GetAllTagsFromCurrentCrs (MYSQL_RES **mysql_res);
@ -54,6 +58,7 @@ bool Tag_DB_CheckIfCurrentCrsHasTestTags (void);
long Tag_DB_GetTagCodFromTagTxt (const char *TagTxt);
void Tag_DB_RemTagsFromQst (long QstCod);
void Tag_DB_RemoveTag (long TagCod);
void Tag_DB_RemoveUnusedTagsFromCrs (long CrsCod);
#endif