jours = PrivateMessage.find_by_sql("SELECT ui.* FROM (SELECT * FROM private_messages WHERE STATUS != 2 AND user_id = #{@user.id} ORDER BY id DESC) ui GROUP BY ui.target_id ORDER BY ui.send_time DESC")
# jours = PrivateMessage.without_deleted.where("user_id = ?", @user.id).order("created_at desc")
user_message_details = PrivateMessage.find_by_sql("SELECT ui.* FROM (SELECT * FROM private_messages WHERE STATUS != 2 AND user_id = #{@user.id} ORDER BY id DESC) ui GROUP BY ui.target_id ORDER BY ui.send_time DESC")
@search_users = User.where("id != #{@user.id} and LOWER(concat(lastname, firstname)) LIKE '%#{params[:receiver_name].strip.to_s.downcase}%'").includes(:user_extensions)
# @search_users = User.where("LOWER(concat(lastname, ifnull(firstname, ''), login)) LIKE ?", "#{params[:receiver_name].strip.to_s.downcase}")
@message_alls = CourseMessage.where("course_message_type in ('HomeworkCommon','StudentWorksScore','JournalsForMessage','StudentWork','Exercise') and user_id =? and course_id NOT IN #{course_ids}", @user.id).order("created_at desc")
when 'course_message'
@message_alls = CourseMessage.where("course_message_type =? and user_id =? and course_id NOT IN #{course_ids}", "Message", @user.id).order("created_at desc")
when 'course_news'
# 课程通知包含发布的通知和回复的通知
@message_alls = CourseMessage.where("course_message_type in ('News', 'Comment') and user_id =? and course_id NOT IN #{course_ids}", @user.id).order("created_at desc")
when 'poll'
@message_alls = CourseMessage.where("course_message_type =? and user_id =? and course_id NOT IN #{course_ids}", "Poll", @user.id).order("created_at desc")
#项目相关消息
when 'issue'
@message_alls = ForgeMessage.where("forge_message_type in ('Issue', 'Journal') and user_id =?" , @user.id).order("created_at desc")
when 'forge_message'
@message_alls = ForgeMessage.where("forge_message_type =? and user_id =?", "Message", @user.id).order("created_at desc")
when 'forge_news'
@message_alls = ForgeMessage.where("forge_message_type in (?,?) and user_id =?", "News", "Comment", @user.id).order("created_at desc")
when 'apply'
@message_alls = ForgeMessage.where("forge_message_type in ('ProjectInvite', 'AppliedProject', 'JoinProject', 'RemoveFromProject') and user_id =?", @user.id).order("created_at desc")
#贴吧消息
when 'forum'
@message_alls = MemoMessage.where("memo_type =? and user_id =?", "Memo", @user.id).order("created_at desc")
#用户留言
when 'user_feedback'
@message_alls = UserFeedbackMessage.where("journals_for_message_type =? and user_id =?", "JournalsForMessage", @user.id).order("created_at desc")
homeworks = HomeworkCommon.includes(:homework_detail_manual).where("homework_commons.course_id in #{my_course_ids} and homework_detail_manuals.comment_status = 2")
sql = "SELECT * FROM `message_alls` ma left join `forge_messages` fm on ma.message_id=fm.id
left join `course_messages` cm on ma.message_id=cm.id left join `applied_messages` apm on ma.message_id=apm.id
where (ma.user_id =#{user_id} and ma.message_type = 'ForgeMessage' and fm.forge_message_type='PullRequest' and fm.status=0 and fm.user_id=#{user_id} ) or
(ma.user_id=#{user_id} and ma.message_type='AppliedMessage' and apm.applied_type in ('Organization', 'AppliedContest', 'StudentWorksScoresAppeal', 'AppliedProject', 'ApplyAddSchools') and apm.user_id =#{user_id} and apm.status =0 ) or
(ma.user_id=#{user_id} and ma.message_type='CourseMessage' and cm.course_message_type='JoinCourseRequest' and cm.status=0 and cm.user_id=#{user_id} );"
shield_project_ids = ShieldActivity.select("shield_id").where("container_type='User' and container_id=#{@user.id} and shield_type='Project'").map(&:shield_id)
shield_course_ids = ShieldActivity.select("shield_id").where("container_type='User' and container_id=#{@user.id} and shield_type='Course'").map(&:shield_id)
sql = "user_id = #{@user.id} and ((container_type = 'Project' and container_id in #{user_project_ids} and act_type in #{project_types}) or (container_type = 'Course' and container_id in #{user_course_ids} and act_type in #{course_types}))"
if params[:type].present?
case params[:type]
when "course_homework"
container_type = 'Course'
act_type = 'HomeworkCommon'
when "course_news"
container_type = 'Course'
act_type = 'News'
when "course_message"
container_type = 'Course'
act_type = 'Message'
when "course_poll"
container_type = 'Course'
act_type = 'Poll'
when "course_journals"
container_type = 'Course'
act_type = 'JournalsForMessage'
when "project_issue"
container_type = 'Project'
act_type = 'Issue'
when "project_message"
container_type = 'Project'
act_type = 'Message'
when "user_journals"
container_type = 'Principal'
act_type = 'JournalsForMessage'
when "current_user"
container_type = 'Principal'
act_type = 'Principal'
when "all"
container_type = 'all'
act_type = 'all'
end
end
if container_type != '' && container_type != 'all'
if container_type == 'Course'
sql = "container_type = '#{container_type}' and container_id in #{user_course_ids} and act_type = '#{act_type}'"
elsif container_type == 'Project'
sql = "container_type = '#{container_type}' and container_id in #{user_project_ids} and act_type = '#{act_type}'"
sql = "user_id = #{@user.id} and ((container_type = 'Project' and container_id in #{user_project_ids} and act_type in #{project_types}) or (container_type = 'Course' and container_id in #{user_course_ids} and act_type in #{course_types}))"
shield_project_ids = ShieldActivity.select("shield_id").where("container_type='User' and container_id=#{@user.id} and shield_type='Project'").pluck(:shield_id)
# homeworks = HomeworkCommon.includes(:homework_detail_manual).where("homework_commons.course_id in #{my_course_ids} and homework_detail_manuals.comment_status = 2")
# sql = "SELECT * FROM `message_alls` ma left join `forge_messages` fm on ma.message_id=fm.id
# left join `course_messages` cm on ma.message_id=cm.id left join `applied_messages` apm on ma.message_id=apm.id
# where (ma.user_id =#{user_id} and ma.message_type = 'ForgeMessage' and fm.forge_message_type='PullRequest' and fm.status=0 and fm.user_id=#{user_id} ) or
# (ma.user_id=#{user_id} and ma.message_type='AppliedMessage' and apm.applied_type in ('Organization', 'AppliedContest', 'StudentWorksScoresAppeal', 'AppliedProject', 'ApplyAddSchools') and apm.user_id =#{user_id} and apm.status =0 ) or
# (ma.user_id=#{user_id} and ma.message_type='CourseMessage' and cm.course_message_type='JoinCourseRequest' and cm.status=0 and cm.user_id=#{user_id} );"
# shield_project_ids = ShieldActivity.select("shield_id").where("container_type='User' and container_id=#{@user.id} and shield_type='Project'").map(&:shield_id)
# shield_course_ids = ShieldActivity.select("shield_id").where("container_type='User' and container_id=#{@user.id} and shield_type='Course'").map(&:shield_id)
# sql = "user_id = #{@user.id} and ((container_type = 'Project' and container_id in #{user_project_ids} and act_type in #{project_types}) or (container_type = 'Course' and container_id in #{user_course_ids} and act_type in #{course_types}))"
# if params[:type].present?
# case params[:type]
# when "course_homework"
# container_type = 'Course'
# act_type = 'HomeworkCommon'
# when "course_news"
# container_type = 'Course'
# act_type = 'News'
# when "course_message"
# container_type = 'Course'
# act_type = 'Message'
# when "course_poll"
# container_type = 'Course'
# act_type = 'Poll'
# when "course_journals"
# container_type = 'Course'
# act_type = 'JournalsForMessage'
# when "project_issue"
# container_type = 'Project'
# act_type = 'Issue'
# when "project_message"
# container_type = 'Project'
# act_type = 'Message'
# when "user_journals"
# container_type = 'Principal'
# act_type = 'JournalsForMessage'
# when "current_user"
# container_type = 'Principal'
# act_type = 'Principal'
# when "all"
# container_type = 'all'
# act_type = 'all'
# end
# end
# if container_type != '' && container_type != 'all'
# if container_type == 'Course'
# sql = "container_type = '#{container_type}' and container_id in #{user_course_ids} and act_type = '#{act_type}'"
# elsif container_type == 'Project'
# sql = "container_type = '#{container_type}' and container_id in #{user_project_ids} and act_type = '#{act_type}'"
# sql = "user_id = #{@user.id} and ((container_type = 'Project' and container_id in #{user_project_ids} and act_type in #{project_types}) or (container_type = 'Course' and container_id in #{user_course_ids} and act_type in #{course_types}))"
@unfinished_homeworks = HomeworkCommon.where("course_id in #{my_course_ids} and publish_time <= '#{Time.now}' and id not in #{finished_works_id}").order("updated_at desc")
@unfinished_poll = Poll.where("polls_type = 'Course' and polls_group_id in #{my_course_ids} and polls_status = 2 and id not in #{finished_poll_ids}").order("updated_at desc")
# @unfinished_poll = Poll.where("polls_type = 'Course' and polls_group_id in #{my_course_ids} and polls_status = 2 and id not in #{finished_poll_ids}").order("updated_at desc")
homeworks = HomeworkCommon.includes(:homework_detail_manual).where("homework_commons.course_id in #{my_course_ids} and homework_detail_manuals.comment_status in (1,2)")
@anonymous_evaluation_count = StudentWork.where("student_works.id in #{unfinished_evaluations_work_ids} and homework_common_id in #{homework_ids}").count
homework_ids = StudentWork.where("id in #{unfinished_evaluations_work_ids} and homework_common_id in #{homework_ids}").blank? ? "(-1)" : "(" + StudentWork.where("id in #{unfinished_evaluations_work_ids} and homework_common_id in #{homework_ids}").map{|st| st.homework_common_id}.join(",") + ")"
@anonymous_evaluation = HomeworkCommon.where("homework_commons.id in #{homework_ids}")
# homeworks = HomeworkCommon.includes(:homework_detail_manual).where("homework_commons.course_id in #{my_course_ids} and homework_detail_manuals.comment_status in (1,2)")
# @anonymous_evaluation_count = StudentWork.where("student_works.id in #{unfinished_evaluations_work_ids} and homework_common_id in #{homework_ids}").count
# homework_ids = StudentWork.where("id in #{unfinished_evaluations_work_ids} and homework_common_id in #{homework_ids}").blank? ? "(-1)" : "(" + StudentWork.where("id in #{unfinished_evaluations_work_ids} and homework_common_id in #{homework_ids}").map{|st| st.homework_common_id}.join(",") + ")"
#
# @anonymous_evaluation = HomeworkCommon.where("homework_commons.id in #{homework_ids}")
@@ -2326,22 +2334,7 @@ class UsersController < ApplicationController
end
end
# 待审批的申请
# 待审批的消息一般不多,可以特殊处理
def unapproval_applied_list
user_id = User.current.id
sql = "SELECT * FROM `message_alls` ma left join `forge_messages` fm on ma.message_id=fm.id
left join `course_messages` cm on ma.message_id=cm.id left join `applied_messages` apm on ma.message_id=apm.id
where (ma.user_id =#{user_id} and ma.message_type = 'ForgeMessage' and fm.forge_message_type='PullRequest' and fm.status=0 and fm.user_id=#{user_id} ) or
(ma.user_id=#{user_id} and ma.message_type='AppliedMessage' and apm.applied_type in ('Organization', 'AppliedContest', 'StudentWorksScoresAppeal', 'AppliedProject', 'ApplyAddSchools') and apm.user_id =#{user_id} and apm.status =0 ) or
(ma.user_id=#{user_id} and ma.message_type='CourseMessage' and cm.course_message_type='JoinCourseRequest' and cm.status=0 and cm.user_id=#{user_id}) order by ma.created_at desc;"
@message_alls = MessageAll.find_by_sql(sql)
@message_count = @message_alls.count
@message_alls = paginateHelper @message_alls, 20
respond_to do |format|
format.js
end
end
# 课程社区
def course_community
@@ -2413,7 +2406,7 @@ class UsersController < ApplicationController
end
end
# 项目社区
# TODO: 项目社区, 已废弃
def project_community
# 看别人的主页显示动态
#更新用户申请成为课程老师或教辅消息的状态
@@ -2469,6 +2462,39 @@ class UsersController < ApplicationController
#Project.find_by_sql("SELECT * FROM projects RIGHT OUTER JOIN (SELECT * FROM project_statuses ORDER BY grade DESC LIMIT #{limit} ) AS t ON projects.id = t.project_id ")
sql = "SELECT * FROM `message_alls` ma left join `forge_messages` fm on ma.message_id=fm.id
left join `applied_messages` apm on ma.message_id=apm.id
where (ma.user_id =#{id} and ma.message_type = 'ForgeMessage' and fm.user_id=#{id} and fm.forge_message_type != 'News') or
(ma.user_id=#{id} and ma.message_type='AppliedMessage' and apm.applied_type = 'AppliedProject' and apm.user_id =#{id}) ORDER BY ma.created_at desc;"
MessageAll.find_by_sql(sql)
end
#其他用户的项目消息
def other_project_relate_messages(project_ids)
sql = "SELECT * FROM `message_alls` ma left join `forge_messages` fm on fm.project_id in #{project_ids} and ma.message_id=fm.id
left join `applied_messages` apm on apm.project_id in #{project_ids} and ma.message_id=apm.id
where (ma.user_id =#{id} and ma.message_type = 'ForgeMessage' and fm.user_id=#{id} and fm.forge_message_type != 'News') or
(ma.user_id=#{id} and ma.message_type='AppliedMessage' and apm.applied_type = 'AppliedProject' and apm.user_id =#{id}) ORDER BY ma.created_at desc;"
MessageAll.find_by_sql(sql)
end
#未处理的消息通知
def user_unsolve_messages
sql = "SELECT * FROM `message_alls` ma left join `forge_messages` fm on ma.message_id=fm.id
left join `course_messages` cm on ma.message_id=cm.id left join `applied_messages` apm on ma.message_id=apm.id
where (ma.user_id =#{id} and ma.message_type = 'ForgeMessage' and fm.forge_message_type='PullRequest' and fm.status=0 and fm.user_id=#{id}) or
(ma.user_id=#{id} and ma.message_type='AppliedMessage' and apm.applied_type in ('Organization', 'AppliedContest', 'StudentWorksScoresAppeal', 'AppliedProject', 'ApplyAddSchools') and apm.user_id =#{id} and apm.status =0) or
(ma.user_id=#{id} and ma.message_type='CourseMessage' and cm.course_message_type='JoinCourseRequest' and cm.status=0 and cm.user_id=#{id}) ORDER BY ma.created_at desc;"
MessageAll.find_by_sql(sql)
end
def get_user_course_ids #获取用户的全部课堂id
StudentsForCourse.includes(:course).where("student_id = #{id} and courses.is_delete = 0").map{|sf| sf.try(:course_id)}
unfinished_evaluations_work_ids = student_works_evaluation_distributions.where("student_work_id not in #{anonymous_evaluation_ids}").pluck(:student_work_id)
course_count = CourseMessage.where("user_id =? and viewed =? and created_at >? and course_id not in #{delete_courses}", user.id, 0, onclick_time).count
contest_count = ContestMessage.where("user_id =? and viewed =? and created_at >?", user.id, 0, onclick_time).count
forge_count = ForgeMessage.where("user_id =? and viewed =? and created_at >?", user.id, 0, onclick_time).count
user_feedback_count = UserFeedbackMessage.where("user_id =? and viewed =? and created_at >?", user.id, 0, onclick_time).count
user_memo_count = MemoMessage.where("user_id =? and viewed =? and created_at >?", user.id, 0, onclick_time).count