]> git.plutz.net Git - confetti/blob - pages/courses.sh
make prescriptions colored
[confetti] / pages / courses.sh
1 #!/bin/zsh
2
3 # Copyright 2014 Paul Hänsch
4 #
5 # This file is part of Confetti.
6
7 # Confetti is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # Confetti is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with Confetti.  If not, see <http://www.gnu.org/licenses/>. 
19
20 [ -z "${_GET[order]}" ] && _GET[order]=DOW
21
22 listcards() {
23   ls -1 ${_DATA}/vcard/*vcf 2>/dev/null |while read file; do
24     fn=$(sed -rn 's:^N(;.+)*\:([^;]*;){1} *([^;]*).*$:\3:p' "$file")
25     echo "$fn\t$file"
26   done |sort |sed -r 's:^.*\t(.*/)([^/]+)$:\2:'
27 }
28
29
30 listcourses() {
31   case "${_GET[filtertype]}" in
32     *) ls -1 ${_DATA}/ical/*ics 2>/dev/null
33       ;;
34   esac |case "${_GET[order]}" in
35     DOW)
36       while read file; do
37         icstime="$(sed -rn 's:^DTSTART\:(TZID=.*\:)?([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2})([0-9]{2})([0-9]{2})Z?\r$:\2-\3-\4 \5\:\6\:\7:p' "$file")"
38         echo "$(date -d "$icstime" "+%u %H%M%S")\t$file"
39       done
40       ;;
41     TOD)
42       while read file; do
43         icstime="$(sed -rn 's:^DTSTART\:(TZID=.*\:)?([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2})([0-9]{2})([0-9]{2})Z?\r$:\2-\3-\4 \5\:\6\:\7:p' "$file")"
44         echo "$(date -d "$icstime" +%H%M%S)\t$file"
45       done
46       ;;
47     *)
48       sed -r 's:^.*$:x\t&:'
49       ;;
50   esac |sort |sed -r 's:^.*\t(.*/)([^/]+)$:\2:'
51 }
52
53 list_attendance() {
54   id="$1"
55   sed -rn 's:'$id'\t(.+)$:\1:p' "$_DATA/mappings/attendance" |while read each; do
56     n_last="$(  sed -rn 's:^N(;.+)*\:([^;]*;){0} *([^;]*).*$:\3:p' "$_DATA/vcard/$each")"
57     n_first="$( sed -rn 's:^N(;.+)*\:([^;]*;){1} *([^;]*).*$:\3:p' "$_DATA/vcard/$each")"
58     n_middle="$(sed -rn 's:^N(;.+)*\:([^;]*;){2} *([^;]*).*$:\3:p' "$_DATA/vcard/$each")"
59     n_pre="$(   sed -rn 's:^N(;.+)*\:([^;]*;){3} *([^;]*).*$:\3:p' "$_DATA/vcard/$each")"
60     n_post="$(  sed -rn 's:^N(;.+)*\:([^;]*;){4} *([^;]*).*$:\3:p' "$_DATA/vcard/$each")"
61     byear="$(  sed -rn 's:^BDAY(;.+)*\:([0-9]{4})(-[0-9][0-9]){2}.*$:\2:p' "$_DATA/vcard/$each")"
62     aname="${n_pre} ${n_first} ${n_middle} ${n_last} ${n_post} (*${byear})"
63     echo "$each $aname" |tr -d '\r'
64   done
65 }
66
67 course_mail() {
68   id="$1"
69   coursemail=""
70   sed -rn 's:'$id'\t(.+)$:\1:p' "$_DATA/mappings/attendance" |while read each; do
71     coursemail="$coursemail$(sed -rn 's:^EMAIL(;.+)*\:(.+)\r$:\2,:p' "$_DATA/vcard/$each")"
72   done
73   echo "$coursemail"
74 }
75
76 ics_parse() {
77   tr -d '\n' <"$1" |sed -r 's:\r ::g;s:\r:\n:g' |tr -d '\r' \
78   | sed -r 's:^([^;\:]+)(;[^"\:]+|;"[^"]+")*\:(.*)$:key="\1"\nvalue="\3"\ntag=\2:g' \
79   | while read -r line; do
80     case "$line" in
81       key=*) echo -E "$line"
82         ;;
83       value=*) echo -E "$line"
84         ;;
85       tag=*) ot=''
86              echo -E "$line" \
87              | sed -r 's:^tag=::;s:\;([A-Z+_-]+="[^"]+"|[A-Z+_-]+=[^\;]+):\n\1:g;' \
88              | sed -r 's:([A-Z+_-]+)="?(.*)"?:tag\[\1\]="\2":g' \
89              | sed -r '/^ *$/d' \
90              | sort |while read -r tag; do
91                nt="$(echo -E "$tag" |sed -r 's:^tag\[([A-Z+_-]+)\]="(.*)"$:\1:')"
92                nv="$(echo -E "$tag" |sed -r 's:^tag\[([A-Z+_-]+)\]="(.*)"$:\2:')"
93                [ "$nt" = "$ot" ] && vl="$nv,$vl" || vl="$nv"
94                echo -E "tag[$nt]=\"$vl\""
95                ot="$nt"
96              done
97         ;;
98     esac
99   done
100 }
101
102 view_course() {  #Parameter: Calendarfile
103   id="$1"
104   calendarfile="$_DATA/ical/${id}"
105   cachefile="$_DATA/cache/${id}.cache"
106   unset key
107   if [ "$cachefile" -nt "$calendarfile" ]; then
108     cat "$cachefile"
109   else
110     declare -A tags
111     declare -A values
112     ics_parse "$calendarfile" |while read -r line; do
113       declare -A tag
114       case "$line" in
115         value*) eval "$line";;
116         tag*)   eval "$line";;
117         key*)
118           if [ -z "$key" ]; then
119             eval "$line"
120           else
121             values[$key]="$value"
122             for t in ${(k)tag}; do
123               tags[${key}_$t]="$tag[$t]"
124             done
125             eval "$line"
126             if [ -n "$values[$key]" ]; then
127               n=0
128               while [ -n "$values[$key$n]" ]; do n=$(($n + 1)); done
129               key=$key$n
130             fi
131             unset value
132             unset tag
133           fi
134         ;;
135       esac
136     done
137     . ${_EXEC}/templates/view_course.sh |tee "$cachefile"
138   fi
139 }
140
141 edit_course() {  #Parameter: Calendarfile
142   id="$1"
143   calendarfile="$_DATA/ical/$id"
144   tempfile="$_DATA/temp/$id"
145   [ -f "$tempfile" ] && calendarfile="$tempfile"
146   unset key
147
148   declare -A tags
149   declare -A values
150   ics_parse "$calendarfile" |while read -r line; do
151     declare -A tag
152     case "$line" in
153       value*) eval "$line";;
154       tag*)   eval "$line";;
155       key*)
156         if [ -z "$key" ]; then
157           eval "$line"
158         else
159           [ -n "$value" ] && values[$key]="$value" || values[$key]='\r'
160           for t in ${(k)tag}; do
161             tags[${key}_$t]="$tag[$t]"
162           done
163           eval "$line"
164           if [ -n "$values[$key]" ]; then
165             n=0
166             while [ -n "$values[${key}${n}]" ]; do n=$(($n + 1)); done
167             key=$key$n
168           fi
169             unset value
170             unset tag
171         fi
172       ;;
173     esac
174   done
175   . ${_EXEC}/templates/edit_course.sh
176 }