Update copyright dates
[nci.git] / nci-put-assignment-grades.c
blob0639f6e9c79f56a6eecd455bc61c710541b479cd
1 /*
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
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 prepare_csv_for_upload(char ***csv, size_t rows, size_t cols, long
33 long **course_ids, long long **assignment_ids)
35 size_t j = 0;
36 long long course_id = 0;
37 long long assignment_id = 0;
38 char *past = 0;
39 char *inv = 0;
40 int sverr = 0;
41 uint_fast8_t some_invalid = 0;
43 if (!(*course_ids = calloc((cols - 2), sizeof **course_ids))) {
44 sverr = errno;
45 perror(L("calloc"));
47 return sverr;
50 if (!(*assignment_ids = calloc((cols - 2), sizeof **assignment_ids))) {
51 sverr = errno;
52 perror(L("calloc"));
54 return sverr;
57 if (rows < 2) {
58 fprintf(stderr, "CSV has too few rows to contain data");
60 return EINVAL;
63 if (cols < 3) {
64 fprintf(stderr, "CSV has too few cols to contain data");
66 return EINVAL;
69 if (!csv[0][0] ||
70 strcmp(csv[0][0], "Name") ||
71 !csv[0][1] ||
72 strcmp(csv[0][1], "ID")) {
73 fprintf(stderr, "CSV does not contain required header\n");
75 return EINVAL;
78 for (j = 1; j < rows; ++j) {
79 if (csv[j] &&
80 csv[j][1] &&
81 csv[j][1][0]) {
82 strtoll(csv[j][1], &inv, 0);
84 if (inv &&
85 *inv) {
86 fprintf(stderr,
87 "Student ID in row %zu invalid\n", j);
88 some_invalid = 1;
93 if (some_invalid) {
94 return EINVAL;
97 for (j = 2; j < cols; ++j) {
98 if (!csv[0][j]) {
99 fprintf(stderr,
100 "CSV header cell %zu is blank - must contain "
101 "<course-id>:assignment-id>\n", j + 1);
103 return EINVAL;
106 past = 0;
107 course_id = strtoll(csv[0][j], &past, 0);
109 if (!past ||
110 !*past) {
111 fprintf(stderr, "CSV header cell %zu does not contain "
112 "assignment-id - must contain "
113 "<course-id>:assignment-id>\n", j + 1);
115 return EINVAL;
118 if (course_id <= 0) {
119 fprintf(stderr, "CSV header cell %zu references "
120 "impossible course-id %lld\n", j + 1,
121 course_id);
123 return EINVAL;
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 +
131 1, course_id);
133 return EINVAL;
136 (*course_ids)[j - 2] = course_id;
137 (*assignment_ids)[j - 2] = assignment_id;
140 return 0;
143 int main(void)
145 int ret = EINVAL;
146 char *url_base = 0;
147 char *auth_token = 0;
148 size_t j = 0;
149 size_t k = 0;
150 size_t len = 0;
151 char *built_uri = 0;
152 char ***csv = 0;
153 long long *course_ids = 0;
154 long long *assignment_ids = 0;
155 size_t rows = 0;
156 size_t cols = 0;
157 struct curl_httppost *post = 0;
159 setlocale(LC_ALL, "");
160 UNUSED(built_uri);
161 UNUSED(len);
162 UNUSED(j);
163 curl_global_init(CURL_GLOBAL_DEFAULT);
165 if (!(url_base = get_url_base())) {
166 ret = ENOENT;
168 /* Error should have already been printed */
169 goto cleanup;
172 if (!(auth_token = get_auth_token())) {
173 ret = ENOENT;
175 /* Error should have already been printed */
176 goto cleanup;
179 if ((ret = read_csv(stdin, &csv, &rows, &cols))) {
180 fprintf(stderr, "Error with CSV\n");
181 goto cleanup;
184 if ((ret = prepare_csv_for_upload(csv, rows, cols, &course_ids,
185 &assignment_ids))) {
186 /* Error should have already been printed */
187 goto cleanup;
190 for (j = 2; j < cols; ++j) {
191 free(built_uri);
192 len = snprintf(0, 0, "%s/api/v1/courses/%lld/assignments/"
193 "%lld/submissions/update_grades", url_base,
194 course_ids[j - 2],
195 assignment_ids[j - 2]);
197 if (len + 1 < len) {
198 ret = errno = EOVERFLOW;
199 perror(L(""));
200 goto cleanup;
203 if (!(built_uri = malloc(len + 1))) {
204 ret = errno;
205 perror(L("malloc"));
206 goto cleanup;
209 sprintf(built_uri, "%s/api/v1/courses/%lld/assignments/"
210 "%lld/submissions/update_grades", url_base,
211 course_ids[j - 2],
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() */
216 ret = ENOMEM;
217 goto cleanup;
220 if ((ret = send_and_id_scan(built_uri, auth_token, post, "POST",
221 0))) {
222 goto cleanup;
225 curl_formfree(post);
226 post = 0;
229 ret = 0;
230 cleanup:
232 if (csv) {
233 for (j = 0; j < rows; ++j) {
234 if (csv[j]) {
235 for (k = 0; k < cols; ++k) {
236 free(csv[j][k]);
239 free(csv[j]);
243 free(csv);
246 free(built_uri);
247 curl_global_cleanup();
248 free(url_base);
249 free(auth_token);
250 free(course_ids);
251 free(assignment_ids);
253 return ret;