From 0bcc0beaf1b5d6517b5666c1ea7b9aa135ed6f67 Mon Sep 17 00:00:00 2001 From: z9hang Date: Tue, 1 Jul 2014 10:31:44 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=95=99=E8=A8=80?= =?UTF-8?q?=E8=AE=A1=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/journals_for_message.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/models/journals_for_message.rb b/app/models/journals_for_message.rb index f2b69da81..9551ff28b 100644 --- a/app/models/journals_for_message.rb +++ b/app/models/journals_for_message.rb @@ -117,6 +117,15 @@ class JournalsForMessage < ActiveRecord::Base update_all("m_reply_count = #{count.to_i}", ["id = ?", journals_for_messages.m_parent_id]) end + #如果是在项目中留言则返回该项目否则返回nil - zjc + def project + if self.jour_type == 'Project' + Project.find(self.jour_id) + else + nil + end + end + # 更新用户分数 -by zjc def be_user_score #新建了留言回复 From a7fa444f9e48bc3ba1757b5243722198e4d9fc37 Mon Sep 17 00:00:00 2001 From: z9hang Date: Tue, 1 Jul 2014 15:19:08 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E4=B8=AA=E4=BA=BA=E5=BE=97=E5=88=86=E9=97=AE=E9=A2=98=EF=BC=9A?= =?UTF-8?q?=E7=BB=9F=E8=AE=A1=E6=97=B6=E5=8F=AA=E8=AE=A1=E7=AE=97=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E6=88=90=E5=91=98=EF=BC=8Cuser=5Fgrade=E8=A1=A8user?= =?UTF-8?q?=5Fid=E5=92=8Cproject=5Fid=E8=81=94=E5=90=88=E4=B8=8D=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E9=AA=8C=E8=AF=81=EF=BC=8Cuser=5Fgrade=E5=88=86?= =?UTF-8?q?=E6=95=B0=E9=83=A8=E4=BD=8D=E8=B4=9F=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/members_controller.rb | 2 +- app/models/user_grade.rb | 33 +++++ lib/tasks/userscore.rake | 175 ++++++++++++++++---------- 3 files changed, 145 insertions(+), 65 deletions(-) diff --git a/app/controllers/members_controller.rb b/app/controllers/members_controller.rb index 76be02409..5ddebe707 100644 --- a/app/controllers/members_controller.rb +++ b/app/controllers/members_controller.rb @@ -122,7 +122,7 @@ class MembersController < ApplicationController user_ids = attrs.delete(:user_ids) user_ids.each do |user_id| members << Member.new(:role_ids => params[:membership][:role_ids], :user_id => user_id) - user_grades << UserGrade.new(:user_id => user_id, :course_id => @course.id) + #user_grades << UserGrade.new(:user_id => user_id, :course_id => @course.id) if (params[:membership][:role_ids] && params[:membership][:role_ids][0] == "3") course_info << CourseInfo.new(:user_id => user_id, :course_id => @course.id) end diff --git a/app/models/user_grade.rb b/app/models/user_grade.rb index ea951eadd..074aa516a 100644 --- a/app/models/user_grade.rb +++ b/app/models/user_grade.rb @@ -2,4 +2,37 @@ class UserGrade < ActiveRecord::Base # attr_accessible :title, :body attr_accessible :user_id, :project_id, :grade belongs_to :project + before_save :correct_score + validates_uniqueness_of :user_id, :scope => [:project_id] + #validate :my_validation + + #修正分数 + #分数小于0时修正为0 + #分数大于等于0时不修正 -by zjc + def correct_score + if !self.grade.nil? && self.grade < 0 + self.grade = 0 + end + end + + #def self.exit_user_and_project(user,project) + # unless user.nil? || project.nil? + # if user.class.to_s == 'User' && project.class.to_s == 'Project' + # user_grade = UserGrade.find_by_user_id_and_project_id(user.id,project.id) + # user_grade + # else + # nil + # end + # else + # nil + # end + #end + + #def my_validation + # if user_id.nil? || project_id.nil? + # errors[:Base] << "用户和项目不能为空" + # elsif UserGrade.exit_user_and_project(user_id,project_id) + # errors[:Base] << "当前记录已存在" + # end + #end end diff --git a/lib/tasks/userscore.rake b/lib/tasks/userscore.rake index ab6e38abe..27fae5363 100644 --- a/lib/tasks/userscore.rake +++ b/lib/tasks/userscore.rake @@ -17,11 +17,14 @@ namespace :user_score do users[m.author.id] = users[m.author.id].to_i + 2 project = m.project unless project.nil? - user_grade = UserGrade.find_by_user_id_and_project_id(m.author.id, project.id) - if user_grade.nil? - user_grade = UserGrade.create(:user_id => m.author.id, :project_id => project.id) + if m.author.member_of?(project) + user_grade = UserGrade.find_by_user_id_and_project_id(m.author.id, project.id) + if user_grade.nil? + user_grade = UserGrade.create(:user_id => m.author.id, :project_id => project.id) + end + grades[user_grade.id] = grades[user_grade.id].to_i + 2 end - grades[user_grade.id] = grades[user_grade.id].to_i + 2 + end end @@ -32,11 +35,14 @@ namespace :user_score do users[j.user.id] = users[j.user.id].to_i + 1 project = j.project unless project.nil? - user_grade = UserGrade.find_by_user_id_and_project_id(j.user.id, project.id) - if user_grade.nil? - user_grade = UserGrade.create(:user_id => j.user.id, :project_id => project.id) + if j.user.member_of?(project) + user_grade = UserGrade.find_by_user_id_and_project_id(j.user.id, project.id) + if user_grade.nil? + user_grade = UserGrade.create(:user_id => j.user.id, :project_id => project.id) + end + grades[user_grade.id] = grades[user_grade.id].to_i + 1 end - grades[user_grade.id] = grades[user_grade.id].to_i + 1 + end end puts ":post_issue_message calculate Completed. collaboration users count: #{users.count}" @@ -46,11 +52,14 @@ namespace :user_score do users[j.user.id] = users[j.user.id].to_i + 1 project = j.project unless project.nil? - user_grade = UserGrade.find_by_user_id_and_project_id(j.user.id, project.id) - if user_grade.nil? - user_grade = UserGrade.create(:user_id => j.user.id, :project_id => project.id) + if j.user.member_of?(project) + user_grade = UserGrade.find_by_user_id_and_project_id(j.user.id, project.id) + if user_grade.nil? + user_grade = UserGrade.create(:user_id => j.user.id, :project_id => project.id) + end + grades[user_grade.id] = grades[user_grade.id].to_i + 1 end - grades[user_grade.id] = grades[user_grade.id].to_i + 1 + end end @@ -67,11 +76,14 @@ namespace :user_score do users[m.author.id] = users[m.author.id].to_i + 1 project = m.project unless project.nil? - user_grade = UserGrade.find_by_user_id_and_project_id(m.author.id, project.id) - if user_grade.nil? - user_grade = UserGrade.create(:user_id =>m.author.id, :project_id => project.id) + if m.author.member_of?(project) + user_grade = UserGrade.find_by_user_id_and_project_id(m.author.id, project.id) + if user_grade.nil? + user_grade = UserGrade.create(:user_id =>m.author.id, :project_id => project.id) + end + grades[user_grade.id] = grades[user_grade.id].to_i + 1 end - grades[user_grade.id] = grades[user_grade.id].to_i + 1 + end end puts ":reply_posting calculate Completed. collaboration users count: #{users.count}" @@ -116,38 +128,50 @@ namespace :user_score do #踩帖 users_skill[pt.user.id] = users_skill[pt.user.id].to_i - 2 #踩别人帖-2分 unless project.nil? - user_grade = UserGrade.find_by_user_id_and_project_id(pt.user.id, project.id) - if user_grade.nil? - user_grade = UserGrade.create(:user_id =>pt.user.id, :project_id => project.id) + if pt.user.member_of?(project) + user_grade = UserGrade.find_by_user_id_and_project_id(pt.user.id, project.id) + if user_grade.nil? + user_grade = UserGrade.create(:user_id =>pt.user.id, :project_id => project.id) + end + grades[user_grade.id] = grades[user_grade.id].to_i - 2 end - grades[user_grade.id] = grades[user_grade.id].to_i - 2 + end if level == 1 users_skill[target_user.id] = users_skill[target_user.id].to_i - 2 #帖子被一级会员踩-2分 add praise_tread unless project.nil? - user_grade = UserGrade.find_by_user_id_and_project_id(target_user.id, project.id) - if user_grade.nil? - user_grade = UserGrade.create(:user_id =>target_user.id, :project_id => project.id) + if target_user.member_of?(project) + user_grade = UserGrade.find_by_user_id_and_project_id(target_user.id, project.id) + if user_grade.nil? + user_grade = UserGrade.create(:user_id =>target_user.id, :project_id => project.id) + end + grades[user_grade.id] = grades[user_grade.id].to_i - 2 end - grades[user_grade.id] = grades[user_grade.id].to_i - 2 + end elsif level == 2 users_skill[target_user.id] = users_skill[target_user.id].to_i - 4 #帖子被二级会员踩-4分 add praise_tread unless project.nil? - user_grade = UserGrade.find_by_user_id_and_project_id(target_user.id, project.id) - if user_grade.nil? - user_grade = UserGrade.create(:user_id =>target_user.id, :project_id => project.id) + if target_user.member_of?(project) + user_grade = UserGrade.find_by_user_id_and_project_id(target_user.id, project.id) + if user_grade.nil? + user_grade = UserGrade.create(:user_id =>target_user.id, :project_id => project.id) + end + grades[user_grade.id] = grades[user_grade.id].to_i - 4 end - grades[user_grade.id] = grades[user_grade.id].to_i - 4 + end elsif level == 3 users_skill[target_user.id] = users_skill[target_user.id].to_i - 6 #帖子被三级会员踩-6分 add praise_tread unless project.nil? - user_grade = UserGrade.find_by_user_id_and_project_id(target_user.id, project.id) - if user_grade.nil? - user_grade = UserGrade.create(:user_id =>target_user.id, :project_id => project.id) + if target_user.member_of?(project) + user_grade = UserGrade.find_by_user_id_and_project_id(target_user.id, project.id) + if user_grade.nil? + user_grade = UserGrade.create(:user_id =>target_user.id, :project_id => project.id) + end + grades[user_grade.id] = grades[user_grade.id].to_i - 6 end - grades[user_grade.id] = grades[user_grade.id].to_i - 6 + end end elsif pt.praise_or_tread == 1 @@ -155,29 +179,38 @@ namespace :user_score do if level == 1 users_skill[target_user.id] = users_skill[target_user.id].to_i + 4 #帖子被一级会员顶+4分 add praise_tread unless project.nil? - user_grade = UserGrade.find_by_user_id_and_project_id(target_user.id, project.id) - if user_grade.nil? - user_grade = UserGrade.create(:user_id =>target_user.id, :project_id => project.id) + if target_user.member_of?(project) + user_grade = UserGrade.find_by_user_id_and_project_id(target_user.id, project.id) + if user_grade.nil? + user_grade = UserGrade.create(:user_id =>target_user.id, :project_id => project.id) + end + grades[user_grade.id] = grades[user_grade.id].to_i + 4 end - grades[user_grade.id] = grades[user_grade.id].to_i + 4 + end elsif level == 2 users_skill[target_user.id] = users_skill[target_user.id].to_i + 6 #帖子被二级会员顶+6分 add praise_tread unless project.nil? - user_grade = UserGrade.find_by_user_id_and_project_id(target_user.id, project.id) - if user_grade.nil? - user_grade = UserGrade.create(:user_id =>target_user.id, :project_id => project.id) + if target_user.member_of?(project) + user_grade = UserGrade.find_by_user_id_and_project_id(target_user.id, project.id) + if user_grade.nil? + user_grade = UserGrade.create(:user_id =>target_user.id, :project_id => project.id) + end + grades[user_grade.id] = grades[user_grade.id].to_i + 6 end - grades[user_grade.id] = grades[user_grade.id].to_i + 6 + end elsif level == 3 users_skill[target_user.id] = users_skill[target_user.id].to_i + 8 #帖子被三级会员顶+8分 add praise_tread unless project.nil? - user_grade = UserGrade.find_by_user_id_and_project_id(target_user.id, project.id) - if user_grade.nil? - user_grade = UserGrade.create(:user_id =>target_user.id, :project_id => project.id) + if target_user.member_of?(project) + user_grade = UserGrade.find_by_user_id_and_project_id(target_user.id, project.id) + if user_grade.nil? + user_grade = UserGrade.create(:user_id =>target_user.id, :project_id => project.id) + end + grades[user_grade.id] = grades[user_grade.id].to_i + 8 end - grades[user_grade.id] = grades[user_grade.id].to_i + 8 + end end end @@ -201,11 +234,14 @@ namespace :user_score do users_active[changeset.user.id] = users_active[changeset.user.id].to_i + 4 project = changeset.project unless project.nil? - user_grade = UserGrade.find_by_user_id_and_project_id(changeset.user.id, project.id) - if user_grade.nil? - user_grade = UserGrade.create(:user_id =>changeset.user.id, :project_id => project.id) + if changeset.user.member_of?(project) + user_grade = UserGrade.find_by_user_id_and_project_id(changeset.user.id, project.id) + if user_grade.nil? + user_grade = UserGrade.create(:user_id =>changeset.user.id, :project_id => project.id) + end + grades[user_grade.id] = grades[user_grade.id].to_i + 4 end - grades[user_grade.id] = grades[user_grade.id].to_i + 4 + end end puts ":push_code calculate Completed. active users count: #{users_active.count}" @@ -217,11 +253,14 @@ namespace :user_score do users_active[document.user.id] = users_active[document.user.id].to_i + 4 project = document.project unless project.nil? - user_grade = UserGrade.find_by_user_id_and_project_id(document.user.id, project.id) - if user_grade.nil? - user_grade = UserGrade.create(:user_id =>document.user.id, :project_id => project.id) + if document.user.member_of?(project) + user_grade = UserGrade.find_by_user_id_and_project_id(document.user.id, project.id) + if user_grade.nil? + user_grade = UserGrade.create(:user_id =>document.user.id, :project_id => project.id) + end + grades[user_grade.id] = grades[user_grade.id].to_i + 4 end - grades[user_grade.id] = grades[user_grade.id].to_i + 4 + end end puts ":push_document calculate Completed. active users count: #{users_active.count}" @@ -238,11 +277,14 @@ namespace :user_score do if attachment.container.class.to_s == "Project" project = attachment.project unless project.nil? - user_grade = UserGrade.find_by_user_id_and_project_id(attachment.author.id, project.id) - if user_grade.nil? - user_grade = UserGrade.create(:user_id =>attachment.author.id, :project_id => project.id) + if attachment.author.member_of?(project) + user_grade = UserGrade.find_by_user_id_and_project_id(attachment.author.id, project.id) + if user_grade.nil? + user_grade = UserGrade.create(:user_id =>attachment.author.id, :project_id => project.id) + end + grades[user_grade.id] = grades[user_grade.id].to_i + 4 end - grades[user_grade.id] = grades[user_grade.id].to_i + 4 + end end @@ -255,11 +297,13 @@ namespace :user_score do users_active[j.user.id] = users_active[j.user.id].to_i + 2 project = j.project unless project.nil? - user_grade = UserGrade.find_by_user_id_and_project_id(j.user.id, project.id) - if user_grade.nil? - user_grade = UserGrade.create(:user_id =>j.user.id, :project_id => project.id) + if j.user.member_of?(project) + user_grade = UserGrade.find_by_user_id_and_project_id(j.user.id, project.id) + if user_grade.nil? + user_grade = UserGrade.create(:user_id =>j.user.id, :project_id => project.id) + end + grades[user_grade.id] = grades[user_grade.id].to_i + 4 end - grades[user_grade.id] = grades[user_grade.id].to_i + 4 end end puts ":update_issue_ratio calculate Completed. active users count: #{users_active.count}" @@ -268,11 +312,14 @@ namespace :user_score do users_active[issue.author.id] = users_active[issue.author.id].to_i + 4 project = issue.project unless project.nil? - user_grade = UserGrade.find_by_user_id_and_project_id(issue.author.id, project.id) - if user_grade.nil? - user_grade = UserGrade.create(:user_id =>issue.author.id, :project_id => project.id) + if issue.author.member_of?(project) + user_grade = UserGrade.find_by_user_id_and_project_id(issue.author.id, project.id) + if user_grade.nil? + user_grade = UserGrade.create(:user_id =>issue.author.id, :project_id => project.id) + end + grades[user_grade.id] = grades[user_grade.id].to_i + 4 end - grades[user_grade.id] = grades[user_grade.id].to_i + 4 + end end puts ":post_issue calculate Completed. active users count: #{users_active.count}"