Skip to content
Merged
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
184 changes: 153 additions & 31 deletions app/controllers/courses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,39 @@ class CoursesController < ApplicationController
def show
authorize @course

# query for all projects_instances, owner_type and owner_id for participants table
@course.projects.includes(project_instances: { enrolment: :user }).load
@projects_by_owner = @course.projects.index_by { |p| [p.owner_type, p.owner_id] }

@student_list = @course.students
@description = @course.course_description
@lecturers = @course.lecturers
@group_list = @course.grouped? ? @course.project_groups.to_a : []
@lecturer_enrolment = @course.enrolments.find_by(user: current_user, role: :lecturer)
@current_user_enrolment = @course.enrolments.find_by(user: current_user)

@topic_list = policy_scope(@course.topics, policy_scope_class: TopicPolicy::Scope)
@my_topics = @topic_list.where(owner: current_user)

# SET STUDENT PROJECTS
projects_ownerships = @course.projects.approved
.where(owner_type: 'User')
.pluck('owner_id')

@students_with_projects = @student_list.select do |student|
projects_ownerships.include?(student.id)
end

@students_without_projects = @student_list.reject do |student|
projects_ownerships.include?(student.id)
end
# set students projects
projects_ownerships = @course.projects.approved.where(owner_type: 'User').pluck('owner_id')

@filtered_group_list = filtered_group_list
@filtered_student_list = filtered_student_list
@my_student_projects = []
@incoming_proposals = []
@students_with_projects = @student_list.select { |s| projects_ownerships.include?(s.id) }
@students_without_projects = @student_list.reject { |s| projects_ownerships.include?(s.id) }

if @course.grouped?
@group = current_user.project_groups.find_by(course: @course)

@project = (@course.projects.find_by(owner_type: 'ProjectGroup', owner_id: @group.id) if @group)
@project = @projects_by_owner[['ProjectGroup', @group.id]] if @group
@group_list = @course.project_groups.includes(project_group_members: :user).to_a
else
@group = nil
@project = @course.projects.find_by(owner_type: 'User', owner_id: current_user.id)
@group_list = []
end

# view instances for incoming topics and my_student_projects
@current_status = @project&.current_status || 'not_submitted'

@my_student_projects = []
@incoming_proposals = []
if @current_user_enrolment&.coordinator?
supervisor_enrolment = @lecturer_enrolment || @current_user_enrolment
@my_student_projects = @course.projects.supervised_by(supervisor_enrolment).approved
Expand All @@ -55,13 +49,29 @@ def show
@incoming_proposals = @course.projects.where(enrolment: @current_user_enrolment).proposals
end

# SET LECTURER CAPACITY
# view instances for participants_table
@filtered_group_list = filtered_group_list
@filtered_student_list = filtered_student_list

@show_all = params[:show_all] == 'true'
@total_group_count = @filtered_group_list.count
@total_student_count = @filtered_student_list.count
@total_count = @course.grouped? ? @total_group_count : @total_student_count
@total_count = @course.grouped? ? @filtered_group_list.count : @filtered_student_list.count

unless @show_all
@filtered_group_list = @filtered_group_list.first(Rails.application.config.participants_pagination_threshold)
@filtered_student_list = @filtered_student_list.first(Rails.application.config.participants_pagination_threshold)
end

@displayed_count = @course.grouped? ? @filtered_group_list.count : @filtered_student_list.count

@lecturer_capacity_info = {}
@lecturers.each do |lecturer|
@lecturer_capacity_info[lecturer.id] = @course.lecturer_capacity(lecturer)
end

return unless request.headers['HX-Request'] && params[:status_filter].present?
return unless request.headers['HX-Request']

