Skip to content

Commit

Permalink
shift and delete slot
Browse files Browse the repository at this point in the history
  • Loading branch information
Tania Bogatsch committed Dec 8, 2020
1 parent 4d29583 commit 0cb1240
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 43 deletions.
23 changes: 20 additions & 3 deletions app/controllers/course.go
Expand Up @@ -3,6 +3,7 @@ package controllers
import (
"strings"
"time"
"turm/app"
"turm/app/models"

"github.com/revel/revel"
Expand Down Expand Up @@ -210,15 +211,31 @@ func (c Course) CalendarEvents(ID int) revel.Result {

/*CalendarEvent of a course.
- Roles: if public all, else logged in users. */
func (c Course) CalendarEvent(ID, courseID, shift int, monday time.Time) revel.Result {
func (c Course) CalendarEvent(ID, courseID, shift int, monday string) revel.Result {

c.Log.Debug("load calendar event of course", "ID", ID, "courseID",
courseID, "shift", shift, "monday", monday)

monday.AddDate(0, 0, shift)
loc, err := time.LoadLocation(app.TimeZone)
if err != nil {
c.Log.Error("failed to parse location", "loc", app.TimeZone,
"error", err.Error())
renderQuietError(errTypeConv, err, c.Controller)
return c.Render()
}

t, err := time.ParseInLocation("2006-01-02T15:04:05-07:00", monday, loc)
if err != nil {
c.Log.Error("failed to parse string to time", "monday", monday,
"loc", loc, "error", err.Error())
renderQuietError(errTypeConv, err, c.Controller)
return c.Render()
}

t = t.AddDate(0, 0, shift*7)

event := models.CalendarEvent{ID: ID}
if err := event.Get(nil, &courseID, monday); err != nil {
if err := event.Get(nil, &courseID, t); err != nil {
renderQuietError(errDB, err, c.Controller)
return c.Render()
}
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/enrollment.go
Expand Up @@ -230,6 +230,6 @@ func (c Enrollment) UnsubscribeFromSlot(ID int) revel.Result {
errEMail, err, "", c.Controller, data.User.EMail)
}

c.Flash.Success(c.Message("event.enroll.success"))
c.Flash.Success(c.Message("event.unsubscribe.success"))
return c.Redirect(c.Session["currPath"])
}
50 changes: 27 additions & 23 deletions app/models/calendar_events.go
Expand Up @@ -22,7 +22,7 @@ type CalendarEvent struct {
Annotation sql.NullString `db:"annotation"`

//loaded week
Monday string
Monday time.Time
Week int
Year int

Expand Down Expand Up @@ -83,6 +83,7 @@ func (events *CalendarEvents) Get(tx *sqlx.Tx, courseID *int, monday time.Time)
}

//set the current week
(*events)[i].Monday = monday
_, (*events)[i].Week = monday.ISOWeek()
(*events)[i].Year = monday.Year()

Expand Down Expand Up @@ -135,6 +136,7 @@ func (event *CalendarEvent) Get(tx *sqlx.Tx, courseID *int, monday time.Time) (e
}

//set the current week
event.Monday = monday
_, event.Week = monday.ISOWeek()
event.Year = monday.Year()

Expand Down Expand Up @@ -194,7 +196,7 @@ func (event *CalendarEvent) getSchedule(tx *sqlx.Tx, monday time.Time) (err erro
//set blocked slot from 0 to start of the first day template
if tmplsOfDay[0].StartTime != "00:00" {
schedule.Entries = append(schedule.Entries,
ScheduleEntry{"00:00", tmplsOfDay[0].StartTime, 0, BLOCKED, 0, 0})
ScheduleEntry{"00:00", tmplsOfDay[0].StartTime, 0, BLOCKED, "0", 0})
}

//insert all slots and free spaces of a day template and
Expand All @@ -210,7 +212,7 @@ func (event *CalendarEvent) getSchedule(tx *sqlx.Tx, monday time.Time) (err erro
ScheduleEntry{
schedule.Entries[len(schedule.Entries)-1].EndTime,
tmplsOfDay[i].StartTime,
0, BLOCKED, 0, 0},
0, BLOCKED, "0", 0},
)
}
}
Expand All @@ -233,46 +235,47 @@ func (event *CalendarEvent) getSchedule(tx *sqlx.Tx, monday time.Time) (err erro
//insert FREE schedule entry
if tmplsOfDay[i].StartTime != slotStart.Value {
schedule.Entries = append(schedule.Entries, ScheduleEntry{tmplsOfDay[i].StartTime,
slotStart.Value, tmplsOfDay[i].Interval, FREE, 0, 0})
slotStart.Value, tmplsOfDay[i].Interval, FREE, "0", 0})
}
} else {
//check for FREE space between two slots
if schedule.Entries[len(schedule.Entries)-1].EndTime != slotStart.Value {
schedule.Entries = append(schedule.Entries, ScheduleEntry{schedule.Entries[len(schedule.Entries)-1].EndTime,
slotStart.Value, tmplsOfDay[i].Interval, FREE, 0, 0})
slotStart.Value, tmplsOfDay[i].Interval, FREE, "0", 0})
}
}

