<commit_before>class ConvenorProjectsController < ApplicationController
  helper_method :sort_column, :sort_direction

	def show
		@project_template = ProjectTemplate.includes(:task_templates).find(params[:id])
    authorize! :read, @project_template, :message => "You are not authorised to view Project Template ##{@project_template.id}"

    sort_options = {
      column: sort_column,
      direction: sort_direction
    }

    @projects = sort_projects(
                  Project.includes({
                    team_membership: [:user, :team],
                    tasks: [:task_template]
                    }, :project_template
                  )
                  .where(project_template_id: params[:id]),
                  sort_options
                )

    @project_teams = @projects.map {|project|
      project.team_membership.team
    }.uniq
	end

  private

  def sort_column
    %w[username name progress tasks_completed].include?(params[:sort]) ? params[:sort] : "name"
  end

  def sort_direction
    %w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
  end

  def sort_projects(projects, options=nil)
    options = {column: "name", direction: "asc"} if options.nil?

    projects = case options[:column]
    when "username"
      projects.sort{|a,b| a.user.username <=> b.user.username }
    when "name"
      projects.sort{|a,b| a.user.name <=> b.user.name }
    when "progress"
      projects.sort{|a,b| Progress.new(a.progress) <=> Progress.new(b.progress) }
    when "tasks_completed"
      projects.sort{|a,b| a.completed_tasks <=> b.completed_tasks }
    else
      projects.sort{|a,b| a.user.name <=> b.user.name }
    end

    projects.reverse! if options[:direction] == "desc"

    projects
  end<commit_msg>Changed Tasks Completed column in student_list table to sort by size of completed tasks, rather than array<commit_after>class ConvenorProjectsController < ApplicationController
  helper_method :sort_column, :sort_direction

	def show
		@project_template = ProjectTemplate.includes(:task_templates).find(params[:id])
    authorize! :read, @project_template, :message => "You are not authorised to view Project Template ##{@project_template.id}"

    sort_options = {
      column: sort_column,
      direction: sort_direction
    }

    @projects = sort_projects(
                  Project.includes({
                    team_membership: [:user, :team],
                    tasks: [:task_template]
                    }, :project_template
                  )
                  .where(project_template_id: params[:id]),
                  sort_options
                )

    @project_teams = @projects.map {|project|
      project.team_membership.team
    }.uniq
	end

  private

  def sort_column
    %w[username name progress tasks_completed].include?(params[:sort]) ? params[:sort] : "name"
  end

  def sort_direction
    %w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
  end

  def sort_projects(projects, options=nil)
    options = {column: "name", direction: "asc"} if options.nil?

    projects = case options[:column]
    when "username"
      projects.sort{|a,b| a.user.username <=> b.user.username }
    when "name"
      projects.sort{|a,b| a.user.name <=> b.user.name }
    when "progress"
      projects.sort{|a,b| Progress.new(a.progress) <=> Progress.new(b.progress) }
    when "tasks_completed"
      projects.sort{|a,b| a.completed_tasks.size <=> b.completed_tasks.size }
    else
      projects.sort{|a,b| a.user.name <=> b.user.name }
    end

    projects.reverse! if options[:direction] == "desc"

    projects
  end
{'fraction_non_alphanumeric': 0.12261278715748686, 'fraction_numerical': 0.0, 'mean_word_length': 2.7763845350052248, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4731850', 'n_tokens_mistral': 1108, 'n_tokens_neox': 1051, 'n_words': 229}