Compare commits

..

No commits in common. "5049db064c27fb6c82753b1177f3253586cb6305" and "b78e30d109d1baf59abb954f3172992d9704284c" have entirely different histories.

11 changed files with 209 additions and 441 deletions

View File

@ -9,15 +9,16 @@ DROP TABLE IF EXISTS given_answers;
SET FOREIGN_KEY_CHECKS = 1; SET FOREIGN_KEY_CHECKS = 1;
CREATE TABLE instructors ( CREATE TABLE instructors (
id INT NOT NULL, id INT NOT NULL AUTO_INCREMENT,
first_name VARCHAR(32) NOT NULL, first_name VARCHAR(32) NOT NULL,
last_name VARCHAR(32) NOT NULL, last_name VARCHAR(32) NOT NULL,
personnel_id INT NOT NULL,
PRIMARY KEY(id) PRIMARY KEY(id)
); );
CREATE TABLE briefings ( CREATE TABLE briefings (
id INT AUTO_INCREMENT, id INT NOT NULL AUTO_INCREMENT,
date DATE NOT NULL, date DATE NOT NULL,
time TIME NOT NULL, time TIME NOT NULL,
location VARCHAR(32) NOT NULL, location VARCHAR(32) NOT NULL,
@ -30,7 +31,7 @@ CREATE TABLE briefings (
); );
CREATE TABLE participants ( CREATE TABLE participants (
id INT AUTO_INCREMENT, id INT NOT NULL AUTO_INCREMENT,
first_name VARCHAR(32) NOT NULL, first_name VARCHAR(32) NOT NULL,
last_name VARCHAR(32) NOT NULL, last_name VARCHAR(32) NOT NULL,
company VARCHAR(32) NOT NULL, company VARCHAR(32) NOT NULL,
@ -39,7 +40,7 @@ CREATE TABLE participants (
); );
CREATE TABLE questions ( CREATE TABLE questions (
id INT AUTO_INCREMENT, id INT NOT NULL AUTO_INCREMENT,
question VARCHAR(256) NOT NULL, question VARCHAR(256) NOT NULL,
answer_1 VARCHAR(64) NOT NULL, answer_1 VARCHAR(64) NOT NULL,
answer_2 VARCHAR(64) NOT NULL, answer_2 VARCHAR(64) NOT NULL,
@ -63,17 +64,17 @@ CREATE TABLE given_answers (
); );
INSERT INTO instructors INSERT INTO instructors
(id, first_name, last_name) (first_name, last_name, personnel_id)
VALUES VALUES
( '123456', 'Jason', 'Streifling' ), ( 'Jason', 'Streifling', '123456' ),
( '123457', 'Tim', 'Taler' ), ( 'Tim', 'Taler', '123457' ),
( '123458', 'Georg', 'aus dem Jungel' ); ( 'Georg', 'aus dem Jungel', '123458' );
INSERT INTO briefings ( INSERT INTO briefings (
date, time, location, document_name, as_of, instructor_id date, time, location, document_name, as_of, instructor_id
) VALUES ) VALUES
( '2023-10-16', '17:00:00', 'Werk Langenhagen', 'ICS-2021-LGH', '2021-02-01', '123456' ), ( '2023-10-16', '17:00:00', 'Werk Langenhagen', 'ICS-2021-LGH', '2021-02-01', '1' ),
( '2023-10-16', '17:05:00', 'Werk Langenhagen', 'ICS-2021-LGH', '2021-02-01', '123457' ); ( '2023-10-16', '17:05:00', 'Werk Langenhagen', 'ICS-2021-LGH', '2021-02-01', '2' );
INSERT INTO participants ( INSERT INTO participants (
first_name, last_name, company first_name, last_name, company

45
main.go
View File

@ -11,37 +11,11 @@ import (
"streifling.com/jason/sicherheitsunterweisung/packages/types" "streifling.com/jason/sicherheitsunterweisung/packages/types"
) )
func handleParticipants(mux *http.ServeMux, db *db.DB, cp <-chan *types.Participant, s *types.Session) { func handleSessions(mux *http.ServeMux, db *db.DB, ss []*types.Session, cs <-chan *types.Session) {
for participant := range cp {
mux.HandleFunc("/submit-participant/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(participant.Login)+"/", server.HandleParticipant(s, participant, &s.Questions, db))
for i := range s.Questions {
mux.HandleFunc("/submit-answer/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(participant.Login)+"/"+fmt.Sprint(i+1)+"/", server.HandleAnswer(s, db, participant, &s.Questions, int64(i+1)))
}
mux.HandleFunc("/retry/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(participant.Login)+"/", server.HandleRetry(s, participant, &s.Questions))
}
}
func handleSessions(mux *http.ServeMux, db *db.DB, cs <-chan *types.Session, ss *[]*types.Session) {
for session := range cs { for session := range cs {
(*ss) = append(*ss, session) ss = append(ss, session)
participantChan := make(chan *types.Participant) mux.HandleFunc("/add-participant-"+fmt.Sprint(session.ID)+"/", server.AddParticipant(session))
questionIDs := make([]string, 4) mux.HandleFunc("/submit-form-"+fmt.Sprint(session.ID)+"/", server.SubmitBriefingForm(session, db))
for i := 0; i < len(questionIDs); i++ {
questionIDs[i] = fmt.Sprint(i + 1)
}
var err error
session.Questions, err = db.GetQuestions(questionIDs)
if err != nil {
log.Fatalln(err)
}
mux.HandleFunc("/new-briefing/", server.HandleNewBriefing(session))
mux.HandleFunc("/new-participant/"+fmt.Sprint(session.ID)+"/", server.HandleNewParticipant(session, participantChan))
mux.HandleFunc("/submit-form/"+fmt.Sprint(session.ID)+"/", server.HandleBriefingForm(session, db))
go handleParticipants(mux, db, participantChan, session)
} }
} }
@ -51,19 +25,20 @@ func main() {
log.Fatalln(err) log.Fatalln(err)
} }
mux := http.NewServeMux()
sessions := make([]*types.Session, 0) sessions := make([]*types.Session, 0)
sessionChan := make(chan *types.Session) sessionChan := make(chan *types.Session)
mux := http.NewServeMux()
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/")))) mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
template.Must(template.ParseFiles("templates/index.html", "templates/login.html")).Execute(w, nil) template.Must(template.ParseFiles("templates/index.html", "templates/login.html")).Execute(w, nil)
}) })
mux.HandleFunc("/internal-login/", server.HandleInternalLogin(&sessions, sessionChan, db)) mux.HandleFunc("/internal-login/", server.DisplayTable(db))
mux.HandleFunc("/external-login/", server.HandleExternalLogin(&sessions)) mux.HandleFunc("/external-login/", server.DisplayParticipantForm(sessions))
mux.HandleFunc("/search/", server.HandleSearch(db)) mux.HandleFunc("/search/", server.DisplaySearchResults(db))
mux.HandleFunc("/new-briefing/", server.DisplayInstructorForm(sessionChan))
go handleSessions(mux, db, sessionChan, &sessions) go handleSessions(mux, db, sessions, sessionChan)
log.Fatalln(http.ListenAndServe(":8080", mux)) log.Fatalln(http.ListenAndServe(":8080", mux))
} }

