canvas-lms/app/models/context.rb

217 lines
7.4 KiB
Ruby
Raw Normal View History

2011-02-01 09:57:29 +08:00
#
# Copyright (C) 2011 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
# These methods are mixed into the classes that can be considered a "context".
# See Context::ContextTypes below.
module Context
module ContextTypes
# These are all the classes that can be considered a "context":
Account = ::Account
Course = ::Course
User = ::User
Group = ::Group
end
2011-02-01 09:57:29 +08:00
module AssetTypes
Announcement = ::Announcement
AssessmentQuestion = ::AssessmentQuestion
AssessmentQuestionBank = ::AssessmentQuestionBank
Assignment = ::Assignment
AssignmentGroup = ::AssignmentGroup
Attachment = ::Attachment
CalendarEvent = ::CalendarEvent
Collaboration = ::Collaboration
ContentTag = ::ContentTag
ContextModule = ::ContextModule
DiscussionEntry = ::DiscussionEntry
DiscussionTopic = ::DiscussionTopic
Folder = ::Folder
LearningOutcome = ::LearningOutcome
LearningOutcomeGroup = ::LearningOutcomeGroup
MediaObject = ::MediaObject
Progress = ::Progress
2011-02-01 09:57:29 +08:00
Quiz = ::Quiz
QuizGroup = ::QuizGroup
QuizQuestion = ::QuizQuestion
QuizSubmission = ::QuizSubmission
Rubric = ::Rubric
RubricAssociation = ::RubricAssociation
Submission = ::Submission
WebConference = ::WebConference
Wiki = ::Wiki
WikiPage = ::WikiPage
def self.get_for_string(str)
if RUBY_VERSION >= "1.9."
self.const_defined?(str, false) ? self.const_get(str, false) : nil
else
self.const_defined?(str) ? self.const_get(str) : nil
end
end
2011-02-01 09:57:29 +08:00
end
def add_aggregate_entries(entries, feed)
if feed.feed_purpose == 'announcements'
entries.each do |entry|
user = entry.user || feed.user
# If already existed and has been updated
if entry.entry_changed? && entry.asset
entry.asset.update_attributes(
:title => entry.title,
:message => entry.message
)
elsif !entry.asset
announcement = self.announcements.build(
:title => entry.title,
:message => entry.message
)
announcement.external_feed_id = feed.id
announcement.user = user
announcement.save
entry.update_attributes(:asset => announcement)
end
end
elsif feed.feed_purpose == 'calendar'
entries.each do |entry|
user = entry.user || feed.user
# If already existed and has been updated
if entry.entry_changed? && entry.asset
event = entry.asset
event.attributes = {
:title => entry.title,
:description => entry.message,
:start_at => entry.start_at,
:end_at => entry.end_at
}
event.workflow_state = 'read_only'
event.workflow_state = 'cancelled' if entry.cancelled?
event.save
elsif entry.active? && !entry.asset
event = self.calendar_events.build(
:title => entry.title,
:description => entry.message,
:start_at => entry.start_at,
:end_at => entry.end_at
)
event.workflow_state = 'read_only'
event.workflow_state = 'cancelled' if entry.cancelled?
event.external_feed_id = feed.id
event.save
entry.update_attributes(:asset => event)
end
end
end
end
2011-02-01 09:57:29 +08:00
def sorted_rubrics(user, context)
associations = RubricAssociation.bookmarked.for_context_codes(context.asset_string).include_rubric
associations.to_a.once_per(&:rubric_id).select{|r| r.rubric }.sort_by{|r| r.rubric.title || "zzzz" }
end
2011-02-01 09:57:29 +08:00
def rubric_contexts(user)
context_codes = [self.asset_string]
context_codes << ([user] + user.management_contexts).uniq.map(&:asset_string) if user
context = self
while context && context.respond_to?(:account) || context.respond_to?(:parent_account)
context = context.respond_to?(:account) ? context.account : context.parent_account
context_codes << context.asset_string if context
end
codes_order = {}
context_codes.each_with_index{|c, idx| codes_order[c] = idx }
associations = RubricAssociation.bookmarked.for_context_codes(context_codes).include_rubric
associations = associations.to_a.select{|a| a.rubric }.once_per{|a| [a.rubric_id, a.context_code] }
contexts = associations.group_by{|a| a.context_code }.map do |code, associations|
context_name = associations.first.context_name
res = {
:rubrics => associations.length,
:context_code => code,
:name => context_name
}
end
contexts.sort_by{|c| codes_order[c[:context_code]] || 999 }
end
2011-02-01 09:57:29 +08:00
def active_record_types
@active_record_types ||= Rails.cache.fetch(['active_record_types', self].cache_key) do
res = {}
res[:files] = self.respond_to?(:attachments) && !self.attachments.active.empty?
res[:modules] = self.respond_to?(:context_modules) && !self.context_modules.active.empty?
res[:quizzes] = self.respond_to?(:quizzes) && !self.quizzes.active.empty?
res[:assignments] = self.respond_to?(:assignments) && !self.assignments.active.empty?
res[:pages] = self.respond_to?(:wiki) && self.wiki_id && !self.wiki.wiki_pages.active.empty?
2011-02-01 09:57:29 +08:00
res[:conferences] = self.respond_to?(:web_conferences) && !self.web_conferences.active.empty?
res[:announcements] = self.respond_to?(:announcements) && !self.announcements.active.empty?
learning outcomes refactor This list is *NOT* complete, some items may have snuck in that I forgot to note, and/or some of the noted items may not be completely functional yet. Specs need to be written around a lot of this, other specs will no doubt need to be fixed. Some things, particularly around LearningOutcomeGroups will need data migrations that aren't there yet. * remove LearningOutcome.non_rubric_outcomes? and replace with false where invoked * remove LearningOutcome.enabled? and replace with true where invoked * remove never-taken branches * remove the shared/aligned_outcomes partial and it's supporting javascript, since it's now empty * remove js handler for add_outcome_alignment_link and supporting method since it only occurred in never-taken branches * mix LearningOutcomeContext into Course and Account * replace LearningOutcomeGroup.default_for(context) with LearningOutcomeContext#root_outcome_group * rename LearningOutcome#content_tags to LearningOutcome#alignments * rename LearningOutcomeGroup#content_tags to LearningOutcomeGroup#child_links, and properly restrict * remove ContentTag[Alignment]#rubric_association_id, add ContentTag[Alignment]#has_rubric_association? that looks at the presence of the content's rubric_association_id * condition off the assignment having a rubric_association rather than filtering tags by has_rubric_association (which just looks back at the assignment). all or none of the assignment's alignments are forced to have the association (via the assignment). this was true in practice before, is now codified (and more efficient) * rename AssessmentQuestionBank#learning_outcome_tags to AssessmentQuestionBank#learning_outcome_alignments * rename Assignment#learning_outcome_tags to Assignment#learning_outcome_alignments * rename Rubric#learning_outcome_tags to Rubric#learning_outcome_alignments * move/rename (Course|Account)#learning_outcome_tags to LearningOutcomeContext#learning_outcome_links * move/rename Account#learning_outcomes (corrected) and Course#learning_outcomes to LearningOutcomeContext#linked_learning_outcomes * move/rename Account#created_learning_outcomes and Course#created_learning_outcomes to LearningOutcomeContext#created_learning_outcomes * clarify and correct usage of linked_learning_outcomes vs. created_learning_outcomes * move/rename (Account|Account)#learning_outcome_groups to LearningOutcomeContext#learning_outcome_groups * remove unused Account#associated_learning_outcomes * just remove one link to a learning outcome when deleting * merge Account#has_outcomes?, Course#has_outcomes? and Course#has_outcomes into LearningOutcomeContext#has_outcomes?, add a use in Context#active_record_types * kill LearningOutcomeGroup#root_learning_outcome_group (unused) * rename LearningOutcomeResult#content_tag to LearningOutcomeResult#alignment * kill unused (and broken) OutcomesController#add_outcome_group * kill unused OutcomesController#update_outcomes_for_asset * kill unused OutcomesController#outcomes_for_asset * remove unused (outside specs, correct specs) AssessmentQuestionBank#outcomes= * remove unused ContentTag#learning_outcome_content * replace ContentTag.learning_outcome_tags_for(asset) (only ever called with asset=an assignment) with call to Assignment#learning_outcome_alignments * remove unused ContentTag.not_rubric * remove (now) unused ContentTag.include_outcome * remove unused LearningOutcome#learning_outcome_group_associations * avoid explicit use of ContentTag in outcome-related specs * replace LearningOutcomeGroup#learning_outcome_tags with LearningOutcomeGroup#child_outcome_links (and only use for outcome links; not tags for child groups) * split ContentTag#create_outcome_result into Submission#create_outcome_result, QuizSubmission#create_outcome_result, and RubricAssessment#create_outcome_result. fix some bugs along the way * refactor ContentTag.outcome_tags_for_banks and some code from QuizSubmission#(track_outcomes|update_outcomes_for_assessment_questions) into QuizSubmission#questions_and_alignments * refactor RubricAssociation#update_outcome_relations and Rubric#update_alignments into LearningOutcome.update_alignments * don't use ContentTag#rubric_association with outcome alignments; use the tag's content's rubric_association in its place (they should have been equal anyways) * refactor LearningOutcome.available_in_context and @context.root_outcome_group.sorted_all_outcomes (only time sorted_all_outcomes is used) into LearningOutcomeContext#available_outcomes and LearningOutcomeContext#available_outcome * overhaul LearningOutcomeGroup#sorted_content and rename to LearningOutcomeGroup#sorted_children. it not returns ContentTags (outcome links) and LearningOutcomeGroups, vs. LearningOutcomes and LearningOutcomeGroups; fix usages appropriately * fix UI for arranging/deleting outcome links and groups within a group to refer to the outcome link rather than the outcome Change-Id: I85d99f2634f7206332cb1f5d5ea575b428988d4b Reviewed-on: https://gerrit.instructure.com/12590 Reviewed-by: Jacob Fugal <jacob@instructure.com> Tested-by: Jacob Fugal <jacob@instructure.com>
2012-07-13 01:16:13 +08:00
res[:outcomes] = self.respond_to?(:has_outcomes?) && self.has_outcomes?
2011-02-01 09:57:29 +08:00
res
end
end
2011-02-01 09:57:29 +08:00
def allow_wiki_comments
false
end
def find_asset(asset_string, allowed_types=nil)
return nil unless asset_string
res = Context.find_asset_by_asset_string(asset_string, self, allowed_types)
res = nil if res.respond_to?(:deleted?) && res.deleted?
res
end
2011-02-01 09:57:29 +08:00
def self.find_by_asset_string(string)
opts = string.split("_")
id = opts.pop
if ContextTypes.const_defined?(opts.join('_').classify)
type = ContextTypes.const_get(opts.join('_').classify)
context = type.find(id)
else
nil
end
rescue => e
nil
end
2011-02-01 09:57:29 +08:00
def self.find_asset_by_asset_string(string, context=nil, allowed_types=nil)
opts = string.split("_")
id = opts.pop
type = opts.join('_').classify
klass = nil
if AssetTypes.const_defined?(type)
klass = AssetTypes.const_get(type)
end
klass = nil if allowed_types && !allowed_types.include?(klass.to_s.underscore.to_sym)
return nil unless klass
res = nil
if klass == WikiPage
res = context.wiki.wiki_pages.find_by_id(id)
elsif (klass.column_names & ['context_id', 'context_type']).length == 2
res = klass.find_by_context_id_and_context_type_and_id(context.id, context.class.to_s, id)
else
res = klass.find_by_id(id)
res = nil if context && res && res.respond_to?(:context) && res.context != context
end
res
rescue => e
nil
end
2011-02-01 09:57:29 +08:00
def is_a_context?
true
end
end