2020-10-27 00:46:40 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-11 12:40:45 +08:00
|
|
|
#
|
2017-05-19 07:19:20 +08:00
|
|
|
# Copyright (C) 2017 - present Instructure, Inc.
|
2017-05-11 12:40:45 +08:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
#
|
|
|
|
|
|
|
|
class PlannerOverride < ActiveRecord::Base
|
|
|
|
include Workflow
|
2018-07-10 07:30:04 +08:00
|
|
|
|
2018-10-09 04:42:18 +08:00
|
|
|
CONTENT_TYPES = PlannerHelper::PLANNABLE_TYPES.values
|
2018-07-10 07:30:04 +08:00
|
|
|
|
|
|
|
before_validation :link_to_parent_topic, :link_to_submittable
|
|
|
|
|
2017-08-01 04:30:37 +08:00
|
|
|
belongs_to :plannable, polymorphic:
|
2023-04-29 04:59:51 +08:00
|
|
|
[:announcement,
|
|
|
|
:assignment,
|
|
|
|
:discussion_topic,
|
|
|
|
:planner_note,
|
|
|
|
:wiki_page,
|
|
|
|
:calendar_event,
|
|
|
|
:assessment_request,
|
2018-07-17 03:42:17 +08:00
|
|
|
quiz: "Quizzes::Quiz"]
|
2017-05-11 12:40:45 +08:00
|
|
|
belongs_to :user
|
2018-07-10 07:30:04 +08:00
|
|
|
validates :plannable_id, :plannable_type, :workflow_state, :user_id, presence: true
|
|
|
|
validates :plannable_id, uniqueness: { scope: [:user_id, :plannable_type] }
|
2017-05-11 12:40:45 +08:00
|
|
|
|
|
|
|
scope :active, -> { where workflow_state: "active" }
|
|
|
|
scope :deleted, -> { where workflow_state: "deleted" }
|
|
|
|
scope :not_deleted, -> { where.not deleted }
|
2023-06-02 06:06:09 +08:00
|
|
|
scope :for_user, ->(user) { where user: }
|
2017-05-11 12:40:45 +08:00
|
|
|
|
|
|
|
workflow do
|
|
|
|
state :active do
|
|
|
|
event :unpublish, transitions_to: :unpublished
|
|
|
|
end
|
|
|
|
state :unpublished do
|
|
|
|
event :publish, transitions_to: :active
|
|
|
|
end
|
|
|
|
state :deleted
|
|
|
|
end
|
|
|
|
|
2018-07-10 07:30:04 +08:00
|
|
|
alias_method :published?, :active?
|
|
|
|
|
|
|
|
def link_to_parent_topic
|
|
|
|
return unless plannable_type == "DiscussionTopic"
|
2021-09-23 00:20:17 +08:00
|
|
|
|
2018-07-10 07:30:04 +08:00
|
|
|
plannable = DiscussionTopic.find(plannable_id)
|
|
|
|
self.plannable_id = plannable.root_topic_id if plannable.root_topic_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def link_to_submittable
|
|
|
|
return unless plannable_type == "Assignment"
|
2021-09-23 00:20:17 +08:00
|
|
|
|
2018-07-10 07:30:04 +08:00
|
|
|
plannable = Assignment.find_by(id: plannable_id)
|
|
|
|
if plannable&.quiz?
|
2018-10-09 04:42:18 +08:00
|
|
|
self.plannable_type = PlannerHelper::PLANNABLE_TYPES["quiz"]
|
2018-07-10 07:30:04 +08:00
|
|
|
self.plannable_id = plannable.quiz.id
|
|
|
|
elsif plannable&.discussion_topic?
|
2018-10-09 04:42:18 +08:00
|
|
|
self.plannable_type = PlannerHelper::PLANNABLE_TYPES["discussion_topic"]
|
2018-07-10 07:30:04 +08:00
|
|
|
self.plannable_id = plannable.discussion_topic.id
|
|
|
|
elsif plannable&.wiki_page?
|
2018-10-09 04:42:18 +08:00
|
|
|
self.plannable_type = PlannerHelper::PLANNABLE_TYPES["wiki_page"]
|
2018-07-10 07:30:04 +08:00
|
|
|
self.plannable_id = plannable.wiki_page.id
|
|
|
|
end
|
|
|
|
end
|
2017-05-11 12:40:45 +08:00
|
|
|
|
2018-08-18 06:05:56 +08:00
|
|
|
def associated_assignment_id
|
2018-10-23 23:59:52 +08:00
|
|
|
return plannable_id if plannable_type == "Assignment"
|
2021-09-23 00:20:17 +08:00
|
|
|
|
2018-08-18 06:05:56 +08:00
|
|
|
plannable.assignment_id if plannable.respond_to? :assignment_id
|
|
|
|
end
|
|
|
|
|
2017-05-11 12:40:45 +08:00
|
|
|
def self.update_for(obj)
|
|
|
|
overrides = PlannerOverride.where(plannable_id: obj.id, plannable_type: obj.class.to_s)
|
|
|
|
overrides.update_all(workflow_state: plannable_workflow_state(obj)) if overrides.exists?
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.plannable_workflow_state(plannable)
|
|
|
|
if plannable.respond_to?(:published?)
|
|
|
|
if plannable.respond_to?(:deleted?) && plannable.deleted?
|
|
|
|
"deleted"
|
|
|
|
elsif plannable.published?
|
|
|
|
"active"
|
|
|
|
else
|
|
|
|
"unpublished"
|
|
|
|
end
|
|
|
|
elsif plannable.respond_to?(:workflow_state)
|
|
|
|
workflow_state = plannable.workflow_state.to_s
|
|
|
|
if %w[active available published].include?(workflow_state)
|
|
|
|
"active"
|
|
|
|
elsif ["unpublished", "deleted"].include?(workflow_state)
|
|
|
|
workflow_state
|
|
|
|
end
|
2021-11-22 23:49:53 +08:00
|
|
|
else
|
2017-05-19 07:19:20 +08:00
|
|
|
"unpublished"
|
2017-05-11 12:40:45 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def plannable_workflow_state
|
|
|
|
PlannerOverride.plannable_workflow_state(plannable)
|
|
|
|
end
|
|
|
|
|
|
|
|
alias_method :destroy_permanently!, :destroy
|
|
|
|
def destroy
|
|
|
|
self.workflow_state = "deleted"
|
|
|
|
self.deleted_at = Time.now.utc
|
|
|
|
save
|
|
|
|
end
|
|
|
|
end
|