render partial: 'participants_table',
locals: {
Expand Down Expand Up @@ -250,9 +260,11 @@ def profile
@participant_type = params[:participant_type]
@participant_id = params[:participant_id]
@course = Course.find(params[:id])

@grouped = @course.grouped

@course.projects.includes(project_instances: { enrolment: :user }).load
@projects_by_owner = @course.projects.index_by { |p| [p.owner_type, p.owner_id] }

if @participant_type == 'group'
@group = @course.project_groups.find(@participant_id)
@members = @group.project_group_members.includes(:user)
Expand Down Expand Up @@ -512,6 +524,8 @@ def create_lecturer_enrolments(lecturer_emails, parent_course, unregistered_lect
end
end

# CSV_export helpers

def generate_csv_export
template_fields = @course.project_template&.project_template_fields&.order(:id) || []

Expand Down Expand Up @@ -624,6 +638,8 @@ def get_project_details_values(current_instance, template_fields)
end
end

# Lecturer capcity helpers

def lecturer_approved_proposals_count(lecturer, course)
lecturer_enrolment = course.enrolments.find_by(user: lecturer, role: :lecturer)
return 0 unless lecturer_enrolment
Expand Down Expand Up @@ -653,6 +669,8 @@ def lecturer_capacity_info(lecturer, course)
}
end

# Filter Project by status helpers

def students_by_status(status, student_list, students_with_projects, students_without_projects, course)
return [] unless student_list.present?

Expand Down Expand Up @@ -699,18 +717,122 @@ def groups_by_status(status, group_list, course)
end
end

# Participants Table Filters helpers

def search_groups(group_list, query)
downcased_query = query.downcase

group_list.select do |group|
project = participant_project(group, 'ProjectGroup')

group_name_match = group.group_name.downcase.include?(downcased_query)
member_match = group.project_group_members.any? do |member|
member.user.username.downcase.include?(downcased_query)
end
title_match = project&.current_title&.downcase&.include?(downcased_query) || false

group_name_match || member_match || title_match
end
end

def search_students(student_list, query)
downcased_query = query.downcase

student_list.select do |student|
project = participant_project(student, 'User')

name_match = student.username.downcase.include?(downcased_query)
id_match = student.student_id&.downcase&.include?(downcased_query) || false
title_match = project&.current_title&.downcase&.include?(downcased_query) || false

name_match || id_match || title_match
end
end

def sort_descending?
params[:sort_dir] == 'desc'
end

def participant_project(item, owner_type)
@projects_by_owner[[owner_type, item.id]]
end

def sort_value_for_group(group)
project = participant_project(group, 'ProjectGroup')
case params[:sort_by]
when 'status'
Project::STATUS_SORT_ORDER.fetch(project&.current_status || 'not_submitted', 99)
when 'project_title'
project&.current_title&.downcase || ''
when 'supervisor'
project&.supervisor&.username&.downcase || ''
else
group.group_name.downcase
end
end

def sort_value_for_student(student)
project = participant_project(student, 'User')
case params[:sort_by]
when 'status'
Project::STATUS_SORT_ORDER.fetch(project&.current_status || 'not_submitted', 99)
when 'project_title'
project&.current_title&.downcase || ''
when 'supervisor'
project&.supervisor&.username&.downcase || ''
else
student.username.downcase
end
end

def lecturer_enrolment_filter
return nil unless params[:lecturer_filter].present? && params[:lecturer_filter] != 'all'

@course.enrolments.find_by(user_id: params[:lecturer_filter], role: :lecturer)
end

def supervised_owner_ids(owner_type)
enrolment = lecturer_enrolment_filter
return nil unless enrolment

@course.projects.supervised_by(enrolment).where(owner_type: owner_type).pluck(:owner_id)
end

def filtered_group_list
group_list = if params[:status_filter].present? && params[:status_filter] != 'all'
@course.groups_with_status(params[:status_filter], @group_list)
else
@group_list
end
group_list.sort_by(&:group_name)
group_list = @group_list

if (ids = supervised_owner_ids('ProjectGroup'))
group_list = group_list.select { |g| ids.include?(g.id) }
end

if params[:status_filter].present? && params[:status_filter] != 'all'
group_list = @course.groups_with_status(params[:status_filter], group_list)
end

if params[:search_query].present?
group_list = search_groups(group_list, params[:search_query])
end

sorted_list = group_list.sort_by { |group| sort_value_for_group(group) }
sort_descending? ? sorted_list.reverse : sorted_list
end

def filtered_student_list
return @student_list unless params[:status_filter].present? && params[:status_filter] != 'all'
student_list = @student_list

if (ids = supervised_owner_ids('User'))
student_list = student_list.select { |s| ids.include?(s.id) }
end

if params[:status_filter].present? && params[:status_filter] != 'all'
student_list = @course.students_with_status(params[:status_filter], student_list)
end

if params[:search_query].present?
student_list = search_students(student_list, params[:search_query])
end

@course.students_with_status(params[:status_filter], @student_list)
sorted_list = student_list.sort_by { |student| sort_value_for_student(student) }
sort_descending? ? sorted_list.reverse : sorted_list
end
end
66 changes: 54 additions & 12 deletions app/controllers/participants_controller.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,78 @@
class ParticipantsController < ApplicationController
def index
@course = Course.find(params[:course_id])
authorize @course

# query for all projects_instances, owner_type and owner_id for participants table
@course.projects.includes(project_instances: { enrolment: :user }).load
@projects_by_owner = @course.projects.index_by { |p| [p.owner_type, p.owner_id]

@student_list = @course.students
@group_list = @course.grouped? ? @course.project_groups.to_a : []

if @course.grouped?
@group_list = @course.project_groups.includes(project_group_members: :user).to_a
else
@group_list = []
end

projects_ownerships = @course.projects.approved.where(owner_type: 'User').pluck('owner_id')

@filtered_group_list = filtered_group_list
@filtered_student_list = filtered_student_list

projects_ownerships = @course.projects.approved
.where(owner_type: 'User')
.pluck('owner_id')
@students_with_projects = @student_list.select { |s| projects_ownerships.include?(s.id) }
@students_without_projects = @student_list.reject { |s| projects_ownerships.include?(s.id) }

@students_with_projects = @student_list.select do |student|
projects_ownerships.include?(student.id)
@show_all = params[:show_all] == 'true'
@total_count = @course.grouped? ? @filtered_group_list.count : @filtered_student_list.count

unless @show_all
@filtered_group_list = @filtered_group_list.first(Rails.application.config.participants_pagination_threshold)
@filtered_student_list = @filtered_student_list.first(Rails.application.config.participants_pagination_threshold)
end

@students_without_projects = @student_list.reject do |student|
projects_ownerships.include?(student.id)
@displayed_count = @course.grouped? ? @filtered_group_list.count : @filtered_student_list.count
end
end

private

def lecturer_enrolment_filter
return nil unless params[:lecturer_filter].present? && params[:lecturer_filter] != 'all'

@course.enrolments.find_by(user_id: params[:lecturer_filter], role: :lecturer)
end

def supervised_owner_ids(owner_type)
enrolment = lecturer_enrolment_filter
return nil unless enrolment

@course.projects.supervised_by(enrolment).where(owner_type: owner_type).pluck(:owner_id)
end

def filtered_group_list
return @group_list unless params[:status_filter].present? && params[:status_filter] != 'all'
group_list = @group_list

@course.groups_with_status(params[:status_filter], @group_list)
if (ids = supervised_owner_ids('ProjectGroup'))
group_list = group_list.select { |g| ids.include?(g.id) }
end

if params[:status_filter].present? && params[:status_filter] != 'all'
group_list = @course.groups_with_status(params[:status_filter], group_list)
end

return group_list.sort_by(&:group_name)
end

def filtered_student_list
return @student_list unless params[:status_filter].present? && params[:status_filter] != 'all'
student_list = @student_list

if (ids = supervised_owner_ids('User'))
student_list = student_list.select { |s| ids.include?(s.id) }
end

return student_list unless params[:status_filter].present? && params[:status_filter] != 'all'

@course.students_with_status(params[:status_filter], @student_list)
@course.students_with_status(params[:status_filter], student_list)
end
end
Loading
Loading