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}"