2 * Copyright (c) 2016-2019 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>
33 prepare_csv_for_upload(char ***csv
, size_t rows
, size_t cols
, long
34 long **course_ids
, long long **assignment_ids
)
37 long long course_id
= 0;
38 long long assignment_id
= 0;
42 uint_fast8_t some_invalid
= 0;
44 if (!(*course_ids
= calloc((cols
- 2), sizeof **course_ids
))) {
51 if (!(*assignment_ids
= calloc((cols
- 2), sizeof **assignment_ids
))) {
59 fprintf(stderr
, "CSV has too few rows to contain data");
65 fprintf(stderr
, "CSV has too few cols to contain data");
71 strcmp(csv
[0][0], "Name") ||
73 strcmp(csv
[0][1], "ID")) {
74 fprintf(stderr
, "CSV does not contain required header\n");
79 for (j
= 1; j
< rows
; ++j
) {
83 strtoll(csv
[j
][1], &inv
, 0);
88 "Student ID in row %zu invalid\n", j
);
98 for (j
= 2; j
< cols
; ++j
) {
101 "CSV header cell %zu is blank - must contain "
102 "<course-id>:assignment-id>\n", j
+ 1);
108 course_id
= strtoll(csv
[0][j
], &past
, 0);
112 fprintf(stderr
, "CSV header cell %zu does not contain "
113 "assignment-id - must contain "
114 "<course-id>:assignment-id>\n", j
+ 1);
119 if (course_id
<= 0) {
120 fprintf(stderr
, "CSV header cell %zu references "
121 "impossible course-id %lld\n", j
+ 1,
127 assignment_id
= strtoll(past
+ 1, 0, 0);
129 if (assignment_id
<= 0) {
130 fprintf(stderr
, "CSV header cell %zu references "
131 "impossible assignment-id %lld\n", j
+
137 (*course_ids
)[j
- 2] = course_id
;
138 (*assignment_ids
)[j
- 2] = assignment_id
;
149 char *auth_token
= 0;
155 long long *course_ids
= 0;
156 long long *assignment_ids
= 0;
159 struct curl_httppost
*post
= 0;
161 setlocale(LC_ALL
, "");
165 curl_global_init(CURL_GLOBAL_DEFAULT
);
167 if (!(url_base
= get_url_base())) {
170 /* Error should have already been printed */
174 if (!(auth_token
= get_auth_token())) {
177 /* Error should have already been printed */
181 if ((ret
= read_csv(stdin
, &csv
, &rows
, &cols
))) {
182 fprintf(stderr
, "Error with CSV\n");
186 if ((ret
= prepare_csv_for_upload(csv
, rows
, cols
, &course_ids
,
188 /* Error should have already been printed */
192 for (j
= 2; j
< cols
; ++j
) {
194 len
= snprintf(0, 0, "%s/api/v1/courses/%lld/assignments/"
195 "%lld/submissions/update_grades", url_base
,
197 assignment_ids
[j
- 2]);
200 ret
= errno
= EOVERFLOW
;
205 if (!(built_uri
= malloc(len
+ 1))) {
211 sprintf(built_uri
, "%s/api/v1/courses/%lld/assignments/"
212 "%lld/submissions/update_grades", url_base
,
214 assignment_ids
[j
- 2]);
216 if (!(post
= make_update_grade_post(csv
, j
, rows
))) {
217 /* XXX: pass back proper error code from make_update_grade_post() */
222 if ((ret
= send_and_id_scan(built_uri
, auth_token
, post
, "POST",
235 for (j
= 0; j
< rows
; ++j
) {
237 for (k
= 0; k
< cols
; ++k
) {
249 curl_global_cleanup();
253 free(assignment_ids
);