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

From Expertiza_Wiki
Jump to navigation Jump to search
Line 37: Line 37:
'''Before'''
'''Before'''
<pre>
<pre>
   def create
   def overwrite_session
    @assignment_form = AssignmentForm.new(assignment_form_params)
    # If not impersonatable, then original user's session remains
    if params[:button]
    if params[:impersonate].nil?
      if @assignment_form.save
      # E1991 : check whether instructor is currently in anonymized view
        @assignment_form.create_assignment_node
      user = User.anonymized_view?(session[:ip]) ? User.real_user_from_anonymized_name(params[:user][:name]) : user = User.find_by(name: params[:user][:name])
        exist_assignment = Assignment.find_by(id: @assignment_form.assignment.id)
      session[:super_user] = session[:user] if session[:super_user].nil?
        assignment_form_params[:assignment][:id] = exist_assignment.id.to_s
      AuthController.clear_user_info(session, nil)
        if assignment_form_params[:assignment][:directory_path].blank?
      session[:original_user] = @original_user
          assignment_form_params[:assignment][:directory_path] = "assignment_#{assignment_form_params[:assignment][:id]}"
      session[:impersonate] = true
        end
      session[:user] = user
        ques_array = assignment_form_params[:assignment_questionnaire]
    else
        due_array = assignment_form_params[:due_date]
      # If some user is to be impersonated, their session details are overwritten onto the current to impersonate
        ques_array.each do |cur_questionnaire|
      if !params[:impersonate][:name].empty?
          cur_questionnaire[:assignment_id] = exist_assignment.id.to_s
        # E1991 : check whether instructor is currently in anonymized view
        end
        user = User.anonymized_view?(session[:ip]) ? User.real_user_from_anonymized_name(params[:impersonate][:name]) : user = user = User.find_by(name: params[:impersonate][:name])
        due_array.each do |cur_due|
        AuthController.clear_user_info(session, nil)
          cur_due[:parent_id] = exist_assignment.id.to_s
        session[:user] = user
        end
        session[:impersonate] = true
        assignment_form_params[:assignment_questionnaire] = ques_array
        session[:original_user] = @original_user
        assignment_form_params[:due_date] = due_array
      else
        @assignment_form.update(assignment_form_params, current_user)
        # E1991 : check whether instructor is currently in anonymized view
        aid = Assignment.find_by(id: @assignment_form.assignment.id).id
        user = User.anonymized_view?(session[:ip]) ? User.real_user_from_anonymized_name(params[:user][:name]) : user = User.find_by(name: params[:user][:name])
        ExpertizaLogger.info "Assignment created: #{@assignment_form.as_json}"
        AuthController.clear_user_info(session, nil)
        redirect_to edit_assignment_path aid
        session[:user] = session[:super_user]
        undo_link("Assignment \"#{@assignment_form.assignment.name}\" has been created successfully. ")
        user = session[:user]
        return
        session[:super_user] = nil
      else
      end
        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
   end
   end
Line 76: Line 70:
'''After'''
'''After'''
<pre>
<pre>
  def create
  def overwrite_session
     @assignment_form = AssignmentForm.new(assignment_form_params)
     # If not impersonatable, then original user's session remains
     if params[:button]
     if params[:impersonate].nil?
       # E2138 issue #3
       # E1991 : check whether instructor is currently in anonymized view
       find_existing_assignment = Assignment.find_by(name: @assignment_form.assignment.name, course_id: @assignment_form.assignment.course_id)
       if User.anonymized_view?(session[:ip])
      dir_path = assignment_form_params[:assignment][:directory_path]
        user = User.real_user_from_anonymized_name(params[:user][:name])
      find_existing_directory = Assignment.find_by(directory_path: dir_path, course_id: @assignment_form.assignment.course_id)
       else
       if !find_existing_assignment and !find_existing_directory and @assignment_form.save #No existing names/directories
         user = User.find_by(name: params[:user][:name])
         @assignment_form.create_assignment_node
      end
        current_assignment = Assignment.find_by(name: @assignment_form.assignment.name, course_id: @assignment_form.assignment.course_id)
      session[:super_user] = session[:user] if session[:super_user].nil?
        assignment_form_params[:assignment][:id] = current_assignment.id.to_s
      AuthController.clear_user_info(session, nil)
        ques_array = assignment_form_params[:assignment_questionnaire]
      session[:original_user] = @original_user
        due_array = assignment_form_params[:due_date]
      session[:impersonate] = true
        ques_array.each do |cur_questionnaire|
      session[:user] = user
          cur_questionnaire[:assignment_id] = current_assignment.id.to_s
      # If some user is to be impersonated, their session details are overwritten onto the current to impersonate
        end
    elsif !params[:impersonate][:name].empty?
        due_array.each do |cur_due|
      # E1991 : check whether instructor is currently in anonymized view
          cur_due[:parent_id] = current_assignment.id.to_s
      if User.anonymized_view?(session[:ip])
        end
         user = User.real_user_from_anonymized_name(params[:impersonate][:name])
        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
       else
         flash[:error] = "Failed to create assignment."
         user = User.find_by(name: params[:impersonate][:name])
        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
       end
      AuthController.clear_user_info(session, nil)
      session[:user] = user
      session[:impersonate] =  true
      session[:original_user] = @original_user
     else
     else
       render 'new'
       # E1991 : check whether instructor is currently in anonymized view
       undo_link("Assignment \"#{@assignment_form.assignment.name}\" has been created successfully. ")
       AuthController.clear_user_info(session, nil)
      session[:user] = session[:super_user]
      session[:super_user] = nil
     end
     end
   end
   end

