From aca7684414ca58150cad002f2d00532ecf5ff75c Mon Sep 17 00:00:00 2001 From: huang Date: Thu, 26 Nov 2015 18:22:03 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=BE=E7=A8=8B=E8=8B=B1=E9=9B=84=E6=A6=9C?= =?UTF-8?q?=20=E5=90=84=E6=A8=A1=E5=9D=97=E5=8A=A8=E4=BD=9C=20=E8=A7=A6?= =?UTF-8?q?=E5=8F=91=E5=8A=A0=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/helpers/application_helper.rb | 40 +++++++++++++++++++ app/models/attachment.rb | 16 +++++++- app/models/comment.rb | 13 +++++- app/models/journals_for_message.rb | 10 ++++- app/models/message.rb | 14 ++++++- ...1125072235_add_course_contributor_score.rb | 30 ++++++++++++++ db/schema.rb | 2 +- lib/trustie/gitlab/manage_member.rb | 3 ++ 8 files changed, 123 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20151125072235_add_course_contributor_score.rb diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index f4e0f88ff..a65ed639c 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -75,6 +75,46 @@ module ApplicationHelper end end + # 更新课程英雄榜得分 + # user传过来必须是学生 + def course_member_score(course_id,user_id,type) + course_contributor_score = CourseContributorScore.where("course_id =? and user_id =?", course_id, user_id).first + case type + when "JournalForMessage" + if course_contributor_score.nil? + CourseContributorScore.create(:course_id => course_id, :user_id => user_id, :message_num => 0, :message_reply_num => 0, + :news_reply_num => 0, :resource_num => 0, :journal_num => 1, :journal_reply_num => 0) + else + score = course_contributor_score.journal_num + 1 + course_contributor_score.update_attributes(:journal_num => score) + end + when "Message" + if course_contributor_score.nil? + CourseContributorScore.create(:course_id => course_id, :user_id => user_id, :message_num => 2, :message_reply_num => 0, + :news_reply_num => 0, :resource_num => 0, :journal_num => 0, :journal_reply_num => 0) + else + score = course_contributor_score.message_num + 2 + course_contributor_score.update_attributes(:message_num => score) + end + when "MessageReply" + if course_contributor_score.nil? + CourseContributorScore.create(:course_id => course_id, :user_id => user_id, :message_num => 0, :message_reply_num => 1, + :news_reply_num => 0, :resource_num => 0, :journal_num => 0, :journal_reply_num => 0) + else + score = course_contributor_score.message_reply_num + 1 + course_contributor_score.update_attributes(:message_reply_num => score) + end + when "NewReply" + if course_contributor_score.nil? + CourseContributorScore.create(:course_id => course_id, :user_id => user_id, :message_num => 0, :message_reply_num => 0, + :news_reply_num => 1, :resource_num => 0, :journal_num => 0, :journal_reply_num => 0) + else + score = course_contributor_score.news_reply_num + 1 + course_contributor_score.update_attributes(:news_reply_num => score) + end + end + end + # Added by young # Define the course menu's link class # 不是数组的转化成数组,然后判断当前menu_item是否在给定的列表 diff --git a/app/models/attachment.rb b/app/models/attachment.rb index f7fb9b1aa..22be5a42f 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -74,7 +74,7 @@ class Attachment < ActiveRecord::Base @@thumbnails_storage_path = File.join(Rails.root, "tmp", "thumbnails") before_save :files_to_final_location,:act_as_course_activity - after_create :office_conver, :be_user_score,:act_as_forge_activity + after_create :office_conver, :be_user_score,:act_as_forge_activity, :act_as_student_score after_update :office_conver, :be_user_score after_destroy :delete_from_disk,:down_user_score @@ -560,4 +560,18 @@ class Attachment < ActiveRecord::Base self.course_acts << CourseActivity.new(:user_id => self.author_id,:course_id => self.container_id) end end + + # 课程成员得分(英雄榜) + def act_as_student_score + unless self.author.allowed_to?(:as_teacher, self.course) + if self.parent_id.nil? + # 发帖 + course_member_score(self.course.id, self.author_id, "Message") + else + # 回帖 + course_member_score(self.course.id, self.author_id, "MessageReply") + end + end + end + end diff --git a/app/models/comment.rb b/app/models/comment.rb index bb31eb894..9de25c50d 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -35,7 +35,7 @@ class Comment < ActiveRecord::Base belongs_to :author, :class_name => 'User', :foreign_key => 'author_id' validates_presence_of :commented, :author, :comments safe_attributes 'comments' - after_create :send_mail, :act_as_system_message + after_create :send_mail, :act_as_system_message, :act_as_student_score def act_as_system_message if self.commented.course @@ -66,13 +66,24 @@ class Comment < ActiveRecord::Base def set_notify_id(notify_id) @notify_id= notify_id end + def get_notify_id() return @notify_id end + def set_notify_is_read(notify_is_read) @notify_is_read = notify_is_read end + def get_notify_is_read() return @notify_is_read end + + # 课程成员得分(英雄榜) + def act_as_student_score + unless self.author.allowed_to?(:as_teacher, self.commented.course) + course_member_score(self.commented.course.id, self.author_id, "NewReply") + end + end + end diff --git a/app/models/journals_for_message.rb b/app/models/journals_for_message.rb index 5e40267cb..eaece95ce 100644 --- a/app/models/journals_for_message.rb +++ b/app/models/journals_for_message.rb @@ -64,7 +64,7 @@ class JournalsForMessage < ActiveRecord::Base has_many :user_feedback_messages, :class_name => 'UserFeedbackMessage', :as =>:journals_for_message, :dependent => :destroy validates :notes, presence: true, if: :is_homework_jour? - after_create :act_as_activity, :act_as_course_activity, :act_as_course_message, :act_as_user_feedback_message, :act_as_principal_activity + after_create :act_as_activity, :act_as_course_activity, :act_as_course_message, :act_as_user_feedback_message, :act_as_principal_activity, :act_as_student_score after_create :reset_counters! after_destroy :reset_counters! after_save :be_user_score @@ -263,4 +263,12 @@ class JournalsForMessage < ActiveRecord::Base end end + + # 课程成员得分(英雄榜) + def act_as_student_score + unless self.user.allowed_to?(:as_teacher, self.jour) + course_member_score(self.jour_id, self.user_id, "JournalForMessage") + end + end + end diff --git a/app/models/message.rb b/app/models/message.rb index 7af59815b..21e5e1c71 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -74,7 +74,7 @@ class Message < ActiveRecord::Base after_update :update_messages_board after_destroy :reset_counters!,:down_user_score,:delete_kindeditor_assets - after_create :act_as_activity,:act_as_course_activity,:be_user_score,:act_as_forge_activity, :act_as_system_message, :send_mail + after_create :act_as_activity,:act_as_course_activity,:be_user_score,:act_as_forge_activity, :act_as_system_message, :send_mail, :act_as_student_score #before_save :be_user_score scope :visible, lambda {|*args| @@ -285,4 +285,16 @@ class Message < ActiveRecord::Base delete_kindeditor_assets_from_disk self.id,OwnerTypeHelper::MESSAGE end + # 课程成员得分(英雄榜) + def act_as_student_score + unless self.author.allowed_to?(:as_teacher, self.course) + if self.parent_id.nil? + # 发帖 + course_member_score(self.course.id, self.author_id, "Message") + else + # 回帖 + course_member_score(self.course.id, self.author_id, "MessageReply") + end + end + end end diff --git a/db/migrate/20151125072235_add_course_contributor_score.rb b/db/migrate/20151125072235_add_course_contributor_score.rb new file mode 100644 index 000000000..c3b7c3793 --- /dev/null +++ b/db/migrate/20151125072235_add_course_contributor_score.rb @@ -0,0 +1,30 @@ +class AddCourseContributorScore < ActiveRecord::Migration + def up + # course_count = Course.all.count / 30 + 1 + # transaction do + # for i in 1 ... course_count do i + Course.all.each do |course| + if course.course_activities.count > 1 + course.student.each do |s| + puts course.id + puts course.name + puts s.student_id + board_count = CourseActivity.where("user_id =? and course_id =? and course_act_type =?",s.student_id, course.id, "Message").count * 2 + message_reply_count = Message.find_by_sql("select DISTINCT me.* from messages me, boards b where b.id = me.board_id and b.course_id = #{course.id} and b.project_id = '-1' and me.author_id = #{s.student_id} and me.parent_id is not null;").count * 1 + common_reply_count = Comment.find_by_sql("select cm.* from comments cm, news n where cm.author_id = #{s.student_id} and n.course_id = #{course.id} and cm.commented_id = n.id and cm.commented_type ='News'").count * 1 + attachment_count = CourseActivity.where("user_id =? and course_id =? and course_act_type =?", s.student_id, course.id, "Attachment").count * 5 + journal_count = JournalsForMessage.where("user_id =? and jour_id =? and jour_type =? ", s.student_id, course.id, "Course").count * 1 + # journal_count = CourseActivity.where("user_id =? and course_id =? and course_act_type =?", s.student_id, course.id, "JournalsForMessage").count * 1 + # journal_reply_count = JournalsForMessage.where("user_id =? and jour_id =? and jour_type =? and status =?", s.student_id, course.id, "Course",1).count * 1 + CourseContributorScore.create(:course_id => course.id, :user_id => s.student_id, :message_num => board_count, :message_reply_num => message_reply_count, + :news_reply_num => common_reply_count, :resource_num => attachment_count, :journal_num => journal_count, :journal_reply_num => 0) + end + end + end + # end + # end + end + + def down + end +end \ No newline at end of file diff --git a/db/schema.rb b/db/schema.rb index 64f94cc29..51ac1ad7d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20151125064914) do +ActiveRecord::Schema.define(:version => 20151125072235) do create_table "activities", :force => true do |t| t.integer "act_id", :null => false diff --git a/lib/trustie/gitlab/manage_member.rb b/lib/trustie/gitlab/manage_member.rb index b8c2149f4..627c137ce 100644 --- a/lib/trustie/gitlab/manage_member.rb +++ b/lib/trustie/gitlab/manage_member.rb @@ -23,6 +23,9 @@ module Trustie def add_gitlab_member if isGitlabProject? @g ||= ::Gitlab.client + if self.member.user.gid.nil? + add_user(self.member.user) + end @g.add_team_member(project.gpid, self.member.user.gid, self.role.to_gitlab_role ) end end