dev/scripts/set-missing-grades.sh
2023-09-20 09:51:25 +02:00

78 lines
2.1 KiB
Bash

#!/usr/bin/env bash
#
# Set all missing grades, specify query using the options below
function print_help_and_exit() {
printf "Usage: set-missing-grades OPTIONS\n"
printf "\n"
printf "Options:\n"
printf " -g, --group <group_id>\n"
printf " Filter by group\n"
printf " -s, --subject <subject_id>\n"
printf " Filter by subject\n"
printf " -i, --interval <interval_id>\n"
printf " Filter by interval\n"
printf " -t, --token <token>\n"
printf " Token to use for requests\n"
exit 1
}
QUERY="interpolate=true&include=grades"
TOKEN=
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
print_help_and_exit
;;
-g|--group)
shift # past argument
QUERY+="&filter\[group\]=$1"
shift # past value
;;
-s|--subject)
shift # past argument
QUERY+="&filter\[subject\]=$1"
shift # past value
;;
-i|--interval)
shift # past argument
QUERY+="&filter\[interval\]=$1"
shift # past value
;;
-t|--token)
shift # past argument
TOKEN="$1"
shift # past value
;;
-*)
echo "Unknown option $1"
print_help_and_exit
;;
*)
echo "Unknown additional argument $1"
print_help_and_exit
;;
esac
done
if [[ -z "$TOKEN" ]]; then
printf "Token is required!\n"
print_help_and_exit
fi
LIST=$(curl "http://localhost:8000/api/collections?$QUERY" -H "Authorization: Bearer $TOKEN" | jq '.data[] | { collection_id: .id, student_id: (.grades[] | select(.value == "") | .student.id) }' -r --compact-output)
for item in $LIST; do
collection_id=$(echo "$item" | jq '.collection_id')
student_id=$(echo "$item" | jq '.student_id')
value=$((RANDOM % 6 + 1))
given_at=$(date +"%Y-%m-%d")
data=$(jq -n --compact-output "{ collection_id: $collection_id, student_id: $student_id, value: $value, given_at: \"$given_at\" }")
printf "Setting grade for %6s in collection %6s to %2s.." "$student_id" "$collection_id" "$value"
echo "$data" | curl -s 'http://localhost:8000/api/grades' -H "Authorization: Bearer $TOKEN" -X POST --json @- >/dev/null
printf "done\n"
done