Compare commits

..

No commits in common. "5019432b24a30c330e82ac68298a7532e676a094" and "7144489afb0d4b66361da9cd235afeeff436433e" have entirely different histories.

5 changed files with 241 additions and 339 deletions

View File

@ -19,7 +19,6 @@ CREATE TABLE briefings (
date DATE NOT NULL,
time TIME NOT NULL,
location VARCHAR(32) NOT NULL,
document_name VARCHAR(16) NOT NULL,
as_of DATE NOT NULL,
instructor_id INT NOT NULL,
@ -70,8 +69,8 @@ VALUES
INSERT INTO briefings (
date, time, location, as_of, instructor_id
) VALUES
( '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', '2' );
( '2023-10-16', '17:00:00', 'Werk Langenhagen', '2021-02-01', '1' ),
( '2023-10-16', '17:05:00', 'Werk Langenhagen', '2021-02-01', '2' );
INSERT INTO participants (
first_name, last_name, company

37
main.go
View File

@ -1,33 +1,60 @@
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"streifling.com/jason/sicherheitsunterweisung/packages/data"
"streifling.com/jason/sicherheitsunterweisung/packages/server"
"streifling.com/jason/sicherheitsunterweisung/packages/types"
)
func waitForParticipants(sb []*types.Briefing, sp []*types.Participant, cp <-chan *types.Participant, m *http.ServeMux) {
for p := range cp {
p.Questions = data.InitQuestions()
sp = append(sp, p)
var i int
for i = range p.Questions {
m.HandleFunc("/display-question-"+fmt.Sprintf("%d", p.ID)+"-"+fmt.Sprintf("%d", i)+"/", server.DisplayQuestion(i, p))
}
m.HandleFunc("/display-question-"+fmt.Sprintf("%d", p.ID)+"-"+fmt.Sprintf("%d", i+1)+"/", server.DisplayTestResults(sb, p))
}
}
func main() {
logins := make([]string, 0)
participants := make([]*types.Participant, 0)
briefings := make([]*types.Briefing, 0)
mux := http.NewServeMux()
db, err := data.OpenDB("sicherheitsunterweisung")
if err != nil {
log.Fatalln(err)
}
mux := http.NewServeMux()
var i, j int64
if err := db.GetLastID(&i); err != nil {
log.Fatalln(err)
}
j = i
participantChan := make(chan *types.Participant)
defer close(participantChan)
go waitForParticipants(briefings, participants, participantChan, mux)
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
template.Must(template.ParseFiles("templates/index.html", "templates/login.html")).Execute(w, nil)
})
mux.HandleFunc("/search/", server.DisplaySearchResults(db))
mux.HandleFunc("/new-briefing/", server.DisplayInstructorForm())
mux.HandleFunc("/add-participant/", server.AddParticipant(&logins))
mux.HandleFunc("/submit-form/", server.SubmitBriefingForm(db, &logins))
mux.HandleFunc("/new-briefing/", server.DisplayForm(&i))
mux.HandleFunc("/add-participant/", server.AddParticipant(&i, &logins))
mux.HandleFunc("/submit-form/", server.SubmitForm(db, &i, &j))
mux.HandleFunc("/internal-login/", server.DisplayTable(db))
mux.HandleFunc("/external-login/", server.DisplayParticipantForm(&logins))
mux.HandleFunc("/external-login/", server.DisplayParticipantForm(&logins, participantChan))
log.Fatalln(http.ListenAndServe(":8080", mux))
}

View File

