-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetrics.js
153 lines (123 loc) · 4.01 KB
/
metrics.js
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
/**
* Temporarily stores session data.
*/
var data;
/*******************
* Cookie Handling *
******************/
/**
* Creates a cookie called 'SonificationMetricData' containing the information
* stored in 'data'.
*/
function bakeCookie() {
var cookie = ["SonificationMetricData=", JSON.stringify(data), "; domain=.", window.location.host.toString(), "; path=/;"].join('');
document.cookie = cookie;
}
/**
* Deletes the 'SonificationMetricData' cookie.
*
* (Currently unused, keeping for future need/reference.)
*/
function deleteCookie() {
document.cookie = ["SonificationMetricData=; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.", window.location.host.toString()].join('');
}
/**
* Inserts a string representation of the 'data' object into an element at a
* specified id and disables said element to prevent tampering by the user.
*
* (Called when submit page is loaded.)
*
* @param id ID of the element in which to submit the cookie
*/
function submitCookie(id) {
var element = document.getElementById(id);
//element.value = JSON.stringify(data);
//element.disabled = true;
//element.style.display = "none";
element.disabled = false;
element.style.display = "block";
parseDate(id);
}
/**
* Initializes 'data' with the contents of the 'SonificationMetricData' cookie
* if it exists or an empty array if it does not.
*
* (Called each time a graph is loaded.)
*/
function readCookie() {
var result = document.cookie.match(new RegExp("SonificationMetricData=([^;]+)"));
if (result && (result = JSON.parse(result[1]))) {
data = result;
} else {
data = {"sessions":[]};
}
}
/**
* Parses the 'data' string, with the contents of the cookie
* @param id ID of the element in which to submit the cookie
*
*/
function parseDate(id) {
var element = document.getElementById(id);
var stringElement;
element.value = "";
for(var i = 0; i < data.sessions.length; i++) {
stringElement = "Graph: " + data.sessions[i].graph;
stringElement += "; Replays: " + data.sessions[i].replays;
stringElement += "; Total time: " + data.sessions[i].totalTime / 1000; //Converting to seconds
stringElement += " - ";
element.value += stringElement;
element.disabled = true;
}
}
/*********************
* Question Handling *
********************/
/**
* Initializes an object representing the user's interaction with the current
* audio graph and appends it to 'data'.
*
* (Called each time a graph is loaded.)
*
* @param name Name of the function currently being represented (i.e. "f1").
*/
function initQuestion(name) {
var date = new Date();
session = {
graph: name, // Name of graph (i.e., "f1")
startTime: date.getTime(), // Time the user loaded the page (in milliseconds since 01/01/1970)
replays: 0, // Number of times the sound is played
draws: null // Number of times the user has drawn an interactive graph, if applicable.
};
data.sessions.push(session);
}
/**
* Updates the object representing the user's interaction with the current audio
* graph.
*
* (Called each time a graph is played.)
*/
function updateQuestion() {
var date = new Date();
var num = data.sessions.length - 1; // Current graph is most recent in array.
// Update replay count.
data.sessions[num].replays = data.sessions[num].replays + 1;
// Stores the last time the user played the graph (same format as 'startTime').
data.sessions[num].endTime = date.getTime();
// Determines and stores the total time the user interacted with the graph (in milliseconds).
data.sessions[num].totalTime = data.sessions[num].endTime - data.sessions[num].startTime;
bakeCookie();
}
/**
* Iterates the graph draws counter.
*
* (Called each time a graph is drawn.)
*/
function updateDrawCount() {
var num = data.sessions.length - 1; // Current graph is most recent in array.
if (data.sessions[num].draws) {
data.sessions[num].draws += 1;
} else {
data.sessions[num].draws = 1;
}
}