Skip to content

Commit 465ecce

Browse files
author
Irene
committed
Add points to course#show
Use PointVisualisation concern to add required variables
1 parent 0ff4a72 commit 465ecce

File tree

7 files changed

+111
-94
lines changed

7 files changed

+111
-94
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true.
2+
module PointVisualisation
3+
extend ActiveSupport::Concern
4+
5+
def define_point_stats(user)
6+
# TODO: bit ugly
7+
@awarded_points = Hash[AwardedPoint.where(id: AwardedPoint.all_awarded(user)).to_a.sort!.group_by(&:course_id).map { |k, v| [k, v.map(&:name)] }]
8+
@courses = []
9+
@missing_points = {}
10+
@percent_completed = {}
11+
@group_completion_ratios = {}
12+
@awarded_points.keys.each do |course_id|
13+
course = Course.find(course_id)
14+
next if course.hide_submissions?
15+
@courses << course
16+
17+
awarded = @awarded_points[course.id]
18+
missing = AvailablePoint.course_points(course).order!.map(&:name) - awarded
19+
@missing_points[course_id] = missing
20+
21+
@percent_completed[course_id] =
22+
if (awarded.size + missing.size).positive?
23+
100 * (awarded.size.to_f / (awarded.size + missing.size))
24+
else
25+
0
26+
end
27+
@group_completion_ratios[course_id] = course.exercise_group_completion_ratio_for_user(user)
28+
end
29+
end
30+
end

app/controllers/courses_controller.rb

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require 'exercise_completion_status_generator'
55

66
class CoursesController < ApplicationController
7+
include PointVisualisation
78
before_action :set_organization
89
before_action :set_course, except: [:help, :index, :show_json]
910

@@ -41,6 +42,8 @@ def show
4142
authorize! :read, @course
4243
UncomputedUnlock.resolve(@course, current_user)
4344

45+
define_point_stats(current_user)
46+
4447
respond_to do |format|
4548
format.html do
4649
assign_show_view_vars

app/controllers/participants_controller.rb

+2-24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'portable_csv'
22

33
class ParticipantsController < ApplicationController
4+
include PointVisualisation
45
before_action :set_organization, only: [:index]
56

67
def index
@@ -61,8 +62,6 @@ def index
6162
def show
6263
@user = User.find(params[:id])
6364
authorize! :view_participant_information, @user
64-
# TODO: bit ugly
65-
@awarded_points = Hash[AwardedPoint.where(id: AwardedPoint.all_awarded(@user)).to_a.sort!.group_by(&:course_id).map { |k, v| [k, v.map(&:name)] }]
6665

6766
if current_user.administrator?
6867
add_breadcrumb 'Participants', :participants_path
@@ -71,27 +70,7 @@ def show
7170
add_breadcrumb 'My stats', participant_path(@user)
7271
end
7372

74-
@courses = []
75-
@missing_points = {}
76-
@percent_completed = {}
77-
@group_completion_ratios = {}
78-
for course_id in @awarded_points.keys
79-
course = Course.find(course_id)
80-
if !course.hide_submissions?
81-
@courses << course
82-
83-
awarded = @awarded_points[course.id]
84-
missing = AvailablePoint.course_points(course).order!.map(&:name) - awarded
85-
@missing_points[course_id] = missing
86-
87-
if awarded.size + missing.size > 0
88-
@percent_completed[course_id] = 100 * (awarded.size.to_f / (awarded.size + missing.size))
89-
else
90-
@percent_completed[course_id] = 0
91-
end
92-
@group_completion_ratios[course_id] = course.exercise_group_completion_ratio_for_user(@user)
93-
end
94-
end
73+
define_point_stats(@user)
9574

9675
if current_user.administrator? || current_user.id == @user.id
9776
@submissions = @user.submissions.order('created_at DESC').includes(:user).includes(:course)
@@ -102,7 +81,6 @@ def show
10281
@submissions = @submissions.limit(100) unless !!params[:view_all]
10382

10483
Submission.eager_load_exercises(@submissions)
105-
10684
end
10785

10886
def me

app/views/courses/show.html.erb

+4
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@
246246
%>
247247
</div>
248248

249+
<% if signed_in? %>
250+
<%= render 'layouts/points', courses: [@course], title: 'My points', show_course_name: false %>
251+
<% end %>
252+
249253
<% if @submissions %>
250254
<h2>Latest submissions</h2>
251255
<section>

