############################################################################
# Copyright 2009,2010 Benjamin Kellermann #
# #
# This file is part of dudle. #
# #
# Dudle is free software: you can redistribute it and/or modify it under #
# the terms of the GNU Affero General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# Dudle is distributed in the hope that it will be useful, but WITHOUT ANY #
# WARRANTY; without even the implied warranty of MERCHANTABILITY or #
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public #
# License for more details. #
# #
# You should have received a copy of the GNU Affero General Public License #
# along with dudle. If not, see . #
############################################################################
require "hash"
require "yaml"
require "time"
require "pollhead"
require "timepollhead"
$KCODE = "u"
class String
def to_htmlID
CGI.escapeHTML(self.gsub(/[^A-Z^a-z^0-9^\-^_^:^\.]/,"."))
end
end
class Poll
attr_reader :head, :name
YESVAL = "ayes"
MAYBEVAL = "bmaybe"
NOVAL = "cno"
def initialize name,type
@name = name
case type
when "normal"
@head = PollHead.new
when "time"
@head = TimePollHead.new
else
raise("unknown poll type: #{type}")
end
@data = {}
@comment = []
store "Poll #{name} created"
end
def sort_data fields
if fields.include?("name")
until fields.pop == "name"
end
@data.sort{|x,y|
cmp = x[1].compare_by_values(y[1],fields)
cmp == 0 ? x[0].downcase <=> y[0].downcase : cmp
}
else
@data.sort{|x,y| x[1].compare_by_values(y[1],fields)}
end
end
# showparticipation \in {true, false, "invite"}
def to_html(edituser = "", showparticipation = true)
if showparticipation == "invite"
showparticipation = false
invite = true
end
ret = "
\n"
sortcolumns = $cgi.include?("sort") ? $cgi.params["sort"] : ["timestamp"]
ret += @head.to_html(sortcolumns)
sort_data(sortcolumns).each{|participant,poll|
if edituser == participant
ret += participate_to_html(edituser)
else
ret += "
\n"
@head.columns.each{|column|
klasse = poll[column]
case klasse
when nil
value = UNKNOWN
klasse = "undecided"
when YESVAL
value = YES
when NOVAL
value = NO
when MAYBEVAL
value = MAYBE
end
ret += "
#{value}
\n"
}
ret += "
#{poll['timestamp'].strftime('%d.%m, %H:%M')}
"
ret += "
\n"
end
}
# PARTICIPATE
ret += participate_to_html(edituser) unless @data.keys.include?(edituser) || !showparticipation
ret += invite_to_html if invite
# SUMMARY
ret += "
" + _("total") + "
\n"
@head.columns.each{|column|
yes = 0
undecided = 0
@data.each_value{|participant|
if participant[column] == YESVAL
yes += 1
elsif !participant.has_key?(column) or participant[column] == MAYBEVAL
undecided += 1
end
}
if @data.empty?
percent_f = 0
else
percent_f = 100*yes/@data.size
end
percent = "#{percent_f}%" unless @data.empty?
if undecided > 0
percent += "-#{(100.0*(undecided+yes)/@data.size).round}%"
end
ret += "
#{yes}
\n"
}
ret += "
"
ret += "
\n"
ret
end
def invite_to_html
invitestr = _("Invite")
ret = <
INVITE
end
def participate_to_html(edituser)
checked = {}
if @data.include?(edituser)
@head.columns.each{|k| checked[k] = @data[edituser][k]}
else
edituser = $cgi.cookies["username"][0] unless @data.include?($cgi.cookies["username"][0])
@head.columns.each{|k| checked[k] = NOVAL}
end
ret = "
\n"
ret += "
\n"
ret += "
"
ret += "
\n"
@head.columns.each{|column|
ret += "
"
[[YES, YESVAL],[NO, NOVAL],[MAYBE, MAYBEVAL]].each{|valhuman, valbinary|
ret += <
TR
}
ret += "
"
}
ret += "
"
if @data.include?(edituser)
ret += ""
ret += " "
else
ret += ""
end
ret += "
\n"
ret += "
\n"
ret += "
\n"
ret
end
def comment_to_html
ret = "
"
ret += "
" + _("Comments") + "
"
saidstr = _("said on")
unless @comment.empty?
@comment.each_with_index{|c,i|
time,name,comment = c
deletestr = _("Delete")
ret += <
" + _("Comments") + "
" saidstr = _("said on") unless @comment.empty? @comment.each_with_index{|c,i| time,name,comment = c deletestr = _("Delete") ret += <#{name} #{saidstr} #{time.strftime("%d.%m., %H:%M")}
#{comment}