@ -60,6 +60,12 @@ func getCredentials() (string, string, error) {
return user, pass, nil
}
func reverseOrder(bs []*types.Briefing) {
for i, j := 0, len(bs)-1; i < j; i, j = i+1, j-1 {
bs[i], bs[j] = bs[j], bs[i]
}
}
func OpenDB(dbName string) (*DB, error) {
var err error
db := new(DB)
@ -84,226 +90,103 @@ func OpenDB(dbName string) (*DB, error) {
}
func (db *DB) WriteBriefing(b *types.Briefing) error {
result, err := db.Exec(`
INSERT INTO briefings
(date, time, location, document_name, as_of, instructor_id)
VALUES
(?, ?, ?, ?, ?, ?)
`, b.Date, b.Time, b.Location, b.DocumentName, b.AsOf, b.InstructorID)
for i := 0; i < len(b.Participants); i++ {
result, err := db.Exec("INSERT INTO "+db.Name+" (instructor_first,"+
" instructor_last, date, time, state, location, participant_first,"+
" participant_last, company) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
b.FirstName, b.LastName, b.Date, b.Time, b.State, b.Location,
b.Participants[i].FirstName, b.Participants[i].LastName,
b.Participants[i].Company)
if err != nil {
return fmt.Errorf("*DB.writeBriefing: db.Exec(): %v\n", err)
return fmt.Errorf("*DB.WriteBriefing: db.Exec(\"INSERT INTO"+
" \"+db.Name+\" (instructor_first, instructor_last, date, time, state,"+
" location, participant_first, participant_last, company) VALUES (?, ?,"+
" ?, ?, ?, ?, ?, ?, ?)\", b.FirstName, b.LastName, b.Date, b.Time,"+
" b.State, b.Location, b.Participants[i].FirstName,"+
" b.Participants[i].LastName, b.Participants[i].Company): %v\n", err)
}
b.ID, err = result.LastInsertId()
_, err = result.LastInsertId()
if err != nil {
return fmt.Errorf("*DB.writeBriefing: result.LastInsertId(): %v\n", err)
}
return nil
}
func (db *DB) WriteParticipant(p *types.Participant) error {
result, err := db.Exec(`
INSERT INTO participants
(first_name, last_name, company)
VALUES
(?, ?, ?)
`, p.FirstName, p.LastName, p.Company)
if err != nil {
return fmt.Errorf("*DB.writeParticipants: db.Exec(): %v\n", err)
}
p.ID, err = result.LastInsertId()
if err != nil {
return fmt.Errorf("*DB.writeParticipants: result.LastInsertId(): %v\n", err)
}
return nil
}
func (db *DB) WriteGivenAnswer(b *types.Briefing, p *types.Participant, q *types.Question, g *types.GivenAnswer) error {
_, err := db.Exec(`
INSERT INTO given_answers
(briefing_id, participant_id, question_id, given_answer)
VALUES
(?, ?, ?, ?)
`, b.ID, p.ID, q.ID, g)
if err != nil {
return fmt.Errorf("*DB.writeGivenAnswers: db.Exec(): %v\n", err)
}
return nil
}
func (db *DB) WriteAllDataOfBriefing(b *types.Briefing, sp *[]*types.Participant, sq *[]*types.Question, sg *[]*types.GivenAnswer) error {
if err := db.WriteBriefing(b); err != nil {
return fmt.Errorf("*DB.WriteAllDataOfBriefing: db.writeBriefing(): %v\n", err)
}
for _, p := range *sp {
if err := db.WriteParticipant(p); err != nil {
return fmt.Errorf("*DB.WriteAllDataOfBriefing: db.writeParticipants(): %v\n", err)
}
}
for _, p := range *sp {
for i, q := range *sq {
db.WriteGivenAnswer(b, p, q, (*sg)[i])
return fmt.Errorf("*DB.WriteBriefing: result.LastInsertId(): %v\n", err)
}
}
return nil
}
func (db *DB) GetAllOverviewTableData() (*[]*types.OverviewTableData, error) {
rows, err := db.Query(`
SELECT
i.first_name,
i.last_name,
b.date,
b.time,
b.location,
b.document_name,
b.as_of,
p.first_name,
p.last_name,
p.company
FROM given_answers AS g
INNER JOIN briefings AS b
ON b.id = g.briefing_id
INNER JOIN participants AS p
ON p.id = g.participant_id
INNER JOIN instructors AS i
ON i.id = b.instructor_id
ORDER BY
b.id DESC,
p.id
`)
func (db *DB) ReadAll() ([]*types.Briefing, error) {
bs := make([]*types.Briefing, 0)
rows, err := db.Query("SELECT *" + " FROM " + db.Name)
if err != nil {
return nil, fmt.Errorf("*DB.ReadAllBriefings: db.Query(): %v\n", err)
return nil, fmt.Errorf("*DB.ReadAll: db.Query(\"SELECT * FROM \"+db.Name): %v\n", err)
}
defer rows.Close()
data := make([]*types.OverviewTableData, 0)
for rows.Next() {
otd := new(types.OverviewTableData)
b := new(types.Briefing)
p := new(types.Participant)
err := rows.Scan(
&otd.InstructorFirstName,
&otd.InstructorLastName,
&otd.BriefingDate,
&otd.BriefingTime,
&otd.BriefingLocation,
&otd.BriefingDocumentName,
&otd.BriefingAsOf,
&otd.ParticipantFirstName,
&otd.ParticipantLastName,
&otd.ParticipantCompany,
)
if err != nil {
return nil, fmt.Errorf("*DB.ReadAllBriefings: rows.Scan(): %v\n", err)
if err := rows.Scan(&p.ID, &b.FirstName, &b.LastName, &b.Date, &b.Time,
&b.State, &b.Location, &p.FirstName, &p.LastName, &p.Company); err != nil {
return nil, fmt.Errorf("*DB.ReadAll: db.Query(): %v\n", err)
}
data = append(data, otd)
b.Participants = append(b.Participants, p)
bs = append(bs, b)
}
return &data, nil
reverseOrder(bs)
return bs, nil
}
func (db *DB) GetOverviewTableDataByName(n string) (*[]*types.OverviewTableData, error) {
rows, err := db.Query(`
SELECT
i.first_name,
i.last_name,
b.date,
b.time,
b.location,
b.document_name,
b.as_of,
p.first_name,
p.last_name,
p.company
FROM given_answers AS g
INNER JOIN briefings AS b
ON b.id = g.briefing_id
INNER JOIN participants AS p
ON p.id = g.participant_id
INNER JOIN instructors AS i
ON i.id = b.instructor_id
WHERE
i.first_name LIKE ? OR
i.last_name LIKE ? OR
p.first_name LIKE ? OR
p.last_name LIKE ?
ORDER BY
b.id DESC,
p.id
`, "%"+n+"%", "%"+n+"%", "%"+n+"%", "%"+n+"%")
func (db *DB) ReadByName(name string) ([]*types.Briefing, error) {
bs := make([]*types.Briefing, 0)
rows, err := db.Query("SELECT *"+
" FROM "+db.Name+
" WHERE instructor_first LIKE ?"+
" OR instructor_last LIKE ?"+
" OR participant_first LIKE ?"+
" OR participant_last LIKE ?",
"%"+name+"%", "%"+name+"%", "%"+name+"%", "%"+name+"%")
if err != nil {
return nil, fmt.Errorf("*DB.GetOverviewTableDataByName: db.Query(): %v\n", err)
return nil, fmt.Errorf("*DB.ReadByName: db.Query(\"SELECT *"+
" FROM \"+db.Name+"+
" WHERE instructor_first LIKE ?"+
" OR instructor_last LIKE ?"+
" OR participant_first LIKE ?"+
" OR participant_last LIKE ?\"): %v\n", err)
}
defer rows.Close()
data := make([]*types.OverviewTableData, 0)
for rows.Next() {
otd := new(types.OverviewTableData)
b := new(types.Briefing)
p := new(types.Participant)
err := rows.Scan(
&otd.InstructorFirstName,
&otd.InstructorLastName,
&otd.BriefingDate,
&otd.BriefingTime,
&otd.BriefingLocation,
&otd.BriefingDocumentName,
&otd.BriefingAsOf,
&otd.ParticipantFirstName,
&otd.ParticipantLastName,
&otd.ParticipantCompany,
)
if err != nil {
return nil, fmt.Errorf("*DB.ReadAllBriefings: rows.Scan(): %v\n", err)
if err := rows.Scan(&p.ID, &b.FirstName, &b.LastName, &b.Date, &b.Time, &b.State,
&b.Location, &p.FirstName, &p.LastName, &p.Company); err != nil {
return nil, fmt.Errorf("*DB.ReadByName: rows.Scan(&p.ID, &b.FirstName,"+
" &b.LastName, &b.Date, &b.Time, &b.State, &b.Location, &p.FirstName,"+
" &p.LastName, &p.Company): %v\n", err)
}
b.Participants = append(b.Participants, p)
bs = append(bs, b)
}
data = append(data, otd)
}
return &data, nil
reverseOrder(bs)
return bs, nil
}
func (db *DB) GetLastID(table string) (int, error) {
var id int
func (db *DB) GetLastID(i *int64) error {
row := db.QueryRow("SELECT id" +
" FROM " + db.Name +
" ORDER BY id DESC LIMIT 0, 1")
row := db.QueryRow(`
SELECT id
FROM ?
ORDER BY id DESC
LIMIT 0, 1
`, table)
if err := row.Scan(&id); err != nil {
return -1, fmt.Errorf("*DB.GetLastID: row.Scan(): %v\n", err)
if err := row.Scan(i); err != nil {
return fmt.Errorf("*DB.GetLastID: row.Scan(&i): %v\n", err)
}
return id, nil
}
func (db *DB) GetInstructors() (*[]*types.Instructor, error) {
rows, err := db.Query(`
SELECT *
FROM instructors
`)
if err != nil {
return nil, fmt.Errorf("*DB.GetInstructors: db.Query(): %v\n", err)
}
instructors := make([]*types.Instructor, 0)
for rows.Next() {
instructor := new(types.Instructor)
if err = rows.Scan(instructor); err != nil {
return nil, fmt.Errorf("*DB.GetInstructors: rows.Scan(): %v\n", err)
}
instructors = append(instructors, instructor)
}
return &instructors, nil
return nil
}

View File

@ -5,26 +5,27 @@ import (
"encoding/hex"
"fmt"
"html/template"
"log"
"net/http"
"strconv"
"time"
"strings"
"streifling.com/jason/sicherheitsunterweisung/packages/data"
"streifling.com/jason/sicherheitsunterweisung/packages/types"
)
// type questionData struct {
// ID int64
// Q types.Question
// I int
// J int
// }
type questionData struct {
ID int64
Q types.Question
I int
J int
}
func DisplayTable(db *data.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
bs, err := db.GetAllOverviewTableData()
bs, err := db.ReadAll()
if err != nil {
http.Error(w, "DisplayTable: *DB.GetAllOverviewTableData(): "+fmt.Sprint(err), http.StatusInternalServerError)
_ = fmt.Errorf("DisplayTable: %v\n", err)
}
template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "content", bs)
}
@ -32,61 +33,70 @@ func DisplayTable(db *data.DB) http.HandlerFunc {
func DisplaySearchResults(db *data.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
bs, err := db.GetOverviewTableDataByName(r.PostFormValue("search"))
bs, err := db.ReadByName(r.PostFormValue("search"))
if err != nil {
http.Error(w, "DisplayResults: db.ReadByName(r.PostFormValue()): "+fmt.Sprint(err), http.StatusInternalServerError)
_ = fmt.Errorf("DisplayResults: db.ReadByName(r.PostFormValue()): %v\n", err)
}
template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "rows", bs)
}
}
func DisplayInstructorForm() http.HandlerFunc {
func DisplayForm(i *int64) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "content", nil)
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "content", i)
}
}
func generateUUID() (string, error) {
bs := make([]byte, 4)
func generateUUID() string {
bs := make([]byte, 2)
if _, err := rand.Read(bs); err != nil {
return "", fmt.Errorf("GenerateUUID: rand.Read(bs): %v\n", err)
_ = fmt.Errorf("GenerateUUID: rand.Read(bs): %v\n", err)
return ""
}
return hex.EncodeToString(bs), nil
return hex.EncodeToString(bs)
}
func AddParticipant(sl *[]string) http.HandlerFunc {
func AddParticipant(i *int64, ls *[]string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
login, err := generateUUID()
if err != nil {
http.Error(w, "AddParticipant: generateUUID(): "+fmt.Sprint(err), http.StatusInternalServerError)
}
(*sl) = append(*sl, login)
*i++
login := fmt.Sprintf("%d", *i) + "-" + generateUUID()
(*ls) = append(*ls, login)
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "new", login)
}
}
// 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(db *data.DB, sl *[]string) http.HandlerFunc {
func SubmitForm(db *data.DB, i, j *int64) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
now := time.Now()
briefing := new(types.Briefing)
b := new(types.Briefing)
// TODO: Dropdownmenü
// instructorFirstName := r.PostFormValue("instructor-first")
// instructorLastName := r.PostFormValue("instructor-last")
b.FirstName = r.PostFormValue("instructor-first")
b.LastName = r.PostFormValue("instructor-last")
b.Date = r.PostFormValue("date")
b.Time = r.PostFormValue("time")
b.State = r.PostFormValue("state")
b.Location = r.PostFormValue("location")
briefing.Date = now.Format("2006-01-02")
briefing.Time = now.Format("15:04:05")
briefing.Location = r.PostFormValue("location")
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
for ; *j <= *i; *j++ {
b.Participants = append(b.Participants, &types.Participant{
ID: *j,
Person: types.Person{
FirstName: r.PostFormValue("participant-first-" + fmt.Sprint(*j)),
LastName: r.PostFormValue(("participant-last-" + fmt.Sprint(*j))),
},
Company: r.PostFormValue(("participant-company-" + fmt.Sprint(*j))),
})
}
db.WriteBriefing(briefing)
log.Println(b)
db.WriteBriefing(b)
bs, err := db.ReadAll()
if err != nil {
_ = fmt.Errorf("SubmitForm: db.ReadAll(): %v\n", err)
}
template.Must(template.ParseFiles("templates/table.html")).Execute(w, bs)
}
}
@ -102,92 +112,95 @@ func loginIsCorrect(l string, logins *[]string) bool {
}
func newParticipant(l string) (*types.Participant, error) {
var err error
p := new(types.Participant)
p.ID, err = strconv.ParseInt(l, 10, 64)
idInt, err := strconv.Atoi(strings.Split(l, "-")[0])
if err != nil {
return nil, fmt.Errorf("newParticipant: strconv.Atoi(idString): %v\n", err)
}
p.ID = int64(idInt)
return p, nil
}
func DisplayParticipantForm(sl *[]string) http.HandlerFunc {
func DisplayParticipantForm(ls *[]string, cp chan<- *types.Participant) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if loginIsCorrect(r.PostFormValue("login"), sl) {
uuid, err := generateUUID()
l := r.PostFormValue("login")
if loginIsCorrect(l, ls) {
p, err := newParticipant(l)
if err != nil {
http.Error(w, "DisplayParticipantForm: generateUUID(): "+fmt.Sprint(err), http.StatusInternalServerError)
http.Error(w, "GetParticipantData: newParticipant(l): "+fmt.Sprint(err), http.StatusInternalServerError)
}
template.Must(template.ParseFiles("templates/participant.html")).ExecuteTemplate(w, "content", uuid)
cp <- p
template.Must(template.ParseFiles("templates/participant.html")).ExecuteTemplate(w, "content", p.ID)
} else {
template.Must(template.ParseFiles("templates/login.html")).ExecuteTemplate(w, "content", nil)
}
}
}
// func readAnswer(r *http.Request, p *types.Participant, i int) error {
// v, err := strconv.Atoi(r.PostFormValue("answer"))
// if err != nil {
// return fmt.Errorf("readAnswer: strconv.Atoi(): %v\n", err)
// }
//
// p.Questions[i].Chosen = v
//
// return nil
// }
//
// func DisplayQuestion(i int, p *types.Participant) http.HandlerFunc {
// return func(w http.ResponseWriter, r *http.Request) {
// if i == 0 {
// p.FirstName = r.PostFormValue("participant-first-" + fmt.Sprintf("%d", p.ID))
// p.LastName = r.PostFormValue("participant-last-" + fmt.Sprintf("%d", p.ID))
// p.Company = r.PostFormValue("participant-company-" + fmt.Sprintf("%d", p.ID))
// } else {
// if err := readAnswer(r, p, i-1); err != nil {
// http.Error(w, "DisplayQuestion: readAnswer(r, p, i): "+fmt.Sprint(err), http.StatusInternalServerError)
// }
// }
//
// data := new(questionData)
// data.ID = p.ID
// data.Q = p.Questions[i]
// data.I = i
// data.J = i + 1
//
// template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
// }
// }
//
// func DisplayTestResults(b *types.Briefing, p *types.Participant) http.HandlerFunc {
// return func(w http.ResponseWriter, r *http.Request) {
// numQuestions := len(p.Questions)
// wrongAnswers := make([]int, 0)
// fmt.Println(wrongAnswers)
//
// if err := readAnswer(r, p, numQuestions-1); err != nil {
// http.Error(w, "DisplayTestResults: readAnswer(r, p, i): "+fmt.Sprint(err), http.StatusInternalServerError)
// }
//
// for i, q := range p.Questions {
// if q.Chosen != q.Correct {
// wrongAnswers = append(wrongAnswers, i)
// }
// }
//
// if wrongAnswers == nil {
// b.Participants = append(b.Participants, p)
// } else {
// data := new(questionData)
// data.ID = p.ID
// data.Q = p.Questions[0]
// data.I = 0
// data.J = data.I + 1
// template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
// }
//
// template.Must(template.ParseFiles("templates/results.html")).ExecuteTemplate(w, "content", nil)
// }
// }
func readAnswer(r *http.Request, p *types.Participant, i int) error {
v, err := strconv.Atoi(r.PostFormValue("answer"))
if err != nil {
return fmt.Errorf("readAnswer: strconv.Atoi(r.PostFormValue(\"answer\")): %v\n", err)
}
p.Questions[i].Chosen = v
return nil
}
func DisplayQuestion(i int, p *types.Participant) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if i == 0 {
p.FirstName = r.PostFormValue("participant-first-" + fmt.Sprintf("%d", p.ID))
p.LastName = r.PostFormValue("participant-last-" + fmt.Sprintf("%d", p.ID))
p.Company = r.PostFormValue("participant-company-" + fmt.Sprintf("%d", p.ID))
} else {
if err := readAnswer(r, p, i-1); err != nil {
http.Error(w, "DisplayQuestion: readAnswer(r, p, i): "+fmt.Sprint(err), http.StatusInternalServerError)
}
}
data := new(questionData)
data.ID = p.ID
data.Q = p.Questions[i]
data.I = i
data.J = i + 1
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
}
}
func DisplayTestResults(b *types.Briefing, p *types.Participant) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
numQuestions := len(p.Questions)
wrongAnswers := make([]int, 0)
fmt.Println(wrongAnswers)
if err := readAnswer(r, p, numQuestions-1); err != nil {
http.Error(w, "DisplayTestResults: readAnswer(r, p, i): "+fmt.Sprint(err), http.StatusInternalServerError)
}
for i, q := range p.Questions {
if q.Chosen != q.Correct {
wrongAnswers = append(wrongAnswers, i)
}
}
if wrongAnswers == nil {
b.Participants = append(b.Participants, p)
} else {
data := new(questionData)
data.ID = p.ID
data.Q = p.Questions[0]
data.I = 0
data.J = data.I + 1
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
}
template.Must(template.ParseFiles("templates/results.html")).ExecuteTemplate(w, "content", nil)
}
}

View File

@ -1,56 +1,36 @@
package types
type Person struct {
ID int64
FirstName string
LastName string
}
type Instructor Person
type Participant struct {
Person
Company string
}
type Briefing struct {
ID int64
Date string
Time string
Location string
DocumentName string
AsOf string
InstructorID int64
}
type Answer struct {
ID int64
ID int
Text string
}
type Question struct {
ID int64
Text string
Answers []Answer
Chosen int
Correct int
}
type GivenAnswer struct {
BriefingID int64
ParticipantID int64
QuestionID int64
GivenAnswer int
type Participant struct {
ID int64
Person
Company string
Questions []Question
}
type OverviewTableData struct {
InstructorFirstName string
InstructorLastName string
BriefingDate string
BriefingTime string
BriefingLocation string
BriefingDocumentName string
BriefingAsOf string
ParticipantFirstName string
ParticipantLastName string
ParticipantCompany string
type Briefing struct {
Instructor
Date string
Time string
State string
Location string
Participants []*Participant
}