mirror of
https://gitea.msk.dinamika-avia.ru/Constanta-Design/RRJServer.git
synced 2026-03-28 19:55:48 +03:00
1144 lines
42 KiB
C++
1144 lines
42 KiB
C++
#include "databaselms.h"
|
|
|
|
#include <QtSql>
|
|
#include <QSqlDatabase>
|
|
#include <QSqlDriver>
|
|
#include <QMessageBox>
|
|
|
|
DataBaseLMS::DataBaseLMS():
|
|
db(nullptr),
|
|
transactionBegined(false)
|
|
{
|
|
|
|
}
|
|
|
|
DataBaseLMS::~DataBaseLMS()
|
|
{
|
|
deleteConnection();
|
|
}
|
|
|
|
bool DataBaseLMS::createConnection()
|
|
{
|
|
mtxAccess.lock();
|
|
|
|
db = new QSqlDatabase(QSqlDatabase::addDatabase(dbType, connectionName));
|
|
db->setDatabaseName(dbName);
|
|
db->setUserName(dbUserName);
|
|
db->setPassword(dbPassword);
|
|
db->setPort(5432);
|
|
db->setHostName("192.168.100.87");
|
|
|
|
bool res = db->open();
|
|
|
|
if(!res)
|
|
{
|
|
mtxAccess.unlock();
|
|
deleteConnection();
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
bool flHas = db->driver()->hasFeature(QSqlDriver::Transactions);
|
|
|
|
//bool resBool = QSqlDatabase::database(connectionName).transaction();
|
|
//resBool = QSqlDatabase::database(connectionName).commit();
|
|
|
|
mtxAccess.unlock();
|
|
return true;
|
|
}
|
|
}
|
|
|
|
void DataBaseLMS::deleteConnection()
|
|
{
|
|
mtxAccess.lock();
|
|
|
|
if(transactionBegined)
|
|
QSqlDatabase::database().rollback();
|
|
|
|
if(db != nullptr)
|
|
{
|
|
if(db->isOpen())
|
|
db->close();
|
|
|
|
delete db;
|
|
db = nullptr;
|
|
}
|
|
|
|
mtxAccess.unlock();
|
|
}
|
|
|
|
bool DataBaseLMS::isConnected()
|
|
{
|
|
mtxAccess.lock();
|
|
|
|
if(db == nullptr)
|
|
{
|
|
mtxAccess.unlock();
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
if(db->isOpen())
|
|
{
|
|
mtxAccess.unlock();
|
|
return true;
|
|
}
|
|
}
|
|
|
|
mtxAccess.unlock();
|
|
return false;
|
|
}
|
|
|
|
bool DataBaseLMS::transactionBegin()
|
|
{
|
|
/*
|
|
if(transactionBegined)
|
|
QSqlDatabase::database().rollback();
|
|
|
|
transactionBegined = true;
|
|
|
|
return QSqlDatabase::database().transaction();
|
|
*/
|
|
return true;
|
|
}
|
|
|
|
bool DataBaseLMS::transactionEnd()
|
|
{
|
|
/*
|
|
if(transactionBegined)
|
|
{
|
|
transactionBegined = false;
|
|
|
|
return QSqlDatabase::database().commit();
|
|
}
|
|
return false;
|
|
*/
|
|
return true;
|
|
}
|
|
|
|
QList<Instructor> DataBaseLMS::selectAllInstructors()
|
|
{
|
|
QList<Instructor> listInstructors;
|
|
|
|
QString queryStr = QString("SELECT instructor_id, name, login, password, is_admin, archived, logged_in "
|
|
"FROM public.instructors "
|
|
"ORDER BY instructor_id ASC");
|
|
|
|
QSqlQuery query = QSqlQuery(*db);
|
|
|
|
if(queryExec(queryStr, &query))
|
|
{
|
|
while (query.next())
|
|
{//Инструктор
|
|
Instructor instructor;
|
|
instructor.setID(query.value(0).toInt());
|
|
instructor.setName(query.value(1).toString());
|
|
instructor.setLogin(query.value(2).toString());
|
|
instructor.setPassword(query.value(3).toString());
|
|
instructor.setIsAdmin(query.value(4).toBool());
|
|
instructor.setArchived(query.value(5).toBool());
|
|
instructor.setLoggedIn(query.value(6).toBool());
|
|
|
|
listInstructors.append(instructor);
|
|
}
|
|
}
|
|
|
|
return listInstructors;
|
|
}
|
|
|
|
QList<Trainee> DataBaseLMS::selectAllTrainees()
|
|
{
|
|
QList<Trainee> listTrainees;
|
|
|
|
QString queryStr = QString("SELECT trainees.trainee_id, trainees.name, trainees.login, trainees.password, trainees.archived, trainees.logged_in, "
|
|
"groups.group_id, groups.name, "
|
|
"computers.computer_id, computers.name, computers.ip_address, "
|
|
"classrooms.classroom_id, classrooms.name "
|
|
"FROM public.trainees JOIN public.groups ON groups.group_id = trainees.group_trainee "
|
|
"LEFT OUTER JOIN public.computers ON computers.computer_id = trainees.computer_trainee "
|
|
"LEFT OUTER JOIN public.classrooms ON classrooms.classroom_id = computers.classroom_computer "
|
|
"ORDER BY groups.name, trainees.name ASC");
|
|
|
|
QSqlQuery query = QSqlQuery(*db);
|
|
|
|
if(queryExec(queryStr, &query))
|
|
{
|
|
while (query.next())
|
|
{//Обучаемый
|
|
Trainee trainee;
|
|
|
|
trainee.setID(query.value(0).toInt());
|
|
trainee.setName(query.value(1).toString());
|
|
trainee.setLogin(query.value(2).toString());
|
|
trainee.setPassword(query.value(3).toString());
|
|
trainee.setArchived(query.value(4).toBool());
|
|
trainee.setLoggedIn(query.value(5).toBool());
|
|
|
|
Group group = Group(query.value(6).toInt(), query.value(7).toString());
|
|
trainee.setGroup(group);
|
|
|
|
Classroom classroom = Classroom(query.value(11).toInt(), query.value(12).toString());
|
|
Computer computer = Computer(query.value(8).toInt(), query.value(9).toString(), query.value(10).toString(), classroom);
|
|
trainee.setComputer(computer);
|
|
|
|
listTrainees.append(trainee);
|
|
}
|
|
}
|
|
|
|
return listTrainees;
|
|
}
|
|
|
|
QList<Group> DataBaseLMS::selectAllGroups()
|
|
{
|
|
QList<Group> listGroups;
|
|
|
|
QString queryStr = QString("SELECT group_id, name "
|
|
"FROM public.groups "
|
|
"ORDER BY group_id ASC");
|
|
|
|
QSqlQuery query = QSqlQuery(*db);
|
|
|
|
if(queryExec(queryStr, &query))
|
|
{
|
|
while (query.next())
|
|
{//Группа
|
|
Group group;
|
|
group.setID(query.value(0).toInt());
|
|
group.setName(query.value(1).toString());
|
|
|
|
listGroups.append(group);
|
|
}
|
|
}
|
|
|
|
return listGroups;
|
|
}
|
|
|
|
Instructor DataBaseLMS::selectInstructor(int id_instructor)
|
|
{
|
|
Instructor instructor;
|
|
|
|
QString queryStr = QString("SELECT instructor_id, name, login, password, is_admin, archived, logged_in "
|
|
"FROM public.instructors "
|
|
"WHERE instructors.instructor_id = %1 ").arg(
|
|
id_instructor);
|
|
|
|
QSqlQuery query = QSqlQuery(*db);
|
|
|
|
if(queryExec(queryStr, &query))
|
|
{
|
|
if (query.first())
|
|
{//Инструктор
|
|
instructor.setID(query.value(0).toInt());
|
|
instructor.setName(query.value(1).toString());
|
|
instructor.setLogin(query.value(2).toString());
|
|
instructor.setPassword(query.value(3).toString());
|
|
instructor.setIsAdmin(query.value(4).toBool());
|
|
instructor.setArchived(query.value(5).toBool());
|
|
instructor.setLoggedIn(query.value(6).toBool());
|
|
}
|
|
}
|
|
|
|
return instructor;
|
|
}
|
|
|
|
int DataBaseLMS::selectInstructorID(QString login, QString password)
|
|
{
|
|
QString queryStr;
|
|
|
|
if(password != QStringLiteral(""))
|
|
{
|
|
queryStr = QString("SELECT instructors.instructor_id "
|
|
"FROM public.instructors "
|
|
"WHERE login = '%1' AND password = '%2' ").arg(
|
|
login,
|
|
password );
|
|
}
|
|
else
|
|
{
|
|
queryStr = QString("SELECT instructors.instructor_id "
|
|
"FROM public.instructors "
|
|
"WHERE login = '%1' ").arg(
|
|
login );
|
|
}
|
|
|
|
return queryExecInt(queryStr);
|
|
}
|
|
|
|
QString DataBaseLMS::selectInstructorNameByLogin(QString login)
|
|
{
|
|
QString queryStr = QString("SELECT instructors.name "
|
|
"FROM public.instructors "
|
|
"WHERE instructors.login = '%1' ").arg(
|
|
login );
|
|
|
|
return queryExecString(queryStr);
|
|
}
|
|
|
|
bool DataBaseLMS::selectInstructorIsAdmin(int id_instructor)
|
|
{
|
|
QString queryStr = QString("SELECT instructors.is_admin "
|
|
"FROM public.instructors "
|
|
"WHERE instructor_id = %1 ").arg(
|
|
id_instructor );
|
|
|
|
return queryExecBool(queryStr);
|
|
}
|
|
|
|
bool DataBaseLMS::selectInstructorLoggedIn(int id_instructor)
|
|
{
|
|
QString queryStr = QString("SELECT instructors.logged_in "
|
|
"FROM public.instructors "
|
|
"WHERE instructor_id = %1 ").arg(
|
|
id_instructor );
|
|
|
|
return queryExecBool(queryStr);
|
|
}
|
|
|
|
bool DataBaseLMS::selectInstructorArchived(int id_instructor)
|
|
{
|
|
QString queryStr = QString("SELECT instructors.archived "
|
|
"FROM public.instructors "
|
|
"WHERE instructor_id = %1 ").arg(
|
|
id_instructor );
|
|
|
|
return queryExecBool(queryStr);
|
|
}
|
|
|
|
int DataBaseLMS::updateInstructorLoggedIn(int id_instructor, bool loggedIn)
|
|
{
|
|
QString queryStr = QString("UPDATE public.instructors "
|
|
"SET logged_in = %1 "
|
|
"WHERE instructor_id = %2 "
|
|
"RETURNING instructors.instructor_id").arg(
|
|
loggedIn ? "true" : "false",
|
|
QString::number(id_instructor) );
|
|
|
|
return queryExecInt(queryStr);
|
|
}
|
|
|
|
bool DataBaseLMS::updateAllInstructorsLoggedIn(bool loggedIn)
|
|
{
|
|
QString queryStr = QString("UPDATE public.instructors "
|
|
"SET logged_in = %1 ").arg(
|
|
loggedIn ? "true" : "false");
|
|
|
|
QSqlQuery query = QSqlQuery(*db);
|
|
|
|
return queryExec(queryStr, &query);
|
|
}
|
|
|
|
int DataBaseLMS::updateInstructorArchived(int id_instructor, bool archived)
|
|
{
|
|
QString queryStr = QString("UPDATE public.instructors "
|
|
"SET archived = %1 "
|
|
"WHERE instructor_id = %2 "
|
|
"RETURNING instructors.instructor_id").arg(
|
|
archived ? "true" : "false",
|
|
QString::number(id_instructor) );
|
|
|
|
return queryExecInt(queryStr);
|
|
}
|
|
|
|
int DataBaseLMS::insertInstructor()
|
|
{
|
|
QString queryStr = QString("INSERT INTO public.instructors "
|
|
"DEFAULT VALUES "
|
|
"RETURNING instructor_id");
|
|
|
|
return queryExecInt(queryStr);
|
|
}
|
|
|
|
int DataBaseLMS::insertInstructor(Instructor instructor)
|
|
{
|
|
QString queryStr = QString("INSERT INTO public.instructors (name, login, password, is_admin, archived, logged_in) "
|
|
"VALUES ('%1', '%2', '%3', %4, %5, %6) "
|
|
"RETURNING instructor_id").arg(
|
|
instructor.getName(),
|
|
instructor.getLogin(),
|
|
instructor.getPassword(),
|
|
instructor.getIsAdmin() ? "true" : "false",
|
|
instructor.getArchived() ? "true" : "false",
|
|
instructor.getLoggedIn() ? "true" : "false");
|
|
|
|
return queryExecInt(queryStr);
|
|
}
|
|
|
|
int DataBaseLMS::deleteInstructor(int id_instructor)
|
|
{
|
|
QString queryStr = QString("DELETE FROM public.instructors "
|
|
"WHERE instructor_id = %1 "
|
|
"RETURNING instructors.instructor_id").arg(
|
|
QString::number(id_instructor));
|
|
|
|
return queryExecInt(queryStr);
|
|
}
|
|
|
|
int DataBaseLMS::updateInstructor(Instructor instructor)
|
|
{
|
|
QString queryStr = QString("UPDATE public.instructors "
|
|
"SET name = '%1', login = '%2', password = '%3', is_admin = %4, archived = %5, logged_in = %6 "
|
|
"WHERE instructor_id = %7 "
|
|
"RETURNING instructors.instructor_id").arg(
|
|
instructor.getName(),
|
|
instructor.getLogin(),
|
|
instructor.getPassword(),
|
|
instructor.getIsAdmin() ? "true" : "false",
|
|
instructor.getArchived() ? "true" : "false",
|
|
instructor.getLoggedIn() ? "true" : "false",
|
|
QString::number(instructor.getID()) );
|
|
|
|
return queryExecInt(queryStr);
|
|
}
|
|
|
|
Group DataBaseLMS::selectGroup(int id_group)
|
|
{
|
|
Group group;
|
|
|
|
QString queryStr = QString("SELECT group_id, name "
|
|
"FROM public.groups "
|
|
"WHERE groups.group_id = %1 ").arg(
|
|
id_group);
|
|
|
|
QSqlQuery query = QSqlQuery(*db);
|
|
|
|
if(queryExec(queryStr, &query))
|
|
{
|
|
if (query.first())
|
|
{//Инструктор
|
|
group.setID(query.value(0).toInt());
|
|
group.setName(query.value(1).toString());
|
|
}
|
|
}
|
|
|
|
return group;
|
|
}
|
|
|
|
int DataBaseLMS::insertGroup()
|
|
{
|
|
QString queryStr = QString("INSERT INTO public.groups "
|
|
"DEFAULT VALUES "
|
|
"RETURNING group_id");
|
|
|
|
return queryExecInt(queryStr);
|
|
}
|
|
|
|
int DataBaseLMS::insertGroup(Group group)
|
|
{
|
|
QString queryStr = QString("INSERT INTO public.groups (name) "
|
|
"VALUES ('%1') "
|
|
"RETURNING groups.group_id").arg(
|
|
group.getName());
|
|
|
|
return queryExecInt(queryStr);
|
|
}
|
|
|
|
int DataBaseLMS::deleteGroup(int id_group)
|
|
{
|
|
QString queryStr = QString("DELETE FROM public.groups "
|
|
"WHERE group_id = %1 "
|
|
"RETURNING groups.group_id").arg(
|
|
QString::number(id_group));
|
|
|
|
return queryExecInt(queryStr);
|
|
}
|
|
|
|
int DataBaseLMS::updateGroup(Group group)
|
|
{
|
|
QString queryStr = QString("UPDATE public.groups SET name = '%1' "
|
|
"WHERE group_id = %2 "
|
|
"RETURNING groups.group_id").arg(
|
|
group.getName(),
|
|
QString::number(group.getID()) );
|
|
|
|
return queryExecInt(queryStr);
|
|
}
|
|
|
|
int DataBaseLMS::insertTaskAMM(TaskAmmFim task, int id_trainee)
|
|
{
|
|
task.ammProcedure.title = task.ammProcedure.title.replace("'", "''"); //Задваиваем одинарные кавычки
|
|
|
|
QString queryStr = QString("INSERT INTO public.tasks_amm (title, dm_code, trainee_task) "
|
|
"VALUES ('%1', '%2', %3) "
|
|
"RETURNING tasks_amm.task_id").arg(
|
|
task.ammProcedure.title,
|
|
task.ammProcedure.dmCode,
|
|
QString::number(id_trainee));
|
|
|
|
return queryExecInt(queryStr);
|
|
}
|
|
|
|
int DataBaseLMS::updateTaskAMM(TaskAmmFim task)
|
|
{
|
|
QString queryStr = QString("UPDATE public.tasks_amm SET title = '%1', dm_code = '%2' "
|
|
"WHERE task_id = %3 "
|
|
"RETURNING tasks_amm.task_id").arg(
|
|
task.ammProcedure.title,
|
|
task.ammProcedure.dmCode,
|
|
QString::number(task.getID()) );
|
|
|
|
return queryExecInt(queryStr);
|
|
}
|
|
|
|
int DataBaseLMS::deleteTaskAMM(int id_task)
|
|
{
|
|
QString queryStr;
|
|
bool resBool = false;
|
|
int id_trainee = 0;
|
|
|
|
resBool = db->transaction();
|
|
|
|
queryStr = QString("SELECT trainees.trainee_id "
|
|
"FROM public.trainees JOIN public.tasks_amm ON trainees.trainee_id = tasks_amm.trainee_task "
|
|
"WHERE tasks_amm.task_id = %1 "
|
|
"ORDER BY trainees.trainee_id ASC").arg(
|
|
QString::number(id_task));
|
|
|
|
QSqlQuery query = QSqlQuery(*db);
|
|
|
|
if(queryExec(queryStr, &query))
|
|
{
|
|
if (query.first())
|
|
{//Обучаемый
|
|
id_trainee = query.value(0).toInt();
|
|
}
|
|
}
|
|
if(!id_trainee)
|
|
{
|
|
resBool = db->rollback();
|
|
return 0;
|
|
}
|
|
|
|
queryStr = QString("DELETE FROM public.tasks_amm "
|
|
"WHERE task_id = %1 "
|
|
"RETURNING tasks_amm.task_id").arg(
|
|
QString::number(id_task));
|
|
|
|
if(!queryExecInt(queryStr))
|
|
{
|
|
resBool = db->rollback();
|
|
return 0;
|
|
}
|
|
|
|
resBool = db->commit();
|
|
return id_trainee;
|
|
}
|
|
|
|
QList<TaskAmmFim> DataBaseLMS::selectTasksAMMofTrainee(int id_trainee)
|
|
{
|
|
QList<TaskAmmFim> listTasks;
|
|
|
|
QString queryStr = QString("SELECT tasks_amm.task_id, tasks_amm.title, tasks_amm.dm_code, "
|
|
"trainees.trainee_id "
|
|
"FROM public.tasks_amm JOIN public.trainees ON trainees.trainee_id = tasks_amm.trainee_task "
|
|
"WHERE tasks_amm.trainee_task = %1 "
|
|
"ORDER BY tasks_amm.task_id ASC").arg(
|
|
id_trainee);
|
|
|
|
QSqlQuery query = QSqlQuery(*db);
|
|
|
|
if(queryExec(queryStr, &query))
|
|
{
|
|
while (query.next())
|
|
{//Задача
|
|
TaskAmmFim task;
|
|
|
|
task.setID(query.value(0).toInt());
|
|
task.ammProcedure.title = query.value(1).toString();
|
|
task.ammProcedure.dmCode = query.value(2).toString();
|
|
|
|
listTasks.append(task);
|
|
}
|
|
}
|
|
|
|
return listTasks;
|
|
}
|
|
|
|
int DataBaseLMS::insertTaskFIM(TaskAmmFim task, int id_trainee)
|
|
{
|
|
QString queryStr;
|
|
bool resBool = false;
|
|
|
|
resBool = db->transaction();
|
|
|
|
task.title = task.title.replace("'", "''"); //Задваиваем одинарные кавычки
|
|
|
|
queryStr = QString("INSERT INTO public.tasks_fim (title, trainee_task) "
|
|
"VALUES ('%1', %2) "
|
|
"RETURNING tasks_fim.task_id").arg(
|
|
task.title,
|
|
QString::number(id_trainee));
|
|
|
|
int task_id = queryExecInt(queryStr);
|
|
if(!task_id)
|
|
{
|
|
resBool = db->rollback();
|
|
return 0;
|
|
}
|
|
|
|
for(Malfunction malfanction : task.malfunctionList)
|
|
{
|
|
malfanction.description = malfanction.description.replace("'", "''"); //Задваиваем одинарные кавычки
|
|
|
|
queryStr = QString("INSERT INTO public.malfunctions (num, dm_code, description, task_fim_malf) "
|
|
"VALUES ('%1', '%2', '%3', %4) "
|
|
"RETURNING malfunctions.malfunction_id").arg(
|
|
malfanction.num,
|
|
malfanction.dmCode,
|
|
malfanction.description,
|
|
QString::number(task_id));
|
|
|
|
if(!queryExecInt(queryStr))
|
|
{
|
|
resBool = db->rollback();
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
resBool = db->commit();
|
|
return task_id;
|
|
}
|
|
|
|
int DataBaseLMS::updateTaskFIM(TaskAmmFim task)
|
|
{
|
|
QString queryStr = QString("UPDATE public.tasks_fim SET title = '%1' "
|
|
"WHERE task_id = %2 "
|
|
"RETURNING tasks_fim.task_id").arg(
|
|
task.title,
|
|
QString::number(task.getID()) );
|
|
|
|
return queryExecInt(queryStr);
|
|
}
|
|
|
|
int DataBaseLMS::deleteTaskFIM(int id_task)
|
|
{
|
|
QString queryStr;
|
|
bool resBool = false;
|
|
int id_trainee = 0;
|
|
|
|
resBool = db->transaction();
|
|
|
|
queryStr = QString("SELECT trainees.trainee_id "
|
|
"FROM public.trainees JOIN public.tasks_fim ON trainees.trainee_id = tasks_fim.trainee_task "
|
|
"WHERE tasks_fim.task_id = %1 "
|
|
"ORDER BY trainees.trainee_id ASC").arg(
|
|
QString::number(id_task));
|
|
|
|
QSqlQuery query = QSqlQuery(*db);
|
|
|
|
if(queryExec(queryStr, &query))
|
|
{
|
|
if (query.first())
|
|
{//Обучаемый
|
|
id_trainee = query.value(0).toInt();
|
|
}
|
|
}
|
|
if(!id_trainee)
|
|
{
|
|
resBool = db->rollback();
|
|
return 0;
|
|
}
|
|
|
|
queryStr = QString("DELETE FROM public.malfunctions "
|
|
"WHERE task_fim_malf = %1 ").arg(
|
|
QString::number(id_task));
|
|
|
|
QSqlQuery query1 = QSqlQuery(*db);
|
|
if(!queryExec(queryStr, &query1))
|
|
{
|
|
resBool = db->rollback();
|
|
return 0;
|
|
}
|
|
|
|
queryStr = QString("DELETE FROM public.tasks_fim "
|
|
"WHERE task_id = %1 "
|
|
"RETURNING tasks_fim.task_id").arg(
|
|
QString::number(id_task));
|
|
|
|
if(!queryExecInt(queryStr))
|
|
{
|
|
resBool = db->rollback();
|
|
return 0;
|
|
}
|
|
|
|
resBool = db->commit();
|
|
return id_trainee;
|
|
}
|
|
|
|
QList<TaskAmmFim> DataBaseLMS::selectTasksFIMofTrainee(int id_trainee)
|
|
{
|
|
QList<TaskAmmFim> listTasks;
|
|
QString queryStr;
|
|
bool resBool = false;
|
|
|
|
resBool = db->transaction();
|
|
|
|
queryStr = QString("SELECT tasks_fim.task_id, tasks_fim.title, "
|
|
"trainees.trainee_id "
|
|
"FROM public.tasks_fim JOIN public.trainees ON trainees.trainee_id = tasks_fim.trainee_task "
|
|
"WHERE tasks_fim.trainee_task = %1 "
|
|
"ORDER BY tasks_fim.task_id ASC").arg(
|
|
id_trainee);
|
|
|
|
QSqlQuery query = QSqlQuery(*db);
|
|
|
|
if(queryExec(queryStr, &query))
|
|
{
|
|
while (query.next())
|
|
{//Задача
|
|
TaskAmmFim task;
|
|
|
|
task.setID(query.value(0).toInt());
|
|
task.title = query.value(1).toString();
|
|
|
|
//Выгребаем все malfunction для этой задачи
|
|
queryStr = QString("SELECT malfunctions.malfunction_id, malfunctions.num, malfunctions.dm_code, malfunctions.description, "
|
|
"tasks_fim.task_id "
|
|
"FROM public.malfunctions JOIN public.tasks_fim ON tasks_fim.task_id = malfunctions.task_fim_malf "
|
|
"WHERE malfunctions.task_fim_malf = %1 "
|
|
"ORDER BY malfunctions.num ASC").arg(
|
|
task.getID());
|
|
|
|
QSqlQuery queryMalf = QSqlQuery(*db);
|
|
|
|
if(queryExec(queryStr, &queryMalf))
|
|
{
|
|
while (queryMalf.next())
|
|
{//Неисправность
|
|
Malfunction malfanction;
|
|
|
|
malfanction.num = queryMalf.value(1).toString();
|
|
malfanction.dmCode = queryMalf.value(2).toString();
|
|
malfanction.description = queryMalf.value(3).toString();
|
|
|
|
task.addMalfunction(malfanction);
|
|
};
|
|
}
|
|
else
|
|
{
|
|
resBool = db->rollback();
|
|
return QList<TaskAmmFim>();
|
|
}
|
|
|
|
listTasks.append(task);
|
|
};
|
|
}
|
|
else
|
|
{
|
|
resBool = db->rollback();
|
|
return QList<TaskAmmFim>();
|
|
}
|
|
|
|
resBool = db->commit();
|
|
return listTasks;
|
|
}
|
|
|
|
Trainee DataBaseLMS::selectTrainee(int id_trainee)
|
|
{
|
|
Trainee trainee;
|
|
|
|
QString queryStr = QString("SELECT trainees.trainee_id, trainees.name, trainees.login, trainees.password, trainees.archived, trainees.logged_in, "
|
|
"groups.group_id, groups.name, "
|
|
"computers.computer_id, computers.name, computers.ip_address, "
|
|
"classrooms.classroom_id, classrooms.name "
|
|
"FROM public.trainees JOIN public.groups ON groups.group_id = trainees.group_trainee "
|
|
"LEFT OUTER JOIN public.computers ON computers.computer_id = trainees.computer_trainee "
|
|
"LEFT OUTER JOIN public.classrooms ON classrooms.classroom_id = computers.classroom_computer "
|
|
"WHERE trainees.trainee_id = %1 "
|
|
"ORDER BY groups.name, trainees.name ASC").arg(
|
|
id_trainee);
|
|
|
|
QSqlQuery query = QSqlQuery(*db);
|
|
|
|
if(queryExec(queryStr, &query))
|
|
{
|
|
if (query.first())
|
|
{//Инструктор
|
|
trainee.setID(query.value(0).toInt());
|
|
trainee.setName(query.value(1).toString());
|
|
trainee.setLogin(query.value(2).toString());
|
|
trainee.setPassword(query.value(3).toString());
|
|
trainee.setArchived(query.value(4).toBool());
|
|
trainee.setLoggedIn(query.value(5).toBool());
|
|
|
|
Group group = Group(query.value(6).toInt(), query.value(7).toString());
|
|
trainee.setGroup(group);
|
|
|
|
Classroom classroom = Classroom(query.value(11).toInt(), query.value(12).toString());
|
|
Computer computer = Computer(query.value(8).toInt(), query.value(9).toString(), query.value(10).toString(), classroom);
|
|
trainee.setComputer(computer);
|
|
}
|
|
}
|
|
|
|
return trainee;
|
|
}
|
|
|
|
QList<Trainee> DataBaseLMS::selectAllTraineesInGroup(int id_group)
|
|
{
|
|
QList<Trainee> listTrainees;
|
|
|
|
QString queryStr = QString("SELECT trainees.trainee_id, trainees.name, trainees.login, trainees.password, trainees.archived, trainees.logged_in, "
|
|
"groups.group_id, groups.name, "
|
|
"computers.computer_id, computers.name, computers.ip_address, "
|
|
"classrooms.classroom_id, classrooms.name "
|
|
"FROM public.trainees JOIN public.groups ON groups.group_id = trainees.group_trainee "
|
|
"LEFT OUTER JOIN public.computers ON computers.computer_id = trainees.computer_trainee "
|
|
"LEFT OUTER JOIN public.classrooms ON classrooms.classroom_id = computers.classroom_computer "
|
|
"WHERE trainees.group_trainee = %1 "
|
|
"ORDER BY groups.name, trainees.name ASC").arg(
|
|
id_group);
|
|
|
|
QSqlQuery query = QSqlQuery(*db);
|
|
|
|
if(queryExec(queryStr, &query))
|
|
{
|
|
while (query.next())
|
|
{//Обучаемый
|
|
Trainee trainee;
|
|
|
|
trainee.setID(query.value(0).toInt());
|
|
trainee.setName(query.value(1).toString());
|
|
trainee.setLogin(query.value(2).toString());
|
|
trainee.setPassword(query.value(3).toString());
|
|
trainee.setArchived(query.value(4).toBool());
|
|
trainee.setLoggedIn(query.value(5).toBool());
|
|
|
|
Group group = Group(query.value(6).toInt(), query.value(7).toString());
|
|
trainee.setGroup(group);
|
|
|
|
Classroom classroom = Classroom(query.value(11).toInt(), query.value(12).toString());
|
|
Computer computer = Computer(query.value(8).toInt(), query.value(9).toString(), query.value(10).toString(), classroom);
|
|
trainee.setComputer(computer);
|
|
|
|
listTrainees.append(trainee);
|
|
}
|
|
}
|
|
|
|
return listTrainees;
|
|
}
|
|
|
|
int DataBaseLMS::selectTraineeID(QString login, QString password)
|
|
{
|
|
QString queryStr;
|
|
|
|
if(password != QStringLiteral(""))
|
|
{
|
|
queryStr = QString("SELECT trainees.trainee_id "
|
|
"FROM public.trainees "
|
|
"WHERE login = '%1' AND password = '%2' ").arg(
|
|
login,
|
|
password );
|
|
}
|
|
else
|
|
{
|
|
queryStr = QString("SELECT trainees.trainee_id "
|
|
"FROM public.trainees "
|
|
"WHERE login = '%1' ").arg(
|
|
login );
|
|
}
|
|
|
|
return queryExecInt(queryStr);
|
|
}
|
|
|
|
QString DataBaseLMS::selectTraineeNameByLogin(QString login)
|
|
{
|
|
QString queryStr = QString("SELECT trainees.name "
|
|
"FROM public.trainees "
|
|
"WHERE trainees.login = '%1' ").arg(
|
|
login );
|
|
|
|
return queryExecString(queryStr);
|
|
}
|
|
|
|
QString DataBaseLMS::selectTraineeNameOnComputer(QString computer_name)
|
|
{
|
|
QString queryStr = QString("SELECT trainees.name "
|
|
"FROM public.trainees JOIN public.computers ON computers.computer_id = trainees.computer_trainee "
|
|
"WHERE computers.name = '%1' ").arg(
|
|
computer_name);
|
|
|
|
return queryExecString(queryStr);
|
|
}
|
|
|
|
Trainee DataBaseLMS::selectTraineeOnComputer(QString computer_name)
|
|
{
|
|
Trainee trainee;
|
|
|
|
QString queryStr = QString("SELECT trainees.trainee_id, trainees.name, trainees.login, trainees.password, trainees.archived, trainees.logged_in, "
|
|
"groups.group_id, groups.name, "
|
|
"computers.computer_id, computers.name, computers.ip_address, "
|
|
"classrooms.classroom_id, classrooms.name "
|
|
"FROM public.trainees JOIN public.groups ON groups.group_id = trainees.group_trainee "
|
|
"LEFT OUTER JOIN public.computers ON computers.computer_id = trainees.computer_trainee "
|
|
"LEFT OUTER JOIN public.classrooms ON classrooms.classroom_id = computers.classroom_computer "
|
|
"WHERE computers.name = '%1' ").arg(
|
|
computer_name);
|
|
|
|
QSqlQuery query = QSqlQuery(*db);
|
|
|
|
if(queryExec(queryStr, &query))
|
|
{
|
|
if (query.first())
|
|
{//Инструктор
|
|
trainee.setID(query.value(0).toInt());
|
|
trainee.setName(query.value(1).toString());
|
|
trainee.setLogin(query.value(2).toString());
|
|
trainee.setPassword(query.value(3).toString());
|
|
trainee.setArchived(query.value(4).toBool());
|
|
trainee.setLoggedIn(query.value(5).toBool());
|
|
|
|
Group group = Group(query.value(6).toInt(), query.value(7).toString());
|
|
trainee.setGroup(group);
|
|
|
|
Classroom classroom = Classroom(query.value(11).toInt(), query.value(12).toString());
|
|
Computer computer = Computer(query.value(8).toInt(), query.value(9).toString(), query.value(10).toString(), classroom);
|
|
trainee.setComputer(computer);
|
|
}
|
|
}
|
|
|
|
return trainee;
|
|
}
|
|
|
|
bool DataBaseLMS::selectTraineeArchived(int id_trainee)
|
|
{
|
|
QString queryStr = QString("SELECT trainees.archived "
|
|
"FROM public.trainees "
|
|
"WHERE trainee_id = %1 ").arg(
|
|
id_trainee );
|
|
|
|
return queryExecBool(queryStr);
|
|
}
|
|
|
|
bool DataBaseLMS::selectTraineeLoggedIn(int id_trainee)
|
|
{
|
|
QString queryStr = QString("SELECT trainees.logged_in "
|
|
"FROM public.trainees "
|
|
"WHERE trainee_id = %1 ").arg(
|
|
id_trainee );
|
|
|
|
return queryExecBool(queryStr);
|
|
}
|
|
|
|
int DataBaseLMS::updateTraineeLoggedIn(int id_trainee, bool loggedIn)
|
|
{
|
|
QString queryStr = QString("UPDATE public.trainees "
|
|
"SET logged_in = %1 "
|
|
"WHERE trainee_id = %2 "
|
|
"RETURNING trainees.trainee_id").arg(
|
|
loggedIn ? "true" : "false",
|
|
QString::number(id_trainee) );
|
|
|
|
return queryExecInt(queryStr);
|
|
}
|
|
|
|
bool DataBaseLMS::updateAllTraineesLoggedIn(bool loggedIn)
|
|
{
|
|
QString queryStr = QString("UPDATE public.trainees "
|
|
"SET logged_in = %1 ").arg(
|
|
loggedIn ? "true" : "false");
|
|
|
|
QSqlQuery query = QSqlQuery(*db);
|
|
|
|
return queryExec(queryStr, &query);
|
|
}
|
|
|
|
int DataBaseLMS::updateTraineeArchived(int id_trainee, bool archived)
|
|
{
|
|
QString queryStr = QString("UPDATE public.trainees "
|
|
"SET archived = %1 "
|
|
"WHERE trainee_id = %2 "
|
|
"RETURNING trainees.trainee_id").arg(
|
|
archived ? "true" : "false",
|
|
QString::number(id_trainee) );
|
|
|
|
return queryExecInt(queryStr);
|
|
}
|
|
|
|
int DataBaseLMS::insertTrainee(int id_group)
|
|
{
|
|
QString queryStr = QString("INSERT INTO public.trainees (name, login, password, archived, logged_in, group_trainee) "
|
|
"VALUES (DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, %1) "
|
|
"RETURNING trainees.trainee_id").arg(
|
|
QString::number(id_group));
|
|
|
|
return queryExecInt(queryStr);
|
|
}
|
|
|
|
int DataBaseLMS::insertTrainee(Trainee trainee)
|
|
{
|
|
QString queryStr = QString("INSERT INTO public.trainees (name, login, password, archived, logged_in, group_trainee) "
|
|
"VALUES ('%1', '%2', '%3', %4, %5, %6) "
|
|
"RETURNING trainees.trainee_id").arg(
|
|
trainee.getName(),
|
|
trainee.getLogin(),
|
|
trainee.getPassword(),
|
|
trainee.getArchived() ? "true" : "false",
|
|
trainee.getLoggedIn() ? "true" : "false",
|
|
QString::number(trainee.getGroup().getID()));
|
|
|
|
return queryExecInt(queryStr);
|
|
}
|
|
|
|
int DataBaseLMS::deleteTrainee(int id_trainee)
|
|
{
|
|
QString queryStr;
|
|
int res = 0;
|
|
bool resBool = false;
|
|
|
|
resBool = db->transaction();
|
|
|
|
QSqlQuery query = QSqlQuery(*db);
|
|
|
|
|
|
//Удаление задач AMM
|
|
queryStr = QString("DELETE FROM public.tasks_amm "
|
|
"WHERE trainee_task = %1 ").arg(
|
|
QString::number(id_trainee));
|
|
|
|
if(!queryExec(queryStr, &query))
|
|
{
|
|
resBool = db->rollback();
|
|
return 0;
|
|
}
|
|
|
|
|
|
//Удаление задач FIM
|
|
|
|
/*Выборка задач FIM для этого обучаемого*/
|
|
queryStr = QString("SELECT tasks_fim.task_id "
|
|
"FROM public.tasks_fim "
|
|
"WHERE tasks_fim.trainee_task = %1 "
|
|
"ORDER BY tasks_fim.task_id ASC").arg(
|
|
id_trainee);
|
|
|
|
if(queryExec(queryStr, &query))
|
|
{
|
|
while (query.next())
|
|
{//Задача
|
|
/*Удаляем все malfunction для этой задачи*/
|
|
queryStr = QString("DELETE FROM public.malfunctions "
|
|
"WHERE malfunctions.task_fim_malf = %1 ").arg(
|
|
query.value(0).toInt());
|
|
QSqlQuery queryDel = QSqlQuery(*db);
|
|
if(!queryExec(queryStr, &queryDel))
|
|
{
|
|
resBool = db->rollback();
|
|
return 0;
|
|
}
|
|
};
|
|
}
|
|
else
|
|
{
|
|
resBool = db->rollback();
|
|
return 0;
|
|
}
|
|
|
|
/*Удаление непосредственно задач FIM*/
|
|
queryStr = QString("DELETE FROM public.tasks_fim "
|
|
"WHERE trainee_task = %1 ").arg(
|
|
QString::number(id_trainee));
|
|
|
|
if(!queryExec(queryStr, &query))
|
|
{
|
|
resBool = db->rollback();
|
|
return 0;
|
|
}
|
|
|
|
|
|
//Удаление обучаемого
|
|
queryStr = QString("DELETE FROM public.trainees "
|
|
"WHERE trainee_id = %1 "
|
|
"RETURNING trainees.trainee_id").arg(
|
|
QString::number(id_trainee));
|
|
|
|
res = queryExecInt(queryStr);
|
|
if(res)
|
|
{
|
|
resBool = db->commit();
|
|
return res;
|
|
}
|
|
else
|
|
{
|
|
resBool = db->rollback();
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
int DataBaseLMS::updateTrainee(Trainee trainee)
|
|
{
|
|
QString computer_id = (trainee.getComputer().getID() == 0 ? QStringLiteral("null") : QString::number(trainee.getComputer().getID()));
|
|
|
|
QString queryStr = QString("UPDATE public.trainees "
|
|
"SET name = '%1', login = '%2', password = '%3', archived = %4, logged_in = %5, "
|
|
"group_trainee = %6, "
|
|
"computer_trainee = %7 "
|
|
"WHERE trainee_id = %8 "
|
|
"RETURNING trainees.trainee_id").arg(
|
|
trainee.getName(),
|
|
trainee.getLogin(),
|
|
trainee.getPassword(),
|
|
trainee.getArchived() ? "true" : "false",
|
|
trainee.getLoggedIn() ? "true" : "false",
|
|
QString::number(trainee.getGroup().getID()),
|
|
computer_id,
|
|
QString::number(trainee.getID()) );
|
|
|
|
return queryExecInt(queryStr);
|
|
}
|
|
|
|
int DataBaseLMS::queryExecInt(QString queryStr)
|
|
{
|
|
QSqlQuery query = QSqlQuery(*db);
|
|
|
|
if(queryExec(queryStr, &query))
|
|
{
|
|
if(query.first())
|
|
return query.value(0).toInt();
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
QString DataBaseLMS::queryExecString(QString queryStr)
|
|
{
|
|
QSqlQuery query = QSqlQuery(*db);
|
|
|
|
if(queryExec(queryStr, &query))
|
|
{
|
|
if(query.first())
|
|
return query.value(0).toString();
|
|
}
|
|
|
|
return QStringLiteral("");
|
|
}
|
|
|
|
bool DataBaseLMS::queryExecBool(QString queryStr)
|
|
{
|
|
QSqlQuery query = QSqlQuery(*db);
|
|
|
|
if(queryExec(queryStr, &query))
|
|
{
|
|
if(query.first())
|
|
return query.value(0).toBool();
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
bool DataBaseLMS::queryExec(QString queryStr, QSqlQuery *query)
|
|
{
|
|
bool res = false;
|
|
mtxAccess.lock();
|
|
if(! (res = query->exec(queryStr)) )
|
|
messageWarningErrorQuery(queryStr, query);
|
|
mtxAccess.unlock();
|
|
return res;
|
|
}
|
|
|
|
void DataBaseLMS::messageWarningErrorQuery(QString queryStr, QSqlQuery* query)
|
|
{
|
|
QMessageBox::warning(nullptr, dbName,
|
|
"Error query:\n" + query->lastError().text() + "\n" +
|
|
"String of query:\n" + queryStr + "\n" +
|
|
"Executed query:\n" + query->executedQuery());
|
|
}
|