Skip to content

Add point views #483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/IndentationWidth: Use 2 (not 1) spaces for indentation.


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?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/ConditionalAssignment: Use the return of the conditional for variable assignment and comparison.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/EndAlignment: end at 32, 0 is not aligned with module at 2, 1.

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
3 changes: 3 additions & 0 deletions app/controllers/points_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ def show
end
respond_to do |format|
format.html
format.csv do
render_csv(filename: "#{@course.name}_#{@sheetname}_points.csv")
end
format.json do
output = {
api_version: ApiVersion::API_VERSION,
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
29 changes: 29 additions & 0 deletions app/views/points/show.csv.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<% require 'portable_csv' %>
<%= PortableCSV.generate(:force_quotes => true) do |csv|
arr = ["Username"]
arr += @user_fields.map(&:label) if @user_fields
arr += @exercises.map{ |exercise| [exercise[:name]] + (exercise.available_points.length > 1 ? [nil] * (exercise.available_points.length-1) : []) }.flatten
csv << arr

arr = [nil]
arr += [nil] * @user_fields.length if @user_fields
arr += @exercises.map{|exercise| exercise.available_points.sort.map{|point| point.name }}.flatten
csv << arr

@users.each do |user, index|
user_points = @users_to_points[user.login]
arr = [user.login]
if @user_fields
@user_fields.each do |field|
value = user.user_field_values.find { |o| o.field_name == field.name }
arr += [value.value] if value
end
end
@exercises.each do |exercise|
exercise.available_points.sort.each do |p|
arr += user_points.include?(p.name) ? [1] : [0]
end
end
csv << arr
end
end.html_safe %>
7 changes: 5 additions & 2 deletions app/views/points/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@

<div class="alternative-options-links">
<% if params[:sort_by].blank? %>
<%= link_to('Sort by points', organization_course_point_path(@organization, @course, @sheetname, sort_by: 'points', show_attempted: params[:show_attempted]), class: "btn btn-primary")%>
<%= link_to 'Sort by points', organization_course_point_path(@organization, @course, @sheetname, :sort_by => 'points'),class: "btn btn-primary" %>
<% else %>
<%= link_to('Sort by username', organization_course_point_path(@organization, @course, @sheetname, show_attempted: params[:show_attempted]), class: "btn btn-primary") %>
<% end %>

<% if can?(:teach, @course) %>
<%= link_to('Export as CSV', organization_course_point_path(@organization, @course, @sheetname, :sort_by => params[:sort_by], :format => 'csv'), class: "btn btn-primary") %>
<% end %>
</div>
<% if can? :refresh_gdocs_spreadsheet, @course %>
<% link_to 'Refresh Google Docs worksheet', refresh_gdocs_organization_course_point_path(@organization, @course, @sheetname), class: "btn btn-primary" %>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion ext/tmc-langs
Submodule tmc-langs updated 360 files