app/views/layouts/_points.html.erb

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<section>
2+
<br>
3+
<h2><%= title %></h2>
4+
<br>
5+
<% for course in courses %>
6+
<% if course && @percent_completed[course.id] %>
7+
<div class="card">
8+
<div class="card-body">
9+
<% if show_course_name %>
10+
<h3 class="card-title">
11+
<% if can? :read, course %>
12+
<%= link_to course.title, organization_course_path(course.organization, course) %>
13+
<% else %>
14+
<%= course.title %>
15+
<% end %>
16+
</h3>
17+
<br>
18+
<% end %>
19+
<% if can? :see_points, course%>
20+
<span class="progress-label">Awarded points</span>
21+
<div class="progress course-points-progress">
22+
<div class="progress-bar" role="progressbar" style="width: <%= @percent_completed[course.id] %>%" aria-valuenow="<%= @percent_completed[course.id] %>" aria-valuemin="0" aria-valuemax="100">
23+
<%= sprintf("%.0f", @percent_completed[course.id]) %>%
24+
</div>
25+
</div>
26+
<% if @group_completion_ratios[course.id] %>
27+
<% @group_completion_ratios[course.id].each do |group, ratio| %>
28+
<br>
29+
<span class="progress-label">Awarded points for <%= group %></span>
30+
<div class="progress course-points-progress">
31+
<% unless ratio.zero? %>
32+
<div class="progress-bar bg-info" role="progressbar" style="width: <%= ratio * 100 %>%" aria-valuenow="<%= ratio * 100 %>" aria-valuemin="0" aria-valuemax="100">
33+
<%= sprintf("%.0f", ratio * 100) %>%
34+
</div>
35+
<% end %>
36+
</div>
37+
<% end %>
38+
<br>
39+
<% end %>
40+
<br>
41+
<table class="table table-hover">
42+
<thead class="">
43+
<tr>
44+
<th></th>
45+
<th>Point names</th>
46+
</tr>
47+
</thead>
48+
<tbody>
49+
<tr>
50+
<th scope="row">Awarded points</td>
51+
<td><%= points_list(@awarded_points[course.id]) %></td>
52+
</tr>
53+
<tr>
54+
<th scope="row">Missing points</td>
55+
<td><%= points_list(@missing_points[course.id]) %></td>
56+
</tr>
57+
</tbody>
58+
</table>
59+
<% else %>
60+
For this course points are not visible.
61+
<% end %>
62+
</div>
63+
</div>
64+
<%else%>
65+
You don't have any points for this course
66+
<br>
67+
<% end %>
68+
<br>
69+
<% end %>
70+
</section>

app/views/participants/show.html.erb

+1-69
Original file line numberDiff line numberDiff line change
@@ -18,75 +18,7 @@
1818
</section>
1919
</div>
2020

21-
<% unless @user.email_verified? %>
22-
<div class="alert alert-warning" role="alert">
23-
Your email address is not verified yet. <%= link_to 'Resend verification email', send_verification_email_path(@user), method: :post %>.
24-
</div>
25-
<% end %>
26-
27-
<section>
28-
<br>
29-
<h2>Points</h2>
30-
<br>
31-
<% for course in @courses %>
32-
<div class="card">
33-
<div class="card-body">
34-
<h3 class="card-title">
35-
<% if can? :read, course %>
36-
<%= link_to course.title, organization_course_path(course.organization, course) %>
37-
<% else %>
38-
<%= course.title %>
39-
<% end %>
40-
</h3>
41-
<br>
42-
<% if can? :see_points, course %>
43-
<span class="progress-label">Awarded points</span>
44-
<div class="progress course-points-progress">
45-
<div class="progress-bar" role="progressbar" style="width: <%= @percent_completed[course.id] %>%" aria-valuenow="<%= @percent_completed[course.id] %>" aria-valuemin="0" aria-valuemax="100">
46-
<%= sprintf("%.0f", @percent_completed[course.id]) %>%
47-
</div>
48-
</div>
49-
<% if @group_completion_ratios[course.id] %>
50-
<% @group_completion_ratios[course.id].each do |group, ratio| %>
51-
<br>
52-
<span class="progress-label">Awarded points for <%= group %></span>
53-
<div class="progress course-points-progress">
54-
<% unless ratio.zero? %>
55-
<div class="progress-bar bg-info" role="progressbar" style="width: <%= ratio * 100 %>%" aria-valuenow="<%= ratio * 100 %>" aria-valuemin="0" aria-valuemax="100">
56-
<%= sprintf("%.0f", ratio * 100) %>%
57-
</div>
58-
<% end %>
59-
</div>
60-
<% end %>
61-
<br>
62-
<% end %>
63-
<br>
64-
<table class="table table-hover">
65-
<thead class="">
66-
<tr>
67-
<th></th>
68-
<th>Point names</th>
69-
</tr>
70-
</thead>
71-
<tbody>
72-
<tr>
73-
<th scope="row">Awarded points</td>
74-
<td><%= points_list(@awarded_points[course.id]) %></td>
75-
</tr>
76-
<tr>
77-
<th scope="row">Missing points</td>
78-
<td><%= points_list(@missing_points[course.id]) %></td>
79-
</tr>
80-
</tbody>
81-
</table>
82-
<% else %>
83-
For this course points are not visible.
84-
<% end %>
85-
</div>
86-
</div>
87-
<br>
88-
<% end %>
89-
</section>
21+
<%= render 'layouts/points', courses: @courses, title: 'Points', show_course_name: true %>
9022

9123
<section>
9224
<h2>Submissions</h2>

app/views/points/show.csv.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
end
2222
@exercises.each do |exercise|
2323
exercise.available_points.sort.each do |p|
24-
arr += user_points.include?(p.name) ? [1] : [nil]
24+
arr += user_points.include?(p.name) ? [1] : [0]
2525
end
2626
end
2727
csv << arr

0 commit comments

Comments
 (0)