2021-10-12 22:13:16 +02:00
|
|
|
|
// swad_record_database.c: users' record cards, operations with database
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
SWAD (Shared Workspace At a Distance),
|
|
|
|
|
is a web platform developed at the University of Granada (Spain),
|
|
|
|
|
and used to support university teaching.
|
|
|
|
|
|
|
|
|
|
This file is part of SWAD core.
|
|
|
|
|
Copyright (C) 1999-2021 Antonio Ca<EFBFBD>as Vargas
|
|
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU Affero General Public License as
|
|
|
|
|
published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/********************************* Headers ***********************************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "swad_database.h"
|
|
|
|
|
#include "swad_record_database.h"
|
|
|
|
|
|
2021-10-13 10:21:02 +02:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/************************* Create a field of record **************************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
void Rec_DB_CreateField (long CrsCod,const struct RecordField *Field)
|
|
|
|
|
{
|
|
|
|
|
DB_QueryINSERT ("can not create field of record",
|
|
|
|
|
"INSERT INTO crs_record_fields"
|
|
|
|
|
" (CrsCod,FieldName,NumLines,Visibility)"
|
|
|
|
|
" VALUES"
|
|
|
|
|
" (%ld,'%s',%u,%u)",
|
|
|
|
|
CrsCod,
|
|
|
|
|
Field->Name,
|
|
|
|
|
Field->NumLines,
|
|
|
|
|
(unsigned) Field->Visibility);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 22:13:16 +02:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/********************* Insert text field of course record ********************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2021-10-13 10:21:02 +02:00
|
|
|
|
void Rec_DB_CreateFieldContent (long FieldCod,long UsrCod,const char *Text)
|
2021-10-12 22:13:16 +02:00
|
|
|
|
{
|
|
|
|
|
DB_QueryINSERT ("can not create field of record",
|
|
|
|
|
"INSERT INTO crs_records"
|
|
|
|
|
" (FieldCod,UsrCod,Txt)"
|
|
|
|
|
" VALUES"
|
|
|
|
|
" (%ld,%ld,'%s')",
|
|
|
|
|
FieldCod,
|
|
|
|
|
UsrCod,
|
|
|
|
|
Text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/********************* Update text field of course record ********************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2021-10-13 10:21:02 +02:00
|
|
|
|
void Rec_DB_UpdateFieldTxt (long FieldCod,long UsrCod,const char *Text)
|
2021-10-12 22:13:16 +02:00
|
|
|
|
{
|
|
|
|
|
DB_QueryUPDATE ("can not update field of record",
|
|
|
|
|
"UPDATE crs_records"
|
|
|
|
|
" SET Txt='%s'"
|
|
|
|
|
" WHERE UsrCod=%ld"
|
|
|
|
|
" AND FieldCod=%ld",
|
|
|
|
|
Text,
|
|
|
|
|
UsrCod,
|
|
|
|
|
FieldCod);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/******** Update course record field changing the old name by the new ********/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2021-10-13 10:21:02 +02:00
|
|
|
|
void Rec_DB_UpdateFieldName (long FieldCod,const char NewFieldName[Rec_MAX_BYTES_NAME_FIELD + 1])
|
2021-10-12 22:13:16 +02:00
|
|
|
|
{
|
|
|
|
|
DB_QueryUPDATE ("can not update name of field of record",
|
|
|
|
|
"UPDATE crs_record_fields"
|
|
|
|
|
" SET FieldName='%s'"
|
|
|
|
|
" WHERE FieldCod=%ld",
|
|
|
|
|
NewFieldName,
|
|
|
|
|
FieldCod);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/*** Update course record field changing the old number of lines by the new **/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2021-10-13 10:21:02 +02:00
|
|
|
|
void Rec_DB_UpdateFieldNumLines (long FieldCod,unsigned NewNumLines)
|
2021-10-12 22:13:16 +02:00
|
|
|
|
{
|
|
|
|
|
DB_QueryUPDATE ("can not update the number of lines of a record field",
|
|
|
|
|
"UPDATE crs_record_fields"
|
|
|
|
|
" SET NumLines=%u"
|
|
|
|
|
" WHERE FieldCod=%ld",
|
|
|
|
|
NewNumLines,
|
|
|
|
|
FieldCod);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/***** Update course record field changing the old visibility by the new *****/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2021-10-13 10:21:02 +02:00
|
|
|
|
void Rec_DB_UpdateFieldVisibility (long FieldCod,Rec_VisibilityRecordFields_t NewVisibility)
|
2021-10-12 22:13:16 +02:00
|
|
|
|
{
|
|
|
|
|
DB_QueryUPDATE ("can not update the visibility of a record field",
|
|
|
|
|
"UPDATE crs_record_fields"
|
|
|
|
|
" SET Visibility=%u"
|
|
|
|
|
" WHERE FieldCod=%ld",
|
|
|
|
|
(unsigned) NewVisibility,
|
|
|
|
|
FieldCod);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 10:21:02 +02:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/*************** Get the number of records with a field filled ***************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
unsigned Rec_DB_CountNumRecordsWithFieldContent (long FieldCod)
|
|
|
|
|
{
|
|
|
|
|
return (unsigned)
|
|
|
|
|
DB_QueryCOUNT ("can not get number of records with a given field not empty",
|
|
|
|
|
"SELECT COUNT(*)"
|
|
|
|
|
" FROM crs_records"
|
|
|
|
|
" WHERE FieldCod=%ld",
|
|
|
|
|
FieldCod);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/******* Get the fields of records already present in current course *********/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
unsigned Rec_DB_GetAllFieldsInCrs (MYSQL_RES **mysql_res,long CrsCod)
|
|
|
|
|
{
|
|
|
|
|
return (unsigned)
|
|
|
|
|
DB_QuerySELECT (mysql_res,"can not get fields of records in a course",
|
|
|
|
|
"SELECT FieldCod," // row[0]
|
|
|
|
|
"FieldName," // row[1]
|
|
|
|
|
"NumLines," // row[2]
|
|
|
|
|
"Visibility" // row[3]
|
|
|
|
|
" FROM crs_record_fields"
|
|
|
|
|
" WHERE CrsCod=%ld"
|
|
|
|
|
" ORDER BY FieldName",
|
|
|
|
|
CrsCod);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 22:13:16 +02:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/************** Get the data of a field of records from its code *************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
unsigned Rec_DB_GetFieldByCod (MYSQL_RES **mysql_res,long CrsCod,long FieldCod)
|
|
|
|
|
{
|
|
|
|
|
return (unsigned)
|
|
|
|
|
DB_QuerySELECT (mysql_res,"can not get a field of a record in a course",
|
|
|
|
|
"SELECT FieldName," // row[0]
|
|
|
|
|
"NumLines," // row[1]
|
|
|
|
|
"Visibility" // row[2]
|
|
|
|
|
" FROM crs_record_fields"
|
|
|
|
|
" WHERE CrsCod=%ld"
|
|
|
|
|
" AND FieldCod=%ld",
|
|
|
|
|
CrsCod,
|
|
|
|
|
FieldCod);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/************** Get the text of a field of a record of course ****************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2021-10-13 10:21:02 +02:00
|
|
|
|
unsigned Rec_DB_GetFieldTxtFromUsrRecord (MYSQL_RES **mysql_res,
|
|
|
|
|
long FieldCod,long UsrCod)
|
2021-10-12 22:13:16 +02:00
|
|
|
|
{
|
|
|
|
|
return (unsigned)
|
|
|
|
|
DB_QuerySELECT (mysql_res,"can not get the text"
|
|
|
|
|
" of a field of a record",
|
|
|
|
|
"SELECT Txt" // row[0]
|
|
|
|
|
" FROM crs_records"
|
|
|
|
|
" WHERE FieldCod=%ld"
|
|
|
|
|
" AND UsrCod=%ld",
|
|
|
|
|
FieldCod,
|
|
|
|
|
UsrCod);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 10:21:02 +02:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/******************* Remove field from all students' records *****************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
void Rec_DB_RemoveFieldContentFromAllUsrsRecords (long FieldCod)
|
|
|
|
|
{
|
|
|
|
|
DB_QueryDELETE ("can not remove field from all students' records",
|
|
|
|
|
"DELETE FROM crs_records"
|
|
|
|
|
" WHERE FieldCod=%ld",
|
|
|
|
|
FieldCod);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 22:13:16 +02:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/******************* Remove text of field of course record *******************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2021-10-13 10:21:02 +02:00
|
|
|
|
void Rec_DB_RemoveFieldContentFromUsrRecord (long FieldCod,long UsrCod)
|
2021-10-12 22:13:16 +02:00
|
|
|
|
{
|
|
|
|
|
DB_QueryDELETE ("can not remove field of record",
|
|
|
|
|
"DELETE FROM crs_records"
|
|
|
|
|
" WHERE UsrCod=%ld"
|
|
|
|
|
" AND FieldCod=%ld",
|
|
|
|
|
UsrCod,
|
|
|
|
|
FieldCod);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/************ Remove fields of record of a user in current course ************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2021-10-13 10:21:02 +02:00
|
|
|
|
void Rec_DB_RemoveAllFieldContentsFromUsrRecordInCrs (long UsrCod,long CrsCod)
|
2021-10-12 22:13:16 +02:00
|
|
|
|
{
|
|
|
|
|
DB_QueryDELETE ("can not remove user's record in a course",
|
|
|
|
|
"DELETE FROM crs_records"
|
|
|
|
|
" WHERE UsrCod=%ld"
|
|
|
|
|
" AND FieldCod IN"
|
|
|
|
|
" (SELECT FieldCod"
|
|
|
|
|
" FROM crs_record_fields"
|
|
|
|
|
" WHERE CrsCod=%ld)",
|
|
|
|
|
UsrCod,
|
|
|
|
|
CrsCod);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/************* Remove fields of record of a user in all courses **************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2021-10-13 10:21:02 +02:00
|
|
|
|
void Rec_DB_RemoveAllFieldContentsFromUsrRecords (long UsrCod)
|
2021-10-12 22:13:16 +02:00
|
|
|
|
{
|
|
|
|
|
DB_QueryDELETE ("can not remove user's records in all courses",
|
|
|
|
|
"DELETE FROM crs_records"
|
|
|
|
|
" WHERE UsrCod=%ld",
|
|
|
|
|
UsrCod);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/******************** Remove content of course record cards ******************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2021-10-13 10:21:02 +02:00
|
|
|
|
void Rec_DB_RemoveAllFieldContentsInCrs (long CrsCod)
|
2021-10-12 22:13:16 +02:00
|
|
|
|
{
|
|
|
|
|
DB_QueryDELETE ("can not remove content of cards in a course",
|
|
|
|
|
"DELETE FROM crs_records"
|
|
|
|
|
" USING crs_record_fields,"
|
|
|
|
|
"crs_records"
|
|
|
|
|
" WHERE crs_record_fields.CrsCod=%ld"
|
|
|
|
|
" AND crs_record_fields.FieldCod=crs_records.FieldCod",
|
|
|
|
|
CrsCod);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 10:21:02 +02:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/*************************** Remove record field *****************************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
void Rec_DB_RemoveField (long FieldCod)
|
|
|
|
|
{
|
|
|
|
|
DB_QueryDELETE ("can not remove field of record",
|
|
|
|
|
"DELETE FROM crs_record_fields"
|
|
|
|
|
" WHERE FieldCod=%ld",
|
|
|
|
|
FieldCod);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 22:13:16 +02:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/************ Remove definition of fields in course record cards *************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2021-10-13 10:21:02 +02:00
|
|
|
|
void Rec_DB_RemoveAllFieldsInCrs (long CrsCod)
|
2021-10-12 22:13:16 +02:00
|
|
|
|
{
|
|
|
|
|
DB_QueryDELETE ("can not remove fields of cards in a course",
|
|
|
|
|
"DELETE FROM crs_record_fields"
|
|
|
|
|
" WHERE CrsCod=%ld",
|
|
|
|
|
CrsCod);
|
|
|
|
|
}
|