structs und funcs privat gemacht, wenn möglich
This commit is contained in:
parent
c607b837b9
commit
920c9b8ef3
@ -24,11 +24,11 @@ import (
|
|||||||
"streifling.com/jason/sicherheitsunterweisung/packages/data"
|
"streifling.com/jason/sicherheitsunterweisung/packages/data"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Briefing struct {
|
type briefing struct {
|
||||||
*data.Briefing
|
*data.Briefing
|
||||||
UUID uuid.UUID
|
uuid uuid.UUID
|
||||||
Participants []*Participant
|
participants []*participant
|
||||||
Questions []data.Question
|
questions []data.Question
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateLogin() (string, error) {
|
func generateLogin() (string, error) {
|
||||||
@ -41,33 +41,33 @@ func generateLogin() (string, error) {
|
|||||||
return hex.EncodeToString(bs), nil
|
return hex.EncodeToString(bs), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Briefing) HandleNewParticipant(cp chan<- *Participant) http.HandlerFunc {
|
func (b *briefing) handleNewParticipant(cp chan<- *participant) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
var err error
|
var err error
|
||||||
participant := Participant{
|
participant := participant{
|
||||||
Participant: new(data.Participant),
|
Participant: new(data.Participant),
|
||||||
NoIncorrect: -1,
|
numberIncorrect: -1,
|
||||||
AllowRetry: false,
|
allowRetry: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
participant.Login, err = generateLogin()
|
participant.login, err = generateLogin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
log.Panicln(err)
|
log.Panicln(err)
|
||||||
}
|
}
|
||||||
b.Participants = append(b.Participants, &participant)
|
b.participants = append(b.participants, &participant)
|
||||||
cp <- &participant
|
cp <- &participant
|
||||||
|
|
||||||
data := participantHTMLData{
|
data := participantHTMLData{
|
||||||
BriefingID: b.UUID,
|
briefingID: b.uuid,
|
||||||
Participant: Participant{Login: participant.Login},
|
participant: participant,
|
||||||
}
|
}
|
||||||
|
|
||||||
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "new", data)
|
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "new", data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Briefing) HandleBriefingForm(db *data.DB, s *Session) http.HandlerFunc {
|
func (b *briefing) handleBriefingForm(db *data.DB, s *Session) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
b.DateTime = time.Now().Format("2006-01-02 15:04:05")
|
b.DateTime = time.Now().Format("2006-01-02 15:04:05")
|
||||||
b.Location = r.PostFormValue("location")
|
b.Location = r.PostFormValue("location")
|
||||||
@ -81,13 +81,13 @@ func (b *Briefing) HandleBriefingForm(db *data.DB, s *Session) http.HandlerFunc
|
|||||||
}
|
}
|
||||||
|
|
||||||
data := summaryHTMLData{
|
data := summaryHTMLData{
|
||||||
SessionID: s.UUID,
|
sessionID: s.uuid,
|
||||||
BriefingID: b.UUID,
|
briefingID: b.uuid,
|
||||||
ParticipantsData: make([]participantHTMLData, len(b.Participants)),
|
participantsData: make([]participantHTMLData, len(b.participants)),
|
||||||
}
|
}
|
||||||
for i, p := range b.Participants {
|
for i, p := range b.participants {
|
||||||
data.ParticipantsData[i].BriefingID = b.UUID
|
data.participantsData[i].briefingID = b.uuid
|
||||||
data.ParticipantsData[i].Participant = *p
|
data.participantsData[i].participant = *p
|
||||||
}
|
}
|
||||||
|
|
||||||
template.Must(template.ParseFiles("templates/summary.html")).ExecuteTemplate(w, "content", data)
|
template.Must(template.ParseFiles("templates/summary.html")).ExecuteTemplate(w, "content", data)
|
||||||
|
@ -17,41 +17,41 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type tableHTMLData struct {
|
type tableHTMLData struct {
|
||||||
SessionID uuid.UUID
|
sessionID uuid.UUID
|
||||||
OTD []data.OverviewTableData
|
otd []data.OverviewTableData
|
||||||
}
|
}
|
||||||
|
|
||||||
type participantHTMLData struct {
|
type participantHTMLData struct {
|
||||||
BriefingID uuid.UUID
|
briefingID uuid.UUID
|
||||||
Participant
|
participant
|
||||||
}
|
}
|
||||||
|
|
||||||
type questionHTMLData struct {
|
type questionHTMLData struct {
|
||||||
participantHTMLData
|
participantHTMLData
|
||||||
QuestionID int64
|
questionID int64
|
||||||
Question data.Question
|
question data.Question
|
||||||
}
|
}
|
||||||
|
|
||||||
type resultAnswer struct {
|
type resultAnswer struct {
|
||||||
ID int64
|
id int64
|
||||||
Text string
|
text string
|
||||||
Correct bool
|
correct bool
|
||||||
Chosen bool
|
chosen bool
|
||||||
IsImage bool // TODO: relocate to sessionStructs if possible
|
isImage bool // TODO: relocate to sessionStructs if possible
|
||||||
}
|
}
|
||||||
|
|
||||||
type resultQuestion struct {
|
type resultQuestion struct {
|
||||||
Text string
|
text string
|
||||||
Answers []resultAnswer
|
answers []resultAnswer
|
||||||
}
|
}
|
||||||
|
|
||||||
type resultHTMLData struct {
|
type resultHTMLData struct {
|
||||||
participantHTMLData
|
participantHTMLData
|
||||||
Questions []resultQuestion
|
questions []resultQuestion
|
||||||
}
|
}
|
||||||
|
|
||||||
type summaryHTMLData struct {
|
type summaryHTMLData struct {
|
||||||
SessionID uuid.UUID
|
sessionID uuid.UUID
|
||||||
BriefingID uuid.UUID
|
briefingID uuid.UUID
|
||||||
ParticipantsData []participantHTMLData
|
participantsData []participantHTMLData
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,19 @@ import (
|
|||||||
"streifling.com/jason/sicherheitsunterweisung/packages/data"
|
"streifling.com/jason/sicherheitsunterweisung/packages/data"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func findCorrectLogin(l string, ss *[]*Session) (*briefing, *participant, bool) {
|
||||||
|
for _, s := range *ss {
|
||||||
|
for _, b := range s.briefings {
|
||||||
|
for _, p := range b.participants {
|
||||||
|
if l == p.login {
|
||||||
|
return b, p, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, nil, false
|
||||||
|
}
|
||||||
|
|
||||||
func HandleInternalLogin(db *data.DB, ss *[]*Session, cs chan<- *Session) http.HandlerFunc {
|
func HandleInternalLogin(db *data.DB, ss *[]*Session, cs chan<- *Session) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
instructors, err := db.GetInstructors()
|
instructors, err := db.GetInstructors()
|
||||||
@ -32,15 +45,15 @@ func HandleInternalLogin(db *data.DB, ss *[]*Session, cs chan<- *Session) http.H
|
|||||||
for _, i := range instructors {
|
for _, i := range instructors {
|
||||||
if r.PostFormValue("login") == fmt.Sprint(i.ID) {
|
if r.PostFormValue("login") == fmt.Sprint(i.ID) {
|
||||||
session := Session{
|
session := Session{
|
||||||
UUID: uuid.New(),
|
uuid: uuid.New(),
|
||||||
Instructor: *i,
|
instructor: *i,
|
||||||
Briefings: make([]*Briefing, 0),
|
briefings: make([]*briefing, 0),
|
||||||
}
|
}
|
||||||
(*ss) = append((*ss), &session)
|
(*ss) = append((*ss), &session)
|
||||||
cs <- &session
|
cs <- &session
|
||||||
|
|
||||||
data := tableHTMLData{SessionID: session.UUID}
|
data := tableHTMLData{sessionID: session.uuid}
|
||||||
data.OTD, err = db.GetAllOverviewTableData()
|
data.otd, err = db.GetAllOverviewTableData()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
log.Panicln(err)
|
log.Panicln(err)
|
||||||
@ -54,26 +67,13 @@ func HandleInternalLogin(db *data.DB, ss *[]*Session, cs chan<- *Session) http.H
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func findCorrectLogin(l string, ss *[]*Session) (*Briefing, *Participant, bool) {
|
|
||||||
for _, s := range *ss {
|
|
||||||
for _, b := range s.Briefings {
|
|
||||||
for _, p := range b.Participants {
|
|
||||||
if l == p.Login {
|
|
||||||
return b, p, true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil, nil, false
|
|
||||||
}
|
|
||||||
|
|
||||||
func HandleExternalLogin(ss *[]*Session) http.HandlerFunc {
|
func HandleExternalLogin(ss *[]*Session) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
briefing, participant, loginCorrect := findCorrectLogin(r.PostFormValue("login"), ss)
|
briefing, participant, loginCorrect := findCorrectLogin(r.PostFormValue("login"), ss)
|
||||||
if loginCorrect {
|
if loginCorrect {
|
||||||
data := participantHTMLData{
|
data := participantHTMLData{
|
||||||
BriefingID: briefing.UUID,
|
briefingID: briefing.uuid,
|
||||||
Participant: Participant{Login: participant.Login},
|
participant: *participant,
|
||||||
}
|
}
|
||||||
|
|
||||||
template.Must(template.ParseFiles("templates/participant.html")).ExecuteTemplate(w, "content", data)
|
template.Must(template.ParseFiles("templates/participant.html")).ExecuteTemplate(w, "content", data)
|
||||||
|
@ -28,23 +28,7 @@ func NewMux() *Mux {
|
|||||||
return &Mux{ServeMux: http.NewServeMux()}
|
return &Mux{ServeMux: http.NewServeMux()}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mux *Mux) handleParticipants(db *data.DB, cp <-chan *Participant, b *Briefing) {
|
func getQuestions(db *data.DB, b *briefing) {
|
||||||
for p := range cp {
|
|
||||||
p.GivenAnswers = make([]int, len(b.Questions))
|
|
||||||
|
|
||||||
mux.HandleFunc("/submit-participant/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.HandleParticipant(db, b))
|
|
||||||
mux.HandleFunc("/end-video/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.HandleEndOfVideo(b))
|
|
||||||
var i int
|
|
||||||
for i = range b.Questions {
|
|
||||||
mux.HandleFunc("/submit-answer/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/"+fmt.Sprint(i+1)+"/", p.HandleAnswer(db, b, int64(i+1)))
|
|
||||||
}
|
|
||||||
mux.HandleFunc("/allow-retry/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.HandleAllowRetry())
|
|
||||||
mux.HandleFunc("/retry/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.HandleRetry(b, &i))
|
|
||||||
mux.HandleFunc("/refresh-summary/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.HandleRefresh(b))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func getQuestions(db *data.DB, b *Briefing) {
|
|
||||||
questionIDs := make([]string, 4)
|
questionIDs := make([]string, 4)
|
||||||
|
|
||||||
for i := 0; i < len(questionIDs); i++ {
|
for i := 0; i < len(questionIDs); i++ {
|
||||||
@ -52,43 +36,59 @@ func getQuestions(db *data.DB, b *Briefing) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
b.Questions, err = db.GetQuestions(questionIDs)
|
b.questions, err = db.GetQuestions(questionIDs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := range b.Questions {
|
for i := range b.questions {
|
||||||
for j := range b.Questions[i].Answers {
|
for j := range b.questions[i].Answers {
|
||||||
if parts := strings.Split(b.Questions[i].Answers[j].Text, ":/"); parts[0] == "file" {
|
if parts := strings.Split(b.questions[i].Answers[j].Text, ":/"); parts[0] == "file" {
|
||||||
b.Questions[i].Answers[j].Text = parts[1]
|
b.questions[i].Answers[j].Text = parts[1]
|
||||||
b.Questions[i].Answers[j].IsImage = true
|
b.questions[i].Answers[j].IsImage = true
|
||||||
} else {
|
} else {
|
||||||
b.Questions[i].Answers[j].IsImage = false
|
b.questions[i].Answers[j].IsImage = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mux *Mux) handleBriefings(db *data.DB, cb <-chan *Briefing, s *Session) {
|
func (mux *Mux) handleParticipants(db *data.DB, cp <-chan *participant, b *briefing) {
|
||||||
participantChan := make(chan *Participant)
|
for p := range cp {
|
||||||
|
p.givenAnswers = make([]int, len(b.questions))
|
||||||
|
|
||||||
|
mux.HandleFunc("/submit-participant/"+fmt.Sprint(b.uuid)+"/"+fmt.Sprint(p.login)+"/", p.handleParticipant(db, b))
|
||||||
|
mux.HandleFunc("/end-video/"+fmt.Sprint(b.uuid)+"/"+fmt.Sprint(p.login)+"/", p.handleEndOfVideo(b))
|
||||||
|
var i int
|
||||||
|
for i = range b.questions {
|
||||||
|
mux.HandleFunc("/submit-answer/"+fmt.Sprint(b.uuid)+"/"+fmt.Sprint(p.login)+"/"+fmt.Sprint(i+1)+"/", p.handleAnswer(db, b, int64(i+1)))
|
||||||
|
}
|
||||||
|
mux.HandleFunc("/allow-retry/"+fmt.Sprint(b.uuid)+"/"+fmt.Sprint(p.login)+"/", p.handleAllowRetry())
|
||||||
|
mux.HandleFunc("/retry/"+fmt.Sprint(b.uuid)+"/"+fmt.Sprint(p.login)+"/", p.handleRetry(b, &i))
|
||||||
|
mux.HandleFunc("/refresh-summary/"+fmt.Sprint(b.uuid)+"/"+fmt.Sprint(p.login)+"/", p.handleRefresh(b))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mux *Mux) handleBriefings(db *data.DB, cb <-chan *briefing, s *Session) {
|
||||||
|
participantChan := make(chan *participant)
|
||||||
for b := range cb {
|
for b := range cb {
|
||||||
getQuestions(db, b)
|
getQuestions(db, b)
|
||||||
|
|
||||||
mux.HandleFunc("/new-participant/"+fmt.Sprint(b.UUID)+"/", b.HandleNewParticipant(participantChan))
|
mux.HandleFunc("/new-participant/"+fmt.Sprint(b.uuid)+"/", b.handleNewParticipant(participantChan))
|
||||||
mux.HandleFunc("/submit-form/"+fmt.Sprint(b.UUID)+"/", b.HandleBriefingForm(db, s))
|
mux.HandleFunc("/submit-form/"+fmt.Sprint(b.uuid)+"/", b.handleBriefingForm(db, s))
|
||||||
|
|
||||||
go mux.handleParticipants(db, participantChan, b)
|
go mux.handleParticipants(db, participantChan, b)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mux *Mux) HandleSessions(db *data.DB, cs <-chan *Session, ss *[]*Session) {
|
func (mux *Mux) HandleSessions(db *data.DB, cs <-chan *Session, ss *[]*Session) {
|
||||||
briefingChan := make(chan *Briefing)
|
briefingChan := make(chan *briefing)
|
||||||
for s := range cs {
|
for s := range cs {
|
||||||
(*ss) = append((*ss), s)
|
(*ss) = append((*ss), s)
|
||||||
|
|
||||||
mux.HandleFunc("/search/"+fmt.Sprint(s.UUID)+"/", s.HandleSearch(db))
|
mux.HandleFunc("/search/"+fmt.Sprint(s.uuid)+"/", s.handleSearch(db))
|
||||||
mux.HandleFunc("/new-briefing/"+fmt.Sprint(s.UUID)+"/", s.HandleNewBriefing(briefingChan))
|
mux.HandleFunc("/new-briefing/"+fmt.Sprint(s.uuid)+"/", s.handleNewBriefing(briefingChan))
|
||||||
mux.HandleFunc("/briefing-done/"+fmt.Sprint(s.UUID)+"/", s.HandleBriefingDone(db))
|
mux.HandleFunc("/briefing-done/"+fmt.Sprint(s.uuid)+"/", s.handleBriefingDone(db))
|
||||||
|
|
||||||
go mux.handleBriefings(db, briefingChan, s)
|
go mux.handleBriefings(db, briefingChan, s)
|
||||||
}
|
}
|
||||||
|
@ -21,45 +21,45 @@ import (
|
|||||||
"streifling.com/jason/sicherheitsunterweisung/packages/data"
|
"streifling.com/jason/sicherheitsunterweisung/packages/data"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Participant struct {
|
type participant struct {
|
||||||
*data.Participant
|
*data.Participant
|
||||||
Login string
|
login string
|
||||||
GivenAnswers []int
|
givenAnswers []int
|
||||||
NoIncorrect int
|
numberIncorrect int
|
||||||
AllowRetry bool
|
allowRetry bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleGivenAnswer(p *Participant, i int64, r *http.Request) error {
|
func handleGivenAnswer(p *participant, i int64, r *http.Request) error {
|
||||||
answer, err := strconv.Atoi(r.PostFormValue("answer"))
|
answer, err := strconv.Atoi(r.PostFormValue("answer"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error: handleGivenAnswer: strconv.Atoi(): %v", err)
|
return fmt.Errorf("error: handleGivenAnswer: strconv.Atoi(): %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
p.GivenAnswers[i] = answer
|
p.givenAnswers[i] = answer
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeResultQuestions(sq []data.Question, givenAnswers []int) []resultQuestion {
|
func makeResultQuestions(sq []data.Question, givenAnswers []int) []resultQuestion {
|
||||||
questions := make([]resultQuestion, len(sq))
|
questions := make([]resultQuestion, len(sq))
|
||||||
for i, q := range sq {
|
for i, q := range sq {
|
||||||
questions[i].Text = q.Text
|
questions[i].text = q.Text
|
||||||
questions[i].Answers = make([]resultAnswer, len(q.Answers))
|
questions[i].answers = make([]resultAnswer, len(q.Answers))
|
||||||
|
|
||||||
for j := range q.Answers {
|
for j := range q.Answers {
|
||||||
questions[i].Answers[j].ID = q.Answers[j].ID
|
questions[i].answers[j].id = q.Answers[j].ID
|
||||||
questions[i].Answers[j].Text = q.Answers[j].Text
|
questions[i].answers[j].text = q.Answers[j].Text
|
||||||
questions[i].Answers[j].IsImage = q.Answers[j].IsImage
|
questions[i].answers[j].isImage = q.Answers[j].IsImage
|
||||||
|
|
||||||
if j+1 == q.Correct {
|
if j+1 == q.Correct {
|
||||||
questions[i].Answers[j].Correct = true
|
questions[i].answers[j].correct = true
|
||||||
} else {
|
} else {
|
||||||
questions[i].Answers[j].Correct = false
|
questions[i].answers[j].correct = false
|
||||||
}
|
}
|
||||||
|
|
||||||
if j+1 == givenAnswers[i] {
|
if j+1 == givenAnswers[i] {
|
||||||
questions[i].Answers[j].Chosen = true
|
questions[i].answers[j].chosen = true
|
||||||
} else {
|
} else {
|
||||||
questions[i].Answers[j].Chosen = false
|
questions[i].answers[j].chosen = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -67,11 +67,11 @@ func makeResultQuestions(sq []data.Question, givenAnswers []int) []resultQuestio
|
|||||||
return questions
|
return questions
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Participant) HandleParticipant(db *data.DB, b *Briefing) http.HandlerFunc {
|
func (p *participant) handleParticipant(db *data.DB, b *briefing) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
p.FirstName = r.PostFormValue("first-" + fmt.Sprint(p.Login))
|
p.FirstName = r.PostFormValue("first-" + fmt.Sprint(p.login))
|
||||||
p.LastName = r.PostFormValue("last-" + fmt.Sprint(p.Login))
|
p.LastName = r.PostFormValue("last-" + fmt.Sprint(p.login))
|
||||||
p.Company = r.PostFormValue("company-" + fmt.Sprint(p.Login))
|
p.Company = r.PostFormValue("company-" + fmt.Sprint(p.login))
|
||||||
|
|
||||||
err := db.WriteParticipant(p.Participant)
|
err := db.WriteParticipant(p.Participant)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -80,17 +80,17 @@ func (p *Participant) HandleParticipant(db *data.DB, b *Briefing) http.HandlerFu
|
|||||||
}
|
}
|
||||||
|
|
||||||
data := participantHTMLData{
|
data := participantHTMLData{
|
||||||
BriefingID: b.UUID,
|
briefingID: b.uuid,
|
||||||
Participant: *p,
|
participant: *p,
|
||||||
}
|
}
|
||||||
|
|
||||||
template.Must(template.ParseFiles("templates/video.html")).ExecuteTemplate(w, "content", data)
|
template.Must(template.ParseFiles("templates/video.html")).ExecuteTemplate(w, "content", data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Participant) HandleAnswer(db *data.DB, b *Briefing, i int64) http.HandlerFunc {
|
func (p *participant) handleAnswer(db *data.DB, b *briefing, i int64) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
if i < int64(len(b.Questions)) {
|
if i < int64(len(b.questions)) {
|
||||||
if err := handleGivenAnswer(p, i-1, r); err != nil {
|
if err := handleGivenAnswer(p, i-1, r); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
log.Panicln(err)
|
log.Panicln(err)
|
||||||
@ -98,11 +98,11 @@ func (p *Participant) HandleAnswer(db *data.DB, b *Briefing, i int64) http.Handl
|
|||||||
|
|
||||||
data := questionHTMLData{
|
data := questionHTMLData{
|
||||||
participantHTMLData: participantHTMLData{
|
participantHTMLData: participantHTMLData{
|
||||||
BriefingID: b.UUID,
|
briefingID: b.uuid,
|
||||||
Participant: Participant{Login: p.Login},
|
participant: participant{login: p.login},
|
||||||
},
|
},
|
||||||
QuestionID: i + 1,
|
questionID: i + 1,
|
||||||
Question: b.Questions[i],
|
question: b.questions[i],
|
||||||
}
|
}
|
||||||
|
|
||||||
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
|
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
|
||||||
@ -112,23 +112,23 @@ func (p *Participant) HandleAnswer(db *data.DB, b *Briefing, i int64) http.Handl
|
|||||||
log.Panicln(err)
|
log.Panicln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
p.NoIncorrect = 0
|
p.numberIncorrect = 0
|
||||||
for i, q := range b.Questions {
|
for i, q := range b.questions {
|
||||||
if p.GivenAnswers[i] != q.Correct {
|
if p.givenAnswers[i] != q.Correct {
|
||||||
p.NoIncorrect++
|
p.numberIncorrect++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
data := resultHTMLData{
|
data := resultHTMLData{
|
||||||
participantHTMLData: participantHTMLData{
|
participantHTMLData: participantHTMLData{
|
||||||
BriefingID: b.UUID,
|
briefingID: b.uuid,
|
||||||
Participant: *p,
|
participant: *p,
|
||||||
},
|
},
|
||||||
Questions: makeResultQuestions(b.Questions, p.GivenAnswers),
|
questions: makeResultQuestions(b.questions, p.givenAnswers),
|
||||||
}
|
}
|
||||||
|
|
||||||
if data.NoIncorrect == 0 {
|
if data.numberIncorrect == 0 {
|
||||||
if err := db.WriteGivenAnswers(*b.Briefing, *p.Participant, b.Questions, p.GivenAnswers); err != nil {
|
if err := db.WriteGivenAnswers(*b.Briefing, *p.Participant, b.questions, p.givenAnswers); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
log.Panicln(err)
|
log.Panicln(err)
|
||||||
}
|
}
|
||||||
@ -139,36 +139,36 @@ func (p *Participant) HandleAnswer(db *data.DB, b *Briefing, i int64) http.Handl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Participant) HandleAllowRetry() http.HandlerFunc {
|
func (p *participant) handleAllowRetry() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
p.NoIncorrect = -1
|
p.numberIncorrect = -1
|
||||||
p.AllowRetry = true
|
p.allowRetry = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Participant) HandleRetry(b *Briefing, i *int) http.HandlerFunc {
|
func (p *participant) handleRetry(b *briefing, i *int) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
if p.AllowRetry {
|
if p.allowRetry {
|
||||||
p.AllowRetry = false
|
p.allowRetry = false
|
||||||
(*i) = 0
|
(*i) = 0
|
||||||
|
|
||||||
data := questionHTMLData{
|
data := questionHTMLData{
|
||||||
participantHTMLData: participantHTMLData{
|
participantHTMLData: participantHTMLData{
|
||||||
BriefingID: b.UUID,
|
briefingID: b.uuid,
|
||||||
Participant: Participant{Login: p.Login},
|
participant: participant{login: p.login},
|
||||||
},
|
},
|
||||||
QuestionID: int64(*i + 1),
|
questionID: int64(*i + 1),
|
||||||
Question: b.Questions[*i],
|
question: b.questions[*i],
|
||||||
}
|
}
|
||||||
|
|
||||||
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
|
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
|
||||||
} else {
|
} else {
|
||||||
data := resultHTMLData{
|
data := resultHTMLData{
|
||||||
participantHTMLData: participantHTMLData{
|
participantHTMLData: participantHTMLData{
|
||||||
BriefingID: b.UUID,
|
briefingID: b.uuid,
|
||||||
Participant: *p,
|
participant: *p,
|
||||||
},
|
},
|
||||||
Questions: makeResultQuestions(b.Questions, p.GivenAnswers),
|
questions: makeResultQuestions(b.questions, p.givenAnswers),
|
||||||
}
|
}
|
||||||
|
|
||||||
template.Must(template.ParseFiles("templates/result.html")).ExecuteTemplate(w, "content", data)
|
template.Must(template.ParseFiles("templates/result.html")).ExecuteTemplate(w, "content", data)
|
||||||
@ -176,26 +176,26 @@ func (p *Participant) HandleRetry(b *Briefing, i *int) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Participant) HandleRefresh(b *Briefing) http.HandlerFunc {
|
func (p *participant) handleRefresh(b *briefing) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
data := participantHTMLData{
|
data := participantHTMLData{
|
||||||
BriefingID: b.UUID,
|
briefingID: b.uuid,
|
||||||
Participant: *p,
|
participant: *p,
|
||||||
}
|
}
|
||||||
|
|
||||||
template.Must(template.ParseFiles("templates/summary.html")).ExecuteTemplate(w, "participant", data)
|
template.Must(template.ParseFiles("templates/summary.html")).ExecuteTemplate(w, "participant", data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Participant) HandleEndOfVideo(b *Briefing) http.HandlerFunc {
|
func (p *participant) handleEndOfVideo(b *briefing) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
data := questionHTMLData{
|
data := questionHTMLData{
|
||||||
participantHTMLData: participantHTMLData{
|
participantHTMLData: participantHTMLData{
|
||||||
BriefingID: b.UUID,
|
briefingID: b.uuid,
|
||||||
Participant: Participant{Login: p.Login},
|
participant: participant{login: p.login},
|
||||||
},
|
},
|
||||||
QuestionID: 1,
|
questionID: 1,
|
||||||
Question: b.Questions[0],
|
question: b.questions[0],
|
||||||
}
|
}
|
||||||
|
|
||||||
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
|
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
|
||||||
|
@ -21,18 +21,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Session struct {
|
type Session struct {
|
||||||
UUID uuid.UUID
|
uuid uuid.UUID
|
||||||
Instructor data.Instructor
|
instructor data.Instructor
|
||||||
Briefings []*Briefing
|
briefings []*briefing
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) HandleSearch(db *data.DB) http.HandlerFunc {
|
func (s *Session) handleSearch(db *data.DB) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
var err error
|
var err error
|
||||||
data := tableHTMLData{}
|
data := tableHTMLData{}
|
||||||
|
|
||||||
data.SessionID = s.UUID
|
data.sessionID = s.uuid
|
||||||
data.OTD, err = db.GetOverviewTableDataByName(r.PostFormValue("search"))
|
data.otd, err = db.GetOverviewTableDataByName(r.PostFormValue("search"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
log.Panicln(err)
|
log.Panicln(err)
|
||||||
@ -41,22 +41,22 @@ func (s *Session) HandleSearch(db *data.DB) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) HandleNewBriefing(cb chan<- *Briefing) http.HandlerFunc {
|
func (s *Session) handleNewBriefing(cb chan<- *briefing) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
briefing := Briefing{Briefing: &data.Briefing{InstructorID: s.Instructor.ID}, UUID: uuid.New()}
|
briefing := briefing{Briefing: &data.Briefing{InstructorID: s.instructor.ID}, uuid: uuid.New()}
|
||||||
s.Briefings = append(s.Briefings, &briefing)
|
s.briefings = append(s.briefings, &briefing)
|
||||||
cb <- &briefing
|
cb <- &briefing
|
||||||
|
|
||||||
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "content", participantHTMLData{BriefingID: briefing.UUID})
|
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "content", participantHTMLData{briefingID: briefing.uuid})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) HandleBriefingDone(db *data.DB) http.HandlerFunc {
|
func (s *Session) handleBriefingDone(db *data.DB) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
data := tableHTMLData{SessionID: s.UUID}
|
data := tableHTMLData{sessionID: s.uuid}
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
data.OTD, err = db.GetAllOverviewTableData()
|
data.otd, err = db.GetAllOverviewTableData()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
log.Panicln(err)
|
log.Panicln(err)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user