# encoding: utf-8
#
# Redmine - project management software
# Copyright (C) 2006-2013  Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

module PollHelper
  #判断选项是否被选中
  def answer_be_selected?(answer,user)
     pv = answer.poll_votes.where("#{PollVote.table_name}.user_id = #{user.id} ")
    if !pv.nil? && pv.count > 0
      true
    else
      false
    end
  end
  
  #获取文本题答案
  def get_anwser_vote_text(question_id,user_id)
    pv = PollVote.find_by_poll_question_id_and_user_id(question_id,user_id)
    if pv.nil?
      ''
    else
      pv.vote_text
    end
  end

  #判断用户是否已经提交了问卷
  def has_commit_poll?(poll_id,user_id)
    pu = PollUser.find_by_poll_id_and_user_id(poll_id,user_id)
    if pu.nil?
      false
    else
      true
    end
  end

  #统计答题百分比，统计结果保留两位小数
  def statistics_result_percentage(e, t)
    e = e.to_f
    t = t.to_f
    t == 0 ? 0 : format("%.2f", e*100/t)
  end
  
  #多选的时候查询去重
  def total_answer id
    total = PollVote.find_by_sql("SELECT distinct(user_id) FROM `poll_votes` where poll_question_id = #{id}").count
  end
  
  #页面体型显示
  def options_show pq
    case pq
    when 1
      l(:label_MC)
    when 2
      l(:label_MCQ)
    when 3
      l(:label_single)
    else
      l(:label_mulit)
    end
  end

  #带勾选框的问卷列表
  def poll_check_box_tags(name,polls,current_poll)
    s = ''
    polls.each do |poll|
        s << "<label>#{ check_box_tag name, poll.id, false, :id => nil } #{h poll.polls_name.blank? ? l(:label_poll_new) : poll.polls_name } [#{ h Course.find(poll.polls_group_id).name}]</label><br/>"
    end
    s.html_safe
  end

end