#!/usr/bin/env ruby load "/home/ben/src/lib.rb/pphtml.rb" require "yaml" require "cgi" require "pp" require "date" class Poll attr_reader :head def initialize @head = {} @data = {} @comment = [] store end def head_to_html ret = "\n" @head.sort.each{|columntitle,columndescription| ret += "#{columntitle}\n" } ret += "Last Edit\n" # ret += "" # ret += "
\n" # ret += "
" # ret += "\n" # ret += "\n" # ret += "
" # ret += "
\n" # ret += "\n" ret += "\n" ret end def add_remove_column_htmlform return <
add/remove column
END end def to_html ret = "
\n" ret += "
\n" ret += "\n" ret += head_to_html @data.sort{|x,y| x[1]["timestamp"] <=> y[1]["timestamp"]}.each{|participant,poll| ret += "\n" ret += "\n" @head.sort.each{|columntitle,columndescription| klasse = poll[columntitle].nil? ? "undecided" : poll[columntitle] case poll[columntitle] when nil value = "-" when "yes" value = YES when "no" value = NO when "maybe" value = MAYBE end ret += "\n" } ret += "" ret += "\n" } ret += "\n" ret += "\n" @head.sort.each{|columntitle,columndescription| ret += "\n" } ret += "\n" ret += "\n" ret += "\n" @head.sort.each{|columntitle,columndescription| yes = 0 undecided = 0 @data.each_value{|participant| if participant[columntitle] == "yes" yes += 1 elsif !participant.has_key?(columntitle) or participant[columntitle] == "maybe" undecided += 1 end } if @data.empty? percent_f = 0 else percent_f = 100*yes/@data.size end percent = "#{percent_f}#{CGI.escapeHTML("%")}" unless @data.empty? if undecided > 0 percent += "-#{(100.0*(undecided+yes)/@data.size).round}#{CGI.escapeHTML("%")}" end ret += "\n" } ret += "" ret += "
#{participant}#{value}#{poll['timestamp'].strftime('%d.%m, %H:%M')}
#{YES}
#{NO}
#{MAYBE}
total#{yes}
\n" ret += "
\n" ret += "
" ret += "
" unless @comment.empty? ret += "
Comments" @comment.each{|time,name,comment| ret += "
#{name} said on #{time.strftime("%d.%m, %H:%M")}" ret += comment ret += "
" } ret += "
" end ret += "
\n" ret end def add_participant(name, agreed) name = CGI.escapeHTML(name.strip) @data[name] = {"timestamp" => Time.now} @head.each_key{|columntitle| @data[name][columntitle] = agreed[columntitle.to_s] } store end def delete(name) @data.delete(CGI.escapeHTML(name.strip)) store end def store File.open("#{SITE}.yaml", 'w') do |out| out << "# This is a dudle poll file\n" out << self.to_yaml out.chmod(0660) end end def add_comment name, comment @comment << [Time.now, CGI.escapeHTML(name), CGI.escapeHTML(comment.strip).gsub("\r\n","
")] store end def add_remove_column name, description add_remove_parsed_column CGI.escapeHTML(name.strip), CGI.escapeHTML(description.strip) end def add_remove_parsed_column columntitle, description if @head.include?(columntitle) @head.delete(columntitle) else @head[columntitle] = description end store true end end class DatePoll < Poll def head_to_html ret = "\n" monthhead = Hash.new(0) @head.sort.each{|curdate,curdescription| monthhead["#{curdate.year}-#{curdate.mon.to_s.rjust(2,"0")} "] += 1 } monthhead.sort.each{|title,count| year, month = title.split("-").collect{|e| e.to_i} ret += "#{Date::ABBR_MONTHNAMES[month]} #{year}\n" } ret += "\n" @head.sort.each{|curdate,curdescription| ret += "#{Date::ABBR_DAYNAMES[curdate.wday]}, #{curdate.day}\n" } ret += "Last Edit\n" ret += "\n" ret end def add_remove_column_htmlform if $cgi.include?("__add_remove_column_month") begin startdate = Date.parse("#{$cgi["__add_remove_column_month"]}-1") rescue ArgumentError olddate = $cgi.params["__add_remove_column_month"][1] case $cgi["__add_remove_column_month"] when "<<" startdate = Date.parse("#{olddate}-1")-365 when "<" startdate = Date.parse("#{olddate}-1")-1 when ">" startdate = Date.parse("#{olddate}-1")+31 when ">>" startdate = Date.parse("#{olddate}-1")+366 end startdate = Date.parse("#{startdate.year}-#{startdate.month}-1") end else startdate = Date.parse("#{Date.today.year}-#{Date.today.month}-1") end ret = <
add/remove column
END def navi val "" end ["<<","<"].each{|val| ret += navi(val)} ret += "" [">",">>"].each{|val| ret += navi(val)} ret += "\n" 7.times{|i| ret += "" } ret += "\n" (startdate.wday-1).times{ ret += "" } d = startdate while (d.month == startdate.month) do klasse = "notchoosen" klasse = "disabled" if d < Date.today klasse = "choosen" if @head.include?(d) ret += "\n" ret += "\n" if d.wday == 0 d = d.next end ret += <
" + "#{Date::ABBR_MONTHNAMES[startdate.month]} #{startdate.year}
#{Date::ABBR_DAYNAMES[(i+1)%7]}
END ret end def add_remove_column name,description begin parsed_name = Date.parse("#{$cgi["__add_remove_column_month"]}-#{name}") rescue ArgumentError return false end add_remove_parsed_column(parsed_name,CGI.escapeHTML(description)) end end if __FILE__ == $0 $cgi = CGI.new CONTENTTYPE = "text/html; charset=utf-8" #CONTENTTYPE = "application/xhtml+xml; charset=utf-8" puts "Content-type: #{CONTENTTYPE}" if $cgi.include?("__utf") || $cgi.cookies["utf"][0] YES = CGI.escapeHTML('✔') NO = CGI.escapeHTML('✘') MAYBE = CGI.escapeHTML('?') BACK = CGI.escapeHTML("↩") puts "Set-Cookie: utf=true; path=; expires=#{(Time.now+1*60*60*24*365).getgm.strftime("%a, %d %b %Y %H:%M:%S %Z")}" else YES = CGI.escapeHTML('OK') NO = CGI.escapeHTML('-') MAYBE = CGI.escapeHTML('?') BACK = CGI.escapeHTML("<-") end puts < HEAD $cgi.params.each{|k,v| if "" == v[0].to_s && !(k =~ /^__/) if defined?(SITE) puts "FEHLER, meld dich bei Ben!" exit else SITE = k end end } if defined?(SITE) and File.exist?(SITE + ".yaml" ) and table = YAML::load_file(SITE + ".yaml") puts < dudle - #{SITE}
#{BACK}

