Class: Xforum::Topic

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/xforum/topic.rb

Class Method Summary collapse

Class Method Details

.add_topic(params, user) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
# File 'app/models/xforum/topic.rb', line 131

def self.add_topic(params,user)
  if user.forum_admin?
    params[:user_id]=user_id if params[:user_id] == 'me'
    category=Category.where(name:params[:category]).pluck(:id)[0]
    id=Topic.create(name:params[:topic],user_id:params[:user_id],category_id:category).id
    Forum.add_comment({topic:params[:topic],comment:params[:topic],parent:0},user)
  else
      id='not happening'
  end
  {id:id}
end

.add_topic_monitor(params, user_ids) ⇒ Object



45
46
47
48
# File 'app/models/xforum/topic.rb', line 45

def self.add_topic_monitor(params,user_ids)
  topic=Topic.where(name:params[:topic]).pluck(:id)[0]
  PeopleList.add_to_list({list:'monitor',owner:topic},user_ids)
end

.cast_vote(topic, user, vote) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/models/xforum/topic.rb', line 98

def self.cast_vote(topic,user,vote)
  topic_record=Topic.find_by(name:topic)
  voters=PeopleList.get_voter_list({topic:topic_record.id,list:'voters'})
  votes=JSON.load(topic_record.votes)
  if voters.empty?
    index=nil
    votes=[]
  else
    index=voters.index(user.id)
  end
  if vote == '1' || vote == '-1'
    if index.nil?
      voters.push(user.id)
      votes.push(vote)
    else
      votes[index]=vote
    end
  end
  PeopleList.where(topic_id:topic_record.id,list:'voters').update_all(people:voters.to_json)
  topic_record.votes=votes.to_json
  topic_record.save
  votes.empty? ? 0 : Topic.count_votes(votes)
end

.count_votes(votes) ⇒ Object



91
92
93
94
95
96
# File 'app/models/xforum/topic.rb', line 91

def self.count_votes(votes)
  count=votes.length
  count2=votes.delete_if{|element| element=='-1'
  }.length
  2*count2-count
end

.edit_topic(params, user) ⇒ Object



143
144
145
# File 'app/models/xforum/topic.rb', line 143

def self.edit_topic(params,user)
  Topic.where(id: params[:id]).update_all(name: params[:newline], is_moderated: params[:moderate_me]=='true') if user.forum_admin?
end

.get_suggestions(params, user) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'app/models/xforum/topic.rb', line 68

def self.get_suggestions(params,user)
  if user.forum_admin?
    data=Topic.where(suggestion:true).pluck(:name, :user_id, :category_id,:id)
    data=ForumAssist.named_array(data, [:suggestion, :user, :category, :id]) unless data.empty?
    data=ForumAssist.add_user_name(data.clone) unless data.empty?
    {data:data,list:'forum-topic-prososals-list'}
  else
    {response:'nothing doing'}
  end
end

.remove_object(params, user) ⇒ Object



147
148
149
150
151
152
153
154
# File 'app/models/xforum/topic.rb', line 147

def self.remove_object(params,user)
  case(params[:state_action])
    when 'del' then   Topic.where(id:params[:id]).update_all(closed:true)
    when 'open' then  Topic.where(id:params[:id]).update_all(closed:false)
    else
      # type code here
  end  if user.forum_admin?
end

.restrict_me(params, user) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/models/xforum/topic.rb', line 50

def self.restrict_me(params,user)
  if user.forum_admin?
    topic=topic.find_by(name:params[:topic])
    unless topic.nil?
      unless params[:user_list].nil? || params[:user_list]=='select list'
        people_list=PeopleList.find_by(id:params[:user_list])
        if people_list.topic_id.nil? && people_list.list=='invitation'
          people_list.update(topic_id:topic.id )
        else
          new_list=people_list.clone
          new_list.update(name:topic.name+'-invitees',list:'invitation',topic_id:topic.id)
        end
      end
    topic.update(restricted:!topic.restricted)
    end
  end
end

.subscribe(topic, user) ⇒ Object

if they’re in the list unsubscribe else subscribe



122
123
124
125
126
127
128
129
# File 'app/models/xforum/topic.rb', line 122

def self.subscribe(topic,user) #if they're in the list unsubscribe else subscribe
  id=Topic.where(name:topic).pluck(:id)[0]
  subscribers=JSON.load(PeopleList.where(id:id,list:'subscribers').pluck(:people)[0])
  subscribers=[] if subscribers.nil?
  index= subscribers.index(user_id.to_s) unless subscribers.empty?
  index.nil? ? subscribers.push(user.id.to_s) : subscribers.delete_at(index)
  PeopleList.where(id:id,list:'subscribers').update_all(people:subscribers.to_json)[0]
end

.suggestion(params, user) ⇒ Object

add new category or topic



79
80
81
82
# File 'app/models/xforum/topic.rb', line 79

def self.suggestion(params,user)  #add new category or topic
  Topic.create(suggestion:true,name:params[:suggestion],user_id:user.id,category_id:Category.get_category_id(params))
  {response:'nothing to do'}
end

.suggestion_close(params, user) ⇒ Object



84
85
86
87
88
89
# File 'app/models/xforum/topic.rb', line 84

def self.suggestion_close(params,user)
  params[:result]=='Accept' ?
      (Topic.where(id: params[:id]).update_all(suggestion: false) if user.forum_admin?) :
      Topic.where(id: params[:id]).update_all(state: 'closed') if user.forum_admin? if user.forum_admin?
  {response:'nothing to do'}
end

.topics(params, user) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/xforum/topic.rb', line 20

def self.topics(params,user)
  Topic.create(name:'select topic') if Topic.find_by(name:'select topic').nil?
  Topic.create(name:'request new topic') if Topic.find_by(name:'request new topic').nil?
  unless  params[:category] == 'select category'
    category_id=Category.where(name:params[:category]).pluck(:id)[0]
    params[:hold_test]=='true' ?
        topics=Topic.grab({category_id:category_id,closed:false,suggestion:false},  [:id,:name,:state,:category_id,:restricted,:closed,:is_moderated],{swap:{name: :item}})  :
        topics=Topic.grab({category_id:category_id}, [:id,:name,:state,:category_id,:restricted,:closed,:is_moderated],{swap:{name: :item}})
    topics=[] if topics[0][:id].nil?
    topics=PeopleList.check_restrictions(topics,user,'topic')
    a=Topic.grab({name:'select topic'},[:id,:name,:state,:category_id,:restricted,:closed],{swap:{name: :item}})[0]
    b=Topic.grab({name:'request new topic'}, [:id,:name, :category_id,:state,:restricted,:closed],{swap:{name: :item}})[0]
    topics.unshift(a)
    topics.push(b)
    {list_data:Translation.translate_list(topics,params[:language],'topic'),id:params[:list]}
  end
end

.voting_booth?(params) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
# File 'app/models/xforum/topic.rb', line 38

def self.voting_booth?(params)
  topic=Topic.find_by(name:params[:topic])
  topic.vote=!topic.vote
  topic.save
  {vote:topic.vote,votes:'0'}
end