diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 93ae61d6a..3b63d954c 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -42,7 +42,7 @@ class UsersController < ApplicationController :activity_new_score_index, :influence_new_score_index, :score_new_index,:user_projects_index,:user_resource, :user_courses4show,:user_projects4show,:user_course_activities,:user_project_activities,:user_feedback4show,:user_visitorlist,:user_messages,:edit_brief_introduction, :user_import_homeworks,:user_search_homeworks,:user_import_resource, :user_system_messages,:choose_user_course,:user_courselist,:user_projectlist,:sort_syllabus_list, - :sort_project_list,:my_homeworks,:manage_or_receive_homeworks,:search_m_r_homeworks, :cancel_or_collect,:expand_courses,:homepage, :user_issues] + :sort_project_list,:my_homeworks,:manage_or_receive_homeworks,:search_m_r_homeworks, :cancel_or_collect,:expand_courses,:homepage, :user_issues, :course_community, :project_community] before_filter :auth_user_extension, only: :show #before_filter :rest_user_score, only: :show #before_filter :select_entry, only: :user_projects @@ -433,7 +433,8 @@ class UsersController < ApplicationController render_404 return end - @message_alls = paginateHelper @message_alls,25 + + @message_alls = paginateHelper @message_alls, 25 respond_to do |format| format.html{render :layout=>'new_base_user'} end @@ -1814,22 +1815,140 @@ class UsersController < ApplicationController end def show - #更新用户申请成为课程老师或教辅消息的状态 + if is_current_user + # 自己的主页显示消息 + # 系统消息为管理员发送,我的消息中包含有系统消息 + @message_alls = [] + messages = MessageAll.where("(user_id =? and message_type !=?) or message_type =?" ,@user.id, "SystemMessage", "SystemMessage").includes(:message).order("created_at desc") + messages.each do |message_all| + mess = message_all.message + unless (message_all.message_type == 'CourseMessage' && mess && mess.course && mess.course.is_delete == 1) + @message_alls << mess + end + end + @message_count = @message_alls.count + @message_alls = paginateHelper @message_alls, 20 + # 用户待解决的issue + @unsolved_issues = Issue.where(:assigned_to_id => @user.id, :status_id => [1, 2, 4, 6]) + # 用户待完成的作业 + @my_course = StudentsForCourse.where(:student_id => @user.id) + @unfinished_homework = 1 + # 用户待完成的测验 + @unfinished_test = 1 + # 用户待完成的问卷 + @unfinished_poll = 1 + # 用户待匿评的作业 + @anonymous_evaluation = 1 + # 待评阅的作业 + @unreview_homework = 1 + # 待审批的作业 + @unapproval_homework = 1 + else + # 看别人的主页显示动态 + #更新用户申请成为课程老师或教辅消息的状态 + if params[:course_id] != nil + join_course_messages = CourseMessage.where("course_id =? and course_message_type =? and user_id =? and course_message_id =? and viewed =?", + params[:course_id], 'JoinCourseRequest', User.current.id, @user.id, false) + join_course_messages.update_all(:viewed => true) + end + 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) + @page = params[:page] ? params[:page].to_i + 1 : 0 + user_project_ids = (@user.favorite_projects.visible.map{|project| project.id}-shield_project_ids).empty? ? "(-1)" : "(" + (@user.favorite_projects.visible.map{|project| project.id}-shield_project_ids).join(",") + ")" + user_course_ids = (@user.favorite_courses.visible.where("is_delete = 0").map{|course| course.id}-shield_course_ids).empty? ? "(-1)" : "(" + (@user.favorite_courses.visible.where("is_delete = 0").map{|course| course.id}-shield_course_ids).join(",") + ")" + course_types = "('Message','News','HomeworkCommon','Poll','Course','JournalsForMessage')" + project_types = "('Message','Issue','Project')" + principal_types = "JournalsForMessage" + container_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}))" + 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}'" + elsif container_type == 'Principal' && act_type == 'JournalsForMessage' + sql = "container_type = '#{container_type}' and act_type= '#{act_type}' and container_id = #{@user.id}" + elsif container_type == 'Principal' && act_type == 'Principal' + 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}))" + end + if User.current != @user + sql += " and user_id = #{@user.id}" + end + else + if User.current != @user + blog_ids = "("+@user.blog.id.to_s+")" + else + blog_ids = "("+@user.blog.id.to_s+","+((User.watched_by(@user.id).count == 0 )? '0' :User.watched_by(@user.id).map{|u| u.blog.id}.join(','))+")" + end + sql = "(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}) "+ + "or (container_type = 'Principal' and act_type= '#{principal_types}' and container_id = #{@user.id}) " + + "or (container_type = 'Blog' and act_type= 'BlogComment' and container_id in #{blog_ids})" + if container_type != 'all' && User.current != @user + sql = "user_id = #{@user.id} and(" + sql + ")" + end + end + @user_activities_count = UserActivity.where("#{sql}").order('updated_at desc').count + @user_activities = UserActivity.where("#{sql}").order('updated_at desc').limit(10).offset(@page * 10) + @type = params[:type] + end + respond_to do |format| + format.js + format.html {render :layout => 'new_base_user'} + end + end + + # 课程社区 + def course_community if params[:course_id] != nil join_course_messages = CourseMessage.where("course_id =? and course_message_type =? and user_id =? and course_message_id =? and viewed =?", params[:course_id], 'JoinCourseRequest', User.current.id, @user.id, false) join_course_messages.update_all(:viewed => true) end - 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) @page = params[:page] ? params[:page].to_i + 1 : 0 - user_project_ids = (@user.favorite_projects.visible.map{|project| project.id}-shield_project_ids).empty? ? "(-1)" : "(" + (@user.favorite_projects.visible.map{|project| project.id}-shield_project_ids).join(",") + ")" user_course_ids = (@user.favorite_courses.visible.where("is_delete = 0").map{|course| course.id}-shield_course_ids).empty? ? "(-1)" : "(" + (@user.favorite_courses.visible.where("is_delete = 0").map{|course| course.id}-shield_course_ids).join(",") + ")" course_types = "('Message','News','HomeworkCommon','Poll','Course','JournalsForMessage')" - project_types = "('Message','Issue','Project')" principal_types = "JournalsForMessage" container_type = '' act_type = '' + if params[:type].present? case params[:type] when "course_homework" @@ -1847,6 +1966,63 @@ class UsersController < ApplicationController when "course_journals" container_type = 'Course' act_type = 'JournalsForMessage' + 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 == 'Principal' && act_type == 'JournalsForMessage' + sql = "container_type = '#{container_type}' and act_type= '#{act_type}' and container_id = #{@user.id}" + elsif container_type == 'Principal' && act_type == 'Principal' + sql = "user_id = #{@user.id} and (container_type = 'Course' and container_id in #{user_course_ids} and act_type in #{course_types})" + end + if User.current != @user + sql += " and user_id = #{@user.id}" + end + else + if User.current != @user + blog_ids = "("+@user.blog.id.to_s+")" + else + blog_ids = "("+@user.blog.id.to_s+","+((User.watched_by(@user.id).count == 0 )? '0' :User.watched_by(@user.id).map{|u| u.blog.id}.join(','))+")" + end + sql = "(container_type = 'Course' and container_id in #{user_course_ids} and act_type in #{course_types})" + + "or (container_type = 'Principal' and act_type= '#{principal_types}' and container_id = #{@user.id}) " + + "or (container_type = 'Blog' and act_type= 'BlogComment' and container_id in #{blog_ids})" + if container_type != 'all' && User.current != @user + sql = "user_id = #{@user.id} and(" + sql + ")" + end + end + @user_activities_count = UserActivity.where("#{sql}").order('updated_at desc').count + @user_activities = UserActivity.where("#{sql}").order('updated_at desc').limit(10).offset(@page * 10) + @type = params[:type] + respond_to do |format| + format.js + format.html {render :layout => 'base_course_community'} + end + end + + # 项目社区 + def project_community + # 看别人的主页显示动态 + #更新用户申请成为课程老师或教辅消息的状态 + shield_project_ids = ShieldActivity.select("shield_id").where("container_type='User' and container_id=#{@user.id} and shield_type='Project'").map(&:shield_id) + @page = params[:page] ? params[:page].to_i + 1 : 0 + user_project_ids = (@user.favorite_projects.visible.map{|project| project.id}-shield_project_ids).empty? ? "(-1)" : "(" + (@user.favorite_projects.visible.map{|project| project.id}-shield_project_ids).join(",") + ")" + project_types = "('Message','Issue','Project')" + principal_types = "JournalsForMessage" + container_type = '' + act_type = '' + if params[:type].present? + case params[:type] when "project_issue" container_type = 'Project' act_type = 'Issue' @@ -1865,14 +2041,12 @@ class UsersController < ApplicationController 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' + if container_type == 'Project' sql = "container_type = '#{container_type}' and container_id in #{user_project_ids} and act_type = '#{act_type}'" elsif container_type == 'Principal' && act_type == 'JournalsForMessage' sql = "container_type = '#{container_type}' and act_type= '#{act_type}' and container_id = #{@user.id}" elsif container_type == 'Principal' && act_type == 'Principal' - 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}))" + sql = "user_id = #{@user.id} and ((container_type = 'Project' and container_id in #{user_project_ids} and act_type in #{project_types}))" end if User.current != @user sql += " and user_id = #{@user.id}" @@ -1884,19 +2058,18 @@ class UsersController < ApplicationController blog_ids = "("+@user.blog.id.to_s+","+((User.watched_by(@user.id).count == 0 )? '0' :User.watched_by(@user.id).map{|u| u.blog.id}.join(','))+")" end sql = "(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}) "+ "or (container_type = 'Principal' and act_type= '#{principal_types}' and container_id = #{@user.id}) " + "or (container_type = 'Blog' and act_type= 'BlogComment' and container_id in #{blog_ids})" if container_type != 'all' && User.current != @user sql = "user_id = #{@user.id} and(" + sql + ")" end end - + @user_activities_count = UserActivity.where("#{sql}").order('updated_at desc').count @user_activities = UserActivity.where("#{sql}").order('updated_at desc').limit(10).offset(@page * 10) @type = params[:type] respond_to do |format| format.js - format.html {render :layout => 'new_base_user'} + format.html {render :layout => 'base_project_community'} end end @@ -3547,7 +3720,7 @@ class UsersController < ApplicationController # @syllabus = paginateHelper @syllabus,@limit respond_to do |format| - format.html {render :layout => 'new_base_user'} + format.html {render :layout => 'base_course_community'} end end @@ -3691,7 +3864,7 @@ class UsersController < ApplicationController @my_joined_projects_count = @my_joined_projects.count respond_to do |format| - format.html {render :layout => 'new_base_user'} + format.html {render :layout => 'base_project_community'} end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 35b96ce92..73b5e9b5f 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -37,6 +37,11 @@ module ApplicationHelper # super # end + # 获取多种类型的user用户名 + def user_message_username user + user.try(:show_name) + end + # 超出1w后用k+形式显示 def switch_integer_into_k number number > 10000 ? (number.to_f / 1000).round.to_s + "k" : number @@ -2815,6 +2820,37 @@ module ApplicationHelper end technical_title end + # 用户项目总数 + def user_project_count + @my_projects = @user.projects.select("projects.*,(SELECT MAX(updated_at) FROM `forge_activities` WHERE forge_activities.project_id = projects.id) AS updatetime").order("updatetime DESC") + @my_project_total = @my_projects.count + end + + # 用户的课程总数 + def user_course_count + @my_course_count = @user.syllabuses.count + + sy_courses = @user.courses.visible.not_deleted + syllabus_ids = sy_courses.empty? ? '(-1)' : "(" + sy_courses.map{|course| !course.syllabus_id.nil? && course.syllabus_id}.join(",") + ")" + syllabus_members = SyllabusMember.where("user_id = #{@user.id}") + syllabus_member_ids = syllabus_members.empty? ? "(-1)" : "(" + syllabus_members.map{|syl_mem| syl_mem.syllabus_id}.join(',') + ")" + @join_syllabuses = Syllabus.where("(id in #{syllabus_ids} or id in #{syllabus_member_ids}) and user_id != #{@user.id}") + @my_joined_course_count = @join_syllabuses.count + + @user_course_total = @my_joined_course_count + @my_course_count + end + + # 用户发布的issue数 + def issues_author_is_self_count + @issues = Issue.where( :author_id => @user.id ) + @issues_author_is_self_count = @issues.count + end + + # 用户收到的issue数 + def issues_assigned_is_self_count + @issues = Issue.where( :assigned_to_id => @user.id ) + @issues_assigned_is_self_count = @issues.count + end def get_user_roll user technical_title = "" diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb index 98024954d..879a7bb3c 100644 --- a/app/helpers/users_helper.rb +++ b/app/helpers/users_helper.rb @@ -94,13 +94,13 @@ module UsersHelper def pull_request_message_status ma case ma.status when 1 - "创建了PullRequest:" + "创建了PullRequest:" when 2 - "接受了PullRequest:" + "接受了PullRequest:" when 3 - "重新打开了PullRequest:" + "重新打开了PullRequest:" when 4 - "关闭了PullRequest:" + "关闭了PullRequest:" end end @@ -135,11 +135,11 @@ module UsersHelper def applied_project_tip applied_message case applied_message.status when 4 - "拒绝申请加入项目:" + "拒绝申请加入:" when 5,3,2,1,7 - "申请加入项目:" + "申请加入:" when 6 - "同意申请加入项目" + "同意申请加入:" end end @@ -159,6 +159,10 @@ module UsersHelper end end + def is_current_user + is_current_user = (User.current == @user) + end + def applied_project_tip_header applied_message case applied_message.status when 4 diff --git a/app/models/issue.rb b/app/models/issue.rb index b46e29104..6fdd978f3 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -135,6 +135,10 @@ class Issue < ActiveRecord::Base after_save :after_create_from_copy after_destroy :update_parent_attributes + def user + self.author + end + #动态的更新 def update_activity update_user_activity(self.class, self.id) diff --git a/app/models/message.rb b/app/models/message.rb index 1d76c8ef6..309291183 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -106,6 +106,10 @@ class Message < ActiveRecord::Base end } + def description + self.content + end + def topic? parent_id.nil? end diff --git a/app/views/layouts/_logined_header.html.erb b/app/views/layouts/_logined_header.html.erb index db1e4f1cc..5e1a79314 100644 --- a/app/views/layouts/_logined_header.html.erb +++ b/app/views/layouts/_logined_header.html.erb @@ -7,13 +7,19 @@