diff options
author | Benjamin Kellermann <Benjamin.Kellermann@gmx.de> | 2009-12-07 23:25:41 +0100 |
---|---|---|
committer | Benjamin Kellermann <Benjamin.Kellermann@gmx.de> | 2009-12-07 23:25:41 +0100 |
commit | c12e150b1f22c290490c01de19ab8e11df8ab0db (patch) | |
tree | 61691d489e4d8f36ac46263261331968b8695ac4 | |
parent | 48cd11e7ce34348bc91b144ffbcd46e0e90a8bc0 (diff) |
fix db index issue
-rw-r--r-- | hash.rb | 6 | ||||
-rw-r--r-- | poll.rb | 66 | ||||
-rw-r--r-- | pollhead.rb | 46 | ||||
-rw-r--r-- | timepollhead.rb | 40 |
4 files changed, 62 insertions, 96 deletions
@@ -18,6 +18,12 @@ ############################################################################ class Hash + #################################################### + # compare self with other hash # + # in the order of fieldarray # + # {1=>a,2=>b}.compare_by_values({1=>c,2=>d},[2,1]) # + # -> compares b,d and afterwards a,c # + #################################################### def compare_by_values(other, fieldarray) return 0 if fieldarray.size == 0 return 1 if self[fieldarray[0]].nil? @@ -45,18 +45,15 @@ class Poll end def sort_data fields - parsedfields = fields.collect{|field| - field == "timestamp" || field == "name" ? field : @head.cgi_to_id(field) - } - if parsedfields.include?("name") - until parsedfields.pop == "name" + if fields.include?("name") + until fields.pop == "name" end @data.sort{|x,y| - cmp = x[1].compare_by_values(y[1],parsedfields) - cmp == 0 ? x[0] <=> y[0] : cmp + 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],parsedfields) } + @data.sort{|x,y| x[1].compare_by_values(y[1],fields)} end end @@ -80,8 +77,8 @@ class Poll ret += participant ret += "<span class='edituser'> <sup>#{EDIT}</sup></span></a>" if showparticipation ret += "</td>\n" - @head.each_column{|columnid,columntitle| - klasse = poll[columnid] + @head.each_column{|column| + klasse = poll[column] case klasse when nil value = UNKNOWN @@ -93,7 +90,7 @@ class Poll when MAYBEVAL value = MAYBE end - ret += "<td class='#{klasse}' title=\"#{CGI.escapeHTML(participant)}: #{CGI.escapeHTML(columntitle.to_s)}\">#{value}</td>\n" + ret += "<td class='#{klasse}' title=\"#{CGI.escapeHTML(participant)}: #{CGI.escapeHTML(column.to_s)}\">#{value}</td>\n" } ret += "<td class='date'>#{poll['timestamp'].strftime('%d.%m, %H:%M')}</td>" ret += "</tr>\n" @@ -106,13 +103,13 @@ class Poll # SUMMARY ret += "<tr id='summary'><td class='name'>total</td>\n" - @head.each_columnid{|columnid| + @head.each_column{|column| yes = 0 undecided = 0 @data.each_value{|participant| - if participant[columnid] == YESVAL + if participant[column] == YESVAL yes += 1 - elsif !participant.has_key?(columnid) or participant[columnid] == MAYBEVAL + elsif !participant.has_key?(column) or participant[column] == MAYBEVAL undecided += 1 end } @@ -162,10 +159,10 @@ INVITE def participate_to_html(edituser) checked = {} if @data.include?(edituser) - @head.each_columnid{|k| checked[k] = @data[edituser][k]} + @head.each_column{|k| checked[k] = @data[edituser][k]} else edituser = $cgi.cookies["username"][0] unless @data.include?($cgi.cookies["username"][0]) - @head.each_columnid{|k| checked[k] = NOVAL} + @head.each_column{|k| checked[k] = NOVAL} end ret = "<tr id='add_participant'>\n" ret += "<td class='name'> @@ -175,19 +172,19 @@ INVITE name='add_participant' value=\"#{edituser}\"/>" ret += "</td>\n" - @head.each_column{|columnid,columntitle| + @head.each_column{|column| ret += "<td class='checkboxes'><table summary='Input for one column' class='checkboxes'>" [[YES, YESVAL],[NO, NOVAL],[MAYBE, MAYBEVAL]].each{|valhuman, valbinary| ret += "<tr class='input-#{valbinary}'> <td class='input-#{valbinary}'> <input type='radio' value='#{valbinary}' - id=\"add_participant_checked_#{CGI.escapeHTML(columnid.to_s.gsub(" ","_").gsub("+","_"))}_#{valbinary}\" - name=\"add_participant_checked_#{CGI.escapeHTML(columnid.to_s)}\" - title=\"#{CGI.escapeHTML(columntitle.to_s)}\" #{checked[columnid] == valbinary ? "checked='checked'":""}/> + id=\"add_participant_checked_#{CGI.escapeHTML(column.to_s.gsub(" ","_").gsub("+","_"))}_#{valbinary}\" + name=\"add_participant_checked_#{CGI.escapeHTML(column.to_s)}\" + title=\"#{CGI.escapeHTML(column.to_s)}\" #{checked[column] == valbinary ? "checked='checked'":""}/> </td> <td class='input-#{valbinary}'> - <label for=\"add_participant_checked_#{CGI.escapeHTML(columnid.to_s.gsub(" ","_").gsub("+","_"))}_#{valbinary}\">#{valhuman}</label> + <label for=\"add_participant_checked_#{CGI.escapeHTML(column.to_s.gsub(" ","_").gsub("+","_"))}_#{valbinary}\">#{valhuman}</label> </td> </tr>" } @@ -306,8 +303,8 @@ FORM action = "added" end @data[htmlname] = {"timestamp" => Time.now } - @head.each_columnid{|columnid| - @data[htmlname][columnid] = agreed[columnid.to_s] + @head.each_column{|column| + @data[htmlname][column] = agreed[column.to_s] } store "Participant #{name.strip} #{action}" end @@ -344,19 +341,18 @@ FORM ############################### # column related functions ############################### - def delete_column columnid - title = @head.get_title(columnid) - if @head.delete_column(columnid) - store "Column #{title} deleted" + def delete_column column + if @head.delete_column(column) + store "Column #{column} deleted" return true else return false end end - def edit_column(oldcolumnid, newtitle, cgi) - parsedtitle = @head.edit_column(oldcolumnid, newtitle, cgi) - store "Column #{parsedtitle} #{oldcolumnid == "" ? "added" : "edited"}" if parsedtitle + def edit_column(oldcolumn, newtitle, cgi) + parsedtitle = @head.edit_column(oldcolumn, newtitle, cgi) + store "Column #{parsedtitle} #{oldcolumn == "" ? "added" : "edited"}" if parsedtitle end def edit_column_htmlform(activecolumn, revision) @@ -411,7 +407,7 @@ class PollTest < Test::Unit::TestCase 2.times{|t| @polls[type].edit_column("","2009-05-23 #{t+10}:00", {"columntime" => "#{t+10}:00","columndescription" => ""}) } - @polls[type].edit_column("","2009-05-23", {"columntime" => "foo","columndescription" => ""}) + @polls[type].edit_column("","2009-05-23 foo", {"columntime" => "foo","columndescription" => ""}) add_participant(type,A,[Y,N,Y,N]) @@ -421,12 +417,12 @@ class PollTest < Test::Unit::TestCase } end def test_sort - ["normal","time"].each{|type| + ["time","normal"].each{|type| comment = "Test Type: #{type}" - assert_equal([A,B,C,D],@polls[type].sort_data("name").collect{|a| a[0]},comment) - assert_equal([A,B,D,C],@polls[type].sort_data("timestamp").collect{|a| a[0]},comment) + assert_equal([A,B,C,D],@polls[type].sort_data(["name"]).collect{|a| a[0]},comment) + assert_equal([A,B,D,C],@polls[type].sort_data(["timestamp"]).collect{|a| a[0]},comment) assert_equal([B,C,D,A],@polls[type].sort_data([W,"name"]).collect{|a| a[0]},comment) - assert_equal([B,A,C,D],@polls[type].sort_data([Q,R,E]).collect{|a| a[0]},comment) + assert_equal([B,A,C,D],@polls[type].sort_data([Q,R,E]).collect{|a| a[0]},comment+ " " + [Q,R,E].join("; ")) } end end diff --git a/pollhead.rb b/pollhead.rb index 8dd6121..88ca587 100644 --- a/pollhead.rb +++ b/pollhead.rb @@ -17,8 +17,6 @@ # along with dudle. If not, see <http://www.gnu.org/licenses/>. # ############################################################################ -require "digest/sha2" - class PollHead def initialize @data = {} @@ -27,47 +25,25 @@ class PollHead @data.size end - def get_id(columntitle) - if @data.include?(columntitle) - return Digest::SHA2.hexdigest("#{columntitle}#{@data[columntitle]}" + columntitle) - else - raise("no such column found: #{columntitle}") - end - end - def get_title(columnid) - @data.each_key{|k| return k if get_id(k) == columnid} - raise("no such id found: #{columnid}") - end - def each_columntitle - @data.sort.each{|k,v| - yield(k) - } - end - def each_columnid - @data.sort.each{|k,v| - yield(get_id(k)) - } - end + # iterates over each column + # column should be the internal representation + # column.to_s should deliver humanreadable form def each_column @data.sort.each{|k,v| - yield(get_id(k),k) + yield(k) } end - # returns internal representation of cgi-string - def cgi_to_id(field) - field - end - + # column is in human readable form # returns true if deletion sucessfull - def delete_column(columnid) - @data.delete(get_title(columnid)) != nil + def delete_column(column) + @data.delete(column) != nil end # add new column if columnid = "" # returns parsed title or false if parsed title == "" - def edit_column(columnid, newtitle, cgi) - delete_column(columnid) if columnid != "" + def edit_column(column, newtitle, cgi) + delete_column(column) if column != "" parsedtitle = newtitle.strip if parsedtitle != "" @@ -98,7 +74,7 @@ class PollHead <a href="?editcolumn=#{CGI.escapeHTML(CGI.escape(columntitle))}" title="edit"> #{EDIT} </a>| - <a href="?deletecolumn=#{CGI.escapeHTML(CGI.escape(get_id(columntitle)))}" title="delete"> + <a href="?deletecolumn=#{CGI.escapeHTML(CGI.escape(columntitle))}" title="delete"> #{DELETE} </a> </small> @@ -117,7 +93,7 @@ EDITDELETE title = activecolumn description = @data[title] title = CGI.escapeHTML(title) - hiddeninput = "<input type='hidden' name='columnid' value=\"#{get_id(title)}\" />" + hiddeninput = "<input type='hidden' name='columnid' value=\"#{title}\" />" end return <<END <form method='post' action=''> diff --git a/timepollhead.rb b/timepollhead.rb index b5d25dc..3c7aa6c 100644 --- a/timepollhead.rb +++ b/timepollhead.rb @@ -92,24 +92,16 @@ class TimePollHead def col_size @data.size end - def get_title(columnid) - columnid - end - def each_columntitle - @data.sort.each{|day,time| - yield("#{day} #{time}") - } - end - def each_columnid - @data.sort.each{|day,time| - yield("#{day} #{time}") - } - end + + # iterates over each column + # column should be the internal representation + # column.to_s should deliver humanreadable form def each_column - @data.sort.each{|day,time| - yield("#{day} #{time}","#{day} #{time}") + @data.sort.each{|day| + yield(day.to_s) } end + def each_time h = {} @data.each{|ds| h[ds.time_to_s] = true } @@ -125,16 +117,12 @@ class TimePollHead ret end - # returns internal representation of cgi-string - def cgi_to_id(field) - field - end - + # column is in human readable form # returns true if deletion sucessfull - def delete_column(columnid) - col = TimeString.from_s(columnid) + def delete_column(column) + col = TimeString.from_s(column) if col.time - ret = @data.delete(TimeString.from_s(columnid)) != nil + ret = @data.delete(TimeString.from_s(column)) != nil @data << TimeString.new(col.date,nil) unless date_included?(col.date) return ret else @@ -160,8 +148,8 @@ class TimePollHead end # returns parsed title - def edit_column(columnid, newtitle, cgi) - delete_column(columnid) if columnid != "" + def edit_column(column, newtitle, cgi) + delete_column(column) if column != "" parsed_date = TimeString.new(newtitle, cgi.include?("columntime") ? cgi["columntime"] : nil) @data << parsed_date @data.uniq! @@ -199,7 +187,7 @@ class TimePollHead ret += "<th class='invisible' /></tr><tr><th><a href='?sort=name'>Name #{sortsymb(scols,"name")}</a></th>" @data.sort.each{|date| - ret += "<th><a title='#{date}' href='?sort=#{CGI.escape(date.to_s + " ")}'>#{date.time_to_s} #{sortsymb(scols,date.to_s + " ")}</a></th>\n" + ret += "<th><a title='#{date}' href='?sort=#{CGI.escape(date.to_s)}'>#{date.time_to_s} #{sortsymb(scols,date.to_s)}</a></th>\n" } ret += "<th><a href='?'>Last Edit #{sortsymb(scols,"timestamp")}</a></th>\n</tr>\n" ret |