root/platform/tdiary/plugin/rating.rb

Revision 18588, 12.0 kB (checked in by maripo, 3 months ago)

initial release (でもちょっと古い)

Line 
1# $Revision:0.1$
2# rating.rb: ʣ��ˤ������ȥ���ɽ��
3# for tDiary
4#
5# �Ȥ��
6# ���Τޤ�plugin �ǥ��쥯�ȥ�����ޤ���
7# '��' -> 'rating.rb Configuration' �ǡ�ɾ����� ɽ������ꡣ
8# ���路����
9# http://www.maripo.jp/diary/?date=20071019
10#
11# Copyright (c) 2007 Mariko/Maripo GODA <god@maripo.jp>
12# http://www.maripo.jp/
13# You can redistribute it and/or modify it under GPL.
14
15
16require 'pstore'
17require 'cgi'
18@dbase = "#{@cache_path}/rating.db"
19
20#########################################
21# Config (Form)
22#########################################
23
24add_conf_proc('rating', 'rating.rb Configuration') do
25    rating_config = RatingConfig.new(@dbase)
26    form_string = ""
27
28      if @mode == 'saveconf'
29
30        # save conf
31        index = 0
32
33        # edit axes
34        rating_config.each { |axis_config|
35            #check values
36            if (@cgi.params["label" + index.to_s][0] != "" && (@cgi.params["range" + index.to_s][0]).to_i > 0)
37                axis_config.edit(@cgi.params["label" + index.to_s][0], @cgi.params["label_max" + index.to_s][0],@cgi.params["label_min" + index.to_s][0],@cgi.params["range" + index.to_s][0].to_i,@cgi.params["order" + index.to_s][0].to_i,@cgi.params["display" + index.to_s][0]!=nil)
38                index += 1
39            end
40        }
41        if (@cgi.params["label_new"][0] != "" && @cgi.params["range_new"][0].to_i > 0)
42            # add new axis
43            rating_config.add_axis(@cgi.params["label_new"][0], @cgi.params["label_max_new"][0], @cgi.params["label_min_new"][0], @cgi.params["range_new"][0].to_i)
44        end
45        rating_config.save_to_db
46
47    end
48
49    # print conf form
50
51    form_string += <<HTML
52<p>
53�ե����ɥХå��ե��������򤷤ޤ������Ϥ����ĤǤ�����뤳�Ȥ��Ǥ��ޤ���
54</p>
55<p>
56<a href="http://www.maripo.jp/">��</a>��<a href="http://www.maripo.jp/diary/?date=20071019">blog</a> ��ɲþ���񤤤Ƥ��뤫�⤷������</p>
57<h3>���ˡ</h3>
58<ul>
59<li>���ɽ���������Ǥ����������ۤ�������¤Ӥޤ�����椬������</li>
60<li>ɽ�� �����������ϥ���å��򳰤��Ƥ��ޤäƤ��������
61<li>������ ��(�) "���ε�����ͤˤʤ�������"</li>
62<li>����٥��� "�ޤä������ͤˤʤ���"</li>
63<li>�ǹ��٥��� "�ȤƤ⻲�ͤˤʤä�"</li>
64<li>���� ��� : 1��������ʤ�5"</li>
65</p>
66<h3>����</h3>
67<form>
68<table>
69HTML
70    index = 0;
71
72    rating_config.each { |axis_config|
73        form_string += <<HTML
74<tr>
75<td>��input type="text" size="2" name="order#{index}" value="#{axis_config.order}"></td>
76<td>
77������:<input type="text" size="16" name="label#{index}" value="#{axis_config.label}">��
78����٥�input type="text" size="10" name="label_min#{index}" value="#{axis_config.label_min}">��
79�ǹ��٥�input type="text" size="10" name="label_max#{index}" value="#{axis_config.label_max}">��
80����:<input type="text" size="4" name="range#{index}" value="#{axis_config.range.to_s}">��
81ɽ��:<input type="checkbox" name="display#{index}" value="#{axis_config.label_max}" #{axis_config.check_label}>
82</td>
83</tr>
84HTML
85        index += 1
86    }
87    form_string += <<HTML
88<tr>
89<td>���/td>
90<td>
91������:<input type="text" size="16" name="label_new">
92����٥�input type="text" size="10" name="label_min_new">
93�ǹ��٥�input type="text" size="10" name="label_max_new">
94����:<input type="text" size="4" name="range_new">
95</td>
96</tr>
97</table>
98HTML
99        form_string += '</form>'
100        form_string #evaluate
101    end
102
103
104#########################################
105# Entry of the day
106#########################################
107
108add_body_leave_proc do |date|
109    graph_string = ""
110    form_string = ""
111    contentString = ""
112
113    #initialize DateEval object
114    todays_eval = DateEval.new(date.strftime('%Y%m%d'), @dbase)
115
116    #initialize RatingConfig object
117    @rating_config = RatingConfig.new(@dbase)
118
119    graph_string += <<HTML
120<!-- Generated by plugin "rating.rb" -->
121<div  class="ratingGraphContainer" style="overflow:hidden;height:18px;">
122<div
123onclick="with(this.parentNode.style){if(overflow=='hidden'){overflow='visible';}else{overflow='hidden';}}"
124style="cursor:hand;"
125class="ratingGraphOpener">
126[ʬ����</div>
127<div class="ratingGraphContent">
128HTML
129    form_string += '<!-- Generated by plugin "rating.rb" -->' + "\n"
130    form_string += ('<form action="./"><input type="hidden" name="comment" value="submit"><input type="hidden" name="body" value="rating"><input type="hidden" name="body" value="rating"><input type="hidden" name="name" value=""><input type="hidden" value="' + date.strftime('%Y%m%d') + '"><div class="ratingForm">')
131    @rating_config.each{|axis_config|
132
133        if !axis_config.display
134            next
135        end
136
137        # add axis info
138        form_string += ('<div class="ratingQuestion"><span class="ratingLabel">' + axis_config.label + '</span>')
139       
140        # add radio buttons
141        form_string += ('<span class="ratingRadioButtons"><span class="ratingLabelMin">' + axis_config.label_min + '</span>')
142
143        # append graph string
144        current_rank = 0
145
146        graph_string += '<div class="ratingGraphBox">' #begin "graphBox"
147        graph_string += '<span class="ratingGraphAverage">average : ' +  sprintf("%10.2f", todays_eval.get_average(axis_config.id)) +'</span><br>'
148        while current_rank < axis_config.range
149            graph_string += ('<div style="clear:both"><div
150class="ratingGraphRank">' + (current_rank + 1).to_s + '</div><div
151class="ratingGraphBar" style="width:' +
152todays_eval.get_graph_length(axis_config.id, current_rank).to_s +
153'px"></div><span class="ratingGraphValue"> (' + todays_eval.get_value(axis_config.id, current_rank).to_s + 'ɼ)</span></div>')
154            current_rank += 1
155        end
156        graph_string += '</div>' #end "graphBox"
157
158        for current_rank in 0..axis_config.range - 1
159            form_string += " " + '<input type="radio" name="axis' + axis_config.id.to_s + '" value="' + current_rank.to_s + '">' + (current_rank + 1).to_s
160        end
161        form_string += ('<span class="ratingLabelMax">' + axis_config.label_max + '</span></span>')
162        form_string += '</div>'
163    }
164    graph_string += <<HTML
165</div>
166</div>
167<div style="clear:both;height:0px;"></div>
168HTML
169    form_string += '<input type="hidden" name="date" value="' + date.strftime('%Y%m%d') + '">'
170    form_string += '<input type="submit" value="ɾ�����</div></form>'+"\n"
171    (form_string + graph_string)
172
173end
174
175#########################################
176# class RatingConfig
177#########################################
178
179class RatingConfig
180    @axes
181    @max_axis_id= 0
182    @dbase
183
184    def initialize (dbase)
185        @dbase = dbase
186        db = PStore.new(@dbase)
187        db.transaction do
188            #begin transaction
189            if db.root?("config")
190                @axes = Hash.new
191                obj = db["config"]
192                @max_axis_id = obj[0]
193                obj.each{|ary|
194                    id = ary[4]
195                    @axes[id] = AxisConfig.new(ary[0],ary[1],ary[2],ary[3],ary[4],ary[5],ary[6])
196                }
197            else
198                @axes = Hash.new
199                @max_axis_id = 0
200            end
201        end
202        #end transaction
203    end #end initialize
204
205    def save_to_db
206        save_array = Array.new
207        db = PStore.new(@dbase)
208        save_array.push(@max_axis_id)
209        each {|axis_config|
210            save_array.push(axis_config.to_array)
211        }
212        db.transaction do
213            #begin transaction
214            db["config"] = save_array
215        end
216        #end transaction
217    end #end save_to_db
218
219    def add_axis (label, label_max, label_min, range)
220        @max_axis_id += 1
221        new_axis = AxisConfig.new(label, label_max, label_min, range, @max_axis_id, 0, true)
222        @axes[@max_axis_id] = new_axis
223    end #end add_axis
224
225    def edit_axis (axis_id, label, label_max, label_min, range, order,display)
226        target_axis = @axes[axis_id]
227        target_axis.edit(label, label_max, label_min, range, order,display)
228    end
229
230    def length
231        return @axes.size
232    end #end length
233
234    def each
235        @axes.to_a.sort {|a, b| a[1].order <=> b[1].order}.each {|key ,axis_conf|
236            yield axis_conf
237        }
238    end #end each
239end
240
241#########################################
242# class AxisConfig
243#########################################
244
245class AxisConfig
246    @label
247    @label_max
248    @label_min
249    @range
250    @id
251    @order
252    @display = true
253   
254    #accessors
255    attr_reader :label, :label_max, :label_min, :range, :id, :order, :display
256
257    def initialize (label, label_max, label_min, range, id,order,display)
258        @label = label
259        @label_max = label_max
260        @label_min = label_min
261        @range = range
262        @id = id
263        @order = order
264        @display = display
265    end #end initialize
266
267    def edit (label, label_max, label_min, range, order, display)
268        @label = label
269        @label_max = label_max
270        @label_min = label_min
271        @range = range
272        @order = order
273        @display = display
274    end #end initialize
275
276    def to_array
277        return [@label, @label_max, @label_min, @range, @id, @order,@display]
278    end
279
280    def disable
281        @hidden = true
282    end #end disable
283
284    def enable
285        @hidden = false
286    end #end disable
287   
288    def check_label
289        return @display? "checked":""
290    end #end check_label
291   
292end
293
294#########################################
295# class DateEval
296#########################################
297
298class DateEval
299    @axes #axes[id][rank]
300    @date_string = ""
301    @dbase
302    @average #average[id]
303    @total #total[id]
304    GRAPH_PIXEL_LENGTH = 300
305
306    attr_reader :axes, :average, :total
307
308    # constructor
309    def initialize (date_string, dbase)
310        @dbase = dbase
311        @date_string = date_string
312        db = PStore.new(@dbase)
313        db.transaction do
314            #begin transaction
315            if db.root?(date_string)
316                #read
317                @axes = db[date_string]
318            else
319                #initialize
320                @axes = Hash.new
321            end
322        end
323
324        #end transaction
325
326    end #end constructor
327
328    def get_value(id, rank)
329        if @axes.key?(id) && @axes[id][rank] != nil
330            return @axes[id][rank]
331        else
332            return 0
333        end
334    end
335
336    def get_average(id)
337        sum = 0
338        vote = 0
339        unless @axes.key?(id)
340            return 0
341        end
342        for index in 0..@axes[id].length - 1
343            if @axes.key?(id) && @axes[id][index] != nil
344                vote += @axes[id][index]
345                sum += @axes[id][index] * index
346            end
347        end
348        if @axes[id].length == 0
349            return 0
350        else
351            return sum.to_f/vote + 1
352        end
353    end
354
355    def vote(id, rank)
356        unless @axes.key?(id)
357            @axes[id] = Array.new       
358        end
359        if @axes[id][rank] != nil
360            @axes[id][rank] += 1
361        else
362            @axes[id][rank] = 1
363        end
364    end
365
366    def save_to_db
367        db = PStore.new(@dbase)
368        db.transaction do
369            #begin transaction
370            #save
371            db[@date_string] = @axes
372        end
373        #end transaction
374    end #end save_to_db
375
376    def get_graph_length (id, rank)
377        unless @axes.key?(id)
378            return 0
379        end
380        if @axes[id][rank] != nil
381            total = 0
382            @axes[id].each {|val|
383                unless val == nil
384                    total += val
385                end
386            }
387            return (@axes[id][rank] * GRAPH_PIXEL_LENGTH / total).to_i
388        else
389            return 0
390        end
391    end #end
392
393end # class dateEval end
394
395#########################################
396# Comment (vote)
397#########################################
398
399if (@mode == 'comment')
400
401    if @cgi.params["body"][0] != 'rating'
402        return
403    end
404    @dbase = "#{@cache_path}/rating.db"
405    #initialize RatingConfig object
406    rating_config = RatingConfig.new(@dbase)
407    #initialize DateEval object
408    todays_eval = DateEval.new(@cgi.params['date'][0], @dbase)
409
410    rating_config.each { |axis_config|
411        if @cgi.params["axis" + axis_config.id.to_s][0]!= nil
412            todays_eval.vote(axis_config.id, @cgi.params["axis" + axis_config.id.to_s][0].to_i)
413        end
414    }
415    todays_eval.save_to_db
416end
Note: See TracBrowser for help on using the browser.