diff --git a/flake.nix b/flake.nix index 6f6c930..77161e2 100644 --- a/flake.nix +++ b/flake.nix @@ -35,6 +35,18 @@ diff-so-fancy ]; + scripts = { + beste-schule-set-missing-grades = pkgs.writeShellApplication { + name = "set-missing-grades"; + runtimeInputs = [ + pkgs.coreutils + pkgs.jq + pkgs.curl + ]; + text = builtins.readFile ./scripts/set-missing-grades.sh; + }; + }; + shells = { nix = { name = "generic nix"; @@ -85,6 +97,12 @@ name = "generic markdown"; packages = [mdpls]; }; + "beste-schule" = { + name = "beste.schule"; + packages = [ + scripts.beste-schule-set-missing-grades + ] ++ shells.nodePhp.packages; + }; }; in { devShells = pkgs.lib.attrsets.mapAttrs (name: value: diff --git a/scripts/set-missing-grades.sh b/scripts/set-missing-grades.sh new file mode 100644 index 0000000..84bb40d --- /dev/null +++ b/scripts/set-missing-grades.sh @@ -0,0 +1,77 @@ +#!/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 +