Files
RRJServer/DB_IaT/InstructorsAndTrainees/trainees/databasetrainees.cpp
2024-11-02 13:43:57 +03:00

464 lines
11 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "databasetrainees.h"
#include <QApplication>
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 learnClass, QString computer)
{
//Обучаемые
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);
bool result = dbLMS->updateTrainee(trainee);
if(result)
{
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(""));
bool result = dbLMS->updateTrainee(trainee);
if(result)
{
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 tasks;
//Обучаемые
for(int i = 0; i < listOfTrainees.count(); i++)
{
if(listOfTrainees[i].getLogin() == login)
{
tasks = listOfTrainees[i].getTasks();
}
}
return tasks;
}
QString DataBaseTrainees::getNameTraineeOnComputer(QString computer)
{
for(Trainee trainee : listOfTrainees)
{
if(trainee.getComputer() == computer)
return trainee.getName();
}
return QString(QStringLiteral(""));
}
Trainee DataBaseTrainees::getTraineeOnComputer(QString computer)
{
for(Trainee trainee : listOfTrainees)
{
if(trainee.getComputer() == computer)
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(QString nameGroup)
{
QList<Trainee> listTrainees;
for(Trainee trainee : listOfTrainees)
{
if(trainee.getGroup() == nameGroup)
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(QString name)
{
//Обучаемые
for(int i = 0; i < listOfTrainees.count(); i++)
{
if(listOfTrainees[i].getName() == name)
return listOfTrainees[i];
}
return Trainee();
}
Group DataBaseTrainees::getGroup(QString nameGroup)
{
//Группы
for(int i = 0; i < listOfGroups.count(); i++)
{
if(listOfGroups[i].getName() == nameGroup)
return listOfGroups[i];
}
return Group();
}
QString DataBaseTrainees::newGroup()
{
Group group;
group.setName(generateDefaultNameGroup());
group.setColor(generateDefaultColorGroup());
bool result = dbLMS->insertGroup(group);
if(result)
{
//listOfGroups.append(group);
LoadTraineesGroupsPSQL();
return group.getName();
}
else
return QStringLiteral("");
}
bool DataBaseTrainees::deleteGroup(QString name)
{
if(getListTraineesInGroup(name).count() > 0)
{//Группа не пуста
return false;
}
//Группы
for(int i = 0; i < listOfGroups.count(); i++)
{
if(listOfGroups[i].getName() == name)
{
int id = listOfGroups[i].getID();
bool result = dbLMS->deleteGroup(id);
if(result)
LoadTraineesGroupsPSQL();
//listOfGroups.removeAt(i);
break;
}
}
return true;
}
bool DataBaseTrainees::editGroup(QString name, Group group)
{
//Группы
for(int i = 0; i < listOfGroups.count(); i++)
{
if(listOfGroups[i].getName() == name)
{
if(!checkExistNameGroup(group.getName()) || group.getName() == name)
{
//group.setID(listOfGroups[i].getID());
bool result = dbLMS->updateGroup(group);
if(result)
{
/*
listOfGroups[i].setName(newName);
//Меняем имя группы у всех Обучаемых этой группы
//Обучаемые
for(int i = 0; i < listOfTrainees.count(); i++)
{
if(listOfTrainees[i].getGroup() == name)
listOfTrainees[i].setGroup(newName);
}
*/
LoadTraineesGroupsPSQL();
return true;
}
else
return false;
}
}
}
return false;
}
QString DataBaseTrainees::newTrainee(QString nameGroup)
{
Trainee trainee;
trainee.setGroup(nameGroup);
trainee.setName(generateDefaultNameTrainee());
trainee.setLogin(generateDefaultLoginTrainee());
trainee.setPassword(QStringLiteral("<password>"));
trainee.setLearnClass(QStringLiteral(""));
trainee.setComputer(QStringLiteral(""));
trainee.setArchived(false);
trainee.setLoggedIn(false);
//trainee.setWhatItDoes(QStringLiteral(""));
bool result = dbLMS->insertTrainee(trainee);
if(result)
{
//listOfTrainees.append(trainee);
LoadTraineesGroupsPSQL();
return trainee.getName();
}
else
return QStringLiteral("");
}
void DataBaseTrainees::deleteTrainee(QString name)
{
//Обучаемые
for(int i = 0; i < listOfTrainees.count(); i++)
{
if(listOfTrainees[i].getName() == name)
{
int id = listOfTrainees[i].getID();
bool result = dbLMS->deleteTrainee(id);
if(result)
LoadTraineesGroupsPSQL();
//listOfTrainees.removeAt(i);
return;
}
}
}
bool DataBaseTrainees::editTrainee(QString name, Trainee trainee)
{
//Обучаемые
for(int i = 0; i < listOfTrainees.count(); i++)
{
if(listOfTrainees[i].getName() == name)
{
if( (!checkExistNameTrainee(trainee.getName()) || trainee.getName() == name) &&
(!checkExistLoginTrainee(trainee.getLogin()) || trainee.getLogin() == listOfTrainees[i].getLogin()) )
{
//trainee.setID(listOfTrainees[i].getID());
bool result = dbLMS->updateTrainee(trainee);
if(result)
{
//listOfTrainees.replace(i, trainee);
LoadTraineesGroupsPSQL();
return true;
}
else
return false;
}
}
}
return false;
}
bool DataBaseTrainees::isArchived(QString name)
{
//Обучаемые
for(int i = 0; i < listOfTrainees.count(); i++)
{
if(listOfTrainees[i].getName() == name)
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;
}