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

37
main.go
View File

@ -1,33 +1,60 @@
package main package main
import ( import (
"fmt"
"html/template" "html/template"
"log" "log"
"net/http" "net/http"
"streifling.com/jason/sicherheitsunterweisung/packages/data" "streifling.com/jason/sicherheitsunterweisung/packages/data"
"streifling.com/jason/sicherheitsunterweisung/packages/server" "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() { func main() {
logins := make([]string, 0) logins := make([]string, 0)
participants := make([]*types.Participant, 0)
briefings := make([]*types.Briefing, 0)
mux := http.NewServeMux()
db, err := data.OpenDB("sicherheitsunterweisung") db, err := data.OpenDB("sicherheitsunterweisung")
if err != nil { if err != nil {
log.Fatalln(err) 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.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("/search/", server.DisplaySearchResults(db)) mux.HandleFunc("/search/", server.DisplaySearchResults(db))
mux.HandleFunc("/new-briefing/", server.DisplayInstructorForm()) mux.HandleFunc("/new-briefing/", server.DisplayForm(&i))
mux.HandleFunc("/add-participant/", server.AddParticipant(&logins)) mux.HandleFunc("/add-participant/", server.AddParticipant(&i, &logins))
mux.HandleFunc("/submit-form/", server.SubmitBriefingForm(db, &logins)) mux.HandleFunc("/submit-form/", server.SubmitForm(db, &i, &j))
mux.HandleFunc("/internal-login/", server.DisplayTable(db)) 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)) log.Fatalln(http.ListenAndServe(":8080", mux))
} }

View File

@ -60,6 +60,12 @@ func getCredentials() (string, string, error) {
return user, pass, nil 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) { func OpenDB(dbName string) (*DB, error) {
var err error var err error
db := new(DB) db := new(DB)
@ -84,226 +90,103 @@ func OpenDB(dbName string) (*DB, error) {
} }
func (db *DB) WriteBriefing(b *types.Briefing) error { func (db *DB) WriteBriefing(b *types.Briefing) error {
result, err := db.Exec(` for i := 0; i < len(b.Participants); i++ {
INSERT INTO briefings result, err := db.Exec("INSERT INTO "+db.Name+" (instructor_first,"+
(date, time, location, document_name, as_of, instructor_id) " instructor_last, date, time, state, location, participant_first,"+
VALUES " participant_last, company) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
(?, ?, ?, ?, ?, ?) b.FirstName, b.LastName, b.Date, b.Time, b.State, b.Location,
`, b.Date, b.Time, b.Location, b.DocumentName, b.AsOf, b.InstructorID) b.Participants[i].FirstName, b.Participants[i].LastName,
b.Participants[i].Company)
if err != nil { 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 { if err != nil {
return fmt.Errorf("*DB.writeBriefing: result.LastInsertId(): %v\n", err) 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 nil return nil
} }
func (db *DB) GetAllOverviewTableData() (*[]*types.OverviewTableData, error) { func (db *DB) ReadAll() ([]*types.Briefing, error) {
rows, err := db.Query(` bs := make([]*types.Briefing, 0)
SELECT
i.first_name, rows, err := db.Query("SELECT *" + " FROM " + db.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
`)
if err != nil { 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() defer rows.Close()
data := make([]*types.OverviewTableData, 0)
for rows.Next() { for rows.Next() {
otd := new(types.OverviewTableData) b := new(types.Briefing)
p := new(types.Participant)
err := rows.Scan( if err := rows.Scan(&p.ID, &b.FirstName, &b.LastName, &b.Date, &b.Time,
&otd.InstructorFirstName, &b.State, &b.Location, &p.FirstName, &p.LastName, &p.Company); err != nil {
&otd.InstructorLastName, return nil, fmt.Errorf("*DB.ReadAll: db.Query(): %v\n", err)
&otd.BriefingDate, }
&otd.BriefingTime,
&otd.BriefingLocation, b.Participants = append(b.Participants, p)
&otd.BriefingDocumentName, bs = append(bs, b)
&otd.BriefingAsOf, }
&otd.ParticipantFirstName,
&otd.ParticipantLastName, reverseOrder(bs)
&otd.ParticipantCompany, return bs, nil
) }
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 { if err != nil {
return nil, fmt.Errorf("*DB.ReadAllBriefings: rows.Scan(): %v\n", err) return nil, fmt.Errorf("*DB.ReadByName: db.Query(\"SELECT *"+
} " FROM \"+db.Name+"+
" WHERE instructor_first LIKE ?"+
data = append(data, otd) " OR instructor_last LIKE ?"+
} " OR participant_first LIKE ?"+
" OR participant_last LIKE ?\"): %v\n", err)
return &data, 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+"%")
if err != nil {
return nil, fmt.Errorf("*DB.GetOverviewTableDataByName: db.Query(): %v\n", err)
} }
defer rows.Close() defer rows.Close()
data := make([]*types.OverviewTableData, 0)
for rows.Next() { for rows.Next() {
otd := new(types.OverviewTableData) b := new(types.Briefing)
p := new(types.Participant)
err := rows.Scan( if err := rows.Scan(&p.ID, &b.FirstName, &b.LastName, &b.Date, &b.Time, &b.State,
&otd.InstructorFirstName, &b.Location, &p.FirstName, &p.LastName, &p.Company); err != nil {
&otd.InstructorLastName, return nil, fmt.Errorf("*DB.ReadByName: rows.Scan(&p.ID, &b.FirstName,"+
&otd.BriefingDate, " &b.LastName, &b.Date, &b.Time, &b.State, &b.Location, &p.FirstName,"+
&otd.BriefingTime, " &p.LastName, &p.Company): %v\n", err)
&otd.BriefingLocation, }
&otd.BriefingDocumentName, b.Participants = append(b.Participants, p)
&otd.BriefingAsOf, bs = append(bs, b)
&otd.ParticipantFirstName,
&otd.ParticipantLastName,
&otd.ParticipantCompany,
)
if err != nil {
return nil, fmt.Errorf("*DB.ReadAllBriefings: rows.Scan(): %v\n", err)
} }
data = append(data, otd) reverseOrder(bs)
return bs, nil
} }
return &data, nil func (db *DB) GetLastID(i *int64) error {
row := db.QueryRow("SELECT id" +
" FROM " + db.Name +
" ORDER BY id DESC LIMIT 0, 1")
if err := row.Scan(i); err != nil {
return fmt.Errorf("*DB.GetLastID: row.Scan(&i): %v\n", err)
} }
func (db *DB) GetLastID(table string) (int, error) { return nil
var id int
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)
}
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
} }