//insert slot as schedule entry
schedule.Entries = append(schedule.Entries, ScheduleEntry{slotStart.Value, slotEnd.Value,
tmplsOfDay[i].Interval, SLOT,
tmplsOfDay[i].Slots[j].UserID, tmplsOfDay[i].Slots[j].ID})
strconv.Itoa(tmplsOfDay[i].Slots[j].UserID),
tmplsOfDay[i].Slots[j].ID})

} //end of for loop of slots

if len(schedule.Entries) > 0 {
//check for FREE space from the last slot to the end of the day template
if tmplsOfDay[i].EndTime != schedule.Entries[len(schedule.Entries)-1].EndTime {
schedule.Entries = append(schedule.Entries, ScheduleEntry{schedule.Entries[len(schedule.Entries)-1].EndTime,
tmplsOfDay[i].EndTime, tmplsOfDay[i].Interval, FREE, 0, 0})
tmplsOfDay[i].EndTime, tmplsOfDay[i].Interval, FREE, "0", 0})
}
} else {
schedule.Entries = append(schedule.Entries, ScheduleEntry{tmplsOfDay[i].StartTime,
tmplsOfDay[i].EndTime, tmplsOfDay[i].Interval, FREE, 0, 0})
tmplsOfDay[i].EndTime, tmplsOfDay[i].Interval, FREE, "0", 0})
}

} //end of for loop of day templates

//check for BLOCKED space from the end of the last day template to 24:00
if schedule.Entries[len(schedule.Entries)-1].EndTime != "24:00" {
schedule.Entries = append(schedule.Entries, ScheduleEntry{schedule.Entries[len(schedule.Entries)-1].EndTime,
"24:00", 0, BLOCKED, 0, 0})
"24:00", 0, BLOCKED, "0", 0})
}

} else {
//no day templates for this day
schedule.Entries = append(schedule.Entries,
ScheduleEntry{"00:00", "24:00", 0, BLOCKED, 0, 0})
ScheduleEntry{"00:00", "24:00", 0, BLOCKED, "0", 0})
}

//after each day, loop all exceptions of the week and
Expand Down Expand Up @@ -371,32 +374,32 @@ func (event *CalendarEvent) getSchedule(tx *sqlx.Tx, monday time.Time) (err erro
if startEntry.Interval != 0 {
schedule.Entries = insertScheduleEntry(schedule.Entries,
ScheduleEntry{startEntry.StartTime, startTime,
startEntry.Interval, FREE, 0, 0}, startSlotIdx)
startEntry.Interval, FREE, "0", 0}, startSlotIdx)
startSlotIdx++
} else {
schedule.Entries = insertScheduleEntry(schedule.Entries,
ScheduleEntry{startEntry.StartTime, startTime,
startEntry.Interval, BLOCKED, 0, 0}, startSlotIdx)
startEntry.Interval, BLOCKED, "0", 0}, startSlotIdx)
startSlotIdx++
}
}