Revision as of 21:33, 20 October 2021

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/controllers/impersonate_controller.rb - refactored the codes related to the issues mentioned above.
  • spec/controllers/impersonate_controller_spec.rb - implemented and added some related tests.

Changes made to code

The following code was modified in app/controllers/impersonate_controller.rb, in order to refactor to:

  • make the code more readable.
  • properly delete unused variable .

Before

  def overwrite_session
    # If not impersonatable, then original user's session remains
    if params[:impersonate].nil?
      # E1991 : check whether instructor is currently in anonymized view
      user = User.anonymized_view?(session[:ip]) ? User.real_user_from_anonymized_name(params[:user][:name]) : user = User.find_by(name: params[:user][:name])
      session[:super_user] = session[:user] if session[:super_user].nil?
      AuthController.clear_user_info(session, nil)
      session[:original_user] = @original_user
      session[:impersonate] = true
      session[:user] = user
    else
      # If some user is to be impersonated, their session details are overwritten onto the current to impersonate
      if !params[:impersonate][:name].empty?
        # E1991 : check whether instructor is currently in anonymized view
        user = User.anonymized_view?(session[:ip]) ? User.real_user_from_anonymized_name(params[:impersonate][:name]) : user = user = User.find_by(name: params[:impersonate][:name])
        AuthController.clear_user_info(session, nil)
        session[:user] = user
        session[:impersonate] =  true
        session[:original_user] = @original_user
      else
        # E1991 : check whether instructor is currently in anonymized view
        user = User.anonymized_view?(session[:ip]) ? User.real_user_from_anonymized_name(params[:user][:name]) : user = User.find_by(name: params[:user][:name])
        AuthController.clear_user_info(session, nil)
        session[:user] = session[:super_user]
        user = session[:user]
        session[:super_user] = nil
      end
    end
  end

After

   def overwrite_session
    # If not impersonatable, then original user's session remains
    if params[:impersonate].nil?
      # E1991 : check whether instructor is currently in anonymized view
      if User.anonymized_view?(session[:ip])
        user = User.real_user_from_anonymized_name(params[:user][:name])
      else
        user = User.find_by(name: params[:user][:name])
      end
      session[:super_user] = session[:user] if session[:super_user].nil?
      AuthController.clear_user_info(session, nil)
      session[:original_user] = @original_user
      session[:impersonate] = true
      session[:user] = user
      # If some user is to be impersonated, their session details are overwritten onto the current to impersonate
    elsif !params[:impersonate][:name].empty?
      # E1991 : check whether instructor is currently in anonymized view
      if User.anonymized_view?(session[:ip])
        user = User.real_user_from_anonymized_name(params[:impersonate][:name])
      else
        user = User.find_by(name: params[:impersonate][:name])
      end
      AuthController.clear_user_info(session, nil)
      session[:user] = user
      session[:impersonate] =  true
      session[:original_user] = @original_user
    else
      # E1991 : check whether instructor is currently in anonymized view
      AuthController.clear_user_info(session, nil)
      session[:user] = session[:super_user]
      session[:super_user] = nil
    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 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: Try saving another assignment with same name. Following error is observed. br>



RSpec Testing

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).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

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)