diff --git a/.gitignore b/.gitignore index cf96fa998..1d084c5cc 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,4 @@ /public/images/avatars/* /Gemfile /Gemfile.lock -/db/schema.da +/db/schema.rb diff --git a/app/helpers/user_score_helper.rb b/app/helpers/user_score_helper.rb new file mode 100644 index 000000000..ff4a5adbd --- /dev/null +++ b/app/helpers/user_score_helper.rb @@ -0,0 +1,108 @@ +# encoding: utf-8 +# +# Redmine - project management software +# Copyright (C) 2006-2013 Jean-Philippe Lang +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +module UserScoreHelper + def calculate_collaboration_count(user) + issue_c = 0 + issues = Issue.where('author_id = ?', user.id) + issues.each do |issue| + issue_c = issue_c + issue.journals.where("user_id <> ?", user.id).count + end + issue_c = issue_c + Journal.where("user_id = ?", user.id) + + return issue_c + end + + def calculate_influence_count(user) + watcher_count = watcher_users(User.current.id).count + end + + def calculate_skill_count(user) + 0 + end + + def calculate_level(user) + 1 + end + + def calculate_activity_count(user) + # commit_count = user.changesets.count + # issue_details_count = 0 + # issues = Issue.where('assigned_to_id = ?', user.id) + # issues.each do |issue| + # change_count = issue.journals.where("prop_key = ?", "done_ratio").count + # issue_details_count = change_count + issue_details_count + # end + # file_count = user.file_commit.count + # issue_count = Issue.where('author_id = ?', user.id).count + f = user.user_score.file + i = user.user_score.issue + f_max = UserScore.find_max_file + f_min = UserScore.find_min_file + i_max = UserScore.find_max_issue + i_min = UserScore.find_min_issue + score = 100 * ((f - f_min)/(f_max - f_min) + (i - i_min)/(i_max - i_min)) + end + + def calculate_file(user) + file_count = user.file_commit.count + issue_count = Issue.where('author_id = ?', user.id).count + return (file_count + issue_count) + end + + def calculate_issue(user) + commit_count = user.changesets.count + + issue_details_count = 0 + issues = Issue.where('assigned_to_id = ?', user.id) + + issues.each do |issue| + change_count = issue.journals.where("prop_key = ?", "done_ratio").count + issue_details_count = change_count + issue_details_count + end + + return (commit_count + issue_details_count) + + end + + def calculate_user_score(user) + collaboration = calculate_collaboration_count(user) + influence = calculate_influence_count(user) + skill = calculate_skill_count(user) + activity = calculate_activity_count(user) + file = calculate_file(user) + issue = calculate_issue(user) + level = calculate_level(user) + user.user_score << UserScore.new(:collaboration => collaboration, :influence => influence, :skill => skill, + :activity => activity, :file => file, :issue => issue, :level => level) + end + + def update_user_score(user) + collaboration = calculate_collaboration_count(user) + influence = calculate_influence_count(user) + skill = calculate_skill_count(user) + activity = calculate_activity_count(user) + file = calculate_file(user) + issue = calculate_issue(user) + level = calculate_level(user) + user.user_score.update_attributes(:collaboration => collaboration, :influence => influence, :skill => skill, + :activity => activity, :file => file, :issue => issue, :level => level) + end + +end diff --git a/app/models/user.rb b/app/models/user.rb index d14090c56..b1991b061 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -96,6 +96,7 @@ class User < Principal has_many :students_for_courses has_many :courses, :through => :students_for_courses, :source => :project has_many :acts, :class_name => 'Activity', :as => :act, :dependent => :destroy + has_many :file_commit, :class_name => 'Attachment', :foreign_key => 'author_id', :conditions => "container_tpye = 'Project' or container_type = 'Version'" #### # added by bai has_many :join_in_contests, :dependent => :destroy @@ -104,6 +105,7 @@ class User < Principal has_many :wiki_contents, :foreign_key => 'author_id' has_many :journals has_many :messages, :foreign_key => 'author_id' + has_one :user_socre, :dependent => :destroy # end ######added by nie diff --git a/app/models/user_scores.rb b/app/models/user_scores.rb new file mode 100644 index 000000000..c939a20ff --- /dev/null +++ b/app/models/user_scores.rb @@ -0,0 +1,36 @@ +# Redmine - project management software +# Copyright (C) 2006-2013 Jean-Philippe Lang +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +class UserScore < ActiveRecord::Base + belongs_to :user + + def self.find_max_file + self.maximum(:file) + end + + def self.find_min_file + self.minimum(:file) + end + + def self.find_max_issue + self.maximum(:issue) + end + + def self.find_min_issue + self.minimum(:file) + end +end diff --git a/db/migrate/20140410021724_create_user_score.rb b/db/migrate/20140410021724_create_user_score.rb new file mode 100644 index 000000000..0c1d0e2e4 --- /dev/null +++ b/db/migrate/20140410021724_create_user_score.rb @@ -0,0 +1,13 @@ +class CreateUserScore < ActiveRecord::Migration + def change + create_table :user_scores do |t| + t.integer :user_id, :null => false + t.integer :collaboration + t.integer :influence + t.integer :skill + t.integer :active + + t.timestamps + end + end +end diff --git a/db/migrate/20140413022725_add_filed_to_user_preference.rb b/db/migrate/20140413022725_add_filed_to_user_preference.rb new file mode 100644 index 000000000..5e8d41ae6 --- /dev/null +++ b/db/migrate/20140413022725_add_filed_to_user_preference.rb @@ -0,0 +1,7 @@ +class AddFiledToUserPreference < ActiveRecord::Migration + def change + add_column :user_scores, :level, :integer + add_column :user_scores, :file, :integer + add_column :user_scores, :issue, :integer + end +end diff --git a/test/fixtures/join_in_contests.yml b/test/fixtures/join_in_contests.yml new file mode 100644 index 000000000..3e5279b38 --- /dev/null +++ b/test/fixtures/join_in_contests.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html + +one: + user_id: 1 + bid_id: 1 + +two: + user_id: 1 + bid_id: 1 diff --git a/test/unit/join_in_contest_test.rb b/test/unit/join_in_contest_test.rb new file mode 100644 index 000000000..3d27c1a90 --- /dev/null +++ b/test/unit/join_in_contest_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class JoinInContestTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end