From c12e150b1f22c290490c01de19ab8e11df8ab0db Mon Sep 17 00:00:00 2001 From: Benjamin Kellermann Date: Mon, 7 Dec 2009 23:25:41 +0100 Subject: fix db index issue --- hash.rb | 6 ++++++ poll.rb | 66 +++++++++++++++++++++++++++------------------------------ pollhead.rb | 46 ++++++++++------------------------------ timepollhead.rb | 40 ++++++++++++---------------------- 4 files changed, 62 insertions(+), 96 deletions(-) diff --git a/hash.rb b/hash.rb index df46cf0..5e99a57 100644 --- a/hash.rb +++ b/hash.rb @@ -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? diff --git a/poll.rb b/poll.rb index 85d46be..72e46f2 100644 --- a/poll.rb +++ b/poll.rb @@ -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 += " #{EDIT}" if showparticipation ret += "\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 += "#{value}\n" + ret += "#{value}\n" } ret += "#{poll['timestamp'].strftime('%d.%m, %H:%M')}" ret += "\n" @@ -106,13 +103,13 @@ class Poll # SUMMARY ret += "total\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 = "\n" ret += " @@ -175,19 +172,19 @@ INVITE name='add_participant' value=\"#{edituser}\"/>" ret += "\n" - @head.each_column{|columnid,columntitle| + @head.each_column{|column| ret += "" [[YES, YESVAL],[NO, NOVAL],[MAYBE, MAYBEVAL]].each{|valhuman, valbinary| ret += "" } @@ -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 . # ############################################################################ -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 #{EDIT} | - + #{DELETE} @@ -117,7 +93,7 @@ EDITDELETE title = activecolumn description = @data[title] title = CGI.escapeHTML(title) - hiddeninput = "" + hiddeninput = "" end return < 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 += "" @data.sort.each{|date| - ret += "\n" + ret += "\n" } ret += "\n\n" ret -- cgit v1.2.3
+ 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'":""}/> - +
Name #{sortsymb(scols,"name")}#{date.time_to_s} #{sortsymb(scols,date.to_s + " ")}#{date.time_to_s} #{sortsymb(scols,date.to_s)}Last Edit #{sortsymb(scols,"timestamp")}