mirror of
https://gitea.msk.dinamika-avia.ru/Constanta-Design/RRJServer.git
synced 2026-03-27 19:45:43 +03:00
456 lines
11 KiB
C++
456 lines
11 KiB
C++
#include "databasetrainees.h"
|
|
#include <QApplication>
|
|
#include <QMessageBox>
|
|
|
|
DataBaseTrainees::DataBaseTrainees(DataBaseLMS* dbLMS)
|
|
{
|
|
listOfColorGroup.append(QColor(170, 190, 170));
|
|
listOfColorGroup.append(QColor(180, 180, 220));
|
|
listOfColorGroup.append(QColor(240, 220, 230));
|
|
listOfColorGroup.append(QColor(85, 170, 127));
|
|
listOfColorGroup.append(QColor(170, 115, 120));
|
|
listOfColorGroup.append(QColor(110, 160, 170));
|
|
listOfColorGroup.append(QColor(110, 170, 130));
|
|
listOfColorGroup.append(QColor(170, 170, 120));
|
|
listOfColorGroup.append(QColor(160, 170, 45));
|
|
listOfColorGroup.append(QColor(170, 140, 60));
|
|
listOfColorGroup.append(QColor(200, 200, 200));
|
|
|
|
this->dbLMS = dbLMS;
|
|
|
|
LoadTraineesGroupsPSQL();
|
|
}
|
|
|
|
DataBaseTrainees::~DataBaseTrainees()
|
|
{
|
|
|
|
}
|
|
|
|
void DataBaseTrainees::LoadTraineesGroupsPSQL()
|
|
{
|
|
listOfTrainees.clear();
|
|
listOfGroups.clear();
|
|
|
|
listOfTrainees = dbLMS->selectAllTrainees();
|
|
listOfGroups = dbLMS->selectAllGroups();
|
|
|
|
QApplication::beep();
|
|
}
|
|
|
|
bool DataBaseTrainees::AuthorizationTrainee(QString login, QString password, QString classroom_name, QString computer_name)
|
|
{
|
|
//Обучаемые
|
|
for(int i = 0; i < listOfTrainees.count(); i++)
|
|
{
|
|
if(listOfTrainees[i].getArchived())
|
|
continue;
|
|
|
|
if(listOfTrainees[i].getLogin() == login && listOfTrainees[i].getPassword() == password)
|
|
{
|
|
Trainee trainee = listOfTrainees[i];
|
|
trainee.setLoggedIn(true);
|
|
//trainee.setLearnClass(learnClass);
|
|
//trainee.setComputer(computer);
|
|
|
|
int id = dbLMS->updateTrainee(trainee);
|
|
|
|
if(id)
|
|
{
|
|
LoadTraineesGroupsPSQL();
|
|
return true;
|
|
}
|
|
else
|
|
return false;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool DataBaseTrainees::deAuthorizationTrainee(QString login)
|
|
{
|
|
//Обучаемые
|
|
for(int i = 0; i < listOfTrainees.count(); i++)
|
|
{
|
|
if(listOfTrainees[i].getArchived())
|
|
continue;
|
|
|
|
if(listOfTrainees[i].getLogin() == login)
|
|
{
|
|
Trainee trainee = listOfTrainees[i];
|
|
trainee.setLoggedIn(false);
|
|
//trainee.setLearnClass(QStringLiteral(""));
|
|
//trainee.setComputer(QStringLiteral(""));
|
|
|
|
int id = dbLMS->updateTrainee(trainee);
|
|
|
|
if(id)
|
|
{
|
|
LoadTraineesGroupsPSQL();
|
|
return true;
|
|
}
|
|
else
|
|
return false;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/*
|
|
void DataBaseTrainees::setTasks(QString login, QStringList tasks)
|
|
{
|
|
//Обучаемые
|
|
for(int i = 0; i < listOfTrainees.count(); i++)
|
|
{
|
|
if(listOfTrainees[i].getLogin() == login)
|
|
listOfTrainees[i].setTasks(tasks);
|
|
}
|
|
}*/
|
|
|
|
QStringList DataBaseTrainees::getTasks(QString login)
|
|
{
|
|
QStringList tasksStrList;
|
|
|
|
//Обучаемые
|
|
for(int i = 0; i < listOfTrainees.count(); i++)
|
|
{
|
|
if(listOfTrainees[i].getLogin() == login)
|
|
{
|
|
QList<Task> tasks = listOfTrainees[i].getTasks();
|
|
for(Task task: tasks)
|
|
{
|
|
tasksStrList.append(task.getName());
|
|
}
|
|
}
|
|
}
|
|
|
|
return tasksStrList;
|
|
}
|
|
|
|
QString DataBaseTrainees::getNameTraineeOnComputer(QString computer_name)
|
|
{
|
|
for(Trainee trainee : listOfTrainees)
|
|
{
|
|
if(trainee.getComputer().getName() == computer_name)
|
|
return trainee.getName();
|
|
}
|
|
return QString(QStringLiteral(""));
|
|
}
|
|
|
|
Trainee DataBaseTrainees::getTraineeOnComputer(QString computer_name)
|
|
{
|
|
for(Trainee trainee : listOfTrainees)
|
|
{
|
|
if(trainee.getComputer().getName() == computer_name)
|
|
return trainee;
|
|
}
|
|
return Trainee();
|
|
}
|
|
|
|
QString DataBaseTrainees::getNameTraineeByLogin(QString login)
|
|
{
|
|
for(Trainee trainee : listOfTrainees)
|
|
{
|
|
if(trainee.getLogin() == login)
|
|
return trainee.getName();
|
|
}
|
|
return QString(QStringLiteral(""));
|
|
}
|
|
/*
|
|
QColor DataBaseTrainees::getColorGroupByLogin(QString login)
|
|
{
|
|
QString nameTrainee = getNameTraineeByLogin(login);
|
|
Trainee trainee = getTrainee(nameTrainee);
|
|
QString nameGroup = trainee.getGroup();
|
|
Group group = getGroup(nameGroup);
|
|
return getColorGroup(group.getColor());
|
|
}*/
|
|
|
|
QList<Trainee> DataBaseTrainees::getListTraineesInGroup(int id)
|
|
{
|
|
QList<Trainee> listTrainees;
|
|
|
|
for(Trainee trainee : listOfTrainees)
|
|
{
|
|
if(trainee.getGroup().getID() == id)
|
|
listTrainees.append(trainee);
|
|
}
|
|
|
|
return listTrainees;
|
|
}
|
|
|
|
QList<Group> DataBaseTrainees::getListGroups()
|
|
{
|
|
return listOfGroups;
|
|
}
|
|
/*
|
|
QColor DataBaseTrainees::getColorGroup(Group::ColorGroup numColor)
|
|
{
|
|
if(numColor > listOfColorGroup.count() - 1)
|
|
return listOfColorGroup.at(Group::ColorGroup::colorAther);
|
|
|
|
return listOfColorGroup.at(numColor);
|
|
}*/
|
|
|
|
Trainee DataBaseTrainees::getTrainee(int id)
|
|
{
|
|
//Обучаемые
|
|
for(int i = 0; i < listOfTrainees.count(); i++)
|
|
{
|
|
if(listOfTrainees[i].getID() == id)
|
|
return listOfTrainees[i];
|
|
}
|
|
return Trainee();
|
|
}
|
|
|
|
Group DataBaseTrainees::getGroup(int id)
|
|
{
|
|
//Группы
|
|
for(int i = 0; i < listOfGroups.count(); i++)
|
|
{
|
|
if(listOfGroups[i].getID() == id)
|
|
return listOfGroups[i];
|
|
}
|
|
return Group();
|
|
}
|
|
|
|
int DataBaseTrainees::newGroup()
|
|
{
|
|
Group group;
|
|
group.setName(generateDefaultNameGroup());
|
|
|
|
int id = dbLMS->insertGroup(group);
|
|
|
|
if(id)
|
|
{
|
|
LoadTraineesGroupsPSQL();
|
|
return id;
|
|
}
|
|
else
|
|
return 0;
|
|
}
|
|
|
|
int DataBaseTrainees::deleteGroup(int id)
|
|
{
|
|
//Группы
|
|
for(int i = 0; i < listOfGroups.count(); i++)
|
|
{
|
|
if(listOfGroups[i].getID() == id)
|
|
{
|
|
int id_del = dbLMS->deleteGroup(id);
|
|
|
|
if(id_del)
|
|
{
|
|
LoadTraineesGroupsPSQL();
|
|
return id_del;
|
|
}
|
|
else
|
|
return 0;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int DataBaseTrainees::editGroup(Group group)
|
|
{
|
|
//Группы
|
|
for(int i = 0; i < listOfGroups.count(); i++)
|
|
{
|
|
if(listOfGroups[i].getID() == group.getID())
|
|
{
|
|
if(!checkExistNameGroup(group.getName()) || group.getName() == listOfGroups[i].getName())
|
|
{
|
|
int id = dbLMS->updateGroup(group);
|
|
|
|
if(id)
|
|
{
|
|
LoadTraineesGroupsPSQL();
|
|
return id;
|
|
}
|
|
else
|
|
return 0;
|
|
}
|
|
else
|
|
{
|
|
QMessageBox::critical(nullptr, tr("Editing error!"),
|
|
tr("An existing group name has been entered.\nThe changes will not be accepted."));
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int DataBaseTrainees::newTrainee(int id_group)
|
|
{
|
|
Trainee trainee;
|
|
Group group;
|
|
group.setID(id_group);
|
|
trainee.setGroup(group);
|
|
trainee.setName(generateDefaultNameTrainee());
|
|
trainee.setLogin(generateDefaultLoginTrainee());
|
|
trainee.setPassword(QStringLiteral("<password>"));
|
|
trainee.setArchived(false);
|
|
trainee.setLoggedIn(false);
|
|
|
|
int id = dbLMS->insertTrainee(trainee);
|
|
|
|
if(id)
|
|
{
|
|
LoadTraineesGroupsPSQL();
|
|
return id;
|
|
}
|
|
else
|
|
return 0;
|
|
}
|
|
|
|
int DataBaseTrainees::deleteTrainee(int id)
|
|
{
|
|
//Обучаемые
|
|
for(int i = 0; i < listOfTrainees.count(); i++)
|
|
{
|
|
if(listOfTrainees[i].getID() == id)
|
|
{
|
|
int id_del = dbLMS->deleteTrainee(id);
|
|
|
|
if(id_del)
|
|
{
|
|
LoadTraineesGroupsPSQL();
|
|
return id_del;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int DataBaseTrainees::editTrainee(Trainee trainee)
|
|
{
|
|
//Обучаемые
|
|
for(int i = 0; i < listOfTrainees.count(); i++)
|
|
{
|
|
if(listOfTrainees[i].getID() == trainee.getID())
|
|
{
|
|
if( (!checkExistNameTrainee(trainee.getName()) || trainee.getName() == listOfTrainees[i].getName()) &&
|
|
(!checkExistLoginTrainee(trainee.getLogin()) || trainee.getLogin() == listOfTrainees[i].getLogin()) )
|
|
{
|
|
int id = dbLMS->updateTrainee(trainee);
|
|
|
|
if(id)
|
|
{
|
|
LoadTraineesGroupsPSQL();
|
|
return id;
|
|
}
|
|
else
|
|
return 0;
|
|
}
|
|
else
|
|
{
|
|
QMessageBox::critical(nullptr, tr("Editing error!"),
|
|
tr("An existing trainee's name or login has been entered.\nThe changes will not be accepted."));
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
bool DataBaseTrainees::isArchived(int id)
|
|
{
|
|
//Обучаемые
|
|
for(int i = 0; i < listOfTrainees.count(); i++)
|
|
{
|
|
if(listOfTrainees[i].getID()== id)
|
|
return listOfTrainees[i].getArchived();
|
|
}
|
|
return false;
|
|
}
|
|
|
|
QString DataBaseTrainees::generateDefaultNameGroup()
|
|
{
|
|
int numGroup = 0;
|
|
QString name;
|
|
do
|
|
{
|
|
name = tr("Group") + QStringLiteral(" ") + QString::number(++numGroup);
|
|
}while(checkExistNameGroup(name));
|
|
return name;
|
|
}
|
|
|
|
QString DataBaseTrainees::generateDefaultNameTrainee()
|
|
{
|
|
int numTrainee = 0;
|
|
QString name;
|
|
do
|
|
{
|
|
name = QStringLiteral("<") + tr("Trainee") + QStringLiteral(" ") + QString::number(++numTrainee) + QStringLiteral(">");
|
|
}while(checkExistNameTrainee(name));
|
|
return name;
|
|
}
|
|
|
|
bool DataBaseTrainees::checkExistNameGroup(QString name)
|
|
{
|
|
//Группы
|
|
for(int i = 0; i < listOfGroups.count(); i++)
|
|
{
|
|
if(listOfGroups[i].getName() == name)
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool DataBaseTrainees::checkExistNameTrainee(QString name)
|
|
{
|
|
//Обучаемые
|
|
for(int i = 0; i < listOfTrainees.count(); i++)
|
|
{
|
|
if(listOfTrainees[i].getName() == name)
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
QString DataBaseTrainees::generateDefaultLoginTrainee()
|
|
{
|
|
int numTrainee = 0;
|
|
QString login;
|
|
do
|
|
{
|
|
login = QStringLiteral("<O") + QString::number(++numTrainee) + QStringLiteral(">");
|
|
}while(checkExistLoginTrainee(login));
|
|
return login;
|
|
}
|
|
|
|
bool DataBaseTrainees::checkExistLoginTrainee(QString login)
|
|
{
|
|
//Обучаемые
|
|
for(int i = 0; i < listOfTrainees.count(); i++)
|
|
{
|
|
if(listOfTrainees[i].getLogin() == login)
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
/*
|
|
Group::ColorGroup DataBaseTrainees::generateDefaultColorGroup()
|
|
{
|
|
for(int i = 0; i < Group::ColorGroup::countColor; i++)
|
|
{
|
|
Group::ColorGroup color = (Group::ColorGroup)i;
|
|
if(!checkExistColorGroup(color))
|
|
return color;
|
|
}
|
|
return Group::ColorGroup::colorAther;
|
|
}*/
|
|
/*
|
|
bool DataBaseTrainees::checkExistColorGroup(Group::ColorGroup color)
|
|
{
|
|
//Группы
|
|
for(int i = 0; i < listOfGroups.count(); i++)
|
|
{
|
|
if(listOfGroups[i].getColor() == color)
|
|
return true;
|
|
}
|
|
return false;
|
|
}*/
|