This repository has been archived by the owner on Jul 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
em.go
74 lines (63 loc) · 1.63 KB
/
em.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package handlers
import (
"fmt"
"html/template"
"net/http"
"github.com/andrewcharlton/school-dashboard/analysis/group"
"github.com/andrewcharlton/school-dashboard/env"
)
type emList struct {
Name string
Percentages []float64
}
// EnglishAndMaths produces a summary page with the number/percentage
// of students achieving passes in English and/or Maths.
func EnglishAndMaths(e env.Env) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if redir := checkRedirect(e, w, r, 2); redir {
return
}
header(e, w, r, 2)
defer footer(e, w, r)
f := getFilter(e, r)
g, err := e.GroupByFilter(f)
if err != nil {
fmt.Fprintf(w, "Error: %v", err)
return
}
groups := []group.Group{{}, {}, {}, {}}
for _, s := range g.Students {
eng := s.EBaccArea("E").Achieved
maths := s.EBaccArea("M").Achieved
switch {
case eng && maths:
groups[2].Students = append(groups[2].Students, s)
case eng:
groups[0].Students = append(groups[0].Students, s)
case maths:
groups[1].Students = append(groups[1].Students, s)
default:
groups[3].Students = append(groups[3].Students, s)
}
}
pcts := []float64{}
for _, grp := range groups {
pcts = append(pcts, float64(len(grp.Students))/float64(len(g.Students)))
}
data := struct {
Query template.URL
Names []string
Groups []group.Group
Pcts []float64
}{
template.URL(r.URL.RawQuery),
[]string{"English Only", "Mathematics Only", "English & Maths", "Neither"},
groups,
pcts,
}
err = e.Templates.ExecuteTemplate(w, "em.tmpl", data)
if err != nil {
fmt.Fprintf(w, "Error: %v", err)
}
}
}