class GroupsController

Manages actions relating to editing and modifying groups.

Manages actions relating to editing and modifying groups.

Public Instance Methods

add_group() click to toggle source
# File lib/benchmarking/submission_files/groups_controller.rb, line 72
def add_group
  @assignment = Assignment.find(params[:id])
  begin
    new_grouping_data = @assignment.add_group(params[:new_group_name])
  rescue Exception => e
    @error = e.message
    render :error_single
    return
  end
  @new_grouping = construct_table_row(new_grouping_data, @assignment)
end
csv_upload() click to toggle source

Allows the user to upload a csv file listing groups. If group_name is equal to the only member of a group and the assignment is configured with allow_web_subits == false, the student’s username will be used as the repository name. If MarkUs is not repository admin, the repository name as specified by the second field will be used instead.

# File app/controllers/groups_controller.rb, line 163
def csv_upload
  flash[:error] = nil # reset from previous errors
  file = params[:group][:grouplist]
  @assignment = Assignment.find(params[:assignment_id])
  encoding = params[:encoding]
  if request.post? && !params[:group].blank?
    # Transaction allows us to potentially roll back if something
    # really bad happens.
    ActiveRecord::Base.transaction do
      if encoding != nil
        file = StringIO.new(Iconv.iconv('UTF-8', encoding, file.read).join)
      end
      # Old groupings get wiped out
      if !@assignment.groupings.nil? && @assignment.groupings.length > 0
        @assignment.groupings.destroy_all
      end
      begin
        # Loop over each row, which lists the members to be added to the group.
        CsvHelper::Csv.parse(file.read).each_with_index do |row, line_nr|
          begin
            # Potentially raises CSVInvalidLineError
            collision_error = @assignment.add_csv_group(row)
            unless collision_error.nil?
              flash_message(:error, I18n.t('csv.line_nr_csv_file_prefix',
                { :line_number => line_nr + 1 }) + " #{collision_error}")
            end
          rescue CSVInvalidLineError => e
            flash_message(:error, I18n.t('csv.line_nr_csv_file_prefix',
              { :line_number => line_nr + 1 }) + " #{e.message}")
          end
        end
        @assignment.reload # Need to reload to get newly created groupings
        number_groupings_added = @assignment.groupings.length
        if number_groupings_added > 0 && flash[:error].is_a?(Array)
          invalid_lines_count = flash[:error].length
          flash[:notice] = I18n.t('csv.groups_added_msg', { :number_groups =>
            number_groupings_added, :number_lines => invalid_lines_count })
        end
      rescue Exception => e
        # We should only get here if something *really* bad/unexpected
        # happened.
        flash_message(:error, I18n.t('csv.groups_unrecoverable_error'))
        raise ActiveRecord::Rollback
      end
    end
    # Need to reestablish repository permissions.
    # This is not handled by the roll back.
    @assignment.update_repository_permissions_forall_groupings
  end
  redirect_to :action => 'index', :id => params[:id]
end
csv_upload_grader_mapping() click to toggle source

Assign TAs to Groupings via a csv file

# File lib/benchmarking/submission_files/groups_controller.rb, line 163
def csv_upload_grader_mapping
  if !request.post? || params[:grader_mapping].nil?
    flash[:error] = 'You must supply a CSV file for group to grader mapping'
    redirect_to :action => 'manage', :id => params[:id]
    return
  end

  invalid_lines = Grouping.assign_tas_by_csv(params[:grader_mapping].read, params[:id])
  if invalid_lines.size > 0
    flash[:invalid_lines] = invalid_lines
  end
  redirect_to :action => 'manage', :id => params[:id]
end
download_dialog() click to toggle source
# File app/controllers/groups_controller.rb, line 68
def download_dialog
  @assignment = Assignment.find(params[:id])
  render :partial => 'groups/modal_dialogs/download_dialog.rjs'
