2020-10-27 00:50:13 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-02-07 08:12:51 +08:00
|
|
|
# Copyright (C) 2013 - present 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/>.
|
|
|
|
#
|
|
|
|
|
|
|
|
module Outcomes
|
|
|
|
module Import
|
2018-02-25 11:26:55 +08:00
|
|
|
class InvalidDataError < RuntimeError; end
|
2021-09-23 00:25:11 +08:00
|
|
|
|
2018-04-24 08:33:25 +08:00
|
|
|
class DataFormatError < RuntimeError; end
|
2018-02-07 08:12:51 +08:00
|
|
|
|
2018-02-13 06:56:58 +08:00
|
|
|
OBJECT_ONLY_FIELDS = %i[calculation_method calculation_int ratings].freeze
|
2018-04-03 01:08:11 +08:00
|
|
|
VALID_WORKFLOWS = [nil, '', 'active', 'deleted'].freeze
|
2018-02-13 06:56:58 +08:00
|
|
|
|
|
|
|
def check_object(object)
|
2018-02-25 11:26:55 +08:00
|
|
|
%i[vendor_guid title].each do |field|
|
|
|
|
next if object[field].present?
|
2021-09-23 00:25:11 +08:00
|
|
|
|
2018-02-25 11:26:55 +08:00
|
|
|
raise InvalidDataError, I18n.t(
|
|
|
|
'The "%{field}" field is required', field: field
|
|
|
|
)
|
|
|
|
end
|
|
|
|
if object[:vendor_guid].match?(/\s/)
|
|
|
|
raise InvalidDataError, I18n.t(
|
|
|
|
'The "%{field}" field must have no spaces',
|
|
|
|
field: 'vendor_guid'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
unless VALID_WORKFLOWS.include? object[:workflow_state]
|
|
|
|
raise InvalidDataError, I18n.t(
|
|
|
|
'"%{field}" must be either "%{active}" or "%{deleted}"',
|
|
|
|
field: 'workflow_state',
|
|
|
|
active: 'active',
|
|
|
|
deleted: 'deleted'
|
|
|
|
)
|
|
|
|
end
|
2018-02-13 06:56:58 +08:00
|
|
|
end
|
|
|
|
|
2018-02-07 08:12:51 +08:00
|
|
|
def import_object(object)
|
2018-02-13 06:56:58 +08:00
|
|
|
check_object(object)
|
|
|
|
|
2018-02-07 08:12:51 +08:00
|
|
|
type = object[:object_type]
|
|
|
|
if type == 'outcome'
|
|
|
|
import_outcome(object)
|
|
|
|
elsif type == 'group'
|
|
|
|
import_group(object)
|
|
|
|
else
|
2018-02-25 11:26:55 +08:00
|
|
|
raise InvalidDataError, I18n.t(
|
|
|
|
'Invalid %{field}: "%{type}"',
|
|
|
|
field: 'object_type',
|
|
|
|
type: type
|
|
|
|
)
|
2018-02-07 08:12:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_group(group)
|
2018-02-13 06:56:58 +08:00
|
|
|
invalid = group.keys.select do |k|
|
|
|
|
group[k].present? && OBJECT_ONLY_FIELDS.include?(k)
|
|
|
|
end
|
2018-02-25 11:26:55 +08:00
|
|
|
if invalid.present?
|
|
|
|
raise InvalidDataError, I18n.t(
|
|
|
|
'Invalid fields for a group: %{invalid}',
|
|
|
|
invalid: invalid.map(&:to_s).inspect
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-02-07 08:12:51 +08:00
|
|
|
parents = find_parents(group)
|
2018-02-25 11:26:55 +08:00
|
|
|
raise InvalidDataError, I18n.t("An outcome group can only have one parent") if parents.length > 1
|
2021-09-23 00:25:11 +08:00
|
|
|
|
2018-02-07 08:12:51 +08:00
|
|
|
parent = parents.first
|
|
|
|
|
2018-02-25 11:26:55 +08:00
|
|
|
model = find_prior_group(group)
|
|
|
|
unless model.context == context
|
|
|
|
raise InvalidDataError, I18n.t(
|
2018-08-22 02:01:07 +08:00
|
|
|
'Group with ID %{guid} already exists in another unrelated course or account (%{name})',
|
|
|
|
guid: group[:vendor_guid],
|
|
|
|
name: model.context.name
|
2018-02-25 11:26:55 +08:00
|
|
|
)
|
|
|
|
end
|
2018-03-17 03:32:06 +08:00
|
|
|
if model.outcome_import_id == outcome_import_id
|
|
|
|
raise InvalidDataError, I18n.t(
|
|
|
|
'Group "%{guid}" has already appeared in this import',
|
|
|
|
guid: group[:vendor_guid]
|
|
|
|
)
|
|
|
|
end
|
2018-02-25 11:26:55 +08:00
|
|
|
model.vendor_guid = group[:vendor_guid]
|
|
|
|
model.title = group[:title]
|
|
|
|
model.description = group[:description] || ''
|
2018-04-03 01:08:11 +08:00
|
|
|
model.workflow_state = group[:workflow_state].presence || 'active'
|
2018-02-25 11:26:55 +08:00
|
|
|
model.learning_outcome_group = parent
|
|
|
|
model.outcome_import_id = outcome_import_id
|
|
|
|
model.save!
|
|
|
|
|
|
|
|
if model.workflow_state == 'deleted'
|
|
|
|
model.destroy!
|
|
|
|
end
|
|
|
|
|
|
|
|
model
|
2018-02-07 08:12:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def import_outcome(outcome)
|
|
|
|
parents = find_parents(outcome)
|
|
|
|
|
2018-02-25 11:26:55 +08:00
|
|
|
model = find_prior_outcome(outcome)
|
2018-03-17 03:05:16 +08:00
|
|
|
model.context = context if model.new_record?
|
2018-02-25 11:26:55 +08:00
|
|
|
unless context_visible?(model.context)
|
|
|
|
raise InvalidDataError, I18n.t(
|
2018-08-22 02:01:07 +08:00
|
|
|
'Outcome with ID %{guid} already exists in another unrelated course or account (%{name})',
|
|
|
|
guid: outcome[:vendor_guid],
|
|
|
|
name: model.context.name
|
2018-02-25 11:26:55 +08:00
|
|
|
)
|
|
|
|
end
|
2018-03-17 03:32:06 +08:00
|
|
|
if model.outcome_import_id == outcome_import_id
|
|
|
|
raise InvalidDataError, I18n.t(
|
|
|
|
'Outcome "%{guid}" has already appeared in this import',
|
|
|
|
guid: outcome[:vendor_guid]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-02-25 11:26:55 +08:00
|
|
|
model.vendor_guid = outcome[:vendor_guid]
|
|
|
|
model.title = outcome[:title]
|
2018-04-25 03:50:49 +08:00
|
|
|
model.description = infer_nil_value(model, :description, outcome)
|
|
|
|
model.display_name = infer_nil_value(model, :display_name, outcome)
|
2018-10-05 02:41:21 +08:00
|
|
|
model.calculation_method = outcome[:calculation_method].presence || model.default_calculation_method
|
|
|
|
model.calculation_int = outcome[:calculation_int].presence || model.default_calculation_int
|
2018-03-24 04:35:37 +08:00
|
|
|
# let removing the outcome_links content tags delete the underlying outcome
|
2019-10-16 01:40:20 +08:00
|
|
|
model.workflow_state = 'active' unless outcome[:workflow_state] == 'deleted'
|
2018-02-25 11:26:55 +08:00
|
|
|
|
|
|
|
prior_rubric = model.rubric_criterion || {}
|
2018-03-09 03:42:32 +08:00
|
|
|
changed = ->(k) { outcome[k].present? && outcome[k] != prior_rubric[k] }
|
|
|
|
rubric_change = changed.call(:ratings) || changed.call(:mastery_points)
|
|
|
|
model.rubric_criterion = create_rubric(outcome[:ratings], outcome[:mastery_points]) if rubric_change
|
2018-02-25 11:26:55 +08:00
|
|
|
|
|
|
|
if model.context == context
|
|
|
|
model.outcome_import_id = outcome_import_id
|
|
|
|
model.save!
|
2018-04-07 02:17:40 +08:00
|
|
|
elsif non_vendor_guid_changes?(model)
|
2018-02-25 11:26:55 +08:00
|
|
|
raise InvalidDataError, I18n.t(
|
2018-03-17 03:05:16 +08:00
|
|
|
'Cannot modify outcome from another context: %{changes}; outcome must be modified in %{context}',
|
|
|
|
changes: model.changes.keys.inspect,
|
|
|
|
context: if model.context.present?
|
|
|
|
I18n.t('"%{name}"', name: model.context.name)
|
|
|
|
else
|
|
|
|
I18n.t('the global context')
|
|
|
|
end
|
2018-02-25 11:26:55 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
parents = [] if outcome[:workflow_state] == 'deleted'
|
|
|
|
update_outcome_parents(model, parents)
|
|
|
|
|
|
|
|
model
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-04-25 03:50:49 +08:00
|
|
|
# There is no representation for `nil` in the export format. All nil values
|
|
|
|
# are seen as "" when re-imported. We don't want to change those values when
|
|
|
|
# we re-import (and we want to be able to link to those values from other
|
|
|
|
# contexts), so we infer cases where `nil` was probably exported as ""
|
|
|
|
def infer_nil_value(model, key, outcome)
|
|
|
|
prior = model.send(key)
|
|
|
|
current = outcome[key]
|
|
|
|
if current.blank? && prior.blank?
|
|
|
|
prior
|
|
|
|
else
|
|
|
|
current
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-07 02:17:40 +08:00
|
|
|
def non_vendor_guid_changes?(model)
|
|
|
|
model.has_changes_to_save? && !(model.changes_to_save.length == 1 &&
|
2019-12-20 06:26:22 +08:00
|
|
|
model.changes_to_save.key?('vendor_guid'))
|
2018-04-07 02:17:40 +08:00
|
|
|
end
|
|
|
|
|
2018-02-25 11:26:55 +08:00
|
|
|
def find_prior_outcome(outcome)
|
2018-03-31 01:46:20 +08:00
|
|
|
match = /canvas_outcome:(\d+)/.match(outcome[:vendor_guid])
|
|
|
|
if match
|
|
|
|
canvas_id = match[1]
|
2018-02-25 11:26:55 +08:00
|
|
|
begin
|
2018-03-31 01:46:20 +08:00
|
|
|
LearningOutcome.find(canvas_id)
|
2018-02-25 11:26:55 +08:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
raise InvalidDataError, I18n.t(
|
|
|
|
'Outcome with canvas id "%{id}" not found',
|
|
|
|
id: outcome[:canvas_id]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
else
|
2018-04-04 00:50:42 +08:00
|
|
|
vendor_guid = outcome[:vendor_guid]
|
2019-12-20 06:26:22 +08:00
|
|
|
prior = LearningOutcome.where(vendor_guid: vendor_guid).active_first.first
|
2018-04-04 00:50:42 +08:00
|
|
|
return prior if prior
|
2021-09-23 00:25:11 +08:00
|
|
|
|
2018-04-04 00:50:42 +08:00
|
|
|
LearningOutcome.new(vendor_guid: vendor_guid)
|
2018-02-25 11:26:55 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_prior_group(group)
|
2018-04-04 00:50:42 +08:00
|
|
|
vendor_guid = group[:vendor_guid]
|
2019-12-20 06:26:22 +08:00
|
|
|
prior = LearningOutcomeGroup.where(context: context).where(vendor_guid: vendor_guid).active_first.first
|
2018-03-31 01:46:20 +08:00
|
|
|
return prior if prior
|
|
|
|
|
2019-12-20 06:26:22 +08:00
|
|
|
match = /canvas_outcome_group:(\d+)/.match(vendor_guid)
|
2018-03-31 01:46:20 +08:00
|
|
|
if match
|
|
|
|
canvas_id = match[1]
|
2018-02-25 11:26:55 +08:00
|
|
|
begin
|
2018-03-31 01:46:20 +08:00
|
|
|
by_id = LearningOutcomeGroup.find(canvas_id)
|
|
|
|
return by_id if by_id.context == context
|
2018-02-25 11:26:55 +08:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
raise InvalidDataError, I18n.t(
|
|
|
|
'Outcome group with canvas id %{id} not found',
|
|
|
|
id: group[:canvas_id]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2018-03-31 01:46:20 +08:00
|
|
|
|
2018-04-04 00:50:42 +08:00
|
|
|
LearningOutcomeGroup.new(vendor_guid: vendor_guid, context: context)
|
2018-02-07 08:12:51 +08:00
|
|
|
end
|
|
|
|
|
2018-03-09 03:42:32 +08:00
|
|
|
def create_rubric(ratings, mastery_points)
|
2018-02-07 08:12:51 +08:00
|
|
|
rubric = {}
|
|
|
|
rubric[:enable] = true
|
2018-03-09 03:42:32 +08:00
|
|
|
rubric[:mastery_points] = mastery_points
|
2018-02-07 08:12:51 +08:00
|
|
|
rubric[:ratings] = ratings.map.with_index { |v, i| [i, v] }.to_h
|
|
|
|
rubric
|
|
|
|
end
|
|
|
|
|
|
|
|
def root_parent
|
2018-03-01 04:27:29 +08:00
|
|
|
@root ||= LearningOutcomeGroup.find_or_create_root(context, true)
|
2018-02-07 08:12:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_parents(object)
|
2021-09-28 14:33:30 +08:00
|
|
|
if object[:parent_guids].nil? || object[:parent_guids].blank?
|
|
|
|
group = [LearningOutcomeGroup.find(object[:learning_outcome_group_id])] if object[:learning_outcome_group_id]
|
|
|
|
group ||= [root_parent]
|
|
|
|
|
|
|
|
return group
|
|
|
|
end
|
2018-02-07 08:12:51 +08:00
|
|
|
|
|
|
|
guids = object[:parent_guids].strip.split
|
2021-09-23 00:25:11 +08:00
|
|
|
LearningOutcomeGroup.where(context: context, outcome_import_id: outcome_import_id)
|
|
|
|
.where(vendor_guid: guids)
|
|
|
|
.tap do |parents|
|
|
|
|
if parents.length < guids.length
|
|
|
|
missing = guids - parents.map(&:vendor_guid)
|
|
|
|
raise InvalidDataError, I18n.t(
|
|
|
|
'Parent references not found prior to this row: %{missing}',
|
|
|
|
missing: missing.inspect,
|
|
|
|
)
|
2018-02-25 11:26:55 +08:00
|
|
|
end
|
2021-09-23 00:25:11 +08:00
|
|
|
end
|
2018-02-07 08:12:51 +08:00
|
|
|
end
|
|
|
|
|
2018-02-25 11:26:55 +08:00
|
|
|
def context_visible?(other_context)
|
2018-03-17 03:05:16 +08:00
|
|
|
return true if other_context.nil?
|
2021-09-23 00:25:11 +08:00
|
|
|
|
2018-02-25 11:26:55 +08:00
|
|
|
other_context == context || context.account_chain.include?(other_context)
|
|
|
|
end
|
2018-02-13 06:56:58 +08:00
|
|
|
|
2018-02-25 11:26:55 +08:00
|
|
|
def update_outcome_parents(outcome, parents)
|
|
|
|
existing_links = ContentTag.learning_outcome_links.where(context: context, content: outcome)
|
2018-02-13 06:56:58 +08:00
|
|
|
|
2018-03-24 04:35:37 +08:00
|
|
|
next_parent_ids = parents.map(&:id)
|
2021-09-23 00:25:11 +08:00
|
|
|
resurrect = existing_links
|
|
|
|
.where(associated_asset_id: next_parent_ids)
|
|
|
|
.where(associated_asset_type: 'LearningOutcomeGroup')
|
|
|
|
.where(workflow_state: 'deleted')
|
2018-03-24 04:35:37 +08:00
|
|
|
resurrect.update_all(workflow_state: 'active')
|
|
|
|
|
fix moving outcomes via csv import
closes OUT-3426
flag = none
Test plan:
- save the sample data at the bottom of
http://canvas.docker/doc/api/file.outcomes_csv.html
to a csv file
- on the account outcomes page, import
the file's outcomes into the account
- update the file with a second group
d,group,Child group 2,child group description,G-1.2,,,active,a,
- update the outcome to belong to the new group
c,outcome,...,active,d,3,...
- verify that update completes successfully
and outcome has moved to theh newly
created "Child group 2"
Change-Id: Ie8f26497bb58bf513b2fc6734be213601e2442b4
Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/221207
Tested-by: Jenkins
Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com>
Reviewed-by: Frank Murphy <fmurphy@instructure.com>
QA-Review: Pat Renner <prenner@instructure.com>
Product-Review: Michael Brewer-Davis <mbd@instructure.com>
2019-12-18 06:02:05 +08:00
|
|
|
# add new parents before removing old to avoid deleting last link
|
|
|
|
# to an aligned outcome
|
|
|
|
existing_parent_ids = existing_links.pluck(:associated_asset_id)
|
|
|
|
new_parents = parents.reject { |p| existing_parent_ids.include?(p.id) }
|
|
|
|
new_parents.each { |p| p.add_outcome(outcome) }
|
|
|
|
|
2021-09-23 00:25:11 +08:00
|
|
|
kill = existing_links
|
|
|
|
.where.not(associated_asset_id: next_parent_ids)
|
|
|
|
.where(associated_asset_type: 'LearningOutcomeGroup')
|
|
|
|
.where(workflow_state: 'active')
|
2018-03-24 04:35:37 +08:00
|
|
|
kill.destroy_all
|
2018-02-25 11:26:55 +08:00
|
|
|
end
|
2018-02-13 06:56:58 +08:00
|
|
|
|
2018-02-25 11:26:55 +08:00
|
|
|
def outcome_import_id
|
2018-03-17 03:05:16 +08:00
|
|
|
@outcome_import_id ||= @import&.id || SecureRandom.random_number(2**32)
|
2018-02-13 06:56:58 +08:00
|
|
|
end
|
|
|
|
end
|
2018-02-07 08:12:51 +08:00
|
|
|
end
|