-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
337 lines (298 loc) · 12.5 KB
/
Form1.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
namespace GAM
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
comboAddMember.SelectedIndex = 0;
disableAll();
}
private void btnEnter_Click(object sender, EventArgs e)
{
//Checking that their entry is in the valid format of 000-0000
if (DFECodex(txtDfE.Text) == true)
{
//Reset labels and buttons
disableAll();
return;
}
//Defining the project folder filepath and saving it to a global variable
var docPath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
Program.dirName = $@"{docPath}\"+txtDfE.Text;
//Checking if the defined project folder exists
if (Directory.Exists(Program.dirName))
{
//Enabling all button and labels
disableAll();
btnPrint.Enabled = true;
lblCurrent.Text = "Currently looking at: " + txtDfE.Text;
}
else
{
//Reset labels and buttons
disableAll();
MessageBox.Show("No existing GAM project with the DfE-Code of " + txtDfE.Text, "Invalid GAM Project");
return;
}
}
private void btnPrint_Click(object sender, EventArgs e)
{
//Changing Print Label status
lblFileStatus.Text = "File Status: Loading File...";
lblFileStatus.Refresh();
//Running the Print command
CMD("print");
}
private void btnSetAllActive_Click(object sender, EventArgs e)
{
//Changing Label status
lblActiveStatus.Text = "Status: Processing...";
lblActiveStatus.Refresh();
//Running the Active command
CMD("active");
}
private void btnAddMember_Click(object sender, EventArgs e)
{
//Changing Label status
lblAddStatus.Text = "Status: Processing...";
lblAddStatus.Refresh();
//Running the Add command
CMD("add");
}
private void comboClassList_SelectedIndexChanged(object sender, EventArgs e)
{
//Getting the index number of the selected item
ComboBox cmb = (ComboBox)sender;
int selectedIndex = cmb.SelectedIndex;
//Displaying the class ID and state
lblID.Text = "ID: " + Program.classID[selectedIndex];
lblState.Text = "State: " + Program.classState[selectedIndex];
Program.selectedClassroom = Program.classID[selectedIndex];
}
private void CMD(String cmd) //All the command functions
{
//Starting the Process
Program.startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
Program.startInfo.WorkingDirectory = Program.dirName;
Program.startInfo.FileName = "cmd.exe";
//Print command
if (cmd == "print")
{
try
{
//Checking to see if file already exist, if so delete it
if (File.Exists(Program.dirName + "/class.csv"))
{
File.Delete(Program.dirName + "/class.csv");
}
}
//Catching excel file is open error
catch (System.IO.IOException)
{
MessageBox.Show("Please close the excel file", "Cannot Access Excel File");
//File status reset
lblFileStatus.Text = "File Status:";
//Provisioned to Active reset
lblActiveStatus.Text = "Status:";
//AddMember reset
lblAddStatus.Text = "Status:";
return;
}
//Run the command in command prompt
Program.startInfo.Arguments = "/C gam print courses > class.csv";
Program.process.StartInfo = Program.startInfo;
Program.process.Start();
//Defining filename
var file = new FileInfo(Program.dirName + "/class.csv");
//While File is not accessable because of writing process
while (IsFileLocked(file)) { }
//Wait for process to finish, close the process and let the user know
Program.process.WaitForExit();
Console.WriteLine(Program.process.ExitCode);
Program.process.Close();
lblFileStatus.Text = "File Status: File is ready!";
//Reading through .csv file to export classroom ID and name
//Defining file name
string[] CSV_Output = File.ReadAllLines(Program.dirName + "/class.csv");
//Clear the global list and combobox
Program.classID.Clear();
Program.className.Clear();
Program.classState.Clear();
Program.provisionedList.Clear();
comboClassroomList.Items.Clear();
//Loop through each row and export only the class ID and name, storing it in a global list.
foreach (string line in CSV_Output)
{
//Ignore all invalid entries e.g. GAM update warning
if (line.Split(',')[0].All(char.IsDigit) && !(string.IsNullOrEmpty(line.Split(',')[0])))
{
Program.classID.Add(line.Split(',')[0]);
Program.className.Add(line.Split(',')[1]);
Program.classState.Add(line.Split(',')[5]);
//Getting the school's Google Domain
Program.schoolDomain = line.Split(',')[4].Split('@')[1];
}
}
lblSchoolDomain.Text = "@" + Program.schoolDomain;
//Defining the combobox text and value
comboClassroomList.DisplayMember = "Text";
comboClassroomList.ValueMember = "Value";
//Getting the number of classes
int size = Program.classID.Count;
//Looping through and putting all the classnames into the combobox
for (int i = 0; i < size; i++)
{
comboClassroomList.Items.Add(new { Text = Program.className[i], Value = Program.className[i] });
if (Program.classState[i] == "PROVISIONED")
{
Program.provisionedList.Add(Program.classID[i]);
}
}
//Setting initial value in comboClassroomList
comboClassroomList.SelectedIndex = 0;
lblID.Text = "ID: " + Program.classID[0];
Console.WriteLine(Program.classID[0]);
lblID.Refresh();
lblState.Text = "State: " + Program.classState[0];
lblState.Refresh();
//Enabling buttons
comboClassroomList.Enabled = true;
btnAddMember.Enabled = true;
comboAddMember.Enabled = true;
btnSetAllActive.Enabled = true;
//Provisioned to Active reset
lblActiveStatus.Text = "Status:";
//AddMember reset
lblAddStatus.Text = "Status:";
}
//Active command
if (cmd == "active")
{
int size = Program.provisionedList.Count;
if (size != 0)
{
for (int i = 0; i < size; i++)
{
//Run the command in command prompt
if (i == 0)
{
Program.allProvisionedClasses = "/C gam update course " + Program.provisionedList[i] + " state archived";
}
else
{
Program.allProvisionedClasses += " & gam update course " + Program.provisionedList[i] + " state archived";
}
}
//Starting the Process and add the arguement
Program.startInfo.Arguments = Program.allProvisionedClasses;
Program.process.StartInfo = Program.startInfo;
Program.process.Start();
//Wait for process to finish, close the process and let the user know
Program.process.WaitForExit();
Program.process.Close();
lblActiveStatus.Text = "Status: All done!";
}
else
{
lblActiveStatus.Text = "Status: No Provisioned Classes";
}
}
if (cmd == "add")
{
if (String.IsNullOrEmpty(txtAddMember.Text))
{
MessageBox.Show("Please enter a valid email address", "Invalid Email Address");
lblAddStatus.Text = "Status:";
return;
}
else
{
//Starting the Process and add the arguement
Program.startInfo.Arguments = "/C gam course " + Program.selectedClassroom + " add " + comboAddMember.SelectedItem.ToString() + " " + txtAddMember.Text + "@" + Program.schoolDomain;
Program.process.StartInfo = Program.startInfo;
Program.process.Start();
//Wait for process to finish
Program.process.WaitForExit();
//Error 403, invalid email address
if (Program.process.ExitCode == 403)
{
MessageBox.Show("There is no user with this email address", "Invalid Email Address");
lblAddStatus.Text = "Status:";
return;
}
//Error 409, user already exist
if (Program.process.ExitCode == 409)
{
MessageBox.Show("This user already exist in this classroom", "User Already Exists");
lblAddStatus.Text = "Status:";
return;
}
//Close the process and let the user know
Program.process.Close();
//Changing Label status
lblAddStatus.Text = "Status: User added!";
}
}
}
private bool DFECodex(string s) //Function to check regex for DfE format
{
if (!Regex.IsMatch(s, "^[0-9]{3}-[0-9]{4}$"))
{
MessageBox.Show(this, "DFE code should be in 000-0000 format", "DFE Code Error");
return true;
}
else
{
return false;
}
}
protected virtual bool IsFileLocked(FileInfo file) //Function to check if file is still locked
{
FileStream stream = null;
try
{
stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
}
catch (IOException)
{
return true;
}
finally
{
if (stream != null)
stream.Close();
}
//file is not locked
return false;
}
private void disableAll()
{
//Reset labels and buttons
btnPrint.Enabled = false;
btnSetAllActive.Enabled = false;
comboClassroomList.Enabled = false;
btnAddMember.Enabled = false;
lblCurrent.Text = "Currently looking at:";
lblFileStatus.Text = "File Status:";
lblID.Text = "ID:";
lblState.Text = "State:";
lblActiveStatus.Text = "Status:";
lblSchoolDomain.Text = "@";
lblAddStatus.Text = "Status:";
comboAddMember.Enabled = false;
comboClassroomList.Items.Clear();
}
}
}