Class: Mumuki::Classroom::Assignment

Inherits:
Document
  • Object
show all
Includes:
Mongoid::Timestamps
Defined in:
lib/mumuki/classroom/models/assignment.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Document

inherited, whitelist_attributes

Class Method Details

.attach_all_by!(query) ⇒ Object



101
102
103
# File 'lib/mumuki/classroom/models/assignment.rb', line 101

def attach_all_by!(query)
  where(query).unset(:detached)
end

.classroom_sibling_for(assignment, organization) ⇒ Object



147
148
149
# File 'lib/mumuki/classroom/models/assignment.rb', line 147

def classroom_sibling_for(assignment, organization)
  find_by(organization: organization, 'student.uid': assignment.user.uid, 'exercise.eid': assignment.exercise.bibliotheca_id)
end

.destroy_all_by!(query) ⇒ Object



105
106
107
# File 'lib/mumuki/classroom/models/assignment.rb', line 105

def destroy_all_by!(query)
  where(query).destroy
end

.detach_all_by!(query) ⇒ Object



97
98
99
# File 'lib/mumuki/classroom/models/assignment.rb', line 97

def detach_all_by!(query)
  where(query).set(detached: true)
end

.empty_statsObject



113
114
115
# File 'lib/mumuki/classroom/models/assignment.rb', line 113

def empty_stats
  {passed: 0, failed: 0, passed_with_warnings: 0}
end

.items_to_review(query, exercises) ⇒ Object



122
123
124
125
126
127
128
129
130
# File 'lib/mumuki/classroom/models/assignment.rb', line 122

def items_to_review(query, exercises)
  passed_exercises_ids = where(query)
                           .map { |assignment| [assignment.exercise.eid, assignment.submissions.max_by(&:created_at)] }
                           .map { |eid, submission| eid if solved?(submission.status) }
  exercises.reject { |exercise| passed_exercises_ids.include? exercise[:id] }
    .pluck(:tag_list, :language)
    .flatten
    .uniq
end

.solved?(status) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/mumuki/classroom/models/assignment.rb', line 132

def solved?(status)
  status.passed? || status.skipped?
end

.stats_by(query) ⇒ Object



136
137
138
139
140
141
142
143
144
145
# File 'lib/mumuki/classroom/models/assignment.rb', line 136

def stats_by(query)
  stats = where(query)
            .map { |assignment| assignment.submissions.max_by(&:created_at) }
            .group_by { |submission| submission.status }
            .map { |status, submissions| [status.to_sym, submissions.size] }
            .to_h.compact
  stats = empty_stats.merge(stats)
  stats[:failed] += stats.delete(:errored) || 0
  stats.slice(*empty_stats.keys)
end

.transfer_all_by!(query, new_organization, new_course) ⇒ Object



109
110
111
# File 'lib/mumuki/classroom/models/assignment.rb', line 109

def transfer_all_by!(query, new_organization, new_course)
  where(query).set(organization: new_organization, course: new_course)
end

.with_full_messages(query, user) ⇒ Object



117
118
119
120
# File 'lib/mumuki/classroom/models/assignment.rb', line 117

def with_full_messages(query, user)
  where(query)
    .map { |assignment| assignment.with_full_messages(user) }
end

Instance Method Details

#add_message!(message, sid) ⇒ Object



27
28
29
30
31
32
# File 'lib/mumuki/classroom/models/assignment.rb', line 27

def add_message!(message, sid)
  submission(sid).tap do |it|
    it.add_message! message
    update_submissions!
  end
end

#add_message_to_submission!(message, sid) ⇒ Object



67
68
69
70
71
# File 'lib/mumuki/classroom/models/assignment.rb', line 67

def add_message_to_submission!(message, sid)
  submission = add_message! message, sid
  notify_message! message, sid
  submission
end

#add_submission!(submission) ⇒ Object



34
35
36
37
# File 'lib/mumuki/classroom/models/assignment.rb', line 34

def add_submission!(submission)
  self.submissions << Mumuki::Classroom::Submission.new(submission.as_json)
  update_submissions!
end

#evaluate_manually!(sid, comment, status) ⇒ Object



17
18
19
20
21
# File 'lib/mumuki/classroom/models/assignment.rb', line 17

def evaluate_manually!(sid, comment, status)
  submission = submission(sid)
  submission.evaluate_manually! comment, status
  update_submissions!
end

#json_to_notify(message, sid) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/mumuki/classroom/models/assignment.rb', line 48

def json_to_notify(message, sid)
  {
    message: message,
    submission_id: sid,
    exercise_id: exercise.eid,
    organization: organization
  }.as_json
end

#notification_previewObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/mumuki/classroom/models/assignment.rb', line 73

def notification_preview
  as_json(
      only: %i(course exercise guide student),
      include: {
          exercise: {
              only: %i(eid name)
          },
          guide: {
              only: %i(slug)
          },
          student: {
              only: %i(first_name last_name image_url uid)
          }
      }
  )
end

#notify_manual_evaluation!(sid) ⇒ Object



43
44
45
46
# File 'lib/mumuki/classroom/models/assignment.rb', line 43

def notify_manual_evaluation!(sid)
  assignment = {submission_id: sid}.merge(submission(sid).as_json only: [:status, :manual_evaluation])
  Mumukit::Nuntius.notify_event!('AssignmentManuallyEvaluated', {assignment: assignment}, {sender: :classroom})
end

#notify_message!(message, sid) ⇒ Object



39
40
41
# File 'lib/mumuki/classroom/models/assignment.rb', line 39

def notify_message!(message, sid)
  Mumukit::Nuntius.notify! 'teacher-messages', json_to_notify(message, sid)
end

#submission(sid) ⇒ Object



23
24
25
# File 'lib/mumuki/classroom/models/assignment.rb', line 23

def submission(sid)
  submissions.find_by!(sid: sid)
end

#threads(language) ⇒ Object



57
58
59
60
# File 'lib/mumuki/classroom/models/assignment.rb', line 57

def threads(language)
  language = guide[:language][:name] if language.blank?
  submissions.map { |it| it.thread(language) }.compact
end

#with_full_messages(user) ⇒ Object



62
63
64
65
# File 'lib/mumuki/classroom/models/assignment.rb', line 62

def with_full_messages(user)
  self[:submissions] = submissions.map { |it| it.with_full_messages user }
  self
end