questions_group_list_block.rb
3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
class PairwisePlugin::QuestionsGroupListBlock < Block
def self.description
_('Display question of a group of questions')
end
def help
_('This block displays one of your pairwise questions in a predefined group. You can edit the block to select which one of your questions is going to be displayed in the block.')
end
settings_items :group_description, :type => String
attr_accessible :group_description, :questions_ids, :random_sort
def content(args={})
block = self
questions = questions.shuffle if(questions)
#proc do
# content = block_title(block.title)
# content += ( question ? article_to_html(question,:gallery_view => false, :format => 'full').html_safe : _('No Question selected yet.') )
#end
proc do
render :file => "blocks/questions_group_list", :locals => {:block => block}
end
end
def random_sort= value
self.settings[:random_sort] = value
end
def random_sort
self.settings[:random_sort]
end
def is_random?
random_sort && !'0'.eql?(random_sort)
end
def contains_question?(id)
if self.settings[:questions_ids]
self.settings[:questions_ids].include?(id.to_s)
else
return false
end
end
def questions_ids
self.settings[:questions_ids]
end
def questions_ids= value
if value.is_a?(Array)
self.settings[:questions_ids] = value
else
self.settings[:questions_ids] = value.nil? ? [] : value.split(",")
end
self.settings[:questions_ids].delete('')
end
def questions_for_view
result = nil
if questions && questions.length > 0
result = is_random? ? questions.shuffle : questions
end
result
end
def questions(reload = false)
@questions = nil if reload
if @questions || questions_ids
begin
@questions = []
questions_ids.each do |id|
@questions << Article.find(id)
end
rescue ActiveRecord::RecordNotFound
# dangling reference, clear it
@questions = []
self.questions_ids = nil
self.save!
end
end
@questions
end
def questions=(arr)
self.questions_ids = arr.select {|x| x.attribute[:id] }
@questions = arr
end
def available_questions
return [] if self.owner.nil?
result = []
conditions = {}
if questions_ids && !questions_ids.empty?
questions_ids.each do |id|
if self.owner.kind_of?(Environment)
question = self.owner.portal_community.questions.find(id)
else
question = self.owner.questions.find(id)
end
result << question
end
conditions = { :conditions => ['id not in (?)', questions_ids] }
end
if self.owner.kind_of?(Environment)
result += self.owner.portal_community.questions.find(:all, conditions)
else
result += self.owner.questions.find(:all, conditions)
end
result
end
def self.expire_on
{ :profile => [:article], :environment => [:article] }
end
def timeout
1.hours
end
def embedable?
true
end
def cache_key_with_person(language = 'en', user=nil)
cache_key_without_person + (user.present? ? "-#{user.identifier}" : '')
end
alias_method_chain :cache_key, :person
end