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



123
124
125
126
127
128
129
130
131
132
133
# File 'app/models/xforum/topic.rb', line 123

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



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

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



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/models/xforum/topic.rb', line 90

def self.cast_vote(topic,user,vote)
  topic_record=Topic.find_by(name:topic)
  voters=PeopleList.get_people_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



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

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



135
136
137
# File 'app/models/xforum/topic.rb', line 135

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

.get_suggestions(params, user) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'app/models/xforum/topic.rb', line 60

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



139
140
141
142
143
144
145
146
# File 'app/models/xforum/topic.rb', line 139

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



51
52
53
54
55
56
57
58
# File 'app/models/xforum/topic.rb', line 51

def self.restrict_me(params,user)
  if user.forum_admin?
    topic=Topic.find_by(name:params[:topic])
    topic.restricted= !topic.restricted
    invitation=PeopleList.find_or_create_by(topic_id:topic.id,list:'invitation')
    invitation.people= PeopleList.find_by(name:params[:user_list],list:'distribution').people
  end
end

.subscribe(topic, user) ⇒ Object

if they’re in the list unsubscribe else subscribe



114
115
116
117
118
119
120
121
# File 'app/models/xforum/topic.rb', line 114

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



71
72
73
74
# File 'app/models/xforum/topic.rb', line 71

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



76
77
78
79
80
81
# File 'app/models/xforum/topic.rb', line 76

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
37
# 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,:restricted,:closed],{swap:{name: :item}})  :
        topics=Topic.grab({category_id:category_id}, [:id,:name,:state,:restricted,:closed],{swap:{name: :item}})
    topics=[] if topics[0][:id].nil?
    topics=PeopleList.check_restrictions(topics,user,'topic') unless user.forum_admin?
    a=Topic.grab({name:'select topic'},[:id,:name,:state,:restricted,:closed],{swap:{name: :item}})[0]
    b=Topic.grab({name:'request new topic'}, [:id,:name,: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)


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

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