mirror of
https://gitea.msk.dinamika-avia.ru/Constanta-Design/RRJServer.git
synced 2026-03-28 19:55:48 +03:00
PSQL 30.10.2024
This commit is contained in:
587
DBXML/InstructorsAndTrainees/trainees/databasetrainees.cpp
Normal file
587
DBXML/InstructorsAndTrainees/trainees/databasetrainees.cpp
Normal file
@@ -0,0 +1,587 @@
|
||||
#include "databasetrainees.h"
|
||||
#include <QDomDocument>
|
||||
#include <QFile>
|
||||
#include <QDebug>
|
||||
|
||||
DataBaseTrainees::DataBaseTrainees(QString appPath, 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));
|
||||
|
||||
appDirPath = appPath;
|
||||
|
||||
this->dbLMS = dbLMS;
|
||||
|
||||
if(this->dbLMS != nullptr)
|
||||
LoadTraineesGroupsPSQL();
|
||||
else
|
||||
LoadTraineesGroupsXML();
|
||||
}
|
||||
|
||||
DataBaseTrainees::~DataBaseTrainees()
|
||||
{
|
||||
if(dbLMS == nullptr)
|
||||
SaveTraineesGroupsXML();
|
||||
}
|
||||
|
||||
void DataBaseTrainees::LoadTraineesGroupsXML()
|
||||
{
|
||||
QDomDocument groupsTraineesDOM;
|
||||
QString xmlFileName = appDirPath + QStringLiteral("/DBXML/groupsTrainees.xml");
|
||||
QFile xmlInFile(xmlFileName);
|
||||
if (!xmlInFile.open(QFile::ReadOnly | QFile::Text))
|
||||
{
|
||||
qDebug() << QStringLiteral("LoadTraineesGroupsXML: Не удалось открыть файл ") + xmlFileName;
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
listOfTrainees.clear();
|
||||
listOfGroups.clear();
|
||||
|
||||
groupsTraineesDOM.setContent(xmlInFile.readAll());
|
||||
xmlInFile.close();
|
||||
|
||||
QDomNode groupsTraineesNode = groupsTraineesDOM.namedItem(QStringLiteral("groupsTrainees"));
|
||||
|
||||
for(int i = 0; i < groupsTraineesNode.childNodes().count(); i++)
|
||||
{
|
||||
QDomNode groupNode = groupsTraineesNode.childNodes().at(i);
|
||||
if(groupNode.nodeName().toLower() == QStringLiteral("group"))
|
||||
{//Группа
|
||||
|
||||
Group group;
|
||||
group.setName(groupNode.toElement().attribute(QStringLiteral("name")));
|
||||
group.setColor((Group::ColorGroup)(groupNode.toElement().attribute(QStringLiteral("color")).toInt()));
|
||||
listOfGroups.append(group);
|
||||
|
||||
for(int j = 0; j < groupNode.childNodes().count(); j++)
|
||||
{
|
||||
QDomNode traineeNode = groupNode.childNodes().at(j);
|
||||
if(traineeNode.nodeName().toLower() == QStringLiteral("trainee"))
|
||||
{//Обучаемый
|
||||
|
||||
Trainee trainee;
|
||||
|
||||
trainee.setGroup(group.getName());
|
||||
|
||||
trainee.setName(traineeNode.toElement().attribute(QStringLiteral("name")));
|
||||
trainee.setLogin(traineeNode.toElement().attribute(QStringLiteral("login")));
|
||||
trainee.setPassword(traineeNode.toElement().attribute(QStringLiteral("password")));
|
||||
trainee.setArchived(traineeNode.toElement().attribute(QStringLiteral("archived")) == QStringLiteral("да") ? true : false);
|
||||
trainee.setWhatItDoes(traineeNode.toElement().attribute(QStringLiteral("whatItDoes")));
|
||||
|
||||
trainee.setLoggedIn(false);
|
||||
trainee.setLearnClass(QStringLiteral(""));
|
||||
trainee.setComputer(QStringLiteral(""));
|
||||
|
||||
listOfTrainees.append(trainee);
|
||||
}
|
||||
}
|
||||
}
|
||||
}//for(int i = 0; i < groupsTraineesNode.childNodes().count(); i++)
|
||||
}//else
|
||||
}
|
||||
|
||||
void DataBaseTrainees::SaveTraineesGroupsXML()
|
||||
{
|
||||
QDomDocument groupsTraineesDOM;
|
||||
QFile blankFile(QStringLiteral(":/blankXML/groupsTrainees.xml"));
|
||||
|
||||
if (! blankFile.open(QFile::ReadOnly | QFile::Text)) {
|
||||
qDebug() << "SaveTraineesGroupsXML: Не удалось считать файл :/blankXML/groupsTrainees.xml";
|
||||
return;
|
||||
}
|
||||
|
||||
groupsTraineesDOM.setContent(blankFile.readAll());
|
||||
blankFile.close();
|
||||
|
||||
QDomNode groupsTraineesNode = groupsTraineesDOM.namedItem(QStringLiteral("groupsTrainees"));
|
||||
|
||||
for(Group group : listOfGroups)
|
||||
{
|
||||
//Группа
|
||||
QDomNode groupNode = groupsTraineesDOM.createElement(QStringLiteral("group"));
|
||||
groupsTraineesNode.appendChild(groupNode);
|
||||
groupNode.toElement().setAttribute(QStringLiteral("name"), group.getName());
|
||||
groupNode.toElement().setAttribute(QStringLiteral("color"), QString::number((int)group.getColor()));
|
||||
|
||||
//Обучаемые
|
||||
QList<Trainee> listTrainees;
|
||||
listTrainees = getListTraineesInGroup(group.getName());
|
||||
for(Trainee trainee : listTrainees)
|
||||
{
|
||||
QDomNode traineeNode = groupsTraineesDOM.createElement(QStringLiteral("trainee"));
|
||||
groupNode.appendChild(traineeNode);
|
||||
traineeNode.toElement().setAttribute(QStringLiteral("name"), trainee.getName());
|
||||
traineeNode.toElement().setAttribute(QStringLiteral("login"), trainee.getLogin());
|
||||
traineeNode.toElement().setAttribute(QStringLiteral("password"), trainee.getPassword());
|
||||
traineeNode.toElement().setAttribute(QStringLiteral("archived"), trainee.getArchived() ? QStringLiteral("да") : QStringLiteral("нет"));
|
||||
traineeNode.toElement().setAttribute(QStringLiteral("whatItDoes"), trainee.getWhatItDoes());
|
||||
}
|
||||
}
|
||||
|
||||
QString xmlFileName = appDirPath + QStringLiteral("/DBXML/groupsTrainees.xml");
|
||||
QFile xmlOutFile(xmlFileName);
|
||||
if (!xmlOutFile.open(QFile::WriteOnly | QFile::Text))
|
||||
{
|
||||
qDebug() << QStringLiteral("SaveTraineesGroupsXML: Не удалось записать файл ") + xmlFileName;
|
||||
return;
|
||||
}
|
||||
QTextStream outFile(&xmlOutFile);
|
||||
groupsTraineesDOM.save(outFile, 4);
|
||||
xmlOutFile.close();
|
||||
}
|
||||
|
||||
void DataBaseTrainees::LoadTraineesGroupsPSQL()
|
||||
{
|
||||
listOfTrainees.clear();
|
||||
listOfGroups.clear();
|
||||
|
||||
listOfTrainees = dbLMS->selectAllTrainees();
|
||||
listOfGroups = dbLMS->selectAllGroups();
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
listOfTrainees[i].setLoggedIn(true);
|
||||
listOfTrainees[i].setLearnClass(learnClass);
|
||||
listOfTrainees[i].setComputer(computer);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
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)
|
||||
{
|
||||
listOfTrainees[i].setLoggedIn(false);
|
||||
listOfTrainees[i].setLearnClass(QStringLiteral(""));
|
||||
listOfTrainees[i].setComputer(QStringLiteral(""));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void DataBaseTrainees::setWhatItDoes(QString login, QString whatItDoes)
|
||||
{
|
||||
//Обучаемые
|
||||
for(int i = 0; i < listOfTrainees.count(); i++)
|
||||
{
|
||||
if(listOfTrainees[i].getLogin() == login)
|
||||
listOfTrainees[i].setWhatItDoes(whatItDoes);
|
||||
}
|
||||
}
|
||||
|
||||
QStringList DataBaseTrainees::getWhatItDoes(QString login)
|
||||
{
|
||||
QString whatItDoes = QStringLiteral("");
|
||||
|
||||
//Обучаемые
|
||||
for(int i = 0; i < listOfTrainees.count(); i++)
|
||||
{
|
||||
if(listOfTrainees[i].getLogin() == login)
|
||||
whatItDoes = listOfTrainees[i].getWhatItDoes();
|
||||
}
|
||||
|
||||
return whatItDoes.split(QStringLiteral(";"));
|
||||
}
|
||||
|
||||
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);
|
||||
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)
|
||||
listOfGroups.removeAt(i);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DataBaseTrainees::editGroup(QString name, QString newName)
|
||||
{
|
||||
//Группы
|
||||
for(int i = 0; i < listOfGroups.count(); i++)
|
||||
{
|
||||
if(listOfGroups[i].getName() == name)
|
||||
{
|
||||
if(!checkExistNameGroup(newName) || newName == name)
|
||||
{
|
||||
Group group = listOfGroups[i];
|
||||
group.setName(newName);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
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)
|
||||
listOfTrainees.removeAt(i);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DataBaseTrainees::toArchiveTrainee(QString name)
|
||||
{
|
||||
//Обучаемые
|
||||
for(int i = 0; i < listOfTrainees.count(); i++)
|
||||
{
|
||||
if(listOfTrainees[i].getName() == name)
|
||||
if(! listOfTrainees[i].getArchived())
|
||||
{
|
||||
Trainee trainee = listOfTrainees[i];
|
||||
trainee.setArchived(true);
|
||||
|
||||
bool result = dbLMS->updateTrainee(trainee);
|
||||
|
||||
if(result)
|
||||
listOfTrainees[i].setArchived(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DataBaseTrainees::fromeArchiveTrainee(QString name)
|
||||
{
|
||||
//Обучаемые
|
||||
for(int i = 0; i < listOfTrainees.count(); i++)
|
||||
{
|
||||
if(listOfTrainees[i].getName() == name)
|
||||
if(listOfTrainees[i].getArchived())
|
||||
{
|
||||
Trainee trainee = listOfTrainees[i];
|
||||
trainee.setArchived(false);
|
||||
|
||||
bool result = dbLMS->updateTrainee(trainee);
|
||||
|
||||
if(result)
|
||||
listOfTrainees[i].setArchived(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user