Class: Course::Settings::AssessmentsComponent

Inherits:
Component
  • Object
show all
Defined in:
app/models/course/settings/assessments_component.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.category_email_setting_itemsObject


4
5
6
7
8
9
10
11
12
13
# File 'app/models/course/settings/assessments_component.rb', line 4

def category_email_setting_items
  {
    assessment_opening: { enabled_by_default: true },
    assessment_closing: { enabled_by_default: true },
    new_submission: { enabled_by_default: true },
    new_phantom_submission: { enabled_by_default: true },
    new_comment: { enabled_by_default: true },
    grades_released: { enabled_by_default: true }
  }
end

.delete_lesson_plan_item_setting(current_course, tab_id) ⇒ Object

Do not add this to a destroy callback in the Tab model as it will get invoked when the course is being destroyed and saving of the course here to save the settings will cause the course deletion to fail.

Parameters:

  • current_course (Course)

    The current course, to get the settings object.

  • tab_id (Integer)

    The tab ID of the lesson plan item setting to be cleared.


37
38
39
40
41
# File 'app/models/course/settings/assessments_component.rb', line 37

def delete_lesson_plan_item_setting(current_course, tab_id)
  current_course.settings(Course::AssessmentsComponent.key, :lesson_plan_items).
    send("tab_#{tab_id}=", nil)
  current_course.save
end

.email_enabled?(category, key) ⇒ Boolean

Checks whether a type of email notification is enabled for an assessment category.

Parameters:

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)

19
20
21
22
23
24
25
# File 'app/models/course/settings/assessments_component.rb', line 19

def email_enabled?(category, key)
  raise ArgumentError, 'Invalid email key' unless valid_email_setting_key?(key)

  setting = category.course.
            settings(Course::AssessmentsComponent.key, category.id.to_s, :emails, key).enabled
  setting.nil? ? category_email_setting_items[key][:enabled_by_default] : setting
end

.valid_email_setting_key?(key) ⇒ Boolean

Returns:

  • (Boolean)

27
28
29
# File 'app/models/course/settings/assessments_component.rb', line 27

def valid_email_setting_key?(key)
  category_email_setting_items.key?(key)
end

Instance Method Details

#disabled_tab_ids_for_lesson_planObject


95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/models/course/settings/assessments_component.rb', line 95

def disabled_tab_ids_for_lesson_plan
  disabled_tab_keys = []
  lesson_plan_item_keys = settings.lesson_plan_items

  if lesson_plan_item_keys
    disabled_tab_keys = lesson_plan_item_keys.keys.reject do |tab|
      settings.settings(:lesson_plan_items, tab).enabled
    end
  end

  disabled_tab_keys.map { |tab_key| tab_key[4..-1] }
end

#email_settingsArray<Hash>

Generates a list of concrete email settings meant for use on the notifications settings page. See Notifications#email_settings for details.

Returns:

  • (Array<Hash>)

51
52
53
54
55
56
57
# File 'app/models/course/settings/assessments_component.rb', line 51

def email_settings
  current_course.assessment_categories.map do |category|
    category_email_setting_items.map do |setting_key, defaults|
      email_setting_hash(key, category, setting_key, defaults[:enabled_by_default])
    end
  end.flatten
end

#lesson_plan_item_settingsArray<Hash>

Generates a list of concrete lesson plan item settings for use on the lesson plan settings page. Currently returns settings for assessment tabs.

Returns:

  • (Array<Hash>)

78
79
80
81
82
83
84
# File 'app/models/course/settings/assessments_component.rb', line 78

def lesson_plan_item_settings
  current_course.assessment_categories.map do |category|
    category.tabs.map do |tab|
      lesson_plan_item_setting_hash(key, tab.category, tab)
    end
  end
end

#update_email_setting(attributes) ⇒ Object

Updates an email notification setting.

Parameters:

  • attributes (Hash)

    New setting represented by a hash with keys 'key', 'enabled', 'options' keys, e.g. { 'key' => 'item_opening', 'enabled' => true, 'options' => { 'category_id' => 3 } }

Raises:

  • (ArgumentError)

64
65
66
67
68
69
70
71
72
# File 'app/models/course/settings/assessments_component.rb', line 64

def update_email_setting(attributes)
  category_id = attributes['options']['category_id']
  setting_key = attributes['key'].to_sym
  raise ArgumentError, 'Invalid email key' unless valid_email_setting_key?(setting_key)
  raise ArgumentError, 'Invalid category id' unless valid_category_id?(category_id)

  settings.settings(category_id.to_s, :emails, setting_key).enabled = attributes['enabled']
  true
end

#update_lesson_plan_item_setting(attributes) ⇒ Object


86
87
88
89
90
91
92
93
# File 'app/models/course/settings/assessments_component.rb', line 86

def update_lesson_plan_item_setting(attributes)
  tab_id = attributes['options']['tab_id']
  settings.settings(:lesson_plan_items, "tab_#{tab_id}").enabled = ActiveRecord::Type::Boolean.new.
                                                                   cast(attributes['enabled'])
  settings.settings(:lesson_plan_items, "tab_#{tab_id}").visible = ActiveRecord::Type::Boolean.new.
                                                                   cast(attributes['visible'])
  true
end