~pkal/survey

7477f8adf8fd69fc1d25f9f6d2dbc7e9d53c3475 — Philip K 1 year, 1 month ago 4612604 master
Fixed golint issues
1 files changed, 40 insertions(+), 42 deletions(-)

M survey.go
M survey.go => survey.go +40 -42
@@ 18,7 18,22 @@ import (
	_ "github.com/mattn/go-sqlite3"
)

const page = `<!DOCTYPE html>
type question struct {
	Type    string
	Text    string
	Options []string
	Other   bool
	Require bool
}

type survey struct {
	Name      string
	Comment   string
	Questions []*question
}

const (
	page = `<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="utf-8" />


@@ 147,12 162,10 @@ sup.req {
<label for="captcha">
Sovle the captcha to finish the survey (if necessary, reload the page):
</label>`

const captchat = `
	captchat = `
<img id="captcha" alt="Captcha Image" src="/captcha/{{ . }}.png" />
<input type="hidden" name="captcha-id" value="{{ . }}" />`

const footer = `<input type="text" name="captcha" autocomplete="off" required />
	footer = `<input type="text" name="captcha" autocomplete="off" required />

<hr/>



@@ 163,33 176,18 @@ const footer = `<input type="text" name="captcha" autocomplete="off" required />
</body>
</html>`

const (
	TypeSingle   = "single"
	TypeMultiple = "multiple"
	TypeText     = "text"
	TypeHeading  = "heading"
	typeSingle   = "single"
	typeMultiple = "multiple"
	typeText     = "text"
	typeHeading  = "heading"
)

type Question struct {
	Type    string
	Text    string
	Options []string
	Other   bool
	Require bool
}

type Survey struct {
	Name      string
	Comment   string
	Questions []*Question
}

var (
	T              *template.Template
	t              *template.Template
	db             *sql.DB
	insertResponse *sql.Stmt
	insertAnswer   *sql.Stmt
	survey         *Survey
	survey         *survey
	rawPage        []byte

	l = log.New(os.Stderr, "", 0)


@@ 197,7 195,7 @@ var (

func index(w http.ResponseWriter, r *http.Request) {
	w.Write(rawPage)
	err := T.Execute(w, captcha.New())
	err := t.Execute(w, captcha.New())
	if err != nil {
		l.Print(err)
	}


@@ 246,7 244,7 @@ func submit(w http.ResponseWriter, r *http.Request) {
		stmt := tx.Stmt(insertAnswer)

		switch q.Type {
		case TypeSingle:
		case typeSingle:
			other := r.FormValue(fmt.Sprintf("question-%d-other", i))
			if other != "" {
				_, err = stmt.Exec(i, -1, other)


@@ 267,8 265,8 @@ func submit(w http.ResponseWriter, r *http.Request) {
			}

			_, err = stmt.Exec(i, qid, nil)
		case TypeMultiple:
			for j, _ := range q.Options {
		case typeMultiple:
			for j := range q.Options {
				value := r.FormValue(fmt.Sprintf("question-%d-%d", i, j))
				if value == "" {
					continue


@@ 284,7 282,7 @@ func submit(w http.ResponseWriter, r *http.Request) {
				val := r.FormValue(fmt.Sprintf("question-%d-other-val", i))
				_, err = stmt.Exec(i, -1, val)
			}
		case TypeText:
		case typeText:
			value := r.FormValue(fmt.Sprintf("question-%d", i))
			if value != "" {
				_, err = stmt.Exec(i, nil, value)


@@ 316,9 314,9 @@ func submit(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "Sucsessfuly submitted survey results! Thank you for participating.")
}

func parseSummary(in io.Reader) *Survey {
	var q *Question
	var s Survey
func parseSummary(in io.Reader) *survey {
	var q *question
	var s survey
	var comment bytes.Buffer

	const (


@@ 350,7 348,7 @@ func parseSummary(in io.Reader) *Survey {
				continue
			}

			q = &Question{Text: line[1:]}
			q = &question{Text: line[1:]}
			s.Questions = append(s.Questions, q)

			if line[0] == '*' {


@@ 364,16 362,16 @@ func parseSummary(in io.Reader) *Survey {
				q.Other = true
				fallthrough
			case 'M':
				q.Type = TypeMultiple
				q.Type = typeMultiple
			case 's':
				q.Other = true
				fallthrough
			case 'S':
				q.Type = TypeSingle
				q.Type = typeSingle
			case 'T':
				q.Type = TypeText
				q.Type = typeText
			case 'H':
				q.Type = TypeHeading
				q.Type = typeHeading
				continue
			default:
				l.Fatalf("Invalid type %v", line[0])


@@ 414,15 412,15 @@ func main() {
	l.Printf("Parsed %d questions.", len(survey.Questions))

	var buf bytes.Buffer
	T, err = template.New("page").Parse(page)
	t, err = template.New("page").Parse(page)
	if err != nil {
		l.Fatal(err)
	}
	err = T.Execute(&buf, survey)
	err = t.Execute(&buf, survey)
	rawPage = buf.Bytes()
	l.Printf("Rendered survey.")

	T, err = template.New("captcha").Parse(captchat)
	t, err = template.New("captcha").Parse(captchat)
	if err != nil {
		l.Fatal(err)
	}