class Api::AssignmentsController

Allows for adding, modifying and showing Markus assignments. Uses Rails’ RESTful routes (check ‘rake routes’ for the configured routes)

Public Instance Methods

create() click to toggle source

Creates a new assignment Requires: short_identifier, due_date, description Optional: repository_folder, group_min, group_max, tokens_per_day, submission_rule_type, marking_scheme_type, allow_web_submits, display_grader_names_to_students, enable_test, assign_graders_to_criteria, message, allow_remarks, remark_due_date, remark_message, student_form_groups, group_name_autogenerated, submission_rule_deduction, submission_rule_hours, submission_rule_interval

# File app/controllers/api/assignments_controller.rb, line 55
def create
  if has_missing_params?([:short_identifier, :due_date, :description])
    # incomplete/invalid HTTP params
    render 'shared/http_status', :locals => {:code => '422', :message =>
      HttpStatusHelper::ERROR_CODE['message']['422']}, :status => 422
    return
  end

  # check if there is an existing assignment
  assignment = Assignment.find_by_short_identifier(params[:short_identifier])
  unless assignment.nil?
    render 'shared/http_status', :locals => {:code => '409', :message =>
      'Assignment already exists'}, :status => 409
    return
  end

  # No assignment found so create new one
  attributes = { :short_identifier => params[:short_identifier] }
  attributes = process_attributes(params, attributes)

  new_assignment = Assignment.new(attributes)
  new_assignment.build_assignment_stat

  # Get and assign the submission_rule
  submission_rule = get_submission_rule(params)

  if submission_rule.nil?
    render 'shared/http_status', :locals => {:code => '500', :message =>
      HttpStatusHelper::ERROR_CODE['message']['500']}, :status => 500
    return
  end

  new_assignment.submission_rule = submission_rule

  unless new_assignment.save
    # Some error occurred
    render 'shared/http_status', :locals => {:code => '500', :message =>
      HttpStatusHelper::ERROR_CODE['message']['500']}, :status => 500
    return
  end

  # Otherwise everything went alright.
  render 'shared/http_status', :locals => {:code => '201', :message =>
    HttpStatusHelper::ERROR_CODE['message']['201']}, :status => 201
end
get_submission_rule(params) click to toggle source

Gets the submission rule for POST/PUT requests based on the supplied params Defaults to NoLateSubmissionRule

# File app/controllers/api/assignments_controller.rb, line 191
def get_submission_rule(params)
  if params[:submission_rule_type] == 'GracePeriod'
    submission_rule = GracePeriodSubmissionRule.new
    period = Period.new(:hours => params[:submission_rule_hours])
    submission_rule.periods << period

  elsif params[:submission_rule_type] == 'PenaltyDecayPeriod'
    submission_rule = PenaltyDecayPeriodSubmissionRule.new
    period = Period.new(:hours => params[:submission_rule_hours],
                        :deduction => params[:submission_rule_deduction],
                        :interval => params[:submission_rule_interval])
    submission_rule.periods << period

  elsif params[:submission_rule_type] == 'PenaltyPeriod'
    submission_rule = PenaltyPeriodSubmissionRule.new
    period = Period.new(:hours => params[:submission_rule_hours],
                        :deduction => params[:submission_rule_deduction])
    submission_rule.periods << period

  else
    submission_rule = NoLateSubmissionRule.new
  end

  submission_rule
end
index() click to toggle source

Returns a list of assignments and their attributes Optional: filter, fields

# File app/controllers/api/assignments_controller.rb, line 16
def index
  assignments = get_collection(Assignment)
  fields = fields_to_render(@@default_fields)

  respond_to do |format|
    format.xml{render :xml => assignments.to_xml(:only => fields, :root =>
      'assignments', :skip_types => 'true')}
    format.json{render :json => assignments.to_json(:only => fields)}
  end
end
process_attributes(params, attributes) click to toggle source

Process the parameters passed for assignment creation and update

# File app/controllers/api/assignments_controller.rb, line 165
def process_attributes(params, attributes)
  # Loop through default fields other than id
  fields = Array.new(@@default_fields)
  fields.delete(:id)
  fields.each do |field|
    attributes[field] = params[field] if !params[field].nil?
  end

  # Some attributes have to be set with default values when creating a new
  # assignment. They're based on the view's defaults.
  if request.post?
    required_fields = { :enable_test => 0,  :assign_graders_to_criteria => 0,
                        :repository_folder => attributes[:short_identifier],
                        :allow_web_submits => 1, :group_min => 1,
                        :display_grader_names_to_students => 0,
                        :marking_scheme_type => 'rubric' }
    required_fields.each do |field_name, default_value|
      attributes[field_name] = default_value if params[field_name].nil?
    end
  end

  attributes
end
show() click to toggle source

Returns an assignment and its attributes Requires: id Optional: filter, fields

# File app/controllers/api/assignments_controller.rb, line 30
def show
  assignment = Assignment.find_by_id(params[:id])
  if assignment.nil?
    # No assignment with that id
    render 'shared/http_status', :locals => {:code => '404', :message =>
      'No assignment exists with that id'}, :status => 404
  else
    fields = fields_to_render(@@default_fields)

    respond_to do |format|
      format.xml{render :xml => assignment.to_xml(:only => fields, :root =>
        'assignment', :skip_types => 'true')}
      format.json{render :json => assignment.to_json(:only => fields)}
    end
  end
end
update() click to toggle source

Updates an existing assignment Requires: id Optional: short_identifier, due_date,repository_folder, group_min, group_max, tokens_per_day, submission_rule_type, marking_scheme_type, allow_web_submits, display_grader_names_to_students, enable_test, assign_graders_to_criteria, description, message, allow_remarks, remark_due_date, remark_message, student_form_groups, group_name_autogenerated, submission_rule_deduction, submission_rule_hours, submission_rule_interval

# File app/controllers/api/assignments_controller.rb, line 109
def update
  # If no assignment is found, render an error.
  assignment = Assignment.find_by_id(params[:id])
  if assignment.nil?
    render 'shared/http_status', :locals => {:code => '404', :message =>
      'Assignment was not found'}, :status => 404
    return
  end

  # Create a hash to hold fields/values to be updated for the assignment
  attributes = {}

  unless params[:short_identifier].blank?
    # Make sure another assignment isn't using the new short_identifier
    other_assignment = Assignment.find_by_short_identifier(
      params[:short_identifier])
    if !other_assignment.nil? && other_assignment != assignment
      render 'shared/http_status', :locals => {:code => '409', :message =>
        'short_identifier already in use'}, :status => 409
      return
    end
    attributes[:short_identifier] = params[:short_identifier]
  end

  attributes = process_attributes(params, attributes)
  assignment.attributes = attributes

  # Update the submission rule if provided
  unless params[:submission_rule_type].nil?
    submission_rule = get_submission_rule(params)
    if submission_rule.nil?
      render 'shared/http_status', :locals => {:code => '500', :message =>
        HttpStatusHelper::ERROR_CODE['message']['500']}, :status => 500
      return
    else
      # If it's a valid submission rule, replace the existing one
      if submission_rule.valid?
        assignment.submission_rule.destroy
        assignment.submission_rule = submission_rule
      end
    end
  end

  unless assignment.save
    # Some error occurred
    render 'shared/http_status', :locals => {:code => '500', :message =>
      HttpStatusHelper::ERROR_CODE['message']['500']}, :status => 500
    return
  end

  # Made it this far, render success
  render 'shared/http_status', :locals => {:code => '200', :message =>
    HttpStatusHelper::ERROR_CODE['message']['200']}, :status => 200
end