#{SITE}

HEAD if $cgi.include?("__add_participant") agreed = {} $cgi.params.each{|k,v| if k =~ /^__add_participant_checked_/ agreed[k.gsub(/^__add_participant_checked_/,"")] = v[0] end } table.add_participant($cgi["__add_participant"],agreed) end table.delete($cgi["__delete"]) if $cgi.include?("__delete") if $cgi.include?("__add_remove_column") puts "Could not add/remove column #{$cgi["__add_remove_column"]}" unless table.add_remove_column($cgi["__add_remove_column"],$cgi["__columndescription"]) end table.add_comment($cgi["__commentname"],$cgi.params["__comment"][0]) if $cgi.include?("__comment") puts table.to_html puts "
Hint" puts "To change a line, add a new person with the same name!" puts "
" puts "
" puts "
delete participant" puts "
\n" puts "
" puts "" puts "" puts "" puts "
" puts "
" puts "
" puts "
" puts table.add_remove_column_htmlform puts "
" puts "
Comment" puts "
\n" puts "
" puts "
" puts "
" puts "" puts "" puts "
" puts "
" puts "
" puts "
" else if defined?($cgi["__create_poll"]) SITE=$cgi["__create_poll"] case $cgi["__poll_type"] when "Poll" Poll.new when "DatePoll" DatePoll.new end end puts < dudle HEAD puts "
Available Polls" puts "" Dir.glob("*.yaml").sort_by{|f| File.new(f).mtime }.reverse.collect{|f| f.gsub(/\.yaml$/,'') }.each{|site| puts "" puts "" puts "" puts "" } puts "
PollLast change
#{site}#{File.new(site + ".yaml").mtime.strftime('%d.%m, %H:%M')}
" puts "
" puts <Create new Poll
CREATE end puts "" end