Class: Course::Assessment::Submission::UpdateService

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
app/services/course/assessment/submission/update_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(controller, variables = {}) ⇒ UpdateService (protected)

Service for handling the submission management logic, this serves as the super class for the specific submission services.

Parameters:

  • controller (Course::Assessment::SubmissionsController)

    the controller instance.

  • variables (Hash) (defaults to: {})

    a key value pairs of variables, which will be set as instance variables in the service. { name: 'Bob' } will set a instance variable @name with the value of 'Bob' in the service.


50
51
52
53
54
55
56
# File 'app/services/course/assessment/submission/update_service.rb', line 50

def initialize(controller, variables = {})
  super(controller)

  variables.each do |key, value|
    instance_variable_set("@#{key}", value)
  end
end

Instance Method Details

#load_or_create_answersObject


21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/services/course/assessment/submission/update_service.rb', line 21

def load_or_create_answers
  return unless @submission.attempting?

  new_answers = questions_to_attempt.not_answered(@submission).attempt(@submission)
  new_answers_created = new_answers.map do |answer|
    # When there are no existing answers, the first one will be the current_answer.
    if answer.new_record?
      answer.current_answer = true
      answer.save!
    end
  end
  new_answers_created = new_answers_created.reduce(false) { |a, e| a || e }
  @submission.answers.reload if new_answers_created && @submission.answers.loaded?
end

#load_or_create_submission_questionsObject


36
37
38
39
# File 'app/services/course/assessment/submission/update_service.rb', line 36

def load_or_create_submission_questions
  return unless create_missing_submission_questions && @submission.submission_questions.loaded?
  @submission.submission_questions.reload
end

#submit_answerObject


12
13
14
15
16
17
18
19
# File 'app/services/course/assessment/submission/update_service.rb', line 12

def submit_answer
  answer = @submission.answers.find(submit_answer_params[:id].to_i)
  if update_answer(answer, submit_answer_params)
    auto_grade(answer)
  else
    render json: { errors: answer.errors }, status: :bad_request
  end
end

#updateObject


3
4
5
6
7
8
9
10
# File 'app/services/course/assessment/submission/update_service.rb', line 3

def update
  if update_submission
    load_or_create_answers if unsubmit?
    render 'edit'
  else
    render json: { errors: @submission.errors }, status: :bad_request
  end
end

#update_answers_paramsObject (protected)


62
63
64
# File 'app/services/course/assessment/submission/update_service.rb', line 62

def update_answers_params
  params.require(:submission).permit(answers: [:id] + update_answer_params)
end

#update_submission_paramsObject (protected)


58
59
60
# File 'app/services/course/assessment/submission/update_service.rb', line 58

def update_submission_params
  params.require(:submission).permit(*workflow_state_params, points_awarded_param)
end