# 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 un_commit_num poll
    course = poll.course
    member = course.members.where(:user_id => User.current.id).first  ## 当前用户是否为课堂成员
    poll_users = poll.poll_users  # 问卷的全部用户,包含已回答的/为回答,但是浏览的
    student_count = course.student.count  # 课堂的学生数
    if member.present? && member.teacher_course_groups.count > 0
      group_students = course.members.where(:course_group_id => member.teacher_course_groups.pluck(:course_group_id)).map(&:user_id)  ## 统计当前用户所在班级的全部学生
      student_count = group_students.size
      poll_users = poll_users.where(:user_id => group_students)
    end
    student_count - poll_users.where(:commit_status => 1, :user_id => course.student.map(&:student_id)).count
  end

  def has_commit_num poll
    course = poll.course
    member = course.members.where(:user_id => User.current.id).first
    poll_users = poll.poll_users
    if member.present? && member.teacher_course_groups.count > 0
      group_students = course.members.where(:course_group_id => member.teacher_course_groups.pluck(:course_group_id)).map(&:user_id)
      poll_users = poll_users.where(:user_id => group_students)
    end
    poll_users.where(:commit_status => 1, :user_id => course.student.map(&:student_id)).count
  end

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

  def new_get_anwser_vote_text question, user, answer_id=0
    if answer_id != 0
      pv = question.poll_votes.select{|poll_vote| poll_vote.user_id == user.id && poll_vote.poll_answer_id == answer_id}
    else
      pv = question.poll_votes.select{|poll_vote| poll_vote.user_id == user.id}
    end
    if pv.blank?
      ''
    else
      pv.first.try(: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? || pu.commit_status == 0
      false
    else
      true
    end
  end

  def can_edit_poll poll, poll_user
    poll_end = poll.end_time.nil? ? false : poll.end_time > Time.now
    can_edit_poll = poll_end && poll_user.commit_status == 0
    can_edit_poll
  end
  
  #多选的时候查询去重
  def total_answer id, user_ids
    user_ids = user_ids.blank? ? "(-1)" : "(" + user_ids.join(",") + ")"
    total = PollVote.find_by_sql("SELECT distinct(user_id) FROM `poll_votes` where poll_question_id = #{id} and user_id in #{user_ids}").count
  end

  # 获取其它选项的答案
  def other_answers poll_question, user_ids
    poll_question.poll_votes.where(:user_id => user_ids).select{|pv| !pv.try(:vote_text).blank?}
  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

  #问卷的多选题上下限
  def min_or_max_choices_option pq
    type = []
    count = pq.poll_answers.count
    option = []
    option << '不限'
    option << 0
    type << option
    for i in 1 .. count do
      option = []
      option << i
      option << i
      type << option
    end
    type
  end

end