add: beste-schule

This commit is contained in:
Malte Tammena 2023-09-20 09:51:25 +02:00
parent d5fc6d29b9
commit d80d6a7817
2 changed files with 95 additions and 0 deletions

View file

@ -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:

View file

@ -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 <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