#!/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 \n" printf " Filter by group\n" printf " -s, --subject \n" printf " Filter by subject\n" printf " -i, --interval \n" printf " Filter by interval\n" printf " -t, --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