View File

@ -120,7 +120,7 @@ func (db *DB) WriteParticipant(p *types.Participant) error {
return nil return nil
} }
func (db *DB) WriteGivenAnswer(b *types.Briefing, p *types.Participant, q *types.Question, g int) error { func (db *DB) WriteGivenAnswer(b *types.Briefing, p *types.Participant, q *types.Question, g *types.GivenAnswer) error {
_, err := db.Exec(` _, err := db.Exec(`
INSERT INTO given_answers INSERT INTO given_answers
(briefing_id, participant_id, question_id, given_answer) (briefing_id, participant_id, question_id, given_answer)
@ -147,7 +147,7 @@ func (db *DB) WriteAllDataOfBriefing(b *types.Briefing, sp *[]*types.Participant
for _, p := range *sp { for _, p := range *sp {
for i, q := range *sq { for i, q := range *sq {
db.WriteGivenAnswer(b, p, q, i) db.WriteGivenAnswer(b, p, q, (*sg)[i])
} }
} }
@ -291,91 +291,23 @@ func (db *DB) GetLastID(table string) (int, error) {
return id, nil return id, nil
} }
func (db *DB) GetInstructors() ([]*types.Instructor, error) { func (db *DB) GetInstructors() (*[]*types.Instructor, error) {
rows, err := db.Query(` rows, err := db.Query(`
SELECT * SELECT *
FROM instructors FROM instructors
ORDER BY
last_name,
first_name
`) `)
if err != nil { if err != nil {
return nil, fmt.Errorf("*DB.GetInstructors: db.Query(): %v\n", err) return nil, fmt.Errorf("*DB.GetInstructors: db.Query(): %v\n", err)
} }
defer rows.Close()
instructors := make([]*types.Instructor, 0) instructors := make([]*types.Instructor, 0)
for rows.Next() { for rows.Next() {
instructor := new(types.Instructor) instructor := new(types.Instructor)
if err = rows.Scan(&instructor.ID, &instructor.FirstName, &instructor.LastName); err != nil { if err = rows.Scan(instructor); err != nil {
return nil, fmt.Errorf("*DB.GetInstructors: rows.Scan(): %v\n", err) return nil, fmt.Errorf("*DB.GetInstructors: rows.Scan(): %v\n", err)
} }
instructors = append(instructors, instructor) instructors = append(instructors, instructor)
} }
return instructors, nil return &instructors, nil
}
func (db *DB) GetQuestions(nums []string) ([]types.Question, error) {
rows, err := db.Query(`
SELECT *
FROM questions
WHERE id IN (` + strings.Join(nums, ", ") + `)
`)
if err != nil {
return nil, fmt.Errorf("*DB.GetQuestions: db.Query(): %v\n", err)
}
defer rows.Close()
questions := make([]types.Question, 0)
for rows.Next() {
q := new(types.Question)
a1 := new(types.Answer)
a2 := new(types.Answer)
a3 := new(types.Answer)
a4 := new(types.Answer)
a1.ID = 1
a2.ID = 2
a3.ID = 3
a4.ID = 4
if err := rows.Scan(&q.ID, &q.Text, &a1.Text, &a2.Text, &a3.Text, &a4.Text, &q.Correct); err != nil {
return nil, fmt.Errorf("*DB.GetQuestions: rows.Scan(): %v\n", err)
}
q.Answers = append(q.Answers, *a1)
q.Answers = append(q.Answers, *a2)
q.Answers = append(q.Answers, *a3)
q.Answers = append(q.Answers, *a4)
questions = append(questions, *q)
}
return questions, nil
}
func (db *DB) GetGivenAnswers(bid, pid int64, sq []types.Question) ([]int, error) {
answers := make([]int, 0)
query := `
SELECT given_answer
FROM given_answers
WHERE
briefing_id = ? AND
participant_id = ? AND
question_id = ?
`
for _, q := range sq {
var answer int
row := db.QueryRow(query, bid, pid, q.ID)
if err := row.Scan(&answer); err != nil {
return nil, fmt.Errorf("*DB.GetGivenAnswers: row.Scan(): %v\n", err)
}
answers = append(answers, answer)
}
return answers, nil
} }

View File

@ -5,7 +5,6 @@ import (
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"html/template" "html/template"
"log"
"net/http" "net/http"
"strconv" "strconv"
"time" "time"
@ -15,40 +14,17 @@ import (
"streifling.com/jason/sicherheitsunterweisung/packages/types" "streifling.com/jason/sicherheitsunterweisung/packages/types"
) )
func displayTable(w http.ResponseWriter, db *db.DB) { func DisplayTable(db *db.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
bs, err := db.GetAllOverviewTableData() bs, err := db.GetAllOverviewTableData()
if err != nil { if err != nil {
http.Error(w, "displayTable: *DB.GetAllOverviewTableData(): "+fmt.Sprint(err), http.StatusInternalServerError) http.Error(w, "DisplayTable: *DB.GetAllOverviewTableData(): "+fmt.Sprint(err), http.StatusInternalServerError)
} }
template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "content", bs) template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "content", bs)
} }
func HandleInternalLogin(ss *[]*types.Session, cs chan<- *types.Session, db *db.DB) http.HandlerFunc {
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)
log.Panicln(err)
} }
for _, i := range instructors { func DisplaySearchResults(db *db.DB) http.HandlerFunc {
if r.PostFormValue("login") == fmt.Sprint(i.ID) {
session := new(types.Session)
session.ID = uuid.New()
session.Briefing = new(types.Briefing)
session.Briefing.InstructorID = i.ID
(*ss) = append((*ss), session)
cs <- session
displayTable(w, db)
return
}
}
template.Must(template.ParseFiles("templates/login.html")).ExecuteTemplate(w, "content", nil)
}
}
func HandleSearch(db *db.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
bs, err := db.GetOverviewTableDataByName(r.PostFormValue("search")) bs, err := db.GetOverviewTableDataByName(r.PostFormValue("search"))
if err != nil { if err != nil {
@ -58,88 +34,72 @@ func HandleSearch(db *db.DB) http.HandlerFunc {
} }
} }
func HandleNewBriefing(s *types.Session) http.HandlerFunc { func DisplayInstructorForm(cs chan<- *types.Session) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
type httpData struct { session := new(types.Session)
SessionID uuid.UUID session.ID = uuid.New()
} cs <- session
data := new(httpData) template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "content", session.ID)
data.SessionID = s.ID
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "content", data)
} }
} }
func generateLogin() (string, error) { func generateUUID() (string, error) {
bs := make([]byte, 4) bs := make([]byte, 4)
if _, err := rand.Read(bs); err != nil { if _, err := rand.Read(bs); err != nil {
return "", fmt.Errorf("generateLogin: rand.Read(bs): %v\n", err) return "", fmt.Errorf("GenerateUUID: rand.Read(bs): %v\n", err)
} }
return hex.EncodeToString(bs), nil return hex.EncodeToString(bs), nil
} }
func HandleNewParticipant(s *types.Session, cp chan<- *types.Participant) http.HandlerFunc { func AddParticipant(s *types.Session) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
type httpData struct { login, err := generateUUID()
SessionID uuid.UUID
Login string
}
data := new(httpData)
var err error
p := new(types.Participant)
p.Login, err = generateLogin()
if err != nil { if err != nil {
http.Error(w, "AddParticipant: generateLogin(): "+fmt.Sprint(err), http.StatusInternalServerError) http.Error(w, "AddParticipant: generateUUID(): "+fmt.Sprint(err), http.StatusInternalServerError)
}
s.Participants = append(s.Participants, p)
cp <- p
data.SessionID = s.ID
data.Login = p.Login
if err != nil {
http.Error(w, "AddParticipant: generateLogin(): "+fmt.Sprint(err), http.StatusInternalServerError)
} }
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "new", data) s.Logins = append(s.Logins, login)
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "new", login)
} }
} }
func HandleBriefingForm(s *types.Session, db *db.DB) http.HandlerFunc { // TODO: Hier weiter machen, irgendwie die b.ID herausgeben,
// am besten hier auch die p.IDs rausgeben, damit diese später verknüpft werden können
func SubmitBriefingForm(s *types.Session, db *db.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
now := time.Now() now := time.Now()
var err error briefing := new(types.Briefing)
s.Briefing.Date = now.Format("2006-01-02") // TODO: Dropdownmenü
s.Briefing.Time = now.Format("15:04:05") // instructorFirstName := r.PostFormValue("instructor-first")
s.Briefing.Location = r.PostFormValue("location") // instructorLastName := r.PostFormValue("instructor-last")
s.Briefing.DocumentName = r.PostFormValue("document")
s.Briefing.AsOf = r.PostFormValue("as-of")
err = db.WriteBriefing(s.Briefing) briefing.Date = now.Format("2006-01-02")
if err != nil { briefing.Time = now.Format("15:04:05")
http.Error(w, "SubmitBriefingForm: db.WriteBriefing(): "+fmt.Sprint(err), http.StatusInternalServerError) briefing.Location = r.PostFormValue("location")
log.Panicln(err) briefing.DocumentName = r.PostFormValue("document") // TODO: in HTML einfügen
} briefing.AsOf = r.PostFormValue("state") // TODO: Umbenennen
// briefing.InstructorID = r.PostFormValue("instructor-id") // TODO: aus Dropdown holen
displayTable(w, db) db.WriteBriefing(briefing)
s.BriefingID = briefing.ID
} }
} }
// TODO: Make it only serve one purpose // TODO: Make it only serve one purpose
func findCorrectLogin(l string, ss *[]*types.Session) (*types.Session, *types.Participant, bool) { func loginIsCorrect(l string, ss []*types.Session) bool {
for _, session := range *ss { for _, session := range ss {
for _, p := range session.Participants { for i, v := range session.Logins {
if l == p.Login { if l == v {
return session, p, true session.Logins = append(session.Logins[:i], session.Logins[i+1:]...)
return true
} }
} }
} }
return nil, nil, false return false
} }
func newParticipant(l string) (*types.Participant, error) { func newParticipant(l string) (*types.Participant, error) {
@ -154,18 +114,21 @@ func newParticipant(l string) (*types.Participant, error) {
return p, nil return p, nil
} }
func HandleExternalLogin(ss *[]*types.Session) http.HandlerFunc { func DisplayParticipantForm(ss []*types.Session) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
type httpData struct { type data struct {
SessionID uuid.UUID SessionID uuid.UUID
Login string UUID string
} }
session, participant, loginCorrect := findCorrectLogin(r.PostFormValue("login"), ss) if loginIsCorrect(r.PostFormValue("login"), ss) {
if loginCorrect { data := new(data)
data := new(httpData) var err error
data.SessionID = session.ID
data.Login = participant.Login data.UUID, err = generateUUID()
if err != nil {
http.Error(w, "DisplayParticipantForm: generateUUID(): "+fmt.Sprint(err), http.StatusInternalServerError)
}
template.Must(template.ParseFiles("templates/participant.html")).ExecuteTemplate(w, "content", data) template.Must(template.ParseFiles("templates/participant.html")).ExecuteTemplate(w, "content", data)
} else { } else {
@ -174,156 +137,66 @@ func HandleExternalLogin(ss *[]*types.Session) http.HandlerFunc {
} }
} }
func handleGivenAnswer(s *types.Session, p *types.Participant, i int64, r *http.Request, db *db.DB) error { // func readAnswer(r *http.Request, p *types.Participant, i int) error {
answer, err := strconv.Atoi(r.PostFormValue("answer")) // v, err := strconv.Atoi(r.PostFormValue("answer"))
if err != nil { // if err != nil {
return fmt.Errorf("handleGivenAnswer: strconv.Atoi(): %v\n", err) // return fmt.Errorf("readAnswer: strconv.Atoi(): %v\n", err)
} // }
//
if err := db.WriteGivenAnswer(s.Briefing, p, &s.Questions[i], answer); err != nil { // p.Questions[i].Chosen = v
return fmt.Errorf("handleGivenAnswer: db.WriteGivenAnswer(): %v\n", err) //
} // return nil
// }
return nil //
} // func DisplayQuestion(i int, p *types.Participant) http.HandlerFunc {
// return func(w http.ResponseWriter, r *http.Request) {
func HandleParticipant(s *types.Session, p *types.Participant, sq *[]types.Question, db *db.DB) http.HandlerFunc { // if i == 0 {
return func(w http.ResponseWriter, r *http.Request) { // p.FirstName = r.PostFormValue("participant-first-" + fmt.Sprintf("%d", p.ID))
type httpData struct { // p.LastName = r.PostFormValue("participant-last-" + fmt.Sprintf("%d", p.ID))
SessionID uuid.UUID // p.Company = r.PostFormValue("participant-company-" + fmt.Sprintf("%d", p.ID))
Login string // } else {
Question types.Question // if err := readAnswer(r, p, i-1); err != nil {
QuestionID int64 // http.Error(w, "DisplayQuestion: readAnswer(r, p, i): "+fmt.Sprint(err), http.StatusInternalServerError)
} // }
// }
p.FirstName = r.PostFormValue("first-" + fmt.Sprint(p.Login)) //
p.LastName = r.PostFormValue("last-" + fmt.Sprint(p.Login)) // data := new(questionData)
p.Company = r.PostFormValue("company-" + fmt.Sprint(p.Login)) // data.ID = p.ID
// data.Q = p.Questions[i]
err := db.WriteParticipant(p) // data.I = i
if err != nil { // data.J = i + 1
http.Error(w, "DisplayQuestion: db.WriteParticipant(): "+fmt.Sprint(err), http.StatusInternalServerError) //
log.Panicln(err) // template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
} // }
// }
data := new(httpData) //
data.SessionID = s.ID // func DisplayTestResults(b *types.Briefing, p *types.Participant) http.HandlerFunc {
data.Login = p.Login // return func(w http.ResponseWriter, r *http.Request) {
data.Question = (*sq)[0] // numQuestions := len(p.Questions)
data.QuestionID = 1 // wrongAnswers := make([]int, 0)
// fmt.Println(wrongAnswers)
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data) //
} // if err := readAnswer(r, p, numQuestions-1); err != nil {
} // http.Error(w, "DisplayTestResults: readAnswer(r, p, i): "+fmt.Sprint(err), http.StatusInternalServerError)
// }
func HandleAnswer(s *types.Session, db *db.DB, p *types.Participant, sq *[]types.Question, i int64) http.HandlerFunc { //
return func(w http.ResponseWriter, r *http.Request) { // for i, q := range p.Questions {
log.Println(i, len(*sq)) // if q.Chosen != q.Correct {
if i < int64(len(*sq)) { // wrongAnswers = append(wrongAnswers, i)
type httpData struct { // }
SessionID uuid.UUID // }
Login string //
Question types.Question // if wrongAnswers == nil {
QuestionID int64 // b.Participants = append(b.Participants, p)
} // } else {
// data := new(questionData)
if err := handleGivenAnswer(s, p, i-1, r, db); err != nil { // data.ID = p.ID
http.Error(w, "DisplayQuestion: handleGivenAnswer(): "+fmt.Sprint(err), http.StatusInternalServerError) // data.Q = p.Questions[0]
log.Panicln(err) // data.I = 0
} // data.J = data.I + 1
// template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
data := new(httpData) // }
data.SessionID = s.ID //
data.Login = p.Login // template.Must(template.ParseFiles("templates/results.html")).ExecuteTemplate(w, "content", nil)
data.Question = (*sq)[i] // }
data.QuestionID = i + 1 // }
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
} else {
type answer struct {
Text string
Correct bool
Chosen bool
}
type question struct {
Text string
Answers []answer
}
type httpData struct {
SessionID uuid.UUID
Login string
Questions []question
Incorrect int
}
if err := handleGivenAnswer(s, p, i-1, r, db); err != nil {
http.Error(w, "DisplayTestResults: handleGivenAnswer(): "+fmt.Sprint(err), http.StatusInternalServerError)
log.Panicln(err)
}
givenAnswers, err := db.GetGivenAnswers(s.Briefing.ID, p.ID, s.Questions)
if err != nil {
http.Error(w, "DisplayTestResults: db.GetGivenAnswers(): "+fmt.Sprint(err), http.StatusInternalServerError)
log.Panicln(err)
}
data := new(httpData)
data.SessionID = s.ID
data.Login = p.Login
data.Incorrect = 0
data.Questions = make([]question, 0)
for i, q := range s.Questions {
question := new(question)
question.Text = q.Text
question.Answers = make([]answer, 0)
for j, a := range q.Answers {
answer := new(answer)
answer.Text = a.Text
if j+1 == q.Correct {
answer.Correct = true
} else {
answer.Correct = false
}
if j+1 == givenAnswers[i] {
answer.Chosen = true
} else {
answer.Chosen = false
}
question.Answers = append(question.Answers, *answer)
}
data.Questions = append(data.Questions, *question)
if givenAnswers[i] != q.Correct {
data.Incorrect++
}
}
template.Must(template.ParseFiles("templates/results.html")).ExecuteTemplate(w, "content", data)
}
}
}
func HandleRetry(s *types.Session, p *types.Participant, sq *[]types.Question) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
type httpData struct {
SessionID uuid.UUID
Login string
Question types.Question
QuestionID int64
}
data := new(httpData)
data.SessionID = s.ID
data.Login = p.Login
data.Question = (*sq)[0]
data.QuestionID = 1
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
}
}

