// timetable.go package controllers import ( "bytes" "fmt" "html/template" "sprechstundentool/models" ) func GetTimetable(officeHours []models.OfficeHour) (timetable map[models.Date]map[int]models.OfficeHour, slots []int) { var fullTimetable = make(map[models.Date]map[int]models.OfficeHour) for _, officeHour := range officeHours { var slot int = 0 for minute := 0; minute < officeHour.Duration; minute += models.MinuteGranularity { // find slot id _, exists := fullTimetable[models.Date{0, officeHour.Day, officeHour.Hour + (officeHour.Minute+minute)/60, (officeHour.Minute + minute) % 60}] if exists { _, exists := fullTimetable[models.Date{0, officeHour.Day, officeHour.Hour + (officeHour.Minute+minute)/60, (officeHour.Minute + minute) % 60}][slot] if exists { slot += 1 minute = 0 continue } } else { fullTimetable[models.Date{0, officeHour.Day, officeHour.Hour + (officeHour.Minute+minute)/60, (officeHour.Minute + minute) % 60}] = make(map[int]models.OfficeHour) } } for minute := 0; minute < officeHour.Duration; minute += models.MinuteGranularity { // write officeHour id to timetable fullTimetable[models.Date{0, officeHour.Day, officeHour.Hour + (officeHour.Minute+minute)/60, (officeHour.Minute + minute) % 60}][slot] = officeHour } } slots = []int{1, 1, 1, 1, 1} for date, _ := range fullTimetable { if slots[date.Day] < len(fullTimetable[date]) { slots[date.Day] = len(fullTimetable[date]) } } timetable = make(map[models.Date]map[int]models.OfficeHour) for _, officeHour := range officeHours { for slot := 0; slot < slots[officeHour.Date.Day]; slot += 1 { if fullTimetable[officeHour.Date][slot] == officeHour { timetable[officeHour.Date] = make(map[int]models.OfficeHour) timetable[officeHour.Date][slot] = officeHour } } } return fullTimetable, slots } func printTimetable(timetable map[models.Date]map[int]models.OfficeHour, slots []int) template.HTML { var tableBody string tableCell, _ := template.ParseFiles("templates/td.html") for hour := 8; hour < 19; hour += 1 { for minute := 0; minute < 60; minute += models.MinuteGranularity { tableBody += "