//insert the EXCEPTION entry
schedule.Entries = insertScheduleEntry(schedule.Entries,
ScheduleEntry{startTime, endTime,
0, EXCEPTION, 0, 0}, startSlotIdx)
0, EXCEPTION, "0", 0}, startSlotIdx)
startSlotIdx++

//insert the entry slice after the exception, FREE or BLOCKED
if endTime != endEntry.EndTime {
if endEntry.Interval != 0 {
schedule.Entries = insertScheduleEntry(schedule.Entries,
ScheduleEntry{endTime, endEntry.EndTime,
endEntry.Interval, FREE, 0, 0}, startSlotIdx)
endEntry.Interval, FREE, "0", 0}, startSlotIdx)
} else {
schedule.Entries = insertScheduleEntry(schedule.Entries,
ScheduleEntry{endTime, endEntry.EndTime,
endEntry.Interval, BLOCKED, 0, 0}, startSlotIdx)
endEntry.Interval, BLOCKED, "0", 0}, startSlotIdx)
}
}
} else { //end is 24:00
Expand All @@ -406,19 +409,19 @@ func (event *CalendarEvent) getSchedule(tx *sqlx.Tx, monday time.Time) (err erro
if startEntry.Interval != 0 {
schedule.Entries = insertScheduleEntry(schedule.Entries,
ScheduleEntry{startEntry.StartTime, startTime,
startEntry.Interval, FREE, 0, 0}, startSlotIdx)
startEntry.Interval, FREE, "0", 0}, startSlotIdx)
startSlotIdx++
} else {
schedule.Entries = insertScheduleEntry(schedule.Entries,
ScheduleEntry{startEntry.StartTime, startTime,
startEntry.Interval, BLOCKED, 0, 0}, startSlotIdx)
startEntry.Interval, BLOCKED, "0", 0}, startSlotIdx)
startSlotIdx++
}
}

schedule.Entries = insertScheduleEntry(schedule.Entries,
ScheduleEntry{startTime, "24: 00",
startEntry.Interval, EXCEPTION, 0, 0}, startSlotIdx)
startEntry.Interval, EXCEPTION, "0", 0}, startSlotIdx)

}
} else { //exception start at 00:00
Expand All @@ -429,26 +432,26 @@ func (event *CalendarEvent) getSchedule(tx *sqlx.Tx, monday time.Time) (err erro

schedule.Entries = insertScheduleEntry(schedule.Entries,
ScheduleEntry{"00:00", "24:00",
startEntry.Interval, EXCEPTION, 0, 0}, startSlotIdx)
startEntry.Interval, EXCEPTION, "0", 0}, startSlotIdx)

} else { //exception only starts at 00:00
endTime := getExceptionScheduleTimes(endEntry.Interval,
end, event.ExceptionsOfWeek[eIdx].ExceptionEndDB, false)

schedule.Entries = insertScheduleEntry(schedule.Entries,
ScheduleEntry{"00:00", endTime,
startEntry.Interval, EXCEPTION, 0, 0}, startSlotIdx)
startEntry.Interval, EXCEPTION, "0", 0}, startSlotIdx)

