CSC/ECE 517 Fall 2021 - E2138. Auto-generate submission directory names based on assignment

From Expertiza_Wiki
Jump to navigation Jump to search

This wiki page describes the changes made under E2138, in order to auto-generate submission directory names based on assignment names for Fall 2021, CSC/ECE 517.

Overview

About Expertiza

Expertiza is an open source project based on Ruby on Rails framework. Expertiza allows the instructor to create new assignments and customize new or existing assignments. It also allows the instructor to create a list of topics the students can sign up for. Students can form teams in Expertiza to work on various projects and assignments. Students can also peer review other students' submissions. Expertiza supports submission across various document types, including the URLs and wiki pages.

Issues for this project

When instructors give an assignment a name, submission directories should be auto-generated based on the assignment name, and also be unique. A number of issues existed when a previous team worked on this assignment (E2054), including

  • Issue #1: The directory name should be auto-generated from the assignment name.
  • Issue #2: It should be done by changing spaces in the names to underscores. E.g., the directory for Program 1 is by default "Program_1".
  • Issue #3: A check should be added to prevent two assignments in the same course from having the same name.
  • Issue #4: Verify or add if not present - a check to stop two assignments from sharing the same directory.
  • Issue #5: On changing the name of an assignment while creating it, the code shouldn't throw a NoMethodError.

Project implementation

Submitted work and demonstration of project

Files involved

  • app/views/assignments/edit/_general.html.erb - added function that auto generate directory name, and substitute its spacebar with underscore.
  • app/models/assignment.rb - added presence and uniqueness validation to assignment.
  • app/controllers/assignments_controller.rb - modify create method to check for existing assignment name & directory.
  • spec/controllers/assignments_controller_spec.rb - edited & added tests for the project.
  • spec/models/due_date_spec.rb - fixed issues where tests can fail due to creating assignments with same names & directories.
  • spec/models/student_task_spec.rb - fixed issues where tests can fail due to creating assignments with same names & directories.

Changes made to code

views

The following code was added to app/views/assignments/edit/_general.html.erb. The purpose is to generate submission directory name from the assignment name and substitute the spacebar with underscore.

