Don't print an error message for empty courses
[nci.git] / nci-put-assignment-grades.c
blob7e9bf2d9fe89536662c8eed3ca9573d16fbd4a1a
1 /*
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
7 * in all copies.
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.
18 #include <errno.h>
19 #include <locale.h>
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
26 #include <curl/curl.h>
27 #include <yajl_parse.h>
29 #include "macros.h"
30 #include "util.h"
32 int
33 prepare_csv_for_upload(char ***csv, size_t rows, size_t cols, long
34 long **course_ids, long long **assignment_ids)
36 size_t j = 0;
37 long long course_id = 0;
38 long long assignment_id = 0;
39 char *past = 0;
40 char *inv = 0;
41 int sverr = 0;
42 uint_fast8_t some_invalid = 0;
44 if (!(*course_ids = calloc((cols - 2), sizeof **course_ids))) {
45 sverr = errno;
46 perror(L("calloc"));
48 return sverr;
51 if (!(*assignment_ids = calloc((cols - 2), sizeof **assignment_ids))) {
52 sverr = errno;
53 perror(L("calloc"));
55 return sverr;
58 if (rows < 2) {
59 fprintf(stderr, "CSV has too few rows to contain data");
61 return EINVAL;
64 if (cols < 3) {
65 fprintf(stderr, "CSV has too few cols to contain data");
67 return EINVAL;
70 if (!csv[0][0] ||
71 strcmp(csv[0][0], "Name") ||
72 !csv[0][1] ||
73 strcmp(csv[0][1], "ID")) {
74 fprintf(stderr, "CSV does not contain required header\n");
76 return EINVAL;
79 for (j = 1; j < rows; ++j) {
80 if (csv[j] &&
81 csv[j][1] &&
82 csv[j][1][0]) {
83 strtoll(csv[j][1], &inv, 0);
85 if (inv &&
86 *inv) {
87 fprintf(stderr,
88 "Student ID in row %zu invalid\n", j);
89 some_invalid = 1;
94 if (some_invalid) {
95 return EINVAL;
98 for (j = 2; j < cols; ++j) {
99 if (!csv[0][j]) {
100 fprintf(stderr,
101 "CSV header cell %zu is blank - must contain "
102 "<course-id>:assignment-id>\n", j + 1);
104 return EINVAL;
107 past = 0;
108 course_id = strtoll(csv[0][j], &past, 0);
110 if (!past ||
111 !*past) {
112 fprintf(stderr, "CSV header cell %zu does not contain "
113 "assignment-id - must contain "
114 "<course-id>:assignment-id>\n", j + 1);
116 return EINVAL;
119 if (course_id <= 0) {
120 fprintf(stderr, "CSV header cell %zu references "
121 "impossible course-id %lld\n", j + 1,
122 course_id);
124 return EINVAL;
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 +
132 1, course_id);
134 return EINVAL;
137 (*course_ids)[j - 2] = course_id;
138 (*assignment_ids)[j - 2] = assignment_id;
141 return 0;
145 main(void)
147 int ret = EINVAL;
148 char *url_base = 0;
149 char *auth_token = 0;
150 size_t j = 0;
151 size_t k = 0;
152 size_t len = 0;
153 char *built_uri = 0;
154 char ***csv = 0;
155 long long *course_ids = 0;
156 long long *assignment_ids = 0;
157 size_t rows = 0;
158 size_t cols = 0;
159 struct curl_httppost *post = 0;
161 setlocale(LC_ALL, "");
162 UNUSED(built_uri);
163 UNUSED(len);
164 UNUSED(j);
165 curl_global_init(CURL_GLOBAL_DEFAULT);
167 if (!(url_base = get_url_base())) {
168 ret = ENOENT;
170 /* Error should have already been printed */
171 goto cleanup;
174 if (!(auth_token = get_auth_token())) {
175 ret = ENOENT;
177 /* Error should have already been printed */
178 goto cleanup;
181 if ((ret = read_csv(stdin, &csv, &rows, &cols))) {
182 fprintf(stderr, "Error with CSV\n");
183 goto cleanup;
186 if ((ret = prepare_csv_for_upload(csv, rows, cols, &course_ids,
187 &assignment_ids))) {
188 /* Error should have already been printed */
189 goto cleanup;
192 for (j = 2; j < cols; ++j) {
193 free(built_uri);
194 len = snprintf(0, 0, "%s/api/v1/courses/%lld/assignments/"
195 "%lld/submissions/update_grades", url_base,
196 course_ids[j - 2],
197 assignment_ids[j - 2]);
199 if (len + 1 < len) {
200 ret = errno = EOVERFLOW;
201 perror(L(""));
202 goto cleanup;
205 if (!(built_uri = malloc(len + 1))) {
206 ret = errno;
207 perror(L("malloc"));
208 goto cleanup;
211 sprintf(built_uri, "%s/api/v1/courses/%lld/assignments/"
212 "%lld/submissions/update_grades", url_base,
213 course_ids[j - 2],
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() */
218 ret = ENOMEM;
219 goto cleanup;
222 if ((ret = send_and_id_scan(built_uri, auth_token, post, "POST",
223 0))) {
224 goto cleanup;
227 curl_formfree(post);
228 post = 0;
231 ret = 0;
232 cleanup:
234 if (csv) {
235 for (j = 0; j < rows; ++j) {
236 if (csv[j]) {
237 for (k = 0; k < cols; ++k) {
238 free(csv[j][k]);
241 free(csv[j]);
245 free(csv);
248 free(built_uri);
249 curl_global_cleanup();
250 free(url_base);
251 free(auth_token);
252 free(course_ids);
253 free(assignment_ids);
255 return ret;