2 * Copyright (c) 2016-2017 S. Gilles <sgilles@math.umd.edu>
4 * Permission to use, copy, modify, and/or distribute this software
5 * for any purpose with or without fee is hereby granted, provided
6 * that the above copyright notice and this permission notice appear
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
13 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
14 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
15 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 #include <curl/curl.h>
27 #include <yajl_parse.h>
32 int prepare_csv_for_upload(char ***csv
, size_t rows
, size_t cols
, long
33 long **course_ids
, long long **assignment_ids
)
36 long long course_id
= 0;
37 long long assignment_id
= 0;
41 uint_fast8_t some_invalid
= 0;
43 if (!(*course_ids
= calloc((cols
- 2), sizeof **course_ids
))) {
50 if (!(*assignment_ids
= calloc((cols
- 2), sizeof **assignment_ids
))) {
58 fprintf(stderr
, "CSV has too few rows to contain data");
64 fprintf(stderr
, "CSV has too few cols to contain data");
70 strcmp(csv
[0][0], "Name") ||
72 strcmp(csv
[0][1], "ID")) {
73 fprintf(stderr
, "CSV does not contain required header\n");
78 for (j
= 1; j
< rows
; ++j
) {
82 strtoll(csv
[j
][1], &inv
, 0);
87 "Student ID in row %zu invalid\n", j
);
97 for (j
= 2; j
< cols
; ++j
) {
100 "CSV header cell %zu is blank - must contain "
101 "<course-id>:assignment-id>\n", j
+ 1);
107 course_id
= strtoll(csv
[0][j
], &past
, 0);
111 fprintf(stderr
, "CSV header cell %zu does not contain "
112 "assignment-id - must contain "
113 "<course-id>:assignment-id>\n", j
+ 1);
118 if (course_id
<= 0) {
119 fprintf(stderr
, "CSV header cell %zu references "
120 "impossible course-id %lld\n", j
+ 1,
126 assignment_id
= strtoll(past
+ 1, 0, 0);
128 if (assignment_id
<= 0) {
129 fprintf(stderr
, "CSV header cell %zu references "
130 "impossible assignment-id %lld\n", j
+
136 (*course_ids
)[j
- 2] = course_id
;
137 (*assignment_ids
)[j
- 2] = assignment_id
;
147 char *auth_token
= 0;
153 long long *course_ids
= 0;
154 long long *assignment_ids
= 0;
157 struct curl_httppost
*post
= 0;
159 setlocale(LC_ALL
, "");
163 curl_global_init(CURL_GLOBAL_DEFAULT
);
165 if (!(url_base
= get_url_base())) {
168 /* Error should have already been printed */
172 if (!(auth_token
= get_auth_token())) {
175 /* Error should have already been printed */
179 if ((ret
= read_csv(stdin
, &csv
, &rows
, &cols
))) {
180 fprintf(stderr
, "Error with CSV\n");
184 if ((ret
= prepare_csv_for_upload(csv
, rows
, cols
, &course_ids
,
186 /* Error should have already been printed */
190 for (j
= 2; j
< cols
; ++j
) {
192 len
= snprintf(0, 0, "%s/api/v1/courses/%lld/assignments/"
193 "%lld/submissions/update_grades", url_base
,
195 assignment_ids
[j
- 2]);
198 ret
= errno
= EOVERFLOW
;
203 if (!(built_uri
= malloc(len
+ 1))) {
209 sprintf(built_uri
, "%s/api/v1/courses/%lld/assignments/"
210 "%lld/submissions/update_grades", url_base
,
212 assignment_ids
[j
- 2]);
214 if (!(post
= make_update_grade_post(csv
, j
, rows
))) {
215 /* XXX: pass back proper error code from make_update_grade_post() */
220 if ((ret
= send_and_id_scan(built_uri
, auth_token
, post
, "POST",
233 for (j
= 0; j
< rows
; ++j
) {
235 for (k
= 0; k
< cols
; ++k
) {
247 curl_global_cleanup();
251 free(assignment_ids
);