//insert the entry slice after the exception, FREE or BLOCKED
if endTime != endEntry.EndTime {
if endEntry.Interval != 0 {
schedule.Entries = insertScheduleEntry(schedule.Entries,
ScheduleEntry{endTime, endEntry.EndTime,
endEntry.Interval, FREE, 0, 0}, startSlotIdx)
endEntry.Interval, FREE, "0", 0}, startSlotIdx)
} else {
schedule.Entries = insertScheduleEntry(schedule.Entries,
ScheduleEntry{endTime, endEntry.EndTime,
endEntry.Interval, BLOCKED, 0, 0}, startSlotIdx+1)
endEntry.Interval, BLOCKED, "0", 0}, startSlotIdx+1)
}
}
}
Expand Down Expand Up @@ -487,7 +490,8 @@ func parseDate(tx *sqlx.Tx, year, date, str string) (t time.Time, err error) {

t, err = time.ParseInLocation("2006-01-02T15:04:05", value, loc)
if err != nil {
log.Error("failed to parse string to time", "value", value, "error", err.Error())
log.Error("failed to parse string to time", "value", value, "loc", loc,
"error", err.Error())
tx.Rollback()
return
}
Expand Down
2 changes: 1 addition & 1 deletion app/models/day_tmpls.go
Expand Up @@ -40,7 +40,7 @@ type ScheduleEntry struct {
Interval int
Type ScheduleEntryType

UserID int
UserID string
SlotID int
}

Expand Down
1 change: 0 additions & 1 deletion app/models/slots.go
Expand Up @@ -306,7 +306,6 @@ const (
SELECT start_time
FROM slots
WHERE id = $1
AS start_time
`

stmtSelectAllSlotsOfDayTemplate = `
Expand Down
30 changes: 16 additions & 14 deletions app/views/course/calendarEvent.html
Expand Up @@ -96,8 +96,8 @@
<div class="col-sm-1 text-left">
<a href="#no-scroll" class="badge btn-outline-darkblue"
onclick='renderCalendarEvent({{.event.ID}}, {{.event.CourseID}},
-1, "TODO", {{url "Course.CalendarEvent"}});'
title='TODO'>
-1, {{.event.Monday}}, {{url "Course.CalendarEvent"}});'
title='{{msg $ "title.shift.previous"}}'>
{{template "icons/caretLeft.html" .}}
</a>
</div>
Expand All @@ -111,8 +111,8 @@
<div class="col-sm-1 text-right">
<a href="#no-scroll" class="badge btn-outline-darkblue"
onclick='renderCalendarEvent({{.event.ID}}, {{.event.CourseID}},
1, "TODO", {{url "Course.CalendarEvent"}});'
title='TODO'>
1, {{.event.Monday}}, {{url "Course.CalendarEvent"}});'
title='{{msg $ "title.shift.next"}}'>
{{template "icons/caretRight.html" .}}
</a>
</div>
Expand Down Expand Up @@ -219,7 +219,7 @@

{{$ownSlot := false}}
{{if $.session.userID}}
{{if ne $.session.userID "0"}} <!-- TODO -->
{{if eq $.session.userID .UserID}}
{{$ownSlot = true}}
{{end}}
{{end}}
Expand All @@ -229,7 +229,7 @@
<div class="card-body p-1 text-center">
{{.StartTime}} - {{.EndTime}} {{msg $ "course.clock"}} <br>
<a class="btn btn-outline-light btn-sm" style="color:#004085; border-color: #004085"
href='{{url "Enrollment.UnsubscribeFromSlot" 0}}'>
href='{{url "Enrollment.UnsubscribeFromSlot" .SlotID}}'>
{{msg $ "button.unsubscribe"}}
</a>
</div>
Expand Down Expand Up @@ -263,14 +263,16 @@
{{end}}

{{if $freeSlot}}
<br>
<div class="text-center">
<button type="button" class="btn btn-success w-100"
onclick='bookSlotModal({{$.event.ID}}, {{.Date}}, {{$.event.Year}});'>
{{msg $ "button.book.slot"}}
</button>
</div>
<br>
{{if not $v.InPast}}
<br>
<div class="text-center">
<button type="button" class="btn btn-success w-100"
onclick='bookSlotModal({{$.event.ID}}, {{.Date}}, {{$.event.Year}});'>
{{msg $ "button.book.slot"}}
</button>
</div>
<br>
{{end}}
{{end}}
</div>
{{end}}
Expand Down
3 changes: 3 additions & 0 deletions messages/components.de
Expand Up @@ -265,3 +265,6 @@ title.edit.validate = Kursfelder validieren
title.edit.activate = Kurs aktivieren
title.edit.expire = Kurs auf abgelaufen setzen
title.edit.course = Kurs bearbeiten

title.shift.previous = Vorherige Woche
title.shift.next = Nächste Woche
3 changes: 3 additions & 0 deletions messages/components.en
Expand Up @@ -265,3 +265,6 @@ title.edit.validate = Validate course fields
title.edit.activate = Activate course
title.edit.expire = Change course status to expired
title.edit.course = Edit course

title.shift.left = Previous week
title.shift.right = Next week

0 comments on commit 0cb1240

Please sign in to comment.