<script>
...
$(function() {
    $("#assignment_form_assignment_name").change(function() {
        filename = $( "#assignment_form_assignment_name" ).val().replace(/ /g,"_").replace(/[/\\?%*:|"<>/$&!#%^@]/g, '');;
        $('#assignment_form_assignment_directory_path').val(filename);
    });
});

function autogenerate_submission(){
  assignment_form.assignment.directory_path = assignment_form.assignment.name;
}
</script>

models

The following code was added to app/models/assignment.rb's Assignment class, to add validation for assignment name and directory.

  validates :directory_path, presence: true # E2138 Validation for unique submission directory
  validates :directory_path, uniqueness: {scope: :course_id}

controllers

The following code was modified in app/controllers/assignments_controller.rb's create method, adding checks for assignment name and directory when creating assignment.

Before

  def create
     @assignment_form = AssignmentForm.new(assignment_form_params)
     if params[:button]
       if @assignment_form.save
         @assignment_form.create_assignment_node
         exist_assignment = Assignment.find_by(id: @assignment_form.assignment.id)
         assignment_form_params[:assignment][:id] = exist_assignment.id.to_s
         if assignment_form_params[:assignment][:directory_path].blank?
           assignment_form_params[:assignment][:directory_path] = "assignment_#{assignment_form_params[:assignment][:id]}"
         end
         ques_array = assignment_form_params[:assignment_questionnaire]
         due_array = assignment_form_params[:due_date]
         ques_array.each do |cur_questionnaire|
           cur_questionnaire[:assignment_id] = exist_assignment.id.to_s
         end
         due_array.each do |cur_due|
           cur_due[:parent_id] = exist_assignment.id.to_s
         end
         assignment_form_params[:assignment_questionnaire] = ques_array
         assignment_form_params[:due_date] = due_array
         @assignment_form.update(assignment_form_params, current_user)
         aid = Assignment.find_by(id: @assignment_form.assignment.id).id
         ExpertizaLogger.info "Assignment created: #{@assignment_form.as_json}"
         redirect_to edit_assignment_path aid
         undo_link("Assignment \"#{@assignment_form.assignment.name}\" has been created successfully. ")
         return
       else
         flash.now[:error] = "Failed to create assignment"
         render 'new'
       end
     else
       render 'new'
      undo_link("Assignment \"#{@assignment_form.assignment.name}\" has been created successfully. ")
    end
  end

After

  def create
    @assignment_form = AssignmentForm.new(assignment_form_params)
    if params[:button]
      # E2138 issue #3
      find_existing_assignment = Assignment.find_by(name: @assignment_form.assignment.name, course_id: @assignment_form.assignment.course_id)
      dir_path = assignment_form_params[:assignment][:directory_path]
      find_existing_directory = Assignment.find_by(directory_path: dir_path, course_id: @assignment_form.assignment.course_id)
      if !find_existing_assignment and !find_existing_directory and @assignment_form.save #No existing names/directories 
        @assignment_form.create_assignment_node
        current_assignment = Assignment.find_by(name: @assignment_form.assignment.name, course_id: @assignment_form.assignment.course_id)
        assignment_form_params[:assignment][:id] = current_assignment.id.to_s
        ques_array = assignment_form_params[:assignment_questionnaire]
        due_array = assignment_form_params[:due_date]
        ques_array.each do |cur_questionnaire|
          cur_questionnaire[:assignment_id] = current_assignment.id.to_s
        end
        due_array.each do |cur_due|
          cur_due[:parent_id] = current_assignment.id.to_s
        end
        assignment_form_params[:assignment_questionnaire] = ques_array
        assignment_form_params[:due_date] = due_array
        @assignment_form.update(assignment_form_params, current_user)
        aid = Assignment.find_by(name: @assignment_form.assignment.name, course_id: @assignment_form.assignment.course_id).id
        ExpertizaLogger.info "Assignment created: #{@assignment_form.as_json}"
        redirect_to edit_assignment_path aid
        undo_link("Assignment \"#{@assignment_form.assignment.name}\" has been created successfully. ")
        return
      else
        flash[:error] = "Failed to create assignment."
        if find_existing_assignment
          flash[:error] << "<br>  " + @assignment_form.assignment.name + " already exists as an assignment name"
        end
        if find_existing_directory
          flash[:error] << "<br>  " + dir_path + " already exists as a submission directory name"
        end
        redirect_to "/assignments/new?private=1"
      end
    else
      render 'new'
      undo_link("Assignment \"#{@assignment_form.assignment.name}\" has been created successfully. ")
    end
  end

Test Plan

Manual UI Testing

The following steps must be performed to test the project UI:

Step 1: Log in as an Instructor, with Username - instructor6, Password - password.




Step 2: Create a new assignment. Assignment is named as "Test Assignment", under course CSC/ECE 517 Fall 2020. The directory name gets auto generated with name replacing space by underscore in assignment name.




Step 3: Assignment gets successfully created. Assignment is named as "Test Assignment" and gets saved in the directory "Test_assignment", under course CSC/ECE 517 Fall 2020




Step 4: Create and save another assignment with same name. Following error is observed.




RSpec Testing

The following RSpec test is added in the assignments_contoller_spec.rb file

 # Assignment Names Cannot be duplicated
 context 'when assignment_form name already exists and is not saved properly' do
      it 'redirects to assignment#new page' do
        allow(assignment_form).to receive(:assignment).and_return(assignment)
        allow(Assignment).to receive(:find_by).with(any_args).and_return(false)
        allow(Assignment).to receive(:find_by).with(any_args).and_return(false)
        allow(assignment_form).to receive(:save).and_return(true)
        allow(assignment_form).to receive(:create_assignment_node).and_return(double('node'))
        allow(assignment_form).to receive(:update).with(any_args).and_return(true)
        allow(assignment).to receive(:id).and_return(1)
        allow(Assignment).to receive(:find_by).with(course_id:1, name:'test assignment').and_return(assignment)
        post :create, @params
        expect(flash[:error]).to eq('Failed to create assignment.<br>  test assignment already exists as an assignment name')
        expect(response).to redirect_to('/assignments/new?private=1')
      end
    end


The following RSpec tests are corrected in the assignments_contoller_spec.rb file

Before

    context 'when assignment_form is saved successfully' do
      it 'redirects to assignment#edit page' do
        allow(assignment_form).to receive(:assignment).and_return(assignment)
        allow(assignment_form).to receive(:save).and_return(true)
        allow(assignment_form).to receive(:update).with(any_args).and_return(true)
        allow(assignment_form).to receive(:create_assignment_node).and_return(double('node'))
        allow(assignment).to receive(:id).and_return(1)
        allow(Assignment).to receive(:find_by).with(id: 1).and_return(assignment)
        allow_any_instance_of(AssignmentsController).to receive(:undo_link)
          .with('Assignment "test assignment" has been created successfully. ').and_return(true)
        post :create, @params
        expect(response).to redirect_to('/assignments/1/edit')
      end
    end

After

 context 'when assignment_form is saved successfully' do
      it 'redirects to assignment#edit page' do
        allow(assignment_form).to receive(:assignment).and_return(assignment)
        allow(Assignment).to receive(:find_by).with(any_args).and_return(false)
        allow(Assignment).to receive(:find_by).with(any_args).and_return(false)
        allow(assignment_form).to receive(:save).and_return(true)
        allow(assignment_form).to receive(:create_assignment_node).and_return(double('node'))
        allow(assignment_form).to receive(:update).with(any_args).and_return(true)
        allow(assignment).to receive(:id).and_return(1)
        allow(Assignment).to receive(:find_by).with(course_id:1, name:'test assignment').and_return(assignment)
        allow_any_instance_of(AssignmentsController).to receive(:undo_link)
           .with('Assignment "test assignment" has been created successfully. ').and_return(true)
        post :create, @params
        expect(response).to redirect_to('/assignments/1/edit')
      end
    end

Team Information

Mentor: Nicholas Himes (nnhimes)


Henry Chen (hchen34)

Saurabh Nanda (snanda2)

Snehapriyaa Mathiyalaghan (smathiy)