end
download_grouplist() click to toggle source
# File app/controllers/groups_controller.rb, line 215
def download_grouplist
  assignment = Assignment.find(params[:assignment_id])

  #get all the groups
  groupings = assignment.groupings #FIXME: optimize with eager loading

  file_out = CsvHelper::Csv.generate do |csv|
     groupings.each do |grouping|
       group_array = [grouping.group.group_name, grouping.group.repo_name]
       # csv format is group_name, repo_name, user1_name, user2_name, ... etc
       grouping.student_memberships.all(:include => :user).each do |member|
          group_array.push(member.user.user_name)
       end
       csv << group_array
     end
   end

  send_data(file_out, :type => 'text/csv', :disposition => 'inline')
end
global_actions() click to toggle source

These actions act on all currently selected students & groups

# File app/controllers/groups_controller.rb, line 251
def global_actions
  @assignment = Assignment.find(params[:assignment_id],
                                :include => [{
                                    :groupings => [{
                                        :student_memberships => :user,
                                        :ta_memberships => :user},
                                      :group]}])
  @tas = Ta.all
  grouping_ids = params[:groupings]
  student_ids = params[:students]

  if params[:groupings].nil? or params[:groupings].size ==  0
       #if there is a global action than there should be a group selected
       if params[:global_actions]
             @global_action_warning = I18n.t('assignment.group.select_a_group')
             render :partial => 'shared/global_action_warning.rjs'
             return
       end
    #Just do nothing
    render :nothing => true
    return
  end

  @grouping_data = {}
  @groupings = []
  groupings = Grouping.find(grouping_ids)

  case params[:global_actions]
    when 'delete'
      delete_groupings(groupings)
      return
    when 'invalid'
      invalidate_groupings(groupings)
      return
    when 'valid'
      validate_groupings(groupings)
      return
    when 'assign'
      if grouping_ids.length != 1
        @error = I18n.t('assignment.group.select_only_one_group')
        render :error_single
      elsif student_ids
        add_members(student_ids, grouping_ids[0], @assignment)
        return
      else
        @global_action_warning = I18n.t('assignment.group.select_a_student')
        render :partial => 'shared/global_action_warning.rjs'
        return
      end
    when 'unassign'
      remove_members(groupings, params)
      return
  end
end
index() click to toggle source
# File app/controllers/groups_controller.rb, line 153
def index
  @all_assignments = Assignment.all(:order => :id)
  @assignment = Assignment.find(params[:assignment_id])
end
invalid_grouping() click to toggle source
# File app/controllers/groups_controller.rb, line 125
def invalid_grouping
  @assignment = Assignment.find(params[:assignment_id])
  @grouping = Grouping.find(params[:grouping_id])
  @grouping.invalidate_grouping
  @grouping_data = construct_table_row(@grouping, @assignment)
end
manage() click to toggle source
# File lib/benchmarking/submission_files/groups_controller.rb, line 153
def manage
  @all_assignments = Assignment.all(:order => :id)
  @assignment = Assignment.find(params[:id], :include => [{:groupings => [{:student_memberships => :user, :ta_memberships => :user}, :group]}])
  @groupings = @assignment.groupings
  # Returns a hash where s.id is the key, and student record is the value
  @ungrouped_students = @assignment.ungrouped_students
  @tas = Ta.all
end
new() click to toggle source

Group administration functions —————————————– Verify that all functions below are included in the authorize filter above

# File app/controllers/groups_controller.rb, line 29
def new
  @assignment = Assignment.find(params[:assignment_id])
  begin
    new_grouping_data = @assignment.add_group(params[:new_group_name])
  rescue Exception => e
    @error = e.message
    render :error_single
    return
  end
  @new_grouping = construct_table_row(new_grouping_data, @assignment)
  render :add_group
end
note_message() click to toggle source
# File app/controllers/groups_controller.rb, line 17
def note_message
  @assignment = Assignment.find(params[:id])
  if params[:success]
    flash[:notice] = I18n.t('notes.create.success')
  else
    flash[:error] = I18n.t('notes.error')
  end
