# category_id integer # user_id integer # name string limit: 50 # state string limit: 20 # closed boolean default: false # suggestion boolean default: false # votes binary default: --- []\n # vote boolean default: false # restricted boolean default: false module Xforum class Topic < ActiveRecord::Base require 'json' belongs_to :category has_many :forums, dependent: :destroy has_many :people_lists, dependent: :destroy serialize :votes 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 def self.voting_booth?(params) topic=Topic.find_by(name:params[:topic]) topic.vote=!topic.vote topic.save {vote:topic.vote,votes:'0'} end 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 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 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 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 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 def self.count_votes(votes) count=votes.length count2=votes.delete_if{|element| element=='-1' }.length 2*count2-count end 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 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 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 def self.edit_topic(params,user) Topic.where(id: params[:id]).update_all(name:params[:newline]) if user.forum_admin? end 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 end end