stubs for course lists
[confetti] / courses / list.sh
1 #!/bin/sh
2
3 . "${_EXEC}"/pdiread.sh
4
5 edit_card(){
6   local coursefile="$_DATA/courses/$1" 
7   local tempfile course
8
9   . $_EXEC/session_lock.sh
10
11   if ! tempfile="$(CHECK_SLOCK "$coursefile")"; then
12     printf '[div .message %s]' "$(l10n "This course is not set up for editing within this session.")"
13   else
14     course="$(pdi_load "$tempfile")"
15     cat <<-EOF
16         [form .course #${coursefile##*/} action="/cards/update_course.sh" method="POST"
17           [input type="hidden" name="tid" value="$(transid ${tempfile})"]
18           [div .section .basic $(
19             edit_item "$course" N GENDER
20             [ "$(pdi_count "$course" NICKNAME)" -gt 0 ] \
21             && edit_item "$card" NICKNAME
22             edit_item "$card" BDAY
23             edit_item "$card" X-ZACK-JOINDATE
24             [ "$(pdi_count "$card" X-ZACK-LEAVEDATE)" -gt 0 ] \
25             && edit_item "$card" X-ZACK-LEAVEDATE
26             card_item "$card" SOUND PHOTO LOGO
27           )]
28           [div .section .phone   $(edit_item "$card" TEL)]
29           [div .section .message $(
30             edit_item "$card" EMAIL
31             [ $(pdi_count "$card" IMPP) -gt 0 ] && edit_item "$card" IMPP
32             [ $(pdi_count "$card" URL ) -gt 0 ] && edit_item "$card" URL
33           )]
34           [div .section .address $(edit_item "$card" ADR)]
35           [div .section .note    $(edit_item "$card" NOTE)]
36           [div .section .attendance
37             [h3 $(l10n course_attendance) ] $(
38             for course in "$_DATA"/ical/*.ics; do
39               printf '[label [input type="checkbox" name="attendance" value="%s" %s] %s]' \
40                      "${course##*/}" \
41                      "$(grep -qF "${course##*/} ${cardfile##*/}" "$_DATA/mappings/attendance" \
42                         && printf 'checked="checked"'
43                      )" \
44                      "$(pdi_value "$(pdi_load "$course")" SUMMARY |unescape |HTML)"
45             done)
46             [h3 $(l10n CATEGORIES) ] $(
47             grep -xE '[^ ]+' "$_DATA"/mappings/categories |while read -r cat; do
48               printf '[label [input type="checkbox" name="attendance" value="%s" %s] %s]' \
49                      "$(HTML "$cat")" \
50                      "$(seq 1 $(pdi_count "$card" CATEGORIES) |while read c; do
51                        pdi_value "$card" CATEGORIES $c |grep -qxF "$cat" \
52                        && printf 'checked="checked"' && break
53                      done)" \
54                      "$(HTML "$cat")"
55             done)
56           ]
57           [div .control
58             [select .item name="newfield"
59               [option value="" disabled="disabled" selected="selected" $(l10n edit_addfieldtext)]
60               $(for f in NICKNAME EMAIL TEL IMPP ADR URL NOTE; do
61                 printf '[option value="%s" %s] ' "$f" "$(l10n "$f")"
62               done)
63            ][button .item type="submit" name="action" value="addfield" $(l10n edit_addfield)]
64             [button .item type="submit" name="action" value="update"   $(l10n edit_update)]
65             [input type="checkbox" #delete] [label .item for="delete" $(l10n edit_delete)]
66               [button .item type="submit" name="action" value="delete"]
67             [button .item type="submit" name="action" value="cancel"   $(l10n edit_cancel)]
68           ]
69           [input type="hidden" name="UID" value="$(pdi_value "$card" UID |HTML)"]
70           [input type="hidden" name="card" value="${cardfile##*/}"]
71         ]
72         EOF
73   fi
74 }
75
76 print_course(){
77   local coursefile="$1"
78   local course="$(pdi_load "$coursefile")"
79   cat <<-EOF
80     [div .course #${coursefile##*/}
81       [div .section .basic &shy;$(
82         cal_item "$course" SUMMARY DTSTART RRULE
83       )]
84       [div .section .COMMENT   &shy;$(cal_item "$course" COMMENT)]
85       [div .section .attendance [h3 $(l10n course_attendance) ] [ul &shy;
86         $(grep -F "     ${cardfile##*/}" "$_DATA/mappings/attendance" |while read each discard; do
87           printf '[li [a .item .attendance href="/courses#%s" &shy;%s]]' \
88                  "$each" \
89                  "$(pdi_value "$(pdi_load "$_DATA/ical/$each")" SUMMARY |unescape |HTML)"
90         done)]
91       ]
92       [div .control
93         [a .item href="/courses/edit_course.sh?course=${coursefile##*/}" $(l10n edit)]
94         [a .item href="/courses/export_pdf.sh?course=${coursefile##*/}" target="blank" $(l10n courselist)]
95         [a .item href="/courses/export_ical.sh?course=${coursefile##*/}" $(l10n ics_export)]
96         [a .item href="mailto:zack@vuesch.org?bcc=$(course_mail "${coursefile##*/}")" $(l10n course_mail)]
97       ]
98     ]
99         EOF
100 }
101
102 print_courses(){
103   local calfile cachefile date size name ldate=0 lsize lname
104
105   while read calfile; do
106     cachefile="${_DATA}/cache/${calfile##*/}.cache"
107     if [ -s "$cachefile" -a "$cachefile" -nt "$calfile" ]; then
108       cat "$cachefile"
109     else
110       print_course "$calfile" |tee "$cachefile"
111     fi
112   done
113 }
114
115 order_courses() {
116   local calfile course
117
118   while read calfile; do
119     icstime="$(pdi_value "$(pdi_load "$calfile")" DTSTART |cal_date)"
120     case $order in
121       DOW) printf '%s   %s\n' "$(date -d "$icstime" "+%u %H:%M:%S")" "$calfile";;
122       TOD) printf '%s   %s\n' "$(date -d "$icstime" "+%H:%M:%S")" "$calfile";;
123     esac
124   done \
125   | sort \
126   | sed -E 's;^.*\t;;g'
127 }
128
129 list_courses(){
130   printf '%s\n' ${_DATA}/courses/*.ics \
131   | order_courses \
132   | print_courses
133 }