Skip to content

Commit

Permalink
Add points to course#show
Browse files Browse the repository at this point in the history
Use PointVisualisation concern to add required variables
  • Loading branch information
Irene committed Apr 23, 2018
1 parent 0ff4a72 commit 1354c78
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 94 deletions.
30 changes: 30 additions & 0 deletions app/controllers/concerns/point_visualisation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true.
module PointVisualisation
extend ActiveSupport::Concern

def define_point_stats(user)
# TODO: bit ugly
@awarded_points = Hash[AwardedPoint.where(id: AwardedPoint.all_awarded(user)).to_a.sort!.group_by(&:course_id).map { |k, v| [k, v.map(&:name)] }]
@courses = []
@missing_points = {}
@percent_completed = {}
@group_completion_ratios = {}
@awarded_points.keys.each do |course_id|
course = Course.find(course_id)
next if course.hide_submissions?
@courses << course

awarded = @awarded_points[course.id]
missing = AvailablePoint.course_points(course).order!.map(&:name) - awarded
@missing_points[course_id] = missing

@percent_completed[course_id] =
if (awarded.size + missing.size).positive?
100 * (awarded.size.to_f / (awarded.size + missing.size))
else
0
end
@group_completion_ratios[course_id] = course.exercise_group_completion_ratio_for_user(user)
end
end
end
3 changes: 3 additions & 0 deletions app/controllers/courses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'exercise_completion_status_generator'

class CoursesController < ApplicationController
include PointVisualisation
before_action :set_organization
before_action :set_course, except: [:help, :index, :show_json]

Expand Down Expand Up @@ -41,6 +42,8 @@ def show
authorize! :read, @course
UncomputedUnlock.resolve(@course, current_user)

define_point_stats(current_user)

respond_to do |format|
format.html do
assign_show_view_vars
Expand Down
26 changes: 2 additions & 24 deletions app/controllers/participants_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'portable_csv'

class ParticipantsController < ApplicationController
include PointVisualisation
before_action :set_organization, only: [:index]

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

if current_user.administrator?
add_breadcrumb 'Participants', :participants_path
Expand All @@ -71,27 +70,7 @@ def show
add_breadcrumb 'My stats', participant_path(@user)
end

@courses = []
@missing_points = {}
@percent_completed = {}
@group_completion_ratios = {}
for course_id in @awarded_points.keys
course = Course.find(course_id)
if !course.hide_submissions?
@courses << course

awarded = @awarded_points[course.id]
missing = AvailablePoint.course_points(course).order!.map(&:name) - awarded
@missing_points[course_id] = missing

if awarded.size + missing.size > 0
@percent_completed[course_id] = 100 * (awarded.size.to_f / (awarded.size + missing.size))
else
@percent_completed[course_id] = 0
end
@group_completion_ratios[course_id] = course.exercise_group_completion_ratio_for_user(@user)
end
end
define_point_stats(@user)

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

Submission.eager_load_exercises(@submissions)

end

def me
Expand Down
4 changes: 4 additions & 0 deletions app/views/courses/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@
%>
</div>

<% if signed_in? %>
<%= render 'layouts/points', courses: [@course], title: 'My points', show_course_name: false %>
<% end %>

