# encoding: utf-8
class DiscussesController < ApplicationController
include ApplicationHelper
before_filter :find_dis_object, :except => [:destroy, :hidden_record]
before_filter :find_discuss, :only => [:destroy, :hidden_record]
def create
position = Challenge.where(:id => params[:challenge_id]).pluck(:position).first
if @model
if params[:reply_id].blank?
notes = User.current.show_name.to_s + " 评论了你发布的实训:#{params[:content]}"
user_id = @model.user_id
@model.discusses << Discuss.new(:content => params[:content], :user_id => User.current.id, :praise_count => 0,
:challenge_id => params[:challenge_id], :position => position)
=begin
if @model.status > 1
begin
status = Trustie::Sms.send(mobile:'18173242757', send_type:'discuss', name:'管理员')
rescue => e
Rails.logger.error "发送验证码出错: #{e}"
end
end
=end
else
notes = User.current.show_name.to_s + " 评论了你的回复:#{params[:content]}"
user_id = Discuss.find(params[:reply_id]).try(:user_id)
parent = Discuss.where(:id => params[:reply_id]).first
if parent.present?
@model.discusses << Discuss.new(
:content => params[:content],
:user_id => User.current.id,
:parent_id => params[:reply_id],
:root_id => parent.root_id || parent.id,
:praise_count => 0,
:challenge_id => params[:challenge_id],
:position => position
)
end
end
# JournalsForMessage.create(
# :jour_id => user_id,
# :jour_type => "Principal",
# :user_id => User.current.id,
# :reply_id => 0,
# :is_readed => 0,
# :private => 1,
# :notes => notes
# )
if params[:dis_type] == "Shixun"
redirect_to shixun_discuss_shixun_path(@model, :challenge_id => params[:challenge_id])
end
end
end
def hidden_record
if params[:hidden] == "1"
@discuss.update_attribute(:hidden, true)
elsif params[:hidden] == "0"
@discuss.update_column("hidden", false)
end
respond_to do |format|
format.js
end
end
def destroy
if @discuss
if @discuss.dis_type == "Shixun"
@shixun = @discuss.dis
end
@discuss.destroy
@game_challenge = Challenge.find(params[:challenge_id])
end
end
private
def find_discuss
@discuss = Discuss.find params[:id]
rescue ActiveRecord::RecordNotFound
render_404
end
def find_dis_object
unless params[:dis_id] && params[:dis_type]
render_404
else
case params[:dis_type]
when 'Shixun'
@model = Shixun.find params[:dis_id]
end
end
rescue ActiveRecord::RecordNotFound
render_404
end
end