/
invigilation.go
249 lines (211 loc) · 6.84 KB
/
invigilation.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
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
package db
import (
"context"
"fmt"
"time"
"github.com/obcode/plexams.go/graph/model"
"github.com/rs/zerolog/log"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options"
)
func (db *DB) GetInvigilatorInSlot(ctx context.Context, roomname string, day, slot int) (*model.Teacher, error) {
invigilations, err := db.GetInvigilationInSlot(ctx, roomname, day, slot)
if err != nil {
log.Error().Err(err).Str("room", roomname).Int("day", day).Int("slot", slot).Msg("cannot get invigilations")
return nil, err
}
if len(invigilations) > 1 {
log.Error().Str("room", roomname).Int("day", day).Int("slot", slot).
Interface("invigilations", invigilations).
Msg("found more than one invigilation")
return nil, fmt.Errorf("found more than one invigilation")
}
if len(invigilations) == 0 {
return nil, nil
}
return db.GetTeacher(ctx, invigilations[0].InvigilatorID)
}
func (db *DB) GetInvigilationInSlot(ctx context.Context, roomname string, day, slot int) ([]*model.Invigilation, error) {
invigilations, err := db.getInvigilationInSlot(ctx, collectionSelfInvigilations, roomname, day, slot)
if err != nil {
log.Error().Err(err).Str("room", roomname).Int("day", day).Int("slot", slot).Msg("cannot get self invigilations")
return nil, err
}
other, err := db.getInvigilationInSlot(ctx, collectionOtherInvigilations, roomname, day, slot)
if err != nil {
log.Error().Err(err).Str("room", roomname).Int("day", day).Int("slot", slot).Msg("cannot get other invigilations")
return nil, err
}
return append(invigilations, other...), nil
}
func (db *DB) getInvigilationInSlot(ctx context.Context, collectionName, roomname string, day, slot int) ([]*model.Invigilation, error) {
collection := db.getCollectionSemester(collectionName)
var filter primitive.M
if roomname == "reserve" {
filter = bson.M{
"$and": []bson.M{
{"roomname": nil},
{"isreserve": true},
{"slot.daynumber": day},
{"slot.slotnumber": slot},
},
}
} else {
filter = bson.M{
"$and": []bson.M{
{"roomname": roomname},
{"isreserve": false},
{"slot.daynumber": day},
{"slot.slotnumber": slot},
},
}
}
cur, err := collection.Find(ctx, filter)
if err != nil {
log.Error().Err(err).Str("collection", collectionName).Msg("Cannot find")
return nil, err
}
invigilations := make([]*model.Invigilation, 0)
err = cur.All(ctx, &invigilations)
if err != nil {
log.Error().Err(err).Str("collection", collectionRooms).Msg("Cannot decode to rooms for exams")
return nil, err
}
return invigilations, nil
}
func (db *DB) InvigilationsForInvigilator(ctx context.Context, invigilatorID int) ([]*model.Invigilation, error) {
self, err := db.invigilationsForInvigilator(ctx, collectionSelfInvigilations, invigilatorID)
if err != nil {
return nil, err
}
other, err := db.invigilationsForInvigilator(ctx, collectionOtherInvigilations, invigilatorID)
if err != nil {
return nil, err
}
return append(self, other...), nil
}
func (db *DB) invigilationsForInvigilator(ctx context.Context, collectionName string, invigilatorID int) ([]*model.Invigilation, error) {
collection := db.getCollectionSemester(collectionName)
cur, err := collection.Find(ctx, bson.D{{Key: "invigilatorid", Value: invigilatorID}})
if err != nil {
log.Error().Err(err).Str("collection", collectionSelfInvigilations).Msg("MongoDB Find")
return nil, err
}
defer cur.Close(ctx)
invigilations := make([]*model.Invigilation, 0)
err = cur.All(ctx, &invigilations)
if err != nil {
log.Error().Err(err).Str("collection", collectionSelfInvigilations).Msg("Cannot decode to rooms")
return nil, err
}
return invigilations, nil
}
func (db *DB) GetAllInvigilations(ctx context.Context) ([]*model.Invigilation, error) {
selfInvigilations, err := db.GetSelfInvigilations(ctx)
if err != nil {
log.Error().Err(err).Msg("cannot get self invigilations")
return nil, err
}
otherInvigilations, err := db.GetOtherInvigilations(ctx)
if err != nil {
log.Error().Err(err).Msg("cannot get other invigilations")
return nil, err
}
return append(selfInvigilations, otherInvigilations...), nil
}
func (db *DB) GetSelfInvigilations(ctx context.Context) ([]*model.Invigilation, error) {
collection := db.getCollectionSemester(collectionSelfInvigilations)
cur, err := collection.Find(ctx, bson.M{})
if err != nil {
log.Error().Err(err).Msg("cannot get invgilations")
return nil, err
}
defer cur.Close(ctx)
invigilations := make([]*model.Invigilation, 0)
err = cur.All(ctx, &invigilations)
if err != nil {
log.Error().Err(err).Msg("cannot get decode invgilations")
return nil, err
}
return invigilations, nil
}
func (db *DB) GetOtherInvigilations(ctx context.Context) ([]*model.Invigilation, error) {
collection := db.getCollectionSemester(collectionOtherInvigilations)
cur, err := collection.Find(ctx, bson.M{})
if err != nil {
log.Error().Err(err).Msg("cannot get invgilations")
return nil, err
}
defer cur.Close(ctx)
invigilations := make([]*model.Invigilation, 0)
err = cur.All(ctx, &invigilations)
if err != nil {
log.Error().Err(err).Msg("cannot get decode invgilations")
return nil, err
}
return invigilations, nil
}
func (db *DB) AddInvigilation(ctx context.Context, room string, day, slot, invigilatorID int) error {
collection := db.getCollectionSemester(collectionOtherInvigilations)
var filter primitive.M
// default is reserve
duration := 60 // FIXME: Reserve counts 60
isReserve := true
var roomname *string
if room == "reserve" {
filter = bson.M{
"$and": []bson.M{
{"roomname": nil},
{"isreserve": true},
{"slot.daynumber": day},
{"slot.slotnumber": slot},
},
}
} else {
filter = bson.M{
"$and": []bson.M{
{"roomname": roomname},
{"isreserve": false},
{"slot.daynumber": day},
{"slot.slotnumber": slot},
},
}
duration = db.getMaxDurationForRoomInSlot(ctx, room, day, slot)
isReserve = false
roomname = &room
}
opts := options.Replace().SetUpsert(true)
_, err := collection.ReplaceOne(ctx, filter,
model.Invigilation{
RoomName: roomname,
Duration: duration,
InvigilatorID: invigilatorID,
Slot: &model.Slot{
DayNumber: day,
SlotNumber: slot,
Starttime: time.Time{},
},
IsReserve: isReserve,
IsSelfInvigilation: false,
},
opts)
if err != nil {
log.Error().Err(err).Str("room", room).Int("day", day).Int("slot", slot).Int("invigilator-id", invigilatorID).
Msg("cannot add invigilation")
return err
}
return nil
}
func (db *DB) getMaxDurationForRoomInSlot(ctx context.Context, roomname string, day, slot int) int {
maxDuration := 0
examsInSlot, _ := db.GetExamsInSlot(ctx, day, slot)
for _, exam := range examsInSlot {
for _, room := range exam.PlannedRooms {
if roomname == room.RoomName && maxDuration < room.Duration {
maxDuration = room.Duration
}
}
}
return maxDuration
}