add
This commit is contained in:
@@ -57,6 +57,11 @@ func AutoMigrate(db *gorm.DB) error {
|
||||
&models.EmailDomain{},
|
||||
&models.PasswordResetToken{},
|
||||
&models.ChatMessage{},
|
||||
&models.ExamRoom{},
|
||||
&models.ExamPaper{},
|
||||
&models.ExamPaperResource{},
|
||||
&models.ExamRoomStudent{},
|
||||
&models.ExamSubmission{},
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
52
server/internal/db/exam_active.go
Normal file
52
server/internal/db/exam_active.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"server/internal/models"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// ActiveExamInfo — phòng thi đang diễn ra cho sinh viên
|
||||
type ActiveExamInfo struct {
|
||||
Room models.ExamRoom
|
||||
Enrollment models.ExamRoomStudent
|
||||
Paper *models.ExamPaper
|
||||
}
|
||||
|
||||
// FindActiveExamForStudent trả về phòng thi nếu sinh viên đang trong khung giờ thi.
|
||||
func FindActiveExamForStudent(db *gorm.DB, studentRkID int64) *ActiveExamInfo {
|
||||
if studentRkID <= 0 {
|
||||
return nil
|
||||
}
|
||||
now := time.Now()
|
||||
var enrollment models.ExamRoomStudent
|
||||
err := db.Table("exam_room_students AS ers").
|
||||
Select("ers.*").
|
||||
Joins("JOIN exam_rooms er ON er.id = ers.exam_room_id").
|
||||
Where("ers.student_rk_id = ? AND er.status = ? AND er.start_time <= ? AND er.end_time >= ?",
|
||||
studentRkID, models.ExamStatusReady, now, now).
|
||||
Order("er.start_time DESC").
|
||||
First(&enrollment).Error
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
var room models.ExamRoom
|
||||
if err := db.First(&room, enrollment.ExamRoomID).Error; err != nil {
|
||||
return nil
|
||||
}
|
||||
info := &ActiveExamInfo{Room: room, Enrollment: enrollment}
|
||||
if enrollment.AssignedPaperID != nil {
|
||||
var paper models.ExamPaper
|
||||
if err := db.First(&paper, *enrollment.AssignedPaperID).Error; err == nil {
|
||||
info.Paper = &paper
|
||||
}
|
||||
}
|
||||
return info
|
||||
}
|
||||
|
||||
// IsStudentInActiveExam kiểm tra nhanh sinh viên có đang thi không.
|
||||
func IsStudentInActiveExam(db *gorm.DB, studentRkID int64) bool {
|
||||
return FindActiveExamForStudent(db, studentRkID) != nil
|
||||
}
|
||||
63
server/internal/db/exam_status.go
Normal file
63
server/internal/db/exam_status.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"server/internal/models"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// ExamDisplayStatus trạng thái hiển thị (tính từ DB status + thời gian).
|
||||
func ExamDisplayStatus(room models.ExamRoom, now time.Time) string {
|
||||
switch room.Status {
|
||||
case models.ExamStatusCancelled:
|
||||
return models.ExamStatusCancelled
|
||||
case models.ExamStatusEnded:
|
||||
return models.ExamStatusEnded
|
||||
case models.ExamStatusDraft:
|
||||
return models.ExamStatusDraft
|
||||
case models.ExamStatusReady:
|
||||
if now.After(room.EndTime) {
|
||||
return models.ExamStatusEnded
|
||||
}
|
||||
if !now.Before(room.StartTime) {
|
||||
return "active"
|
||||
}
|
||||
return models.ExamStatusReady
|
||||
default:
|
||||
return models.ExamStatusDraft
|
||||
}
|
||||
}
|
||||
|
||||
func ExamRoomEditable(room models.ExamRoom) bool {
|
||||
return room.Status == models.ExamStatusDraft
|
||||
}
|
||||
|
||||
// ExamRoomPrepEditable — chỉnh gói đề, sinh viên trước khi thi bắt đầu.
|
||||
func ExamRoomPrepEditable(room models.ExamRoom, now time.Time) bool {
|
||||
if room.Status == models.ExamStatusDraft {
|
||||
return true
|
||||
}
|
||||
return room.Status == models.ExamStatusReady && now.Before(room.StartTime)
|
||||
}
|
||||
|
||||
func ExamRoomCanUnpublish(room models.ExamRoom, now time.Time) bool {
|
||||
return room.Status == models.ExamStatusReady && now.Before(room.StartTime)
|
||||
}
|
||||
|
||||
func ExamRoomIsLive(room models.ExamRoom, now time.Time) bool {
|
||||
return ExamDisplayStatus(room, now) == "active"
|
||||
}
|
||||
|
||||
func ExamRoomCanCancel(room models.ExamRoom, now time.Time) bool {
|
||||
return ExamRoomIsLive(room, now)
|
||||
}
|
||||
|
||||
// ProcessExamRoomLifecycle đánh dấu phòng ready đã quá giờ kết thúc.
|
||||
func ProcessExamRoomLifecycle(db *gorm.DB) {
|
||||
now := time.Now()
|
||||
_ = db.Model(&models.ExamRoom{}).
|
||||
Where("status = ? AND end_time < ?", models.ExamStatusReady, now).
|
||||
Update("status", models.ExamStatusEnded).Error
|
||||
}
|
||||
Reference in New Issue
Block a user