Sessions vollständig implementiert
This commit is contained in:
@ -15,7 +15,7 @@ func HandleInternalLogin(db *data.DB, ss *[]*Session, cs chan<- *Session) http.H
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
instructors, err := db.GetInstructors()
|
||||
if err != nil {
|
||||
http.Error(w, "HandleInternalLogin: db.GetInstructors(): "+fmt.Sprint(err), http.StatusInternalServerError)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
log.Panicln(err)
|
||||
}
|
||||
|
||||
@ -24,7 +24,7 @@ func HandleInternalLogin(db *data.DB, ss *[]*Session, cs chan<- *Session) http.H
|
||||
session := new(Session)
|
||||
session.ID = uuid.New()
|
||||
session.Briefing = new(data.Briefing)
|
||||
session.Briefing.InstructorID = i.ID
|
||||
session.InstructorID = i.ID
|
||||
(*ss) = append((*ss), session)
|
||||
cs <- session
|
||||
|
||||
@ -32,7 +32,7 @@ func HandleInternalLogin(db *data.DB, ss *[]*Session, cs chan<- *Session) http.H
|
||||
data.SessionID = session.ID
|
||||
data.OTD, err = db.GetAllOverviewTableData()
|
||||
if err != nil {
|
||||
http.Error(w, "displayTable: *DB.GetAllOverviewTableData(): "+fmt.Sprint(err), http.StatusInternalServerError)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
log.Panicln(err)
|
||||
}
|
||||
|
||||
@ -44,42 +44,51 @@ func HandleInternalLogin(db *data.DB, ss *[]*Session, cs chan<- *Session) http.H
|
||||
}
|
||||
}
|
||||
|
||||
func HandleSearch(db *data.DB) http.HandlerFunc {
|
||||
func (s *Session) HandleSearch(db *data.DB) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
bs, err := db.GetOverviewTableDataByName(r.PostFormValue("search"))
|
||||
log.Println("hier")
|
||||
var err error
|
||||
data := tableHTMLData{}
|
||||
|
||||
data.SessionID = s.ID
|
||||
data.OTD, err = db.GetOverviewTableDataByName(r.PostFormValue("search"))
|
||||
if err != nil {
|
||||
http.Error(w, "DisplayResults: db.ReadByName(r.PostFormValue()): "+fmt.Sprint(err), http.StatusInternalServerError)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
log.Panicln(err)
|
||||
}
|
||||
template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "rows", bs)
|
||||
template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "rows", data)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Session) HandleNewBriefing() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
data := new(briefingHTMLData)
|
||||
data := new(participantHTMLData)
|
||||
data.SessionID = s.ID
|
||||
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "content", data)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Session) HandleNewParticipant(cp chan<- *data.Participant) http.HandlerFunc {
|
||||
func (s *Session) HandleNewParticipant(cp chan<- *BriefingParticipant) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var err error
|
||||
p := new(data.Participant)
|
||||
p := new(BriefingParticipant)
|
||||
p.Participant = new(data.Participant)
|
||||
p.NoIncorrect = -1
|
||||
p.AllowRetry = false
|
||||
|
||||
p.Login, err = generateLogin()
|
||||
if err != nil {
|
||||
http.Error(w, "AddParticipant: generateLogin(): "+fmt.Sprint(err), http.StatusInternalServerError)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
log.Panicln(err)
|
||||
}
|
||||
s.Participants = append(s.Participants, p)
|
||||
cp <- p
|
||||
|
||||
data := new(briefingHTMLData)
|
||||
data := new(participantHTMLData)
|
||||
data.SessionID = s.ID
|
||||
data.Login = p.Login
|
||||
if err != nil {
|
||||
http.Error(w, "AddParticipant: generateLogin(): "+fmt.Sprint(err), http.StatusInternalServerError)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
log.Panicln(err)
|
||||
}
|
||||
|
||||
@ -90,27 +99,28 @@ func (s *Session) HandleNewParticipant(cp chan<- *data.Participant) http.Handler
|
||||
func (s *Session) HandleBriefingForm(db *data.DB) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
now := time.Now()
|
||||
var err error
|
||||
|
||||
s.Briefing.Date = now.Format("2006-01-02")
|
||||
s.Briefing.Time = now.Format("15:04:05")
|
||||
s.Briefing.Location = r.PostFormValue("location")
|
||||
s.Briefing.DocumentName = r.PostFormValue("document")
|
||||
s.Briefing.AsOf = r.PostFormValue("as-of")
|
||||
s.Date = now.Format("2006-01-02")
|
||||
s.Time = now.Format("15:04:05")
|
||||
s.Location = r.PostFormValue("location")
|
||||
s.DocumentName = r.PostFormValue("document")
|
||||
s.AsOf = r.PostFormValue("as-of")
|
||||
|
||||
err = db.WriteBriefing(s.Briefing)
|
||||
err := db.WriteBriefing(s.Briefing)
|
||||
if err != nil {
|
||||
http.Error(w, "SubmitBriefingForm: db.WriteBriefing(): "+fmt.Sprint(err), http.StatusInternalServerError)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
log.Panicln(err)
|
||||
}
|
||||
|
||||
bs, err := db.GetAllOverviewTableData()
|
||||
if err != nil {
|
||||
http.Error(w, "displayTable: *DB.GetAllOverviewTableData(): "+fmt.Sprint(err), http.StatusInternalServerError)
|
||||
log.Panicln(err)
|
||||
data := new(summaryHTMLData)
|
||||
data.SessionID = s.ID
|
||||
data.ParticipantsData = make([]participantHTMLData, len(s.Participants))
|
||||
for i, p := range s.Participants {
|
||||
data.ParticipantsData[i].SessionID = s.ID
|
||||
data.ParticipantsData[i].BriefingParticipant = *p
|
||||
}
|
||||
|
||||
template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "content", bs)
|
||||
template.Must(template.ParseFiles("templates/summary.html")).ExecuteTemplate(w, "content", data)
|
||||
}
|
||||
}
|
||||
|
||||
@ -129,64 +139,64 @@ func HandleExternalLogin(ss *[]*Session) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Session) HandleParticipant(db *data.DB, p *data.Participant, sq *[]data.Question) http.HandlerFunc {
|
||||
func (s *Session) HandleParticipant(db *data.DB, p *BriefingParticipant) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
p.FirstName = r.PostFormValue("first-" + fmt.Sprint(p.Login))
|
||||
p.LastName = r.PostFormValue("last-" + fmt.Sprint(p.Login))
|
||||
p.Company = r.PostFormValue("company-" + fmt.Sprint(p.Login))
|
||||
|
||||
err := db.WriteParticipant(p)
|
||||
err := db.WriteParticipant(p.Participant)
|
||||
if err != nil {
|
||||
http.Error(w, "DisplayQuestion: db.WriteParticipant(): "+fmt.Sprint(err), http.StatusInternalServerError)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
log.Panicln(err)
|
||||
}
|
||||
|
||||
data := new(questionHTMLData)
|
||||
data.SessionID = s.ID
|
||||
data.Login = p.Login
|
||||
data.Question = (*sq)[0]
|
||||
data.Question = s.Questions[0]
|
||||
data.QuestionID = 1
|
||||
|
||||
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Session) HandleAnswer(db *data.DB, p *SessionParticipant, sq *[]data.Question, i int64) http.HandlerFunc {
|
||||
func (s *Session) HandleAnswer(db *data.DB, p *BriefingParticipant, i int64) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if i < int64(len(*sq)) {
|
||||
if err := handleGivenAnswer(s, p, i-1, r); err != nil {
|
||||
http.Error(w, "DisplayQuestion: handleGivenAnswer(): "+fmt.Sprint(err), http.StatusInternalServerError)
|
||||
if i < int64(len(s.Questions)) {
|
||||
if err := handleGivenAnswer(p, i-1, r); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
log.Panicln(err)
|
||||
}
|
||||
|
||||
data := new(questionHTMLData)
|
||||
data.SessionID = s.ID
|
||||
data.Login = p.Login
|
||||
data.Question = (*sq)[i]
|
||||
data.Question = s.Questions[i]
|
||||
data.QuestionID = i + 1
|
||||
|
||||
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
|
||||
} else {
|
||||
if err := handleGivenAnswer(s, p, i-1, r); err != nil {
|
||||
http.Error(w, "DisplayTestResults: handleGivenAnswer(): "+fmt.Sprint(err), http.StatusInternalServerError)
|
||||
if err := handleGivenAnswer(p, i-1, r); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
log.Panicln(err)
|
||||
}
|
||||
log.Println(p.GivenAnswers)
|
||||
|
||||
data := new(resultHTMLData)
|
||||
data.SessionID = s.ID
|
||||
data.Login = p.Login
|
||||
data.Incorrect = 0
|
||||
data.Questions = makeHTMLQuestions(s.Questions, p.GivenAnswers)
|
||||
for i, q := range *sq {
|
||||
p.NoIncorrect = 0
|
||||
for i, q := range s.Questions {
|
||||
if p.GivenAnswers[i] != q.Correct {
|
||||
data.Incorrect++
|
||||
p.NoIncorrect++
|
||||
}
|
||||
}
|
||||
|
||||
if data.Incorrect == 0 {
|
||||
data := new(resultHTMLData)
|
||||
data.SessionID = s.ID
|
||||
data.BriefingParticipant = *p
|
||||
data.Questions = makeHTMLQuestions(s.Questions, p.GivenAnswers)
|
||||
|
||||
if data.NoIncorrect == 0 {
|
||||
if err := db.WriteGivenAnswers(*s.Briefing, *p.Participant, s.Questions, p.GivenAnswers); err != nil {
|
||||
http.Error(w, "HandleAnswer: db.WriteGivenAnswer(): "+fmt.Sprint(err), http.StatusInternalServerError)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
log.Panicln(err)
|
||||
}
|
||||
}
|
||||
@ -196,16 +206,49 @@ func (s *Session) HandleAnswer(db *data.DB, p *SessionParticipant, sq *[]data.Qu
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Session) HandleRetry(p *data.Participant, sq *[]data.Question, i *int) http.HandlerFunc {
|
||||
func (s *Session) HandleAllowRetry(p *BriefingParticipant) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
(*i) = 0
|
||||
|
||||
data := new(questionHTMLData)
|
||||
data.SessionID = s.ID
|
||||
data.Login = p.Login
|
||||
data.Question = (*sq)[*i]
|
||||
data.QuestionID = int64(*i + 1)
|
||||
|
||||
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
|
||||
p.NoIncorrect = -1
|
||||
p.AllowRetry = true
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Session) HandleRetry(p *BriefingParticipant, i *int) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if p.AllowRetry {
|
||||
p.AllowRetry = false
|
||||
(*i) = 0
|
||||
|
||||
data := new(questionHTMLData)
|
||||
data.SessionID = s.ID
|
||||
data.Login = p.Login
|
||||
data.Question = s.Questions[*i]
|
||||
data.QuestionID = int64(*i + 1)
|
||||
|
||||
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
|
||||
} else {
|
||||
data := new(resultHTMLData)
|
||||
data.SessionID = s.ID
|
||||
data.BriefingParticipant = *p
|
||||
data.Questions = makeHTMLQuestions(s.Questions, p.GivenAnswers)
|
||||
|
||||
template.Must(template.ParseFiles("templates/result.html")).ExecuteTemplate(w, "content", data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Session) HandleRefresh(p *BriefingParticipant) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
data := new(participantHTMLData)
|
||||
data.SessionID = s.ID
|
||||
data.BriefingParticipant = *p
|
||||
|
||||
template.Must(template.ParseFiles("templates/summary.html")).ExecuteTemplate(w, "participant", data)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Session) HandleBriefingDone() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
template.Must(template.ParseFiles("templates/login.html")).ExecuteTemplate(w, "content", nil)
|
||||
}
|
||||
}
|
||||
|
@ -10,32 +10,17 @@ import (
|
||||
"streifling.com/jason/sicherheitsunterweisung/packages/data"
|
||||
)
|
||||
|
||||
func (mux *Mux) handleParticipants(db *data.DB, cp <-chan *data.Participant, s *Session) {
|
||||
for p := range cp {
|
||||
sessionParticipant := new(SessionParticipant)
|
||||
sessionParticipant.Participant = p
|
||||
sessionParticipant.GivenAnswers = make([]int, len(s.Questions))
|
||||
|
||||
mux.HandleFunc("/submit-participant/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/", s.HandleParticipant(db, p, &s.Questions))
|
||||
var i int
|
||||
for i = range s.Questions {
|
||||
mux.HandleFunc("/submit-answer/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/"+fmt.Sprint(i+1)+"/", s.HandleAnswer(db, sessionParticipant, &s.Questions, int64(i+1)))
|
||||
}
|
||||
mux.HandleFunc("/retry/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/", s.HandleRetry(p, &s.Questions, &i))
|
||||
}
|
||||
}
|
||||
|
||||
func generateLogin() (string, error) {
|
||||
bs := make([]byte, 4)
|
||||
|
||||
if _, err := rand.Read(bs); err != nil {
|
||||
return "", fmt.Errorf("generateLogin: rand.Read(bs): %v\n", err)
|
||||
return "", fmt.Errorf("error: generateLogin: rand.Read(bs): %v", err)
|
||||
}
|
||||
|
||||
return hex.EncodeToString(bs), nil
|
||||
}
|
||||
|
||||
func findCorrectLogin(l string, ss *[]*Session) (*Session, *data.Participant, bool) {
|
||||
func findCorrectLogin(l string, ss *[]*Session) (*Session, *BriefingParticipant, bool) {
|
||||
for _, session := range *ss {
|
||||
for _, p := range session.Participants {
|
||||
if l == p.Login {
|
||||
@ -52,31 +37,31 @@ func newParticipant(l string) (*data.Participant, error) {
|
||||
|
||||
p.ID, err = strconv.ParseInt(l, 10, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("newParticipant: strconv.Atoi(idString): %v\n", err)
|
||||
return nil, fmt.Errorf("error: newParticipant: strconv.Atoi(): %v", err)
|
||||
}
|
||||
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func handleGivenAnswer(s *Session, p *SessionParticipant, i int64, r *http.Request) error {
|
||||
func handleGivenAnswer(p *BriefingParticipant, i int64, r *http.Request) error {
|
||||
answer, err := strconv.Atoi(r.PostFormValue("answer"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("handleGivenAnswer: strconv.Atoi(): %v\n", err)
|
||||
return fmt.Errorf("error: handleGivenAnswer: strconv.Atoi(): %v", err)
|
||||
}
|
||||
|
||||
p.GivenAnswers[i] = answer
|
||||
return nil
|
||||
}
|
||||
|
||||
func makeHTMLQuestions(sq []data.Question, givenAnswers []int) []htmlQuestion {
|
||||
questions := make([]htmlQuestion, 0)
|
||||
func makeHTMLQuestions(sq []data.Question, givenAnswers []int) []resultQuestion {
|
||||
questions := make([]resultQuestion, 0)
|
||||
for i, q := range sq {
|
||||
question := new(htmlQuestion)
|
||||
question := new(resultQuestion)
|
||||
question.Text = q.Text
|
||||
|
||||
question.Answers = make([]htmlAnswer, 0)
|
||||
question.Answers = make([]resultAnswer, 0)
|
||||
for j, a := range q.Answers {
|
||||
answer := new(htmlAnswer)
|
||||
answer := new(resultAnswer)
|
||||
answer.Text = a.Text
|
||||
|
||||
if j+1 == q.Correct {
|
||||
|
@ -10,37 +10,34 @@ type tableHTMLData struct {
|
||||
OTD []data.OverviewTableData
|
||||
}
|
||||
|
||||
type briefingHTMLData struct {
|
||||
SessionID uuid.UUID
|
||||
Login string
|
||||
}
|
||||
|
||||
type participantHTMLData struct {
|
||||
SessionID uuid.UUID
|
||||
Login string
|
||||
BriefingParticipant
|
||||
}
|
||||
|
||||
type questionHTMLData struct {
|
||||
SessionID uuid.UUID
|
||||
Login string
|
||||
participantHTMLData
|
||||
Question data.Question
|
||||
QuestionID int64
|
||||
}
|
||||
|
||||
type htmlAnswer struct {
|
||||
type resultAnswer struct {
|
||||
Text string
|
||||
Correct bool
|
||||
Chosen bool
|
||||
}
|
||||
|
||||
type htmlQuestion struct {
|
||||
type resultQuestion struct {
|
||||
Text string
|
||||
Answers []htmlAnswer
|
||||
Answers []resultAnswer
|
||||
}
|
||||
|
||||
type resultHTMLData struct {
|
||||
SessionID uuid.UUID
|
||||
Login string
|
||||
Questions []htmlQuestion
|
||||
Incorrect int
|
||||
participantHTMLData
|
||||
Questions []resultQuestion
|
||||
}
|
||||
|
||||
type summaryHTMLData struct {
|
||||
SessionID uuid.UUID
|
||||
ParticipantsData []participantHTMLData
|
||||
}
|
||||
|
@ -8,6 +8,21 @@ import (
|
||||
"streifling.com/jason/sicherheitsunterweisung/packages/data"
|
||||
)
|
||||
|
||||
func (mux *Mux) handleParticipants(db *data.DB, cp <-chan *BriefingParticipant, s *Session) {
|
||||
for p := range cp {
|
||||
p.GivenAnswers = make([]int, len(s.Questions))
|
||||
|
||||
mux.HandleFunc("/submit-participant/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/", s.HandleParticipant(db, p))
|
||||
var i int
|
||||
for i = range s.Questions {
|
||||
mux.HandleFunc("/submit-answer/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/"+fmt.Sprint(i+1)+"/", s.HandleAnswer(db, p, int64(i+1)))
|
||||
}
|
||||
mux.HandleFunc("/allow-retry/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/", s.HandleAllowRetry(p))
|
||||
mux.HandleFunc("/retry/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/", s.HandleRetry(p, &i))
|
||||
mux.HandleFunc("/refresh-summary/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/", s.HandleRefresh(p))
|
||||
}
|
||||
}
|
||||
|
||||
func NewMux() *Mux {
|
||||
mux := new(Mux)
|
||||
mux.ServeMux = http.NewServeMux()
|
||||
@ -17,7 +32,7 @@ func NewMux() *Mux {
|
||||
func (mux *Mux) HandleSessions(db *data.DB, cs <-chan *Session, ss *[]*Session) {
|
||||
for s := range cs {
|
||||
(*ss) = append(*ss, s)
|
||||
participantChan := make(chan *data.Participant)
|
||||
participantChan := make(chan *BriefingParticipant)
|
||||
questionIDs := make([]string, 4)
|
||||
|
||||
for i := 0; i < len(questionIDs); i++ {
|
||||
@ -30,9 +45,11 @@ func (mux *Mux) HandleSessions(db *data.DB, cs <-chan *Session, ss *[]*Session)
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
mux.HandleFunc("/search/"+fmt.Sprint(s.ID)+"/", s.HandleSearch(db))
|
||||
mux.HandleFunc("/new-briefing/"+fmt.Sprint(s.ID)+"/", s.HandleNewBriefing())
|
||||
mux.HandleFunc("/new-participant/"+fmt.Sprint(s.ID)+"/", s.HandleNewParticipant(participantChan))
|
||||
mux.HandleFunc("/submit-form/"+fmt.Sprint(s.ID)+"/", s.HandleBriefingForm(db))
|
||||
mux.HandleFunc("/briefing-done/"+fmt.Sprint(s.ID)+"/", s.HandleBriefingDone())
|
||||
|
||||
go mux.handleParticipants(db, participantChan, s)
|
||||
}
|
||||
|
@ -1,8 +1,9 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"streifling.com/jason/sicherheitsunterweisung/packages/data"
|
||||
)
|
||||
|
||||
@ -10,14 +11,17 @@ type Mux struct {
|
||||
*http.ServeMux
|
||||
}
|
||||
|
||||
type BriefingParticipant struct {
|
||||
*data.Participant
|
||||
Login string
|
||||
GivenAnswers []int
|
||||
NoIncorrect int
|
||||
AllowRetry bool
|
||||
}
|
||||
|
||||
type Session struct {
|
||||
ID uuid.UUID
|
||||
*data.Briefing
|
||||
Participants []*data.Participant
|
||||
Participants []*BriefingParticipant
|
||||
Questions []data.Question
|
||||
}
|
||||
|
||||
type SessionParticipant struct {
|
||||
*data.Participant
|
||||
GivenAnswers []int
|
||||
}
|
||||
|
Reference in New Issue
Block a user