-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathviewReport.gs
More file actions
240 lines (188 loc) · 8.17 KB
/
viewReport.gs
File metadata and controls
240 lines (188 loc) · 8.17 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
// File: view_report.gas
// Description:
// This file contains all relevant functions for displaying and emailing
// the report. Note that the URL for the histogram chart is generated by
// formHistorgramURL(), and is located in 'grading.gas'.
// viewReport: Displays the UI for grading report.
function viewReport()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var grades_sheet = getSheetWithGrades(ss);
if (grades_sheet === null)
{
Browser.msgBox(langstr("FLB_STR_NOTIFICATION"),
langstr("FLB_STR_CANNOT_FIND_GRADES_MSG") + langstr("FLB_STR_SHEETNAME_GRADES"),
Browser.Buttons.OK);
return;
}
var grades_sheet_is_valid = gwsGradesSheetIsValid(grades_sheet);
// Check if the existing Grades sheet is valid.
if (!grades_sheet_is_valid)
{
// Existing Grades sheet is invalid! Cannot continue with re-grading.
UI.showInvalidGradesSheetMessage();
return;
}
/* form the histogram */
var points_possible = grades_sheet.getRange(GRADES_POINTS_POSSIBLE_CELL).getValue();
var histogram_buckets = createHistogramBuckets(grades_sheet, points_possible);
// Save the url of the historgram chart.
var histogram_url = formHistogramURL(histogram_buckets);
var dp = PropertiesService.getDocumentProperties();
dp.setProperty(DOC_PROP_HISTOGRAM_URL, histogram_url);
createReportUI(ss, grades_sheet);
}
function createHistogramBuckets(grades_sheet, points_possible)
{
var dp = PropertiesService.getDocumentProperties();
var num_stud_identifiers = Number(dp.getProperty(DOC_PROP_NUM_STUDENT_IDENTIFIERS));
var score_col_num = 1 + num_stud_identifiers + 1;
var score_col_ltr = convertColNumToColLetter(score_col_num);
var col_range = score_col_ltr + ":" + score_col_ltr; // i.e. "D:D"
var scores = grades_sheet.getRange(col_range).getValues();
// determine the max points possible. could be the case if teacher
// gave bonus pts.
for (var i=0; i < scores.length; i++)
{
var s = scores[i][0];
if (!isNaN(s) && (s > points_possible))
{
points_possible = s;
}
}
// round down partial credit for the sake of bucketing (i.e. 1.6 -> 1)
points_possible = Math.floor(points_possible);
// Track number of submissions had that each possible number
// of points for its score. Init all to 0. Ranges from 0 to points_possible.
var histogram_buckets = new Array(points_possible + 1);
var histogram_len = histogram_buckets.length;
while (--histogram_len >= 0)
{
histogram_buckets[histogram_len] = 0;
}
for (var i=0; i < scores.length; i++)
{
var s = scores[i][0];
if ((s !== "") && !isNaN(s))
{
s = Math.floor(s); // round down partial credit for the sake of bucketing (i.e. 1.6 -> 1).
// Track the number of submissions that had this total score,
// for the histogram.
histogram_buckets[s] += 1;
}
}
return histogram_buckets;
}
function formHistogramURL(histogram_buckets)
{
var max_val = 0;
for (var i = 0; i < histogram_buckets.length; i++)
{
if (histogram_buckets[i] > max_val)
{
max_val = histogram_buckets[i];
}
}
var url = "http://chart.apis.google.com/chart?chxt=x,y,x,y&chbh=a,0,0&chs=650x280";
url += "&cht=bvg&chco=6699ff&chtt=Histogram%20of%20Grades";
url += "&chds=0," + max_val + "&chxr=1,0," + max_val + ",1";
url += "&chxl=0:";
var points_possible = histogram_buckets.length - 1;
for (var i=0; i <= points_possible; i++)
{
url += "%7C" + String(i);
}
//url+= "|2:|Points%20Scored|3:|Submissions&chxp=2,50|3,50";
url+= "%7C2:%7CPoints%20Scored%7C3:%7CSubmissions&chxp=2,50%7C3,50";
url += "&chd=t:";
for (var i=0; i <= points_possible; i++)
{
url += histogram_buckets[i];
if (i < (points_possible))
{
url += ",";
}
}
return url;
}
function createReportUI(ss, grades_sheet)
{
var dp = PropertiesService.getDocumentProperties();
var html = HtmlService.createHtmlOutput()
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setWidth(680).setHeight(500);
var title = langstr("FLB_STR_VIEW_REPORT_WINDOW_TITLE");
html.setTitle(title);
var h = '<!DOCTYPE html><link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">';
h += '<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>';
var gws = new GradesWorksheet(ss, INIT_TYPE_GRADED_META, -1);
var points_possible = gws.getPointsPossible();
var avg_subm_score = gws.getAverageScore().toFixed(2);
var num_subm = gws.getNumGradedSubmissions();
var email_address = Session.getEffectiveUser().getEmail();
var ss_name = ss.getName();
var chart_url = dp.getProperty(DOC_PROP_HISTOGRAM_URL);
var onclick="$('#emailme').prop('disabled',true); google.script.run.withSuccessHandler(vrEmailSent).emailReportHandler()";
// "Email Me Report" button
h += '<div id="notice" style="color: #FF8C00;display:none;"></div>';
h += '<div style="float:right;padding-right:25px;">'
h += '<input type="button" class="action" id="emailme" onclick="' + onclick + '" value="' + langstr("FLB_STR_VIEW_REPORT_BUTTON_EMAIL_ME") + '"/>';
h += '</div>';
// Summary at top
h += '<div>';
h += '<table border=0>';
h += '<tr><td><b>' + langstr("FLB_STR_GRADE_SUMMARY_TEXT_REPORT_FOR") + ': ' + ss_name + '</b></td></tr>';
h += '<tr><td>' + langstr("FLB_STR_GRADE_SUMMARY_TEXT_POINTS_POSSIBLE") + ': ' + points_possible + '</td></tr>';
h += '<tr><td>' + langstr("FLB_STR_GRADE_SUMMARY_TEXT_AVERAGE_POINTS") + ': ' + avg_subm_score + '</td></tr>';
h += '<tr><td>' + langstr("FLB_STR_GRADE_SUMMARY_TEXT_COUNTED_SUBMISSIONS") + ': ' + num_subm + '</td></tr>';
h += '</table>';
h += '</div>';
// Histogram
h += '<br><br>';
h += '<img src="' + chart_url + '">';
// Javascript
var msg = langstr("FLB_STR_VIEW_REPORT_EMAIL_NOTIFICATION") + ': ' + email_address;
h += "<script>function vrEmailSent() { $('#notice').html('" + msg + "'); $('#notice').show(200); \
$('#emailme').prop('disabled',false); }</script>";
html.append(h);
// show the report
SpreadsheetApp.getUi()
.showModalDialog(html, title);
}
function emailReportHandler()
{
var dp = PropertiesService.getDocumentProperties();
//var app = UiApp.getActiveApplication();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var gws = new GradesWorksheet(ss, INIT_TYPE_GRADED_META, -1);
var points_possible = gws.getPointsPossible();
var avg_subm_score = gws.getAverageScore().toFixed(2);
var num_subm = gws.getNumGradedSubmissions();
var title = ss.getName();
var chart_url = dp.getProperty(DOC_PROP_HISTOGRAM_URL);
var title = ss.getName();
var email_address = Session.getEffectiveUser().getEmail();
var msg_title = langstr("FLB_STR_GRADE_SUMMARY_TEXT_REPORT_FOR") + ": " + title;
// form the html to email
var html_body = '<html><body bgcolor="white">';
html_body += '<p><b>' + langstr("FLB_STR_GRADE_SUMMARY_TEXT_REPORT_FOR") + ': <a href="' + ss.getUrl() + '">' + title + '</a></b>';
html_body += '</p>';
html_body += '<table border=0 cellspacing=2>';
html_body += '<tr><td>' + langstr("FLB_STR_GRADE_SUMMARY_TEXT_POINTS_POSSIBLE") + ':</td><td>' + points_possible + '</td></tr>';
html_body += '<tr><td>' + langstr("FLB_STR_GRADE_SUMMARY_TEXT_AVERAGE_POINTS") + ':</td><td>' + avg_subm_score + '</td></tr>';
html_body += '<tr><td>' + langstr("FLB_STR_GRADE_SUMMARY_TEXT_COUNTED_SUBMISSIONS") + ':</td><td>' + num_subm + '</td></tr>';
html_body += '</table><br>';
html_body += '<img src="' + chart_url + '">';
html_body += '</body></html>';
try
{
MailApp.sendEmail(email_address, msg_title, "",
{htmlBody: html_body, noReply: true, name: "Assignment Grader"});
}
catch(exception)
{
Debug.error("emailReportHandler() - failed to email report - " +
exception);
}
return;
}