# encoding: utf-8
include UserScoreHelper

module StudentWorkHelper
  #获取当前用户的项目列表
  def user_projects_option
    projects = User.current.projects.visible
    not_have_project = []
    not_have_project << "请选择关联项目"
    not_have_project << 0
    type = []
    type << not_have_project
    projects.each do |project|
      if project
        option = []
        option <<  project.name
        option <<  project.id
        type << option
      end
    end
    type
  end

  #获取指定用户对某一作业的评分结果
  def student_work_score work,user,reviewer_role=0
    if reviewer_role != 0
      StudentWorksScore.where(:user_id => user.id,:student_work_id => work.id,:reviewer_role => reviewer_role).last
    else
      StudentWorksScore.where(:user_id => user.id,:student_work_id => work.id).last
    end
  end

  #获取指定评分的角色
  def student_work_score_role score
    case score.reviewer_role
      when 1
        role = "教师"
      when 2
        role = "助教"
      when 3
        role = "学生"
    end
  end

  def get_role_by_name role
    case role
      when "Teacher"
        result = 1
      when "Manager"
        result = 1
      when "TeachingAsistant"
        result = 2
      when "Student"
        result = 3
    end
    result
  end

  def get_status status
    str = ""
    case status
      when 0
        str = "未提交"
      when 1
        str = "已提交"
      when 2
        str = "迟交"
    end
    str
  end

  #获取学生作品的评分记录:同一个教师或教辅只显示最后一次评分
  def student_work_score_record work
    sql = "SELECT MAX(id) id FROM student_works_scores WHERE (reviewer_role = 2 OR reviewer_role = 1) AND score IS NOT NULL AND student_work_id = #{work.id} GROUP BY user_id"
    tea_ts_ids = StudentWorksScore.find_by_sql sql
    tea_ts_ids = tea_ts_ids.empty? ? "(-1)" : "(" + tea_ts_ids.map{|tea| tea.id}.join(",") + ")"
    scores = work.student_works_scores.where("score IS NULL or reviewer_role = 3 or id in #{tea_ts_ids}").order("updated_at desc")
    return scores
  end

  #获取赞的总数
  def praise_homework_count obj_id
    PraiseTread.where("praise_tread_object_id = #{obj_id} AND praise_tread_object_type = 'StudentWork'").count
  end

  #判断指定用户是不是已经赞过该作业
  def is_praise_homework user_id, obj_id
    PraiseTread.where("user_id = #{user_id} AND praise_tread_object_id = #{obj_id} AND praise_tread_object_type = 'StudentWork'").empty?
  end

  #获取指定学生在指定作业内应匿评的数量
  def all_evaluation_count user,homework
    StudentWorksEvaluationDistribution.joins(:student_work).where("student_works_evaluation_distributions.user_id = #{user.id} AND student_works.homework_common_id = #{homework.id}").count
  end

  #获取指定学生在指定作业内已匿评的数量
  def has_evaluation_count user,homework
    StudentWorksScore.joins(:student_work).where("student_works_scores.user_id = #{user.id} AND student_works.homework_common_id = #{homework.id}").count
  end

  #传入分数,获取对应颜色
  def score_color score
    if score
      color = score >= 90 ? "c_red" : "c_green"
    else
      color = "c_grey"
    end
    color
  end

  #获取分班信息
  def course_group_list course
    result = []
    if course.course_groups && !course.course_groups.empty?
      base = []
      base << l(:label_chose_group)
      base << -1
      result << base
      base = []
      base << l(:label_no_group)
      base << 0
      result << base
      course.course_groups.each do |group|
        option = []
        option << group.name
        option << group.id
        result << option
      end
    end
    result
  end

  #教辅评分比例下拉框
  def ta_proportion_option
    type = []
    i = 0
    while  i <= 100
      option = []
      option << i.to_s + "%"
      option << i.to_f / 100
      type << option
      i += 10
    end
    type
  end

  def ta_proportion_option_to num
    type = []
    i = 0
    while  i <= num
      option = []
      option << i.to_s + "%"
      option << i.to_f / 100
      type << option
      i += 10
    end
    type
  end

  def revise_attachment_status homework, attach
    date = Time.parse(format_time(attach.created_on.to_s)).strftime("%Y-%m-%d")
    status = ""
    if homework.homework_detail_manual && ((homework.anonymous_comment == 0 &&homework.homework_detail_manual.evaluation_start.to_s <= date) || (homework.anonymous_comment == 1 && homework.end_time.to_s < date))
      status = "此时其他同学作品已公开"
    else
      status = "此时其他同学作品尚未公开"
    end
    return status
  end

  def group_student_works student_work, homework
    pros = student_work.student_work_projects.where("is_leader = 0")
    user_ids = pros.empty? ? "(-1)" : "(" + pros.map{|stu|stu.user_id}.join(",") + ")"
    student_works = homework.student_works.where("user_id in #{user_ids}")
    return student_works
  end

  def add_score_to_member student_work, homework, new_score
    student_works = group_student_works student_work, homework
    student_works.each do |st_work|
      st_work.student_works_scores << StudentWorksScore.create(:user_id => new_score.user_id, :score => new_score.score, :reviewer_role => new_score.reviewer_role, :comment => new_score.comment)
      if new_score.reviewer_role == 1
        st_work.teacher_score = new_score.score
      elsif new_score.reviewer_role == 2
        ts_score = StudentWorksScore.find_by_sql("SELECT AVG(score) AS score FROM (SELECT * FROM (SELECT * FROM student_works_scores WHERE student_work_id = #{st_work.id} AND reviewer_role = 2 AND score IS NOT NULL ORDER BY created_at DESC) AS t GROUP BY user_id) AS a")
        st_work.teaching_asistant_score = ts_score.first.score.nil? ? nil : ts_score.first.score.try(:round, 2).to_f
      else
        st_work.student_score = student_work.student_score
      end
      st_work.save
    end
  end
end