-
Notifications
You must be signed in to change notification settings - Fork 0
/
courses.go
211 lines (195 loc) · 7.73 KB
/
courses.go
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
package yaac_frontend_pages
import (
"errors"
"fmt"
"regexp"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
yaac_shared "github.com/DHBW-SE-2023/YAAC/internal/shared"
"gorm.io/gorm"
)
const DATE_FORMAT = "2006-01-02"
func CoursesScreen(w fyne.Window) fyne.CanvasObject {
course := &SelectionTracker{
courseName: widget.NewLabel(""),
secondary: widget.NewLabel(""),
}
var dates []string
selection := widget.NewLabel("")
title := ReturnHeader("Kursansicht")
tableHeader, courseTable := ReturnAttendanceTable("Name", "Status")
dateDropdown := ReturnDateDropdown(dates)
courseDropdown := ReturnCourseDropdown(course, selection, dateDropdown, "course")
courseInsertButton := ReturnCourseInsertButton(w)
editDropdown := ReturnEditDropdown(w, courseDropdown, dateDropdown, courseTable)
ConfigureDateDropdownVerification(dateDropdown, course, selection, courseTable, editDropdown)
header := container.NewCenter(container.NewGridWrap(fyne.NewSize(400, 200), title))
dropdownArea := container.NewGridWithRows(1, courseDropdown, dateDropdown, courseInsertButton, layout.NewSpacer(), editDropdown)
selectionArea := container.NewVBox(selection, tableHeader)
studentView := container.NewBorder(container.NewVBox(header, widget.NewSeparator(), dropdownArea), nil, nil, nil, container.NewBorder(selectionArea, nil, nil, nil, container.NewVScroll(courseTable)))
return studentView
}
/*
ReturnDateDropdown returns the configured dateDropdown passing the dates list
*/
func ReturnDateDropdown(dates []string) *widget.SelectEntry {
re := regexp.MustCompile(`^\d{4}-\d{2}-\d{2}$`)
dateDropdown := widget.NewSelectEntry(dates)
dateDropdown.PlaceHolder = "YYYY-MM-DD"
dateDropdown.Wrapping = fyne.TextWrap(fyne.TextTruncateClip)
dateDropdown.Scroll = container.ScrollNone
dateDropdown.Validator = func(s string) error {
if !re.MatchString(s) {
return errors.New("geben sie ein valides Datum im Format YYYY-MM-DD ein")
}
return nil
}
dateDropdown.Disable()
return dateDropdown
}
/*
ReturnCourseInsertButton returns the configured courseInsertButton
*/
func ReturnCourseInsertButton(w fyne.Window) *widget.Button {
insertButton := widget.NewButton("Kurs hinzufügen", func() {
DisplayInserCourseDialog(w)
})
return insertButton
}
/*
ReturnCourseInsertButton returns the configured courseInsertButton
*/
func DisplayInserCourseDialog(w fyne.Window) {
content := container.NewVBox()
form := dialog.NewCustomWithoutButtons("Kurs hinzufügen", content, w)
courseEntry := widget.NewEntry()
courseEntry.PlaceHolder = "Kursname"
exitButton := widget.NewButton("Zurück", func() {
form.Hide()
})
confirmButton := widget.NewButton("Bestätigen", func() {
fmt.Println(courseEntry.Text)
course := yaac_shared.Course{
Name: courseEntry.Text,
}
_, err := myMVVM.InsertCourse(course)
if err != nil {
dialog.ShowError(err, w)
} else {
form.Hide()
dialog.ShowInformation("Kurs hinzufügen", fmt.Sprintf("%s %s %s", "Es wurde erfolgreich der Kurs", courseEntry.Text, "angelegt!"), w)
}
})
confirmButton.Disable()
ValidateCourseInput(courseEntry, confirmButton)
content.Add(courseEntry)
content.Add(container.NewGridWithColumns(2, exitButton, confirmButton))
form.Show()
}
/*
ReturnEditDropdown returns the configured editDropdown passing the fyne.Window(for redirection),courseDropdown,dateDropdown, courseTable
since they will be necessary for change handling.
*/
func ReturnEditDropdown(w fyne.Window, courseDropdown *widget.Select, dateDropdown *widget.SelectEntry, courseTable *fyne.Container) *widget.Select {
editDropdown := widget.NewSelect([]string{
"Liste bearbeiten",
"Liste anzeigen",
"Liste hochladen",
}, func(s string) {
if dateDropdown.Text != "" {
if s == "Liste bearbeiten" {
lastView = w.Content()
VerifyList(w, courseDropdown.Selected, dateDropdown.Text, courseTable)
} else if s == "Liste anzeigen" {
ShowImage(w, courseDropdown.Selected, dateDropdown.Text)
courseTable.RemoveAll()
} else {
OpenImageUpload(w, courseDropdown.Selected, dateDropdown.Text)
}
courseTable.RemoveAll()
} else {
dialog.ShowInformation("Es wurde noch kein Datum ausgewählt!", "", w)
}
})
editDropdown.Selected = "Listenkonfiguration"
editDropdown.Disable()
return editDropdown
}
/*
RefreshDateDropdown is responsible for refreshing the date dropdown as soon as course has been selected
*/
func RefreshDateDropdown(dateDropdown *widget.SelectEntry, course string) {
selectedCourse, _ := myMVVM.CourseByName(course)
lists, _ := myMVVM.AllAttendanceListInRangeByCourse(yaac_shared.Course{Model: gorm.Model{ID: selectedCourse.ID}}, time.Now().AddDate(0, 0, -30), time.Now())
var dates []string
for _, element := range lists {
dates = append(dates, element.ReceivedAt.Format(DATE_FORMAT))
}
dateDropdown.SetOptions(dates)
}
/*
ConfigureDateDropdownVerification is responsible for configuring the verification process for text inputs passing the dateDropdown,
course SelectionTracker, selection Label, courseTable Container as well as the editDropdown for further processing.
*/
func ConfigureDateDropdownVerification(dateDropdown *widget.SelectEntry, course *SelectionTracker, selection *widget.Label, courseTable *fyne.Container, editDropdown *widget.Select) {
editDropdown.Disable()
dateDropdown.OnChanged = func(s string) {
if len(s) > 10 {
s = s[0:10]
}
re := regexp.MustCompile(`[^\d-]`)
s = re.ReplaceAllString(s, "")
dateDropdown.SetText(s)
if dateDropdown.Validate() != nil {
course.secondary.SetText("Falsches Datumsformat")
editDropdown.Disable()
} else {
course.secondary.SetText(s)
editDropdown.Enable()
}
selection.SetText(RefreshSelection(course))
courseTable.RemoveAll()
RefreshCourseAttendancy(courseTable, course.courseName.Text, s)
}
}
/*
RefreshCourseAttendancy is responsible for refreshing the course attendancy list as soon as a date and a course has been selected
*/
func RefreshCourseAttendancy(table *fyne.Container, course string, date string) {
selectedCourse, _ := myMVVM.CourseByName(course)
lists, _ := myMVVM.AllAttendanceListInRangeByCourse(yaac_shared.Course{Model: gorm.Model{ID: selectedCourse.ID}}, time.Now().AddDate(0, 0, -180), time.Now())
for _, list := range lists {
if list.ReceivedAt.Format((DATE_FORMAT)) == date {
for _, attendancies := range list.Attendancies {
student, _ := myMVVM.Students(yaac_shared.Student{Model: gorm.Model{ID: attendancies.StudentID}})
table.Add(NewAttendanceRow(fmt.Sprintf("%s %s", student[0].FirstName, student[0].LastName), MapBooleans(attendancies.IsAttending)))
}
}
}
}
/*
VerifyList will redirect the user to the Verification Page passing the currently selected course,date, courseTable
*/
func VerifyList(w fyne.Window, course string, date string, courseTable *fyne.Container) {
selectedCourse, _ := myMVVM.CourseByName(course)
parsedTime, _ := time.Parse(DATE_FORMAT, date)
w.SetContent(VerificationScreen(w, GetImageByDate(course, parsedTime.Add(24*time.Hour)), int(selectedCourse.ID), courseTable, parsedTime))
}
/*
ShowImage will display the currently selected list in a seperate window passing course and date.
*/
func ShowImage(w fyne.Window, course string, date string) {
selectedCourse, _ := myMVVM.CourseByName(course)
parsedTime, _ := time.Parse(DATE_FORMAT, date)
list, _ := myMVVM.AllAttendanceListInRangeByCourse(yaac_shared.Course{Model: gorm.Model{ID: selectedCourse.ID}}, parsedTime, parsedTime.Add(24*time.Hour))
img := RotateImage(list[0].Image)
img.FillMode = canvas.ImageFillOriginal
customDialog := dialog.NewCustom(fmt.Sprintf("%s %s", "Liste vom", date), "Beenden", container.NewVScroll(container.NewGridWrap(fyne.NewSize(800, 1000), img)), w)
customDialog.Resize(fyne.NewSize(800, 800))
customDialog.Show()
}