-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
513 lines (401 loc) · 16.2 KB
/
MainWindow.xaml.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Newtonsoft.Json;
using static homerworker.MainWindow;
using Path = System.IO.Path;
namespace homerworker
{
/// <summary>
/// MainWindow.xaml 的互動邏輯
/// </summary>
public partial class MainWindow : Window
{
List<string> hw = new List<string>();
List<string> student = new List<string>();
List<string> hwDocument = new List<string>();
List<Student_Metadata> StudentMetadata = new List<Student_Metadata>();
public MainWindow()
{
InitializeComponent();
HwListParse();
// MetadataWrite();
MetadataLoader();
}
private void CheckPath()
{
}
private void HwListParse()
{
string _path = homerworker.Properties.Settings.Default.directoryPath;
hwSlection_lb.SelectionMode = System.Windows.Controls.SelectionMode.Single;
string[] hwnames= Directory.GetDirectories(_path);
if (hwnames.Length!=0)
{
foreach (string item in hwnames)
{
hw.Add(System.IO.Path.GetFileName(item));
hwSlection_lb.Items.Add(System.IO.Path.GetFileName(item));
}
}
}
private Student_Metadata MetadataSeek(string StudentID,string HomeworkName)
{
if (StudentMetadata == null)
{
StudentMetadata = new List<Student_Metadata>();
}
bool foundStudent = false;
foreach (Student_Metadata singleStudent in StudentMetadata)
{
if (singleStudent.Student == StudentID)
{
//student found!
foundStudent = !foundStudent;
return singleStudent;
}
}
return null;
}
private void MetadataLoader()
{
//string raw= "[{\"Student\":\"109423069\",\"Hw_Metadatas\":[{\"Title\":\"1-2\",\"Grade\":7878},{\"Title\":\"1-3\",\"Grade\":7878},{\"Title\":\"1-1\",\"Grade\":7878}]},{\"Student\":\"109423070\",\"Hw_Metadatas\":[{\"Title\":\"1-1\",\"Grade\":7878},{\"Title\":\"1-2\",\"Grade\":7878},{\"Title\":\"1-3\",\"Grade\":7878}]}]";
//Load metadata from outside source into StudentMetadata list.
string raw = "";
string metadataPath = homerworker.Properties.Settings.Default.directoryPath;
using (StreamReader reader=new StreamReader(System.IO.Path.Combine(metadataPath, "metadata")))
{
raw = reader.ReadToEnd();
}
StudentMetadata = JsonConvert.DeserializeObject<List<Student_Metadata>>(raw);
}
private void MetadataWrite(string StudentID,string HomeworkName,int Score)
{
bool foundStudent = false;
if (StudentMetadata==null)
{
StudentMetadata = new List<Student_Metadata>();
}
foreach (Student_Metadata singleStudent in StudentMetadata)
{
if (singleStudent.Student ==StudentID)
{
//student found!
foundStudent=!foundStudent;
bool foundHomework = false;
foreach (Hw_Metadata singleHomework in singleStudent.Hw_Metadatas)
{
if (singleHomework.Title==HomeworkName)
{
//homework found!
foundHomework = !foundHomework;
singleHomework.Score = Score;
}
}
if (!foundHomework)
{
//add a new homework metadat due to no existed found.
Hw_Metadata newHomework = new Hw_Metadata
{
Title=HomeworkName,Score=Score
};
singleStudent.Hw_Metadatas.Add(newHomework);
}
}
}
if (!foundStudent)
{
//no existed student found add one.
Hw_Metadata newHomework = new Hw_Metadata
{
Title = HomeworkName,
Score = Score
};
Student_Metadata newStudent = new Student_Metadata
{
Student = StudentID,
};
newStudent.Hw_Metadatas.Add(newHomework);
StudentMetadata.Add(newStudent);
}
string json = JsonConvert.SerializeObject(StudentMetadata);
string metadataPath=homerworker.Properties.Settings.Default.directoryPath;
using (StreamWriter writer = new StreamWriter(System.IO.Path.Combine(metadataPath, "metadata")))
{
writer.Write(json);
}
//manual add a new
//List<Student_Metadata> student_s = new List<Student_Metadata>();
//Student_Metadata sm = new Student_Metadata
//{
// Student="109423069",
// Hw_Metadatas=new List<Hw_Metadata>
// {
// new Hw_Metadata{Title="1-2",Score=7878} ,
// new Hw_Metadata{Title="1-3",Score=7878},
// new Hw_Metadata{Title="1-1",Score=7878}
// }
//};
//Student_Metadata sm1 = new Student_Metadata
//{
// Student = "109423070",
// Hw_Metadatas = new List<Hw_Metadata>
// {
// new Hw_Metadata{Title="1-1",Score=7878} ,
// new Hw_Metadata{Title="1-2",Score=7878},
// new Hw_Metadata{Title="1-3",Score=7878}
// }
//};
//student_s.Add(sm);
//student_s.Add(sm1);
//string json = JsonConvert.SerializeObject(student_s);
}
public class Student_Metadata
{
public string Student { get; set; }
public List<Hw_Metadata> Hw_Metadatas { get; set; } = new List<Hw_Metadata>();
}
public class Hw_Metadata
{
public string Title { get; set; }
public int Score { get; set; }
}
private void settingBtn_Click(object sender, RoutedEventArgs e)
{
}
private void hwSlection_lb_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (hwSlection_lb.SelectedIndex!=-1)
{
student.Clear();
// student_lv.Items.Clear();
hwDocument.Clear();
//hwDocument_lv.Items.Clear();
hwDocument_lv.ItemsSource = null;
// student.Add();
LoadStudent();
}
}
private void LoadStudent()
{
student_lv.ItemsSource = null;
studentHwListRead(hw[hwSlection_lb.SelectedIndex]);
//https://www.wpf-tutorial.com/listview-control/listview-sorting/
//sorting
CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(student_lv.ItemsSource);
view.SortDescriptions.Add(new SortDescription("id", ListSortDirection.Ascending));
}
private void studentHwListRead(string hwDirectory)
{
string _path = homerworker.Properties.Settings.Default.directoryPath;
_path += "\\"+hwDirectory ;
string[] studentId = Directory.GetDirectories(_path);
if (studentId.Length != 0)
{
List<Student> items = new List<Student>();
foreach (string readid in studentId)
{
string hw_id = System.IO.Path.GetFileName(readid); //路徑取資料夾名
string id = hw_id;//hw1_109423069 取右
student.Add(System.IO.Path.GetFileName(readid));
//取HW
string hwSeq =(string) hwSlection_lb.SelectedItem;
hwSeq = hwSeq.Substring(2);
//Student_Metadata sm= StudentMetadata.Find(student => student.Student.Equals(id));
//開頭一致的傳回
//List<Hw_Metadata> hw = sm.Hw_Metadatas.FindAll(hw_ =>
//hw_.Title.Split('_')
//.Select((string x)=>new {Titles=x[0]})
//.Where(x=>x.Titles==hwSeq));
items.Add(new Student { grade = "1-1:69", id = $"{id}" });
}
student_lv.ItemsSource = items;
}
}
public class Student
{
//顯示於listview or lb
public string id { get; set; }
public string grade { get; set; }
}
public class HomeworkKWSK
{
public string Path { get; set; }
//顯示於listview or lb
public string homeworkName { get; set; }
public int grade { get; set; }
}
private void student_lv_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
LoadHomework();
}
private void LoadHomework()
{
if (student_lv.SelectedIndex != -1)
{
string _path = homerworker.Properties.Settings.Default.directoryPath + "\\" + hw[hwSlection_lb.SelectedIndex] + "\\" + student[student_lv.SelectedIndex] + "\\";
_path = DirectorySearch(_path);
string[] document = Directory.GetFiles(_path);
//string[] document = Directory.GetFiles(_path, "*.c");
hwDocument.Clear();
Student selectedItem = (Student)student_lv.SelectedItem;
string studentID = selectedItem.id;
List<HomeworkKWSK> hwdoc = new List<HomeworkKWSK>();
foreach (string item in document)
{
string _name = System.IO.Path.GetFileName(item);
Student_Metadata sm = MetadataSeek(studentID, _name);
int score = 0;
if (sm != null)
{
Hw_Metadata aa = sm.Hw_Metadatas.Find(aHomework => aHomework.Title.Equals(_name));
if (aa != null)
{
score = aa.Score;
}
}
homeworkDocName_label.Content = "檔名:";
score_txt.Text = "";
hwdoc.Add(new HomeworkKWSK { grade = score, homeworkName = $"{_name}" ,Path=item});
}
hwDocument_lv.ItemsSource = hwdoc;
}
}
private string DirectorySearch(string directory)
{
string [] directories=Directory.GetDirectories(directory);
if (directories.Length==1)
{
return DirectorySearch(directories[0]);
}
else if(directories.Length>1)
{
for (int i = 0; i < directories.Length; i++)
{
string n = new DirectoryInfo(directories[i]).Name;
if (n== "__MACOSX")
{
directories = directories.Where(val =>new DirectoryInfo(val).Name != "__MACOSX").ToArray();
break;
}
}
if (directories.Length==1)
{
return DirectorySearch(directories[0]);
}
else if(directories.Length==0)
{
return directory;
}
System.Windows.Forms.MessageBox.Show("太多資料夾ㄌ");
return directory;
}
else
{
return directory;
}
return directory;
}
private void hwDocument_lv_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
HomeworkKWSK selectedhomeworkKWSK = (HomeworkKWSK)hwDocument_lv.SelectedItem;
if (selectedhomeworkKWSK!=null)
{
int score = selectedhomeworkKWSK.grade;
string homeworkName = selectedhomeworkKWSK.homeworkName;
homeworkDocName_label.Content = $"檔名:{homeworkName}";
score_txt.Text = score.ToString();
}
else
{
homeworkDocName_label.Content = "檔名:";
score_txt.Text = "";
}
}
private void saveBtn_Click(object sender, RoutedEventArgs e)
{
Student hw_ =(Student) student_lv.SelectedItem;
HomeworkKWSK kWSK_ = (HomeworkKWSK)hwDocument_lv.SelectedItem;
if ((hw_!=null)&&(kWSK_!=null))
{
string id_ = hw_.id;
int score_ = Convert.ToInt32( score_txt.Text);
string hwName_ = kWSK_.homeworkName;
MetadataWrite(id_, hwName_, score_);
LoadHomework();
}
}
private void score_txt_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
e.Handled = !IsTextAllowed(e.Text);
}
private static readonly Regex _regex = new Regex("[^0-9.-]+"); //regex that matches disallowed text
private static bool IsTextAllowed(string text)
{
return !_regex.IsMatch(text);
}
private void ImportBtn_Click(object sender, RoutedEventArgs e)
{
if (!Directory.Exists("temp"))
{
Directory.CreateDirectory("temp");
}
System.IO.DirectoryInfo di = new DirectoryInfo("temp");
foreach (FileInfo file in di.GetFiles())
{
file.Delete();
}
foreach (DirectoryInfo dir in di.GetDirectories())
{
dir.Delete(true);
}
OpenFileDialog open = new OpenFileDialog();
if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
ZipFile.ExtractToDirectory(open.FileName, "temp");
}
string [] path= Directory.GetDirectories("temp");
if (path.Length>0)
{
foreach (string folder in path)
{
string destPath = DirectorySearch(folder);
string[] zips = Directory.GetFiles(destPath, "*.zip");
if (zips.Length>0)
{
foreach (string zip in zips)
{
ZipFile.ExtractToDirectory(zip, DirectorySearch(folder));
}
}
}
}
string dire = Path.Combine(Directory.GetCurrentDirectory(), "temp");
string proj = homerworker.Properties.Settings.Default.directoryPath;
Process.Start("explorer.exe",dire);
Process.Start("explorer.exe", proj);
}
private void hwDocument_lv_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
HomeworkKWSK kWSK=(HomeworkKWSK) hwDocument_lv.SelectedItem;
Process.Start(kWSK.Path);
}
}
}