instructor.rb 747 Bytes
Newer Older
Zac Wood's avatar
Zac Wood committed
1
class Instructor < ApplicationRecord
2
3
  has_many :course_sections

Zac Wood's avatar
Zac Wood committed
4
5
6
7
8
9
  scope :named, ->(name) {
    name.split(' ').reduce(all) do |query, comp|
      query.where("upper(instructors.name) LIKE ?", "%#{comp.upcase}%")
    end
  }

10
  def self.from_name(base_query, name)
Zac Wood's avatar
Zac Wood committed
11
    base_query.where("upper(instructors.name) LIKE ?", "%#{name.upcase}%")
12
  end
Zac Wood's avatar
Zac Wood committed
13
14
15
16
17
18
19
20
21
22
23
24

  def rating(question = 0, sections = CourseSection.where(instructor_id: id))
    total = 0
    resp = 0
    sections.each do |s|
      next if s.rating_questions.empty?
      resp += s.rating_questions[question]["resp"].to_i
      total += s.rating_questions[question]["instr_mean"].to_f * s.rating_questions[0]["resp"].to_i
    end

    [(total / resp).round(2), resp] unless resp.zero?
  end
Zac Wood's avatar
Zac Wood committed
25
end