course_section.rb 2.26 KB
Newer Older
Zac Wood's avatar
Zac Wood committed
1
2
# Contains logic belonging to the +CourseSection+ model.
class CourseSection < ApplicationRecord
3
  # Each +CourseSection+ belongs to a +Course+ and an +Instructor+.
Zac Wood's avatar
Zac Wood committed
4
  belongs_to :course
5
  belongs_to :instructor
Zac Wood's avatar
Zac Wood committed
6
7

  # Each course belongs to a +Semester+
8
  belongs_to :semester
Zac Wood's avatar
Zac Wood committed
9
10
11
12
13

  # Ensure all necessary fields are present.
  validates :name, presence: true
  validates :crn, presence: true
  validates :title, presence: true
14
  validates :course_id, presence: true
15
  validates :semester_id, presence: true
16

Zac Wood's avatar
Zac Wood committed
17
18
19
20
21
22
  serialize :rating_questions, Array

  def teaching_rating
    if rating_questions.empty?
      nil
    else
Zac Wood's avatar
Zac Wood committed
23
      "#{rating_questions[0]['instr_mean']} / #{rating_questions[0]['resp']} responses"
Zac Wood's avatar
Zac Wood committed
24
25
26
27
28
29
30
    end
  end

  def course_rating
    if rating_questions.empty?
      nil
    else
Zac Wood's avatar
Zac Wood committed
31
      "#{rating_questions[1]['instr_mean']} / #{rating_questions[1]['resp']} responses"
Zac Wood's avatar
Zac Wood committed
32
33
34
    end
  end

35
36
37
38
39
  def overlaps?(other)
    t1_start, t1_end = Time.parse(start_time), Time.parse(end_time)
    t2_start, t2_end = Time.parse(other.start_time), Time.parse(other.end_time)

    (t1_start <= t2_end && t2_start <= t1_end) && Set.new(days.split).intersect?(Set.new(other.days.split))
Zac Wood's avatar
Zac Wood committed
40
41
  end

42
  def self.latest_by_crn(crn)
43
    where(crn: crn).min_by { |s| s.semester.id }
44
45
  end

46
47
  # Select all course sections that have an instructor that matches the given name
  def self.with_instructor(name: "")
Zac Wood's avatar
Zac Wood committed
48
49
50
    joins(:instructor)
      .where("instructors.name LIKE ?", "%#{name}%")
      .select('course_sections.*, instructors.name as instructor_name')
51
  end
Zach Perkins's avatar
Zach Perkins committed
52

53
  def self.from_crn(base_query, crn)
Zac Wood's avatar
Zac Wood committed
54
    base_query.where(crn: crn)
55
  end
Zach Perkins's avatar
Zach Perkins committed
56

57
  def self.from_course_id(base_query, course_id)
Zac Wood's avatar
Zac Wood committed
58
    base_query.where(course_id: course_id)
59
  end
Zach Perkins's avatar
Zach Perkins committed
60

61
62
  # Select all revelevant course sections given the provided filters
  def self.fetch(filters)
63
    query = CourseSection.joins(:course).select("course_sections.*")
Zach Perkins's avatar
Zach Perkins committed
64

65
66
67
68
69
70
    filters.each do |filter, value|
      case filter
      when "crn"
        query = from_crn(query, value)
      when "course_id"
        query = from_course_id(query, value)
Zach Perkins's avatar
Zach Perkins committed
71
72
      when "course_number"
        query = Course.from_course_number(query, value)
73
74
      when "subject"
        query = Course.from_subject(query, value)
Zach Perkins's avatar
Zach Perkins committed
75
76
      when "title"
        query = Course.from_title(query, value)
77
78
      end
    end
Zach Perkins's avatar
Zach Perkins committed
79

80
    query
81
  end
Zac Wood's avatar
Zac Wood committed
82
end