]> git.plutz.net Git - confetti/blobdiff - actions/update_course.sh
simplify distinction between attendees and clients
[confetti] / actions / update_course.sh
index d95d06250fb19ed71408a1b993a7c5a94b8022ff..a5ba8cee56ba68afb78ac0e817bd527fd80c00df 100755 (executable)
@@ -1,6 +1,6 @@
 #!/bin/zsh
 
-# Copyright 2014 Paul Hänsch
+# Copyright 2014, 2015 Paul Hänsch
 #
 # This file is part of Confetti.
 # 
 
 cgi_post
 
-course="$_POST[\"course\"]"
+course="${_POST[course]}"
 tempfile="temp/$course"
 coursefile="ical/$course"
+attfile="$_DATA/mappings/attendance"
 
 # DURATION:
-uid="$_POST[\"UID\"]"
+uid="${_POST[UID]}"
 
 tzid=$(cat /etc/timezone)
 tstamp=$(TZ="$tzid" date +%Y%m%dT%H%M%S)
 
-dts_year="$_POST[\"DTSYEAR\"]"
-dts_month="$_POST[\"DTSMONTH\"]"
-dts_day="$_POST[\"DTSDAY\"]"
-[ -n "$_POST[\"DTSDAY0\"]" ] && dts_day="$_POST[\"DTSDAY0\"]"
-dts_hour="$_POST[\"DTSHOUR\"]"
-dts_minute="$_POST[\"DTSMINUTE\"]"
+dts_year="${_POST[DTSYEAR]}"
+dts_month="${_POST[DTSMONTH]}"
+dts_day="${_POST[DTSDAY]}"
+[ -n "${_POST[DTSDAY0]}" ] && dts_day="${_POST[DTSDAY0]}"
+dts_hour="${_POST[DTSHOUR]}"
+dts_minute="${_POST[DTSMINUTE]}"
 
 [ -z $dts_year ] && dts_year=$(date +%Y)
 [ -z $dts_month ] && dts_month=$(date +%m)
@@ -45,21 +46,21 @@ date -d ${dts_year}-${dts_month}-${dts_day} >/dev/null 2>/dev/null || dts_day="0
 
 dtstart="TZID=${tzid}:${dts_year}${dts_month}${dts_day}T${dts_hour}${dts_minute}00"
 
-rr_int="$_POST[\"RRULE_INTERVAL\"]"
-rr_freq="$_POST[\"RRULE_FREQ\"]"
-rr_limit="$_POST[\"RRULE_LIMIT\"]"
+rr_int="${_POST[RRULE_INTERVAL]}"
+rr_freq="${_POST[RRULE_FREQ]}"
+rr_limit="${_POST[RRULE_LIMIT]}"
 case "$rr_limit" in
   ETERN)
     rrule="FREQ=$rr_freq;INTERVAL=$rr_int"
     ;;
   COUNT)
-    t="$_POST[\"RRULE_COUNT\"]"
+    t="${_POST[RRULE_COUNT]}"
     rrule="FREQ=$rr_freq;INTERVAL=$rr_int;COUNT=$t"
     ;;
   UNTIL)
-    uy="$_POST[\"RRULE_UYEAR\"]"
-    um="$_POST[\"RRULE_UMONTH\"]"
-    ud="$_POST[\"RRULE_UDAY\"]"
+    uy="${_POST[RRULE_UYEAR]}"
+    um="${_POST[RRULE_UMONTH]}"
+    ud="${_POST[RRULE_UDAY]}"
     rrule="FREQ=$rr_freq;INTERVAL=$rr_int;UNTIL=${uy}${um}${ud}T000000Z"
     ;;
 esac
@@ -73,24 +74,39 @@ echo "DTSTAMP:TZID=${tzid}:${tstamp}\r" >>"$tempfile"
 echo "DTSTART:${dtstart}\r" >>"$tempfile"
 echo "RRULE:${rrule}\r" >>"$tempfile"
 for field in SUMMARY COMMENT; do
-  value="$_POST[\"$field\"]"
+  value="${_POST[$field]}"
   n=0
   while [ -n "$value" ]; do
     value="$(echo "$value" |sed -r ':a;N;$!ba;s:\n:\\\\n:g;s:\r:\\\\r:g')"
     echo "${field}:${value}\r"
-    value="$_POST[\"$field$n\"]"
+    value="${_POST[$field$n]}"
     n=$(($n + 1))
   done
 done >>"$tempfile"
 
-case "$_POST[\"action\"]" in
+case "${_POST[action]}" in
   addfield)
-    echo "$_POST[\"newfield\"]:\r" >>"$tempfile"
+    echo "${_POST[newfield]}:\r" >>"$tempfile"
     echo "END:VEVENT\r" >>"$tempfile"
     echo "END:VCALENDAR\r" >>"$tempfile"
     echo -n "Location: ?p=courses&edit=$course\n\n"
     ;;
   update)
+    attendance=()
+    for att in attendance attendance{0..100}; do
+      [ -n "${_POST[$att]}" ] && attendance+=("${_POST[$att]}")
+    done
+    sed -rn 's:^'$course'\t(.+)$:\1:p' "$attfile" |while read card; do
+      touch "$_DATA/vcard/$card"
+    done
+    sed -i -r '/^'$course'\t(.+)$/d' "$attfile"
+    for each in $attendance; do
+      echo "$course\t$each"
+    done >>"$attfile"
+    sed -rn 's:^'$course'\t(.+)$:\1:p' "$attfile" |while read card; do
+      touch "$_DATA/vcard/$card"
+    done
+
     echo "END:VEVENT\r" >>"$tempfile"
     echo "END:VCALENDAR\r" >>"$tempfile"
     mv "$tempfile" "$coursefile"