~ubuntu-branches/ubuntu/utopic/tdiary/utopic

« back to all changes in this revision

Viewing changes to contrib2/plugin/rating.rb

  • Committer: Bazaar Package Importer
  • Author(s): Daigo Moriwaki
  • Date: 2011-04-11 21:53:16 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110411215316-ih4gt4q8p29d2wf8
Tags: 3.0.1-1
* New upstream release (Closes: #542801, #594947)
* debian/control:
 - Bumped up Standards-Version to 3.9.1.
 - Updated version dependency.
* debian/tdiary-setup.rb: Followed the upstream changes, incorporating js and
  index.fcgi

Show diffs side-by-side

added added

removed removed

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