View File

@ -13,7 +13,6 @@ type Instructor Person
type Participant struct { type Participant struct {
Person Person
Company string Company string
Login string
} }
type Briefing struct { type Briefing struct {
@ -60,7 +59,9 @@ type OverviewTableData struct {
type Session struct { type Session struct {
ID uuid.UUID ID uuid.UUID
*Briefing InstructorID int64
Participants []*Participant BriefingID int64
Questions []Question Logins []string
ParticipantIDs []int64
QuestionIDs []int64
} }

View File

@ -1,7 +0,0 @@
.correct {
color: #00ff00;
}
.incorrect {
color: #ff0000;
}

View File

@ -1,37 +1,38 @@
{{ define "add-buttons" }} {{ define "add-button" }}
<div id="briefing-buttons"> <button type="button" hx-post="/add-participant/" hx-target="this" hx-swap="outerHTML">
<button type="button" hx-post="/new-participant/{{ .SessionID }}/" hx-target="#briefing-buttons" hx-swap="outerHTML">
Neuer Teilnehmer Neuer Teilnehmer
</button> </button>
<button type="submit" hx-post="/submit-form/{{ .SessionID }}/" hx-target="#content" hx-swap="innerHTML">
Fertig
</button>
</div>
{{ end }} {{ end }}
{{ define "new" }} {{ define "new" }}
{{ template "add-buttons" . }} <span>{{ . }}</span>
<p>{{ .Login }}</p> {{ template "add-button" . }}
{{ end }} {{ end }}
{{ define "content" }} {{ define "content" }}
<form> <form>
<div> <div id="instructor">
<label for="location">Ort</label> <label for="instructor-first-input">Unterweiser Vorname</label>
<input id="location" name="location" required type="text" /> <input type="text" name="instructor-first" id="instructor-first-input" />
<label for="instructor-last-input">Unterweiser Nachname</label>
<input type="text" name="instructor-last" id="instructor-last-input" />
</div> </div>
<div> <div id="location">
<label for="document">Dokument</label> <label for="location-input">Ort</label>
<input id="document" name="document" required type="text" /> <input type="text" name="location" id="location-input" />
</div> </div>
<div> <div id="state">
<label for="as-of">Stand vom</label> <label for="state-input">Stand vom</label>
<input id="as-of" name="as-of" required type="date" /> <input type="date" name="state" id="state-input" />
</div> </div>
{{ template "add-buttons" . }} {{ template "add-button" . }}
<button type="submit" hx-post="/submit-form/" hx-target="#content" hx-swap="innerHTML">
OK
</button>
</form> </form>
{{ end }} {{ end }}

View File

@ -1,17 +1,16 @@
{{ define "content" }} {{ define "content" }}
<h2>Anmeldung</h2> <h2>Login</h2>
<form> <form>
<input autocomplete="off" id="login-input" name="login" placeholder="Code" required type="text" /> <label for="login-input">Code</label>
<input type="text" name="login" id="login-input" />
<button type="submit" hx-post="/external-login/" hx-target="#content">
Anmelden
</button>
<div>
<button type="submit" hx-post="/internal-login/" hx-target="#content"> <button type="submit" hx-post="/internal-login/" hx-target="#content">
Intern Intern
</button> </button>
<button type="submit" hx-post="/external-login/" hx-target="#content">
Gast
</button>
</div>
</form> </form>
{{ end }} {{ end }}

View File

@ -1,15 +1,15 @@
{{ define "content" }} {{ define "content" }}
<form> <form id="participant-{{ . }}">
<label for="first-{{ .Login }}">Vorname</label> <label for="participant-first-input-{{ . }}">Vorname</label>
<input type="text" name="first-{{ .Login }}" id="first-{{ .Login }}" /> <input type="text" name="participant-first-{{ . }}" id="participant-first-input-{{ . }}" />
<label for="last-{{ .Login }}">Nachname</label> <label for="participant-last-input-{{ . }}">Nachname</label>
<input type="text" name="last-{{ .Login }}" id="last-{{ .Login }}" /> <input type="text" name="participant-last-{{ . }}" id="participant-last-input-{{ . }}" />
<label for="company-{{ .Login }}">Firma</label> <label for="participant-company-input-{{ . }}">Firma</label>
<input type="text" name="company-{{ .Login }}" id="company-{{ .Login }}" /> <input type="text" name="participant-company-{{ . }}" id="participant-company-input-{{ . }}" />
<button type="button" hx-post="/submit-participant/{{ .SessionID }}/{{ .Login }}/" hx-target="#content"> <button type="button" hx-post="/display-question-{{ . }}-0/" hx-target="#content">
Fertig Fertig
</button> </button>
</form> </form>

View File

@ -1,5 +1,5 @@
{{ define "answers" }} {{ define "answers" }}
{{range .Question.Answers}} {{ range .Q.Answers }}
<div> <div>
<input type="radio" name="answer" id="answer-{{ .ID }}" value="{{ .ID }}" /> <input type="radio" name="answer" id="answer-{{ .ID }}" value="{{ .ID }}" />
<label for="answer-{{ .ID }}">{{ .Text }}</label> <label for="answer-{{ .ID }}">{{ .Text }}</label>
@ -8,13 +8,13 @@
{{ end }} {{ end }}
{{ define "content" }} {{ define "content" }}
<h2>Frage {{.QuestionID}}</h2> <h2>Frage {{ .I }}</h2>
<p>{{.Question.Text}}</p> <p>{{ .Q.Text }}</p>
<form> <form>
{{ template "answers" . }} {{ template "answers" . }}
<button hx-post="/submit-answer/{{.SessionID}}/{{.Login}}/{{.QuestionID}}/" hx-target="#content" type="submit"> <button type="submit" hx-post="/display-question-{{ .ID }}-{{ .J }}/" hx-target="#content" hx-swap="innerHTML">
Weiter Weiter
</button> </button>
</form> </form>

View File

@ -1,18 +1,11 @@
{{define "answers"}} {{ define "passed" }}
{{range .Answers}}
<p class="{{if and .Chosen .Correct}} correct {{else if and .Chosen (not .Correct)}} incorrect {{end}}">
{{.Text}}
</p>
{{ end }} {{ end }}
{{ define "failed" }}
{{ end }} {{ end }}
{{ define "content" }} {{ define "content" }}
<p>{{.Incorrect}} Fehler</p> {{ q := range .Participant.Questions }}
{{range .Questions}} <p>{{ . }}{{ q.Text }}</p>
<p>{{.Text}}</p>
{{template "answers" .}}
{{end}}
{{if gt .Incorrect 0}}
<button hx-post="/retry/{{.SessionID}}/{{.Login}}/" hx-target="#content" type="submit">Wiederholen</button>
{{ end }} {{ end }}
{{ end }} {{ end }}