-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrmWeekClassSchedule.cs
207 lines (170 loc) · 7.63 KB
/
FrmWeekClassSchedule.cs
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Data.Entity;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeekClassSchedule.AppData;
using WeekClassSchedule.AppDatalayer;
namespace WeekClassSchedule
{
public partial class FrmWeekClassSchedule : Form
{
private ProfessorDatalayer _professorDatalayer;
private ClassroomDatalayer _classroomDatalayer;
private ScheduleDatalayer _scheduleDatalayer;
private List<Professor> _professorsList;
private bool _scheduleIsModified = false;
private int _scheduleCellOldValue = 0;
private int _scheduleCellNewValue = 0;
public FrmWeekClassSchedule()
{
InitializeComponent();
_professorDatalayer = new ProfessorDatalayer();
_classroomDatalayer = new ClassroomDatalayer();
_scheduleDatalayer = new ScheduleDatalayer();
}
private void FrmWeekClassSchedule_Load(object sender, EventArgs e)
{
dgProfessors.AutoGenerateColumns = false;
_professorsList = _professorDatalayer.GetProfessorList();
dgProfessors.DataSource = _professorsList;
dgClassSchedule.AutoGenerateColumns = false;
if (!_professorsList.Any(p => p.Id == 0))
_professorsList.Add(new Professor() { Id = 0, Name = "Vaga" });
lstClassrooms.DataSource = _classroomDatalayer.ClassroomList;
this.Height = this.MdiParent.Height;
this.Width = this.MdiParent.Width;
}
private void dgProfessors_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
var cell = dgProfessors[0, e.RowIndex];
var professorId = Convert.ToInt32(cell.Value);
if (professorId > 0)
{
var frmProfessor = new FrmProfessor(professorId);
frmProfessor.MdiParent = this.MdiParent;
frmProfessor.Show();
this.Close();
}
}
private void dgSala1_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
Debug.WriteLine(e.Exception.Message);
}
private void dgClassSchedule_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
for (int i = 0; i < dgClassSchedule.Rows.Count; i++)
{
var classNumber = i + 1;
var weekDay = 0;
foreach (DataGridViewComboBoxCell cell in dgClassSchedule.Rows[i].Cells.OfType<DataGridViewComboBoxCell>())
{
weekDay++;
var filteredProfessors = _professorDatalayer.GetForScheduling((DayOfWeek)weekDay, classNumber, Convert.ToInt32(lstClassrooms.SelectedValue));
if (!filteredProfessors.Any(p => p.Id == 0))
filteredProfessors.Add(new Professor() { Id = 0, Name = "Vaga" });
var currentValue = Convert.ToInt32(cell.Value);
if (!filteredProfessors.Any(p => p.Id == currentValue))
{
var currentProfessor = _professorsList.Where(p => p.Id == currentValue).Single();
filteredProfessors.Add(currentProfessor);
}
cell.DataSource = filteredProfessors;
cell.DisplayMember = "Name";
cell.ValueType = typeof(int);
cell.ValueMember = "Id";
}
}
}
private void lstClassrooms_DoubleClick(object sender, EventArgs e)
{
lblLoadingSchedule.Visible = true;
loading.Visible = true;
dgClassSchedule.DataSource = null;
dgClassSchedule.DataSource = _scheduleDatalayer.ScheduleViewByClass(Convert.ToInt32(lstClassrooms.SelectedValue));
loading.Visible = false;
lblLoadingSchedule.Visible = false;
this.btnSaveSchedule.BackColor = System.Drawing.SystemColors.GradientActiveCaption;
}
private async void btnSaveSchedule_Click(object sender, EventArgs e)
{
loading.Visible = true;
if (dgClassSchedule.DataSource == null)
return;
var scheduleView = (List<vWeeklyScheduleByClass>)dgClassSchedule.DataSource;
await _scheduleDatalayer.SaveWeekSchedulesAsync(scheduleView);
await _professorDatalayer.SaveChangesAsync();
loading.Visible = false;
lblScheduleSaved.Visible = true;
this.btnSaveSchedule.BackColor = System.Drawing.SystemColors.GradientActiveCaption;
}
private void FrmWeekClassSchedule_Click(object sender, EventArgs e)
{
lblScheduleSaved.Visible = false;
}
private void dgClassSchedule_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
if (e.RowIndex > -1)
{
var value = (int)dgClassSchedule[e.ColumnIndex, e.RowIndex].Value;
this._scheduleCellOldValue = value;
if (value > 0)
{
var professor = _professorsList.Where(p => p.Id == value).FirstOrDefault();
//return the class to the professor
professor.NumberOfRemainingClasses++;
dgProfessors.DataSource = null;
dgProfessors.DataSource = _professorsList;
}
}
}
private void dgClassSchedule_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex > -1)
{
var value = (int)dgClassSchedule[e.ColumnIndex, e.RowIndex].Value;
this._scheduleCellNewValue = value;
if (value > 0)
{
var professor = _professorsList.Where(p => p.Id == value).FirstOrDefault();
if (professor.NumberOfRemainingClasses > 0)
{
//subtract the class from the professor
professor.NumberOfRemainingClasses--;
dgProfessors.DataSource = null;
dgProfessors.DataSource = _professorsList;
}
else
{
MessageBox.Show(string.Format("O professor {0} não possui mais aulas disponíveis.", dgClassSchedule[e.ColumnIndex, e.RowIndex].FormattedValue),
"Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
dgClassSchedule[e.ColumnIndex, e.RowIndex].Value = this._scheduleCellOldValue;
}
}
this._scheduleIsModified = (this._scheduleCellOldValue != this._scheduleCellNewValue);
if(this._scheduleIsModified) this.btnSaveSchedule.BackColor = System.Drawing.Color.DarkSalmon;
}
}
private void lstClassrooms_Click(object sender, EventArgs e)
{
lblScheduleSaved.Visible = false;
}
private void dgClassSchedule_Click(object sender, EventArgs e)
{
lblScheduleSaved.Visible = false;
}
private void dgProfessors_Click(object sender, EventArgs e)
{
lblScheduleSaved.Visible = false;
}
private void btnRefresh_Click(object sender, EventArgs e)
{
var frmWeekClassSchedule = new FrmWeekClassSchedule();
frmWeekClassSchedule.MdiParent = this.MdiParent;
frmWeekClassSchedule.Show();
this.Close();
}
}
}