<% if @submissions %>
<h2>Latest submissions</h2>
<section>
Expand Down
70 changes: 70 additions & 0 deletions app/views/layouts/_points.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<section>
<br>
<h2><%= title %></h2>
<br>
<% for course in courses %>
<% if course && @percent_completed[course.id] %>
<div class="card">
<div class="card-body">
<% if show_course_name %>
<h3 class="card-title">
<% if can? :read, course %>
<%= link_to course.title, organization_course_path(course.organization, course) %>
<% else %>
<%= course.title %>
<% end %>
</h3>
<br>
<% end %>
<% if can? :see_points, course%>
<span class="progress-label">Awarded points</span>
<div class="progress course-points-progress">
<div class="progress-bar" role="progressbar" style="width: <%= @percent_completed[course.id] %>%" aria-valuenow="<%= @percent_completed[course.id] %>" aria-valuemin="0" aria-valuemax="100">
<%= sprintf("%.0f", @percent_completed[course.id]) %>%
</div>
</div>
<% if @group_completion_ratios[course.id] %>
<% @group_completion_ratios[course.id].each do |group, ratio| %>
<br>
<span class="progress-label">Awarded points for <%= group %></span>
<div class="progress course-points-progress">
<% unless ratio.zero? %>
<div class="progress-bar bg-info" role="progressbar" style="width: <%= ratio * 100 %>%" aria-valuenow="<%= ratio * 100 %>" aria-valuemin="0" aria-valuemax="100">
<%= sprintf("%.0f", ratio * 100) %>%
</div>
<% end %>
</div>
<% end %>
<br>
<% end %>
<br>
<table class="table table-hover">
<thead class="">
<tr>
<th></th>
<th>Point names</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Awarded points</td>
<td><%= points_list(@awarded_points[course.id]) %></td>
</tr>
<tr>
<th scope="row">Missing points</td>
<td><%= points_list(@missing_points[course.id]) %></td>
</tr>
</tbody>
</table>
<% else %>
For this course points are not visible.
<% end %>
</div>
</div>
<%else%>
You don't have any points for this course
<br>
<% end %>
<br>
<% end %>
</section>
70 changes: 1 addition & 69 deletions app/views/participants/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,75 +18,7 @@
</section>
</div>

<% unless @user.email_verified? %>
<div class="alert alert-warning" role="alert">
Your email address is not verified yet. <%= link_to 'Resend verification email', send_verification_email_path(@user), method: :post %>.
</div>
<% end %>

<section>
<br>
<h2>Points</h2>
<br>
<% for course in @courses %>
<div class="card">
<div class="card-body">
<h3 class="card-title">
<% if can? :read, course %>
<%= link_to course.title, organization_course_path(course.organization, course) %>
<% else %>
<%= course.title %>
<% end %>
</h3>
<br>
<% if can? :see_points, course %>
<span class="progress-label">Awarded points</span>
<div class="progress course-points-progress">
<div class="progress-bar" role="progressbar" style="width: <%= @percent_completed[course.id] %>%" aria-valuenow="<%= @percent_completed[course.id] %>" aria-valuemin="0" aria-valuemax="100">
<%= sprintf("%.0f", @percent_completed[course.id]) %>%
</div>
</div>
<% if @group_completion_ratios[course.id] %>
<% @group_completion_ratios[course.id].each do |group, ratio| %>
<br>
<span class="progress-label">Awarded points for <%= group %></span>
<div class="progress course-points-progress">
<% unless ratio.zero? %>
<div class="progress-bar bg-info" role="progressbar" style="width: <%= ratio * 100 %>%" aria-valuenow="<%= ratio * 100 %>" aria-valuemin="0" aria-valuemax="100">
<%= sprintf("%.0f", ratio * 100) %>%
</div>
<% end %>
</div>
<% end %>
<br>
<% end %>
<br>
<table class="table table-hover">
<thead class="">
<tr>
<th></th>
<th>Point names</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Awarded points</td>
<td><%= points_list(@awarded_points[course.id]) %></td>
</tr>
<tr>
<th scope="row">Missing points</td>
<td><%= points_list(@missing_points[course.id]) %></td>
</tr>
</tbody>
</table>
<% else %>
For this course points are not visible.
<% end %>
</div>
</div>
<br>
<% end %>
</section>
<%= render 'layouts/points', courses: @courses, title: 'Points', show_course_name: true %>

<section>
<h2>Submissions</h2>
Expand Down
2 changes: 1 addition & 1 deletion app/views/points/show.csv.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
end
@exercises.each do |exercise|
exercise.available_points.sort.each do |p|
arr += user_points.include?(p.name) ? [1] : [nil]
arr += user_points.include?(p.name) ? [1] : [0]
end
end
csv << arr
Expand Down

0 comments on commit 1354c78

Please sign in to comment.