-
Notifications
You must be signed in to change notification settings - Fork 0
/
LessonForm.cs
792 lines (690 loc) · 28.4 KB
/
LessonForm.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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RecordKeeper
{
public class OperEvent
{
public Lesson SuspectedLesson;
public GCal.CalEvent GoogleEvent;
public bool Linked;
public double SimilarityPlace;
public double SimilarityStart;
public double SimilarityDuration;
public double SimilarityTeacher;
public double SimilarityStudent;
public double SimilarityStatus;
public double OverallSimularity;
private string m_parsed_title;
private string m_parsed_comment;
private string[] m_parsed_students;
private string[] m_parsed_teachers;
public OperEvent(GCal.CalEvent ge)
{
GoogleEvent = ge;
FormGlob.ParseEventDescription(
GoogleEvent.Summary,
out m_parsed_title,
out m_parsed_students,
out m_parsed_teachers,
out m_parsed_comment);
Linked = false;
SimilarityPlace = -1;
SimilarityStart = -1;
SimilarityDuration = -1;
SimilarityTeacher = -1;
SimilarityStudent = -1;
SimilarityStatus = -1;
}
private OperEvent(
GCal.CalEvent ge,
Lesson l,
string title,
string[] students,
string[] teachers,
string comment)
{
GoogleEvent = ge;
SuspectedLesson = l;
m_parsed_title = title;
m_parsed_students = students;
m_parsed_teachers = teachers;
m_parsed_comment = comment;
Linked = false;
SimilarityPlace = -1;
SimilarityStart = -1;
SimilarityDuration = -1;
SimilarityTeacher = -1;
SimilarityStudent = -1;
SimilarityStatus = -1;
}
public OperEvent SetLesson(Lesson l)
{
return new OperEvent(
GoogleEvent,
l,
m_parsed_title,
m_parsed_students,
m_parsed_teachers,
m_parsed_comment);
}
public void CalculateSimilarities()
{
Linked = !FormGlob.IsStringEmpty(SuspectedLesson.GoogleId) &&
(SuspectedLesson.GoogleId == GoogleEvent.Id);
SimilarityDuration = SimilarityDurations(
SuspectedLesson.DateTimeStart,
SuspectedLesson.DateTimeEnd,
GoogleEvent.Start,
GoogleEvent.End);
SimilarityPlace = SimilarytyPlaces(
SuspectedLesson.Room,
GoogleEvent.Location);
SimilarityStart = SimilatityTimes(
SuspectedLesson.DateTimeStart,
SuspectedLesson.DateTimeEnd,
GoogleEvent.Start,
GoogleEvent.End);
SimilarityStatus = SimilarityStates(
SuspectedLesson.State,
GoogleEvent.Status);
// TODO: use all students and teachers, not only 1st
SimilarityStudent = SimilarityStudents(
SuspectedLesson.Student1,
(m_parsed_students.Length > 0 ? m_parsed_students[0] : ""));
SimilarityTeacher = SimilarityTeachers(
SuspectedLesson.Teacher1,
(m_parsed_teachers.Length > 0 ? m_parsed_teachers[0] : ""));
// TODO: grab "canceled" from the end of comment and use it for status
OverallSimularity = CalculateOverallSimularity();
}
private double CalculateOverallSimularity()
{
if (!FormGlob.IsStringEmpty(SuspectedLesson.GoogleId) &&
SuspectedLesson.GoogleId != GoogleEvent.Id)
return 0.0;
if (SuspectedLesson.DateTimeStart.Date !=
GoogleEvent.Start.Date)
return 0.0;
if (SimilarityStart > 0.5 &&
SimilarityTeacher > 0.5 &&
SimilarityStudent > 0.5)
return 1.0;
if (SimilarityStart > 0.5 &&
SimilarityPlace > 0.5)
return 0.9;
int count = 0;
if (SimilarityPlace > 0.5)
count++;
if (SimilarityStart > 0.5)
count++;
if (SimilarityDuration > 0.5)
count++;
if (SimilarityTeacher > 0.5)
count++;
if (SimilarityStudent > 0.5)
count++;
return (double)count / 5.0;
}
private double SimilarytyPlaces(string lRoom, string gLocation)
{
if (FormGlob.IsStringEmpty(lRoom) || FormGlob.IsStringEmpty(gLocation))
return 0;
if (gLocation.Substring(0, 1).ToLower() == lRoom.Substring(0, 1).ToLower())
return 1;
return 0;
}
private double SimilatityTimes(
DateTime lStart, DateTime lEnd,
DateTime gStart, DateTime gEnd)
{
if (lStart == gStart)
return 1.0;
DateTime cs = (lStart > gStart ? lStart : gStart);
DateTime ce = (lEnd < gEnd ? lEnd : gEnd);
DateTime us = (lStart < gStart ? lStart : gStart);
DateTime ue = (lEnd > gEnd ? lEnd : gEnd);
if (cs < ce)
return (ce - cs).Ticks / (ue - us).Ticks;
else
return 0.0; ;
}
private double SimilarityDurations(DateTime lStart, DateTime lEnd, DateTime gStart, DateTime gEnd)
{
TimeSpan ls = lEnd - lStart;
TimeSpan gs = gEnd - gStart;
if (ls.Ticks == 0 && gs.Ticks == 0)
return 1;
return (1.0 - Math.Abs(ls.Ticks - gs.Ticks) / Math.Max(ls.Ticks, gs.Ticks));
}
private double SimilarityStates(string lState, string gStatus)
{
// todo
return -1;
}
private double SimilarityStudents(string lStudent, string gStudent)
{
if (FormGlob.IsStringEmpty(lStudent) || FormGlob.IsStringEmpty(gStudent))
return -1;
string gFirst= FormGlob.ExtractFirstWord(gStudent).ToLower();
string lFirst = FormGlob.ExtractFirstWord(lStudent).ToLower();
if (FormGlob.IsStringEmpty(gFirst) || FormGlob.IsStringEmpty(lFirst))
return -1;
return 1.0 - (double)FormGlob.LevensteinDistance(gFirst, lFirst) /
(double)Math.Max(gFirst.Length, lFirst.Length);
}
private double SimilarityTeachers(string lTeacher, string gTeacher)
{
if (FormGlob.IsStringEmpty(lTeacher) || FormGlob.IsStringEmpty(gTeacher))
return -1;
string gFirst= FormGlob.ExtractFirstWord(gTeacher).ToLower();
string lFirst = FormGlob.ExtractFirstWord(lTeacher).ToLower();
return 1.0 - (double)FormGlob.LevensteinDistance(gFirst, lFirst) /
(double)Math.Max(gFirst.Length, lFirst.Length);
}
}
public partial class FormGlob : Form
{
string m_LessonSelectionTeacher;
string m_LessonSelectionStudent;
DateTime m_LessonSelectionDay;
bool m_use_LessonSelectionDay;
string m_LessonSelectionProgram;
string m_LessonSelectionRoom;
string m_currentLessonKey;
GCal.CalEvent[] m_OperationalEvents;
int m_curOperationalevent;
delegate bool EvaluateLesson(Lesson t);
private void DropLessonSelection()
{
m_LessonSelectionTeacher = null;
m_LessonSelectionStudent = null;
m_use_LessonSelectionDay = false;
m_LessonSelectionProgram = null;
m_LessonSelectionRoom = null;
}
private void LessonToFormConst1()
{
m_dataTypes.Add(Modes.Lessons, typeof(Lesson));
m_recordTypes.Add(Modes.Lessons, new LessonType(this));
}
private void LessonToFormConst2()
{
cbGlobMode.Items.Add(Modes.Lessons.ToString());
}
public void DoLessonSelection()
{
if (!SelectionMode)
{
SelectionMode = true;
CurrentType.StashRecordList();
}
DataList.Clear();
foreach (Lesson s in m_recordTypes[Modes.Lessons].SavedFullListDuringSelection)
{
if (!CurrentType.Fit(m_LessonSelectionTeacher, s.Teacher1, false) &&
!CurrentType.Fit(m_LessonSelectionTeacher, s.Teacher2, false))
continue;
if (!CurrentType.Fit(m_LessonSelectionStudent, s.Student1, false) &&
!CurrentType.Fit(m_LessonSelectionStudent, s.Student2, false) &&
!CurrentType.Fit(m_LessonSelectionStudent, s.Student3, false) &&
!CurrentType.Fit(m_LessonSelectionStudent, s.Student4, false) &&
!CurrentType.Fit(m_LessonSelectionStudent, s.Student5, false) &&
!CurrentType.Fit(m_LessonSelectionStudent, s.Student6, false) &&
!CurrentType.Fit(m_LessonSelectionStudent, s.Student7, false) &&
!CurrentType.Fit(m_LessonSelectionStudent, s.Student8, false) &&
!CurrentType.Fit(m_LessonSelectionStudent, s.Student9, false) &&
!CurrentType.Fit(m_LessonSelectionStudent, s.Student10, false) )
continue;
if (m_use_LessonSelectionDay &&
!RecordType.FitDate(m_LessonSelectionDay, s.Day))
continue;
if (!CurrentType.Fit(m_LessonSelectionProgram, s.Program, false))
continue;
if (!CurrentType.Fit(m_LessonSelectionRoom, s.Room, false))
continue;
DataList.Add(s);
}
ShowCurrentCount();
}
List<Lesson> LessonsByTime(DateTime t1, DateTime t2, bool onlyActual)
{
return FindLessons(l =>
(!onlyActual || l.Actual) &&
l.DateTimeStart <= t2 &&
l.DateTimeEnd >= t1);
}
List<Lesson> LessonsByTeacher(string desc, bool onlyActual)
{
if (IsStringEmpty(desc))
return new List<Lesson>();
return FindLessons(l =>
(!onlyActual || l.Actual) &&
(l.Teacher1 == desc || l.Teacher2 == desc));
}
List<Lesson> LessonsByStudent(string desc, bool onlyActual)
{
if (IsStringEmpty(desc))
return new List<Lesson>();
return FindLessons(l =>
(!onlyActual || l.Actual) &&
(l.Student1 == desc || l.Student2 == desc || l.Student3 == desc ||
l.Student4 == desc || l.Student5 == desc || l.Student6 == desc ||
l.Student7 == desc || l.Student8 == desc || l.Student9 == desc ||
l.Student10 == desc));
}
List<Lesson> LessonsByTeacher(string desc, DateTime t1, DateTime t2, bool onlyActual)
{
if (IsStringEmpty(desc))
return new List<Lesson>();
return FindLessons(l =>
(!onlyActual || l.Actual) &&
(l.Teacher1 == desc || l.Teacher2 == desc) &&
l.DateTimeStart <= t2 && l.DateTimeEnd >= t1);
}
List<Lesson> LessonsByStudent(string desc, DateTime t1, DateTime t2, bool onlyActual)
{
if (IsStringEmpty(desc))
return new List<Lesson>();
return FindLessons(l =>
(!onlyActual || l.Actual) &&
(l.Student1 == desc || l.Student2 == desc || l.Student3 == desc ||
l.Student4 == desc || l.Student5 == desc || l.Student6 == desc ||
l.Student7 == desc || l.Student8 == desc || l.Student9 == desc ||
l.Student10 == desc) &&
l.DateTimeStart <= t2 &&
l.DateTimeEnd >= t1);
}
public bool SetLessonDetails(string key, string program, string room, string comment)
{
foreach (var tt in this.lessonList.List)
{
Lesson t = tt as Lesson;
if (t.Key == key)
{
t.Program = program;
t.Room = room;
t.Comments = comment;
return true;
}
}
return false;
}
List<Lesson> FindLessons(EvaluateLesson comp)
{
List<Lesson> lessons = new List<Lesson>();
foreach (var tt in this.lessonList.List)
{
Lesson t = tt as Lesson;
if (comp(t))
lessons.Add(t);
}
return lessons;
}
List<Lesson> FindLessonSlots(
string state,
string student,
string teacher,
string room,
string program,
DateTime t1,
DateTime t2)
{
List<Lesson> lessons = new List<Lesson>();
foreach (var tt in this.lessonList.List)
{
Lesson t = tt as Lesson;
if (t.DateTimeStart >= t2 || t.DateTimeEnd < t1)
continue;
if (state != null && state.Trim().Length > 1 &&
state != t.State)
continue;
if (room != null && room.Trim().Length > 1 &&
room != t.Room)
continue;
if (program != null && program.Trim().Length > 1 &&
program != t.Program)
continue;
if (teacher != null && teacher.Trim().Length > 1 &&
teacher != t.Teacher1 && teacher != t.Teacher2)
continue;
if (student != null && student.Trim().Length > 1 &&
student != t.Student1 && student != t.Student2 &&
student != t.Student3 && student != t.Student4 &&
student != t.Student5 && student != t.Student6 &&
student != t.Student7 && student != t.Student8 &&
student != t.Student9 && student != t.Student10)
continue;
lessons.Add(t);
}
return lessons;
}
List<GCal.CalEvent> GetEventsForPublishing(DateTime dtMin, DateTime dtMax)
{
List<GCal.CalEvent> evts = new List<GCal.CalEvent>();
foreach (Lesson l in LessonsByTime(dtMin, dtMax, true))
{
evts.Add(new GCal.CalEvent(
l.DateTimeStart,
l.DateTimeEnd,
l.Room,
l.Details,
l.Description));
}
return evts;
}
public MatchingState SetCurrentOperationalEvent()
{
if (m_OperationalEvents == null ||
m_curOperationalevent < 0 ||
m_curOperationalevent >= m_OperationalEvents.Length)
return MatchingState.NoMatch;
GCal.CalEvent ce = m_OperationalEvents[m_curOperationalevent];
labellbReconcileX.Text = (m_curOperationalevent+1).ToString();
labellbReconcileY.Text = m_OperationalEvents.Length.ToString();
lbReconcileDate.Text = ce.Start.ToShortDateString();
lbReconcileFrom.Text = ce.Start.ToShortTimeString();
lbReconcileTo.Text = ce.End.ToShortTimeString();
lbReconcileLocation.Text = (ce.Location == null ? "" : ce.Location);
lbReconcileDescription.Text = (ce.Summary == null ? "" : ce.Summary);
lbReconcileCreated.Text = (ce.Creator == null ? "" : ce.Creator);
lbReconcileGoogleCalId.Text = (ce.Id == null ? "" : ce.Id);
lbReconcileTouched.Text = ce.LastTouched;
double similarity = -1.0;
bool found = FindClosestLesson(out similarity);
Lesson l = lessonList.Current as Lesson;
if (lessonList.Position < lessonList.Count-1 && lessonList.Position >= 0)
{
// Cause RowLeave to populate edit controls
lessonList.Position++;
lessonList.Position--;
}
if (found && l != null)
{
lbReconcileLocationDiff.Visible = !AreRoomsEquivalent(
lbReconcileLocation.Text, l.Room);
lbReconcileDateDiff.Visible = !AreStringsEquivalent(
lbReconcileDate.Text, l.Day);
lbReconcileFromDiff.Visible = !AreStringsEquivalent(
lbReconcileFrom.Text, l.Start);
lbReconcileToDiff.Visible = !AreStringsEquivalent(
lbReconcileTo.Text, l.End);
lbReconcileDescDiff.Visible = !AreStringsEquivalent(
lbReconcileDescription.Text, l.Comments); ;
}
else
{
lbReconcileLocationDiff.Visible = false;
lbReconcileDateDiff.Visible = false;
lbReconcileFromDiff.Visible = false;
lbReconcileToDiff.Visible = false;
lbReconcileDescDiff.Visible = false;
};
MatchingState ms = MatchingState.NoMatch;
if (found)
{
if (l.GoogleId == ce.Id)
ms = MatchingState.Linked;
else if (!IsStringEmpty(l.GoogleId) && l.GoogleId != ce.Id)
ms = MatchingState.LinkedToOther;
else if (similarity == -1.0)
ms = MatchingState.NoMatch;
else if (similarity > 0.9)
ms = MatchingState.Match;
else
ms = MatchingState.Similar;
}
ShowMatching(ms, similarity);
return ms;
}
public void ShowMatching(MatchingState ms, double similarity)
{
switch(ms)
{
case MatchingState.Unknown:
lbReconcileResult.Text = "";
lbReconcileResult.BackColor = Color.White;
lbReconcileResult.ForeColor = Color.Yellow;
break;
case MatchingState.Linked:
lbReconcileResult.Text = "Linked";
lbReconcileResult.BackColor = Color.Blue;
lbReconcileResult.ForeColor = Color.Yellow;
break;
case MatchingState.Match:
lbReconcileResult.Text = "Match";
lbReconcileResult.BackColor = Color.DarkGreen;
lbReconcileResult.ForeColor = Color.Yellow;
break;
case MatchingState.NoMatch:
lbReconcileResult.Text = "No match";
lbReconcileResult.BackColor = Color.Red;
lbReconcileResult.ForeColor = Color.Yellow;
break;
case MatchingState.LinkedToOther:
lbReconcileResult.Text = "OtherLinked";
lbReconcileResult.BackColor = Color.Red;
lbReconcileResult.ForeColor = Color.Yellow;
break;
case MatchingState.Similar:
lbReconcileResult.Text = "Similar";
lbReconcileResult.BackColor = Color.FromArgb(0, (int)(127 * (2.0 - similarity)), 0);
lbReconcileResult.ForeColor = Color.Yellow;
break;
default:
throw new Exception("Bad params");
}
lbReconcileSimilarity.Text = similarity.ToString("F1");
}
public bool FindClosestLesson(out double similarity)
{
similarity = -1.0;
if (m_OperationalEvents == null ||
m_curOperationalevent < 0 ||
m_curOperationalevent >= m_OperationalEvents.Length)
return false;
GCal.CalEvent ge = m_OperationalEvents[m_curOperationalevent];
OperEvent oe = new OperEvent(ge);
List<OperEvent> candidates = new List<OperEvent>();
foreach (Lesson l in lessonList)
{
OperEvent oev = oe.SetLesson(l);
oev.CalculateSimilarities();
if (oev.Linked || oev.OverallSimularity > 0.3)
candidates.Add(oev);
}
if (candidates.Count == 0)
return false;
OperEvent best = candidates.First();
foreach (OperEvent oev in candidates)
{
if (oev.Linked || oev.OverallSimularity > best.OverallSimularity)
best = oev;
}
int i = 0;
foreach(Lesson l in lessonList)
{
if (l == best.SuspectedLesson)
{
lessonList.Position = i;
similarity = best.OverallSimularity;
return true;
}
i++;
}
return false;
}
public ComboBox LessonTeacherComboBox(int i)
{
ComboBox[] boxes = new ComboBox[] { cbLessonTeacher1, cbLessonTeacher2 };
if (i < 1 || i > 2)
throw new Exception("Bad params");
return boxes[i - 1];
}
public ComboBox LessonStudentComboBox(int i)
{
ComboBox[] boxes = new ComboBox[]
{ cbLessonStudent1, cbLessonStudent2, cbLessonStudent3,
cbLessonStudent4, cbLessonStudent5, cbLessonStudent6,
cbLessonStudent7, cbLessonStudent8, cbLessonStudent9, cbLessonStudent10 };
if (i < 1 || i > 10)
throw new Exception("Bad params");
return boxes[i - 1];
}
public void FillLessonFromCalendar(Lesson l)
{
int slot = SlotFromStringTime(lbReconcileFrom.Text, false);
cbLessonStart.SelectedIndex = slot;
l.Start = m_enumTimeSlot[slot];
slot = SlotFromStringTime(lbReconcileTo.Text, true);
cbLessonEnd.SelectedIndex = slot;
l.End = m_enumTimeSlot[slot];
DateTime dt = DateTime.Now;
DateTime.TryParse(lbReconcileDate.Text, out dt);
monthCalLessonDate.SetDate(dt);
l.Day = dt.ToShortDateString();
string s = lbReconcileDescription.Text;
tbLessonComment.Text = s;
l.Comments = s;
bool cancelled = !IsStringEmpty(s) &&
(s.ToLower().IndexOf("cancel") >= 0);
l.Room = SetComboByInitial(cbLessonRoom, lbReconcileLocation.Text);
l.GoogleId = lbReconcileGoogleCalId.Text;
l.State = SetComboByValue(cbLessonState,
(cancelled ? m_enumState[(int)LessonStates.Cancelled] :
m_enumState[(int)LessonStates.Planned]) );
if (cancelled)
l.CancellationTime = m_enumCancellation[1]; // On Time
else
l.CancellationTime = "";
string geTitle, geComment;
string[] geStudents, geTeachers;
ParseEventDescription( lbReconcileDescription.Text,
out geTitle, out geStudents, out geTeachers, out geComment);
for (int i = 0; i < 2 && i < geTeachers.Length; i++)
{
string str = ExtractTeacher(geTeachers[i], i+1);
if (!IsStringEmpty(str))
l.SetTeacher(SetComboByValue(LessonTeacherComboBox(i+1), str), i+1);
}
for (int i = 0; i < 10 && i < geStudents.Length; i++)
{
string str = ExtractStudent(geStudents[i], i+1);
if (!IsStringEmpty(str))
l.SetStudent(SetComboByValue(LessonStudentComboBox(i+1), str), i+1);
}
PopulateLessonProgPrice(l);
}
void PopulateLessonProgPrice(Lesson l)
{
foreach (Student stud in SpecificStudent(l.Student1))
{
for (int i = 1; i <= 3; i++)
{
if (!IsStringEmpty(stud.Program(i)))
{
l.Program = SetComboByValue(cbLessonProg, stud.Program(i));
string spr = stud.Price(i);
if (IsStringEmpty(spr))
spr = GetProgramPrice(l.Program);
l.Price = spr;
return;
}
}
/*if (!IsStringEmpty(prog))
{
string spr = GetProgramPrice(prog);
l.Price = spr;
}*/
}
}
private void TakeDistance(int d, string str, ref int mindist, ref string bestval)
{
if (d >= 0 && d < mindist)
{
mindist = d;
bestval = str;
}
}
public string ExtractPerson(ComboBox cb, string desc)
{
if (IsStringEmpty(desc))
return null;
desc = desc.Trim();
string first, last, initial;
int space = desc.IndexOf(" ");
if (space >= 0)
{
first = (space == 0 ? "" : desc.Substring(0, space));
last = (space < desc.Length - 1 ? desc.Substring(space + 1) : "");
initial = (last.Length > 0 ? last.Substring(0, 1) : "");
}
else
{
first = desc;
last = "";
initial = "";
}
// Find for the best edit distance across 3 choices:
// "first", "first last", "first initial"
int mindist = int.MaxValue;
string bestval = null;
foreach(string str in cb.Items)
{
if (IsStringEmpty(str))
continue;
string[] strx = str.Split(' ');
string str1 = strx[0];
string str2 = (strx.Length > 1 ? strx[strx.Length - 1] : "");
string stri = (str2.Length > 0 ? str2.Substring(0,1) : "");
if (!IsStringEmpty(first))
{
int d = LevensteinDistance(
str1.Trim().ToLower(),
first.Trim().ToLower());
TakeDistance(d, str, ref mindist, ref bestval);
if (!IsStringEmpty(last))
{
d = LevensteinDistance(
(str1.Trim() + " " + str2.Trim()).ToLower(),
(first.Trim() + " " + last.Trim()).ToLower());
TakeDistance(d, str, ref mindist, ref bestval);
d = LevensteinDistance(
(str1.Trim() + " " + stri).ToLower(),
(first.Trim() + " " + initial).ToLower());
TakeDistance(d, str, ref mindist, ref bestval);
}
}
else if (!IsStringEmpty(last))
{
int d = LevensteinDistance(
str2.Trim().ToLower(),
last.Trim().ToLower());
TakeDistance(d, str, ref mindist, ref bestval);
}
}
// we allow up to 2 typos
if (mindist < 0 || mindist > 2)
return null;
else
return bestval;
}
public string ExtractStudent(string desc, int ind)
{
return ExtractPerson( LessonStudentComboBox(ind), desc );
}
public string ExtractTeacher(string desc, int ind)
{
return ExtractPerson(LessonTeacherComboBox(ind), desc);
}
}
}