end
populate() click to toggle source
# File app/controllers/groups_controller.rb, line 132
def populate
  @assignment = Assignment.find(params[:assignment_id],
                                :include => [{
                                    :groupings => [
                                        :students,
                                        :non_rejected_student_memberships,
                                        :group]}])
  @groupings = @assignment.groupings
  @table_rows = {}
  @groupings.each do |grouping|
    @table_rows[grouping.id] = construct_table_row(grouping, @assignment)
  end
end
populate_students() click to toggle source
# File app/controllers/groups_controller.rb, line 146
def populate_students
  @assignment = Assignment.find(params[:assignment_id],
                                :include => [:groupings])
  @students = Student.all
  @table_rows = construct_student_table_rows(@students, @assignment)
end
remove_group() click to toggle source
# File app/controllers/groups_controller.rb, line 42
def remove_group
  return unless request.delete?
  grouping = Grouping.find(params[:grouping_id])
  @assignment = grouping.assignment
  @errors = []
  @removed_groupings = []
              students_to_remove = grouping.students.all
              grouping.student_memberships.all.each do |member|
                      grouping.remove_member(member.id)
              end
              @students_data = construct_student_table_rows(students_to_remove, @assignment)
  if grouping.has_submission?
      @errors.push(grouping.group.group_name)
      render :delete_groupings
  else
    grouping.delete_grouping
    @removed_groupings.push(grouping)
    render :delete_groupings
  end
end
rename_group() click to toggle source
# File app/controllers/groups_controller.rb, line 80
def rename_group
  @assignment = Assignment.find(params[:assignment_id])
  # group_id is really the grouping_id, this is due to rails routing
  @grouping = Grouping.find(params[:id])
  @group = @grouping.group

  # Checking if a group with this name already exists

  if (@groups = Group.first(:conditions => {:group_name =>
  [params[:new_groupname]]}))
     existing = true
     groupexist_id = @groups.id
  end

  unless existing
    #We update the group_name
    @group.group_name = params[:new_groupname]
    @group.save
  else

    # We link the grouping to the group already existing

    # We verify there is no other grouping linked to this group on the
    # same assignement
    params[:groupexist_id] = groupexist_id
    params[:assignment_id] = @assignment.id

    if Grouping.all(:conditions => ["assignment_id =
    :assignment_id and group_id = :groupexist_id", {:groupexist_id =>
    groupexist_id, :assignment_id => @assignment.id}])
       flash[:error] = I18n.t('groups.rename_group.already_in_use')
    else
      @grouping.update_attribute(:group_id, groupexist_id)
    end
  end
  @grouping_data = construct_table_row(@grouping, @assignment)
end
rename_group_dialog() click to toggle source
# File app/controllers/groups_controller.rb, line 73
def rename_group_dialog
  @assignment = Assignment.find(params[:assignment_id])
  # id is really the grouping_id, this is due to rails routing
  @grouping_id = params[:id]
  render :partial => 'groups/modal_dialogs/rename_group_dialog.rjs'
end
upload_dialog() click to toggle source
# File app/controllers/groups_controller.rb, line 63
def upload_dialog
  @assignment = Assignment.find(params[:id])
  render :partial => 'groups/modal_dialogs/upload_dialog.rjs'
end
use_another_assignment_groups() click to toggle source
# File app/controllers/groups_controller.rb, line 235
def use_another_assignment_groups
  @target_assignment = Assignment.find(params[:assignment_id])
  source_assignment = Assignment.find(params[:clone_groups_assignment_id])

  if source_assignment.nil?
    flash[:warning] = I18n.t('groups.csv.could_not_find_source')
  end
  if @target_assignment.nil?
    flash[:warning] = I18n.t('groups.csv.could_not_find_target')
  end

  # Clone the groupings
  @target_assignment.clone_groupings_from(source_assignment.id)
end
valid_grouping() click to toggle source
# File app/controllers/groups_controller.rb, line 118
def valid_grouping
  @assignment = Assignment.find(params[:assignment_id])
  @grouping = Grouping.find(params[:grouping_id])
  @grouping.validate_grouping
  @grouping_data = construct_table_row(@grouping, @assignment)
end