View File

@ -5,26 +5,27 @@ import (
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"html/template" "html/template"
"log"
"net/http" "net/http"
"strconv" "strconv"
"time" "strings"
"streifling.com/jason/sicherheitsunterweisung/packages/data" "streifling.com/jason/sicherheitsunterweisung/packages/data"
"streifling.com/jason/sicherheitsunterweisung/packages/types" "streifling.com/jason/sicherheitsunterweisung/packages/types"
) )
// type questionData struct { type questionData struct {
// ID int64 ID int64
// Q types.Question Q types.Question
// I int I int
// J int J int
// } }
func DisplayTable(db *data.DB) http.HandlerFunc { func DisplayTable(db *data.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
bs, err := db.GetAllOverviewTableData() bs, err := db.ReadAll()
if err != nil { 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) 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 { func DisplaySearchResults(db *data.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.ReadByName(r.PostFormValue("search"))
if err != nil { 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) 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) { 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) { func generateUUID() string {
bs := make([]byte, 4) bs := make([]byte, 2)
if _, err := rand.Read(bs); err != nil { 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) { return func(w http.ResponseWriter, r *http.Request) {
login, err := generateUUID() *i++
if err != nil { login := fmt.Sprintf("%d", *i) + "-" + generateUUID()
http.Error(w, "AddParticipant: generateUUID(): "+fmt.Sprint(err), http.StatusInternalServerError) (*ls) = append(*ls, login)
}
(*sl) = append(*sl, login)
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "new", login) template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "new", login)
} }
} }
// TODO: Hier weiter machen, irgendwie die b.ID herausgeben, func SubmitForm(db *data.DB, i, j *int64) http.HandlerFunc {
// 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 {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
now := time.Now() b := new(types.Briefing)
briefing := new(types.Briefing)
// TODO: Dropdownmenü b.FirstName = r.PostFormValue("instructor-first")
// instructorFirstName := r.PostFormValue("instructor-first") b.LastName = r.PostFormValue("instructor-last")
// instructorLastName := 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") for ; *j <= *i; *j++ {
briefing.Time = now.Format("15:04:05") b.Participants = append(b.Participants, &types.Participant{
briefing.Location = r.PostFormValue("location") ID: *j,
briefing.DocumentName = r.PostFormValue("document") // TODO: in HTML einfügen Person: types.Person{
briefing.AsOf = r.PostFormValue("state") // TODO: Umbenennen FirstName: r.PostFormValue("participant-first-" + fmt.Sprint(*j)),
// briefing.InstructorID = r.PostFormValue("instructor-id") // TODO: aus Dropdown holen 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) { func newParticipant(l string) (*types.Participant, error) {
var err error
p := new(types.Participant) p := new(types.Participant)
p.ID, err = strconv.ParseInt(l, 10, 64) idInt, err := strconv.Atoi(strings.Split(l, "-")[0])
if err != nil { if err != nil {
return nil, fmt.Errorf("newParticipant: strconv.Atoi(idString): %v\n", err) return nil, fmt.Errorf("newParticipant: strconv.Atoi(idString): %v\n", err)
} }
p.ID = int64(idInt)
return p, nil 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) { return func(w http.ResponseWriter, r *http.Request) {
if loginIsCorrect(r.PostFormValue("login"), sl) { l := r.PostFormValue("login")
uuid, err := generateUUID()
if loginIsCorrect(l, ls) {
p, err := newParticipant(l)
if err != nil { 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 { } else {
template.Must(template.ParseFiles("templates/login.html")).ExecuteTemplate(w, "content", nil) template.Must(template.ParseFiles("templates/login.html")).ExecuteTemplate(w, "content", nil)
} }
} }
} }
// func readAnswer(r *http.Request, p *types.Participant, i int) error { func readAnswer(r *http.Request, p *types.Participant, i int) error {
// v, err := strconv.Atoi(r.PostFormValue("answer")) v, err := strconv.Atoi(r.PostFormValue("answer"))
// if err != nil { if err != nil {
// return fmt.Errorf("readAnswer: strconv.Atoi(): %v\n", err) return fmt.Errorf("readAnswer: strconv.Atoi(r.PostFormValue(\"answer\")): %v\n", err)
// } }
//
// p.Questions[i].Chosen = v p.Questions[i].Chosen = v
//
// return nil return nil
// } }
//
// func DisplayQuestion(i int, p *types.Participant) http.HandlerFunc { func DisplayQuestion(i int, p *types.Participant) http.HandlerFunc {
// return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
// if i == 0 { if i == 0 {
// p.FirstName = r.PostFormValue("participant-first-" + fmt.Sprintf("%d", p.ID)) p.FirstName = r.PostFormValue("participant-first-" + fmt.Sprintf("%d", p.ID))
// p.LastName = r.PostFormValue("participant-last-" + 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)) p.Company = r.PostFormValue("participant-company-" + fmt.Sprintf("%d", p.ID))
// } else { } else {
// if err := readAnswer(r, p, i-1); err != nil { if err := readAnswer(r, p, i-1); err != nil {
// http.Error(w, "DisplayQuestion: readAnswer(r, p, i): "+fmt.Sprint(err), http.StatusInternalServerError) http.Error(w, "DisplayQuestion: readAnswer(r, p, i): "+fmt.Sprint(err), http.StatusInternalServerError)
// } }
// } }
//
// data := new(questionData) data := new(questionData)
// data.ID = p.ID data.ID = p.ID
// data.Q = p.Questions[i] data.Q = p.Questions[i]
// data.I = i data.I = i
// data.J = i + 1 data.J = i + 1
//
// template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data) template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
// } }
// } }
//
// func DisplayTestResults(b *types.Briefing, p *types.Participant) http.HandlerFunc { func DisplayTestResults(b *types.Briefing, p *types.Participant) http.HandlerFunc {
// return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
// numQuestions := len(p.Questions) numQuestions := len(p.Questions)
// wrongAnswers := make([]int, 0) wrongAnswers := make([]int, 0)
// fmt.Println(wrongAnswers) fmt.Println(wrongAnswers)
//
// if err := readAnswer(r, p, numQuestions-1); err != nil { if err := readAnswer(r, p, numQuestions-1); err != nil {
// http.Error(w, "DisplayTestResults: readAnswer(r, p, i): "+fmt.Sprint(err), http.StatusInternalServerError) http.Error(w, "DisplayTestResults: readAnswer(r, p, i): "+fmt.Sprint(err), http.StatusInternalServerError)
// } }
//
// for i, q := range p.Questions { for i, q := range p.Questions {
// if q.Chosen != q.Correct { if q.Chosen != q.Correct {
// wrongAnswers = append(wrongAnswers, i) wrongAnswers = append(wrongAnswers, i)
// } }
// } }
//
// if wrongAnswers == nil { if wrongAnswers == nil {
// b.Participants = append(b.Participants, p) b.Participants = append(b.Participants, p)
// } else { } else {
// data := new(questionData) data := new(questionData)
// data.ID = p.ID data.ID = p.ID
// data.Q = p.Questions[0] data.Q = p.Questions[0]
// data.I = 0 data.I = 0
// data.J = data.I + 1 data.J = data.I + 1
// template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data) template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
// } }
//
// template.Must(template.ParseFiles("templates/results.html")).ExecuteTemplate(w, "content", nil) template.Must(template.ParseFiles("templates/results.html")).ExecuteTemplate(w, "content", nil)
// } }
// } }

View File

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