/
nswWritingPearsonY9.go
137 lines (114 loc) · 3.57 KB
/
nswWritingPearsonY9.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package reports
import (
"bufio"
"fmt"
"log"
"strconv"
"strings"
"github.com/iancoleman/strcase"
"github.com/nsip/dev-nrt/helper"
"github.com/nsip/dev-nrt/records"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
type NswWritingPearsonY9 struct {
baseReport // embed common setup capability
cfh helper.CodeframeHelper
}
//
// Creates fixed format report of writing rubric scores in pearson format
// of 205 char lines.
//
func NswWritingPearsonY9Report(cfh helper.CodeframeHelper) *NswWritingPearsonY9 {
r := NswWritingPearsonY9{cfh: cfh}
r.initialise("./config/NswWritingPearsonY9.toml")
r.printStatus()
return &r
}
//
// implement the EventPipe interface, core work of the
// report engine.
//
func (r *NswWritingPearsonY9) ProcessEventRecords(in chan *records.EventOrientedRecord) chan *records.EventOrientedRecord {
out := make(chan *records.EventOrientedRecord)
go func() {
defer close(out)
// open the fixed format file writer
w := bufio.NewWriter(r.outF)
defer r.outF.Close()
defer w.Flush()
var result, paddedResult string
var length int
var convErr error
var row strings.Builder // single string record for this format
for eor := range in {
if !r.config.activated { // only process if activated
out <- eor
continue
}
if !eor.IsWritingResponse() { // only process writing responses
out <- eor
continue
}
if eor.GetValueString("CalculatedFields.YrLevel") != "9" { // only for yr 5
out <- eor
continue
}
//
// generate any calculated fields required
//
eor.CalculatedFields = r.calculateFields(eor)
//
// now loop through the ouput definitions to create a
// 'row' of results
//
//
// now loop through the ouput definitions to create a
// 'row' of results
//
for i, query := range r.config.queries {
result = eor.GetValueString(query)
length, convErr = strconv.Atoi(r.config.header[i])
if convErr != nil {
log.Println("WARNING: unexpected value for field length in ", r.configFileName, r.config.header[i])
}
paddingToken := defaultPaddingToken
if r.config.header[i][0] == '0' {
paddingToken = paddingToken
}
paddedResult = PadLeft(result, length, defaultPaddingToken)
row.WriteString(paddedResult)
}
// write the row to the output file
if _, err := fmt.Fprintln(w, row.String()); err != nil {
fmt.Println("Warning: error writing record to output file:", r.config.name, err)
}
row.Reset()
out <- eor
}
}()
return out
}
//
// generates a block of json that can be added to the
// record containing values that are not in the original data
//
//
func (r *NswWritingPearsonY9) calculateFields(eor *records.EventOrientedRecord) []byte {
json := eor.CalculatedFields // keep any existing data
// create truncated format birthdate
fullDob := eor.GetValueString("StudentPersonal.PersonInfo.Demographics.BirthDate")
truncDob := strings.ReplaceAll(fullDob, "-", "")
json, _ = sjson.SetBytes(json, "CalculatedFields.TruncatedDOB", truncDob)
// create blank placeholder
json, _ = sjson.SetBytes(json, "CalculatedFields.BlankToken", " ")
gjson.GetBytes(eor.NAPStudentResponseSet, "NAPStudentResponseSet.TestletList.Testlet.0.ItemResponseList.ItemResponse.0.SubscoreList.Subscore").
ForEach(func(key, value gjson.Result) bool {
ssType := strcase.ToCamel(value.Get("SubscoreType").String())
ssValue := value.Get("SubscoreValue").String()
path := fmt.Sprintf("CalculatedFields.WritingSubscore.%s.Score", ssType)
json, _ = sjson.SetBytes(json, path, ssValue)
return true // keep iterating
})
return json
}