-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProgram.cs
More file actions
363 lines (314 loc) · 14.7 KB
/
Program.cs
File metadata and controls
363 lines (314 loc) · 14.7 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.IO.Compression;
namespace TabulateSmarterTestResults
{
class Program
{
static string sSyntax =
@"This tool tabulates XML test results files in SmarterApp Test Results
Transmission Format into CSV files that match the SmarterApp Data Warehouse
Student Assessment format and Data Warehouse Item Level format.
Command-line parameters:
-i <input file>
Filenames may include wildcards. If the file is a .zip then all .xml
files in the .zip will be processed. The -i parameter may be repeated
to process multiple input sources.
-os <output file>
Output file for student test results. This will be a .csv formatted file that
contains test results including student information and test scores. Include the
.csv file extension.
-oi <output file>
Output file for item results. This will be a .csv formatted file that
contains item scores. Include the .csv file extension.
-hid <passphrase>
Hash the Student ID using the specified passphrase as a key. And place the
result in the AlternateSSID (ExternalSSID) field. This is usually combined
with de-identification See below for details of the hashing algorithm.
-did <flags>
De-identify the data by setting certain fields to blank. The flags indicate
which fields should be set to blank. See below for details.
-fmt <format>
Specifies the output format. Default is 'dw' which is the format accepted
by the Data Warehouse. The 'all' format includes all fields defined in the
Smarter Balanced ""Test Results Logical Data Model"". The 'allshort' format
includes all fields but only includes the student response if it is 10
characters or shorter in length.
-notxl
When using the 'all' format (-fmt all) studentID and AlternateSSID fields are
padded with a trailing tab character to prevent Microsoft Excel from treating
them as numbers. This option supporesses the tab padding for use by other CSV
readers. The data warehouse format never pads and this option has no effect
in that mode.
Student ID Hash:
The Student ID hash is prepared as follows:
1. The passphrase is encoded into UTF8 and hashed into a 160-bit key using
the SHA1 algorithm. Case is preserved.
2. The student id is encoded into UTF8 and hashed into a 160-bit digest
using the key and the HMAC-SHA1 algorithm.
3. The resulting hash from step 2 is converted to an upper-case hexadecimal
string.
4. The hexadecimal string is placed in the AlternateSSID (ExternalSSID)
field replacing any existing contents.
The De-Identification Option (-did):
One or more of the following flags should follow the -did option.
A space should be between -did and the flags. No space should be between
the flags. For example, ""-did inb"" When values are removed, the fields
are set to blank. Since the sensitivity of student groups is unknown any
de-identification option will cause student groups to be removed.
i Replace the StudentID with the AlternateSSID. If the AlternateSSID
is blank then StudentID will be blank in both the student and item
files. If the -hid is specified then the the newly generated
AlternateSSID will be used for both AlternateSSID and StudentID.
n Remove first, middle, and last names.
b Remove birthdate.
d Remove demographic information (gender, race, ethnicity)
s Remove school and district information. Also removes session location
id and name.
Syntax example:
TabulateSmarterTestResults.exe -i testresults.zip -os studentresults.csv -oi itemresults.csv -hid smarter -did inbds -fmt all
";
static int s_ErrorCount = 0;
static void Main(string[] args)
{
try
{
List<string> inputFilenames = new List<string>();
string osFilename = null;
string oiFilename = null;
bool notExcel = false;
string hashPassPhrase = null;
DIDFlags didFlags = DIDFlags.None;
OutputFormat outputFormat = OutputFormat.Dw;
int maxResponse = 0;
bool help = false;
for (int i=0; i<args.Length; ++i)
{
switch (args[i])
{
case "-h":
help = true;
break;
case "-i":
{
++i;
if (i >= args.Length) throw new ArgumentException("Invalid command line. '-i' option not followed by filename.");
inputFilenames.Add(args[i]);
}
break;
case "-os":
{
++i;
if (i >= args.Length) throw new ArgumentException("Invalid command line. '-o' option not followed by filename.");
if (osFilename != null) throw new ArgumentException("Only one output file may be specified.");
string filename = Path.GetFullPath(args[i]);
osFilename = filename;
}
break;
case "-oi":
{
++i;
if (i >= args.Length) throw new ArgumentException("Invalid command line. '-o' option not followed by filename.");
if (oiFilename != null) throw new ArgumentException("Only one output file may be specified.");
string filename = Path.GetFullPath(args[i]);
oiFilename = filename;
}
break;
case "-notxl":
notExcel = true;
break;
case "-hid":
{
++i;
if (i >= args.Length) throw new ArgumentException("Invalid command line. '-hid' option not followed by passphrase.");
hashPassPhrase = args[i];
}
break;
case "-did":
{
++i;
if (i >= args.Length) throw new ArgumentException("Invalid command line. '-did' option not followed by flags.");
foreach (char c in args[i])
{
switch (Char.ToLowerInvariant(c))
{
case 'i':
didFlags |= DIDFlags.Id;
break;
case 'n':
didFlags |= DIDFlags.Name;
break;
case 'b':
didFlags |= DIDFlags.Birthdate;
break;
case 'd':
didFlags |= DIDFlags.Demographics;
break;
case 's':
didFlags |= DIDFlags.School;
break;
default:
throw new ArgumentException(string.Format("Invalid command line. '.did' flag '{0}' is undefined.", c));
}
}
}
break;
case "-fmt":
++i;
if (i >= args.Length) throw new ArgumentException("Invalid command line. '-fmt' option not followed by format type.");
switch (args[i])
{
case "dw":
outputFormat = OutputFormat.Dw;
break;
case "all":
outputFormat = OutputFormat.All;
break;
case "allshort":
outputFormat = OutputFormat.All;
maxResponse = 10;
break;
default:
throw new ArgumentException(string.Format("Invalid command line. Output format '{0}' is unknown.", args[i]));
}
break;
default:
throw new ArgumentException(string.Format("Unknown command line option '{0}'. Use '-h' for syntax help.", args[i]));
}
}
if (help || args.Length == 0)
{
Console.WriteLine(sSyntax);
}
else
{
if (inputFilenames.Count == 0 || (osFilename == null && oiFilename == null)) throw new ArgumentException("Invalid command line. Use '-h' for syntax help");
if (osFilename != null)
Console.WriteLine("Writing student assessment results to: " + osFilename);
if (oiFilename != null)
Console.WriteLine("Writing item level results to: " + oiFilename);
using (ToCsvProcessor processor = new ToCsvProcessor(osFilename, oiFilename, outputFormat))
{
processor.HashPassPhrase = hashPassPhrase;
processor.DIDFlags = didFlags;
processor.MaxResponse = maxResponse;
processor.NotExcel = notExcel;
foreach (string filename in inputFilenames)
{
ProcessInputFilename(filename, processor);
}
}
}
if (s_ErrorCount > 0)
{
Console.Error.WriteLine("{0} total errors", s_ErrorCount);
}
}
catch (Exception err)
{
Console.WriteLine();
#if DEBUG
Console.WriteLine(err.ToString());
#else
Console.WriteLine(err.Message);
#endif
}
if (Win32Interop.ConsoleHelper.IsSoleConsoleOwner)
{
Console.Write("Press any key to exit.");
Console.ReadKey(true);
}
}
static void ProcessInputFilename(string filenamePattern, ITestResultProcessor processor)
{
int count = 0;
string directory = Path.GetDirectoryName(filenamePattern);
if (string.IsNullOrEmpty(directory)) directory = Environment.CurrentDirectory;
string pattern = Path.GetFileName(filenamePattern);
foreach (string filename in Directory.GetFiles(directory, pattern))
{
switch (Path.GetExtension(filename).ToLower())
{
case ".xml":
ProcessInputTrtFile(filename, processor);
break;
case ".zip":
ProcessInputZipFile(filename, processor);
break;
default:
throw new ArgumentException(string.Format("Input file '{0}' is of unsupported time. Only .xml and .zip are supported.", filename));
}
++count;
}
if (count == 0) throw new ArgumentException(string.Format("Input file '{0}' not found!", filenamePattern));
}
static void ProcessInputTrtFile(string filename, ITestResultProcessor processor)
{
Console.WriteLine("Processing: " + filename);
using (FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
try
{
processor.ProcessResult(stream);
}
catch (Exception err)
{
Console.Error.WriteLine("Error processing input file '{0}\r\n{1}\r\n", filename, err.Message);
++s_ErrorCount;
}
}
Console.WriteLine();
}
static void ProcessInputZipFile(string filename, ITestResultProcessor processor)
{
Console.WriteLine("Processing: " + filename);
using (ZipArchive zip = ZipFile.Open(filename, ZipArchiveMode.Read))
{
foreach(ZipArchiveEntry entry in zip.Entries)
{
// Must not be folder (empty name) and must have .xml extension
if (!string.IsNullOrEmpty(entry.Name) && Path.GetExtension(entry.Name).Equals(".xml", StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine(" Processing: " + entry.FullName);
using (Stream stream = entry.Open())
{
try
{
processor.ProcessResult(stream);
}
catch (Exception err)
{
Console.Error.WriteLine("Error processing input file '{0}/{1}\r\n{2}\r\n", filename, entry.FullName, err.Message);
++s_ErrorCount;
}
}
}
}
}
Console.WriteLine();
}
}
[Flags]
enum DIDFlags : int
{
None = 0,
Id = 1, // Student ID
Name = 2, // Student Name
Birthdate = 4,
Demographics = 8, // Sex, Race, Ethnicity
School = 16 // School and districtID or ExternalSSID is unaffected
}
enum OutputFormat : int
{
Dw = 0, // Data Warehouse Format
All = 1 // All fields format
}
interface ITestResultProcessor : IDisposable
{
void ProcessResult(Stream input);
}
}