Class: Course::Assessment::Answer::Programming

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
app/models/course/assessment/answer/programming.rb

Constant Summary collapse

MAX_ATTEMPTING_TIMES =
1000

Instance Method Summary collapse

Instance Method Details

#assign_params(params) ⇒ Object


56
57
58
59
60
61
62
63
# File 'app/models/course/assessment/answer/programming.rb', line 56

def assign_params(params)
  acting_as.assign_params(params)

  params[:files_attributes]&.each do |file_attributes|
    file = files.find { |f| f.id == file_attributes[:id].to_i }
    file.content = file_attributes[:content] if file.present?
  end
end

#attempting_times_leftInteger

Returns the attempting times left for current answer. The max attempting times will be returned if question don't have the limit.


32
33
34
35
36
37
38
39
40
# File 'app/models/course/assessment/answer/programming.rb', line 32

def attempting_times_left
  @times_left ||= begin
    return MAX_ATTEMPTING_TIMES unless question.actable.attempt_limit

    times = question.actable.attempt_limit - submission.evaluated_or_graded_answers(question).size
    times = 0 if times < 0
    times
  end
end

#create_and_update_files(params) ⇒ Object


65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/course/assessment/answer/programming.rb', line 65

def create_and_update_files(params)
  params[:files_attributes]&.each do |file_attributes|
    file = files.find { |f| f.id == file_attributes[:id].to_i }
    if file.present?
      file.content = file_attributes[:content]
    else
      files.build(filename: file_attributes[:filename], content: file_attributes[:content])
    end
  end
  save
end

#delete_file(file_id) ⇒ Object


77
78
79
80
81
# File 'app/models/course/assessment/answer/programming.rb', line 77

def delete_file(file_id)
  file = files.find { |f| f.id == file_id }
  file.mark_for_destruction if file.present?
  save
end

#download(dir) ⇒ Object


47
48
49
50
51
52
53
54
# File 'app/models/course/assessment/answer/programming.rb', line 47

def download(dir)
  files.each do |src_file|
    dst_path = File.join(dir, src_file.filename)
    File.open(dst_path, 'w') do |dst_file|
      dst_file.write(src_file.content)
    end
  end
end

#grade_inline?Boolean

Programming answers should be graded in a job.


43
44
45
# File 'app/models/course/assessment/answer/programming.rb', line 43

def grade_inline?
  false
end

#reset_answerObject

Specific implementation of Course::Assessment::Answer#reset_answer


18
19
20
21
22
23
24
25
# File 'app/models/course/assessment/answer/programming.rb', line 18

def reset_answer
  self.class.transaction do
    files.clear
    question.specific.copy_template_files_to(self)
    raise ActiveRecord::Rollback unless save
  end
  acting_as
end

#to_partial_pathObject


13
14
15
# File 'app/models/course/assessment/answer/programming.rb', line 13

def to_partial_path
  'course/assessment/answer/programming/programming'
end