summaryrefslogtreecommitdiff
path: root/lib/sampling/peer_review.rb
blob: cbbd2f954e05ebdd12025f78c1344449da9a8e20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
module Sampling
	module PeerReview
		def self.works_with?(game)
			return true
		end

		def self.uses_remote?
			return false
		end

		def self.set_remote_name(user, game, value)
			raise "This sampling method doesn't use remote usernames."
		end

		def self.get_remote_name(value)
			raise "This sampling method doesn't use remote usernames."
		end

		def self.sampling_start(match)
			# do nothing
		end

		def self.render_user_interaction(match, user)
			@user = user
			@team = get_team(match)
			@feedbacks_missing = get_feedbacks_missing(match)

			require 'erb'
			erb_filename = File.join(__FILE__.sub(/\.rb$/, '.svg.erb'))
			erb = ERB.new(File.read(erb_filename))
			erb.filename = erb_filename
			return erb.result.html_safe
		end

		def self.handle_user_interaction(match, reviewing_user, params)
			i = 0
			params[:peer_review].to_s.split(',').each do |user_name|
				reviewed_user = User.find_by_user_name(user_name)
				user.statistics.create(match: match, value: i)
				i += 1
			end
		end

		private

		def self.get_users(match)
			users = []
			match.teams.each{|t| users.concat(t.users)}
			return users
		end

		def self.get_team(match)
			match.teams.find{|t|t.users.include?(@user)}
		end

		def self.get_feedbacks(match)
			ret = {}
			match.statistiscs.where("'name' LIKE 'feedback_from_%'").each do |statistic|
				ret[statistic.user] ||= {}
				ret[statistic.user][User.find_by_user_name(statistic.name.sub(/^feedback_from_/,''))] = statistic.value
			end
			return ret
		end

		def self.get_feedbacks_missing(match)
			require 'set'
			ret = Set.new()

			feedback = get_feedbacks(match)
			users = get_users(match)

			feedback.each do |feedback|
				(users - feedback.keys).each do |user|
					ret.push(user)
				end
			end

			return ret
		endx
	end
end