forked from Saint-Francis-Robotics-Team2367/Attendance
/
mainwindow.cpp
173 lines (154 loc) · 6.34 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <stdio.h>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->numberEntry,SIGNAL(returnPressed()),this,SLOT(gotText()));
// connect(ui->actionNewUser,SIGNAL(triggered(bool)),this,SLOT(newUser()));
manager = new FileManager();
this->students = manager->readIds();
this->setWindowTitle("SFRT Attendance");
this->setWindowIcon(QIcon("icon.png"));
//code for testing because i cannot manually add users
students.append(new Student("Nilay Pachauri",2171048));
}
MainWindow::~MainWindow()
{
delete ui;
}
int MainWindow::gotText()
{
QString name = this->findName(ui->numberEntry->text().toInt());
ui->numberEntry->setText("");
if(name==NULL)
{
ui->log->append("Invalid ID: Please try again");
return 0;
}
Student *currStudent = findStudent(name); //gets the student object
name = currStudent->getName(); //really redundant fix soon please
QString time = (QTime::currentTime().toString());
//int date = (const int QDate::month());
QString date = QDate::currentDate().toString();
if (!currStudent->isSignedIn())
{ //if the user is not signed in
currStudent->setSignedIn(true); //sign him in
ui->log->append("Signed in: " + name);
currStudent->getLastSignIn()->start(); //and start the timer for how long he is there
QFile file("data.csv");
QFileInfo fileInfo("data.csv");
//ui->log->append(fileInfo.absoluteFilePath());
if (file.open(QFile::WriteOnly|QFile::Append))
{
QTextStream stream(&file);
stream << name << "," << time <<"," << "Sign In," << date << "\r\n"; // this writes first line with two columns
file.close();
} else {
ui->log->append("This shit don't work");
}
}
else if (currStudent->isSignedIn()) { //if the user is signed in
currStudent->setSignedIn(false); //sign him out
int elapsed = currStudent->getLastSignIn()->elapsed(); //magically get the numbers for how long he has been there
int seconds = (int) (elapsed / 1000) % 60 ;
int minutes = (int) ((elapsed / (1000*60)) % 60);
int hours = (int) ((elapsed / (1000*60*60)) % 24);
ui->log->append("Signed out: " + name + " || Duration: " + QString::number(hours)+ ":" + QString::number(minutes) + ":" + QString::number(seconds)); //display it to him
currStudent->getLastSignIn()->restart(); //and restart the timer?
QFile file("data.csv");
if (file.open(QFile::WriteOnly|QFile::Append))
{
QTextStream stream(&file);
stream << name << "," << time <<"," << "Sign Out," << date << "\r\n"; // this writes first line with two columns
file.close();
}
//if(currStudent->getLastSignIn()->elapsed()==0 || (currStudent->getCorrectSignIn() && currStudent->getLastSignIn()->elapsed()>57600000))
// {
// currStudent->setCorrectSignOut(false);
// ui->log->append("Signed in: " + name);
// currStudent->getLastSignIn()->start();
// }
// else if(!currStudent->getCorrectSignIn() && currStudent->getLastSignIn()->elapsed()>57600000)//57600000 //16 hours in millesonds
// {
// currStudent->setCorrectSignOut(false);
// ui->log->append("You did not sign-out last time, resigning-in");
// ui->log->append("Signed in: " + name);
// currStudent->getLastSignIn()->restart();
// }
// else if(currStudent->getLastSignIn()->elapsed()<5400000)//5400000 //1 hour 30 minutes in seconds
// {
// int elapsed = currStudent->getLastSignIn()->elapsed();
// int seconds = (int) (elapsed / 1000) % 60 ;
// int minutes = (int) ((elapsed / (1000*60)) % 60);
// int hours = (int) ((elapsed / (1000*60*60)) % 24);
// ui->log->append("Signed out: " + name + " || Duration: " + QString::number(hours)+ ":" + QString::number(minutes) + ":" + QString::number(seconds));
// currStudent->getLastSignIn()->restart();
// }
// else if(currStudent->getLastSignIn()->elapsed()>5400000)
// {
// currStudent->setCorrectSignOut(true);
// int elapsed = currStudent->getLastSignIn()->elapsed();
// int seconds = (int) (elapsed / 1000) % 60 ;
// int minutes = (int) ((elapsed / (1000*60)) % 60);
// int hours = (int) ((elapsed / (1000*60*60)) % 24);
// ui->log->append("Signed out: " + name + " || Duration: " + QString::number(hours)+ ":" + QString::number(minutes) + ":" + QString::number(seconds));
// currStudent->getLastSignIn()->restart();
// this->manager->changeStatus(name);
// }
}
return 1;
}
QString MainWindow::findName(int id)
{
for(int i = 0;i<this->students.size();i++)
{
if(this->students.at(i)->getId()==id)
{
return this->students.at(i)->getName();
}
}
return NULL;
}
int MainWindow::findId(QString &name)
{
for(int i = 0;i<this->students.size();i++)
{
if(this->students.at(i)->getName()==name)
{
return this->students.at(i)->getId();
}
}
return -1;
}
Student *MainWindow::findStudent(int id)
{
for(int i = 0;i<this->students.size();i++)
{
if(this->students.at(i)->getId()==id)
{
return (this->students.at(i));
}
}
return (Student *)NULL;
}
Student *MainWindow::findStudent(QString &name) //looks through array to find the student and returns the Student object
{
for(int i = 0;i<this->students.size();i++)
{
if(this->students.at(i)->getName()==name)
{
return (this->students.at(i));
}
}
return (Student *)NULL;
}
void MainWindow::newUser()
{
QString name = QInputDialog::getText(this, tr("What is your name"),tr("Name:"), QLineEdit::Normal,"John Doe");
QString id = QInputDialog::getText(this, tr("What is your id #"),tr("Id #:"), QLineEdit::Normal,"1234");
this->manager->addUser(name,id);
this->students.append(new Student(name,id.toInt()));
}