0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-25 02:30:28 +01:00
discourse-custom-wizard/lib/custom_wizard/wizard.rb

289 Zeilen
6,8 KiB
Ruby

2017-10-15 05:58:22 +02:00
require_dependency 'wizard/step'
require_dependency 'wizard/field'
require_dependency 'wizard/step_updater'
require_dependency 'wizard/builder'
UserHistory.actions[:custom_wizard_step] = 1000
2017-09-23 04:34:07 +02:00
class CustomWizard::Wizard
include ActiveModel::SerializerSupport
2017-09-23 04:34:07 +02:00
2017-11-01 05:21:14 +01:00
attr_accessor :id,
:name,
:background,
2020-04-13 14:17:22 +02:00
:theme_id,
2017-11-01 05:21:14 +01:00
:save_submissions,
:multiple_submissions,
:after_time,
:after_time_scheduled,
2017-11-01 05:21:14 +01:00
:after_signup,
2017-11-22 10:34:21 +01:00
:required,
:prompt_completion,
:restart_on_revisit,
2020-03-30 08:16:03 +02:00
:permitted,
2020-03-21 18:30:11 +01:00
:needs_categories,
2020-04-13 14:17:22 +02:00
:needs_groups,
:steps,
2020-10-31 08:05:50 +01:00
:step_ids,
2020-04-13 14:17:22 +02:00
:actions,
2020-11-03 01:24:20 +01:00
:user,
:first_step
2017-10-15 05:58:22 +02:00
2020-04-13 14:17:22 +02:00
def initialize(attrs = {}, user=nil)
2017-10-15 05:58:22 +02:00
@user = user
2020-11-03 01:24:20 +01:00
attrs = attrs.with_indifferent_access
2020-04-13 14:17:22 +02:00
@id = attrs['id']
@name = attrs['name']
@background = attrs['background']
2020-11-03 01:24:20 +01:00
@save_submissions = cast_bool(attrs['save_submissions'])
@multiple_submissions = cast_bool(attrs['multiple_submissions'])
@prompt_completion = cast_bool(attrs['prompt_completion'])
@restart_on_revisit = cast_bool(attrs['restart_on_revisit'])
@after_signup = cast_bool(attrs['after_signup'])
@after_time = cast_bool(attrs['after_time'])
2020-04-13 14:17:22 +02:00
@after_time_scheduled = attrs['after_time_scheduled']
2020-11-03 01:24:20 +01:00
@required = cast_bool(attrs['required'])
2020-04-13 14:17:22 +02:00
@permitted = attrs['permitted'] || nil
@needs_categories = false
2020-03-21 18:30:11 +01:00
@needs_groups = false
2020-04-13 14:17:22 +02:00
@theme_id = attrs['theme_id']
2020-04-19 08:42:44 +02:00
if attrs['theme'].present?
theme = ::Theme.find_by(name: attrs['theme'])
2020-04-13 14:17:22 +02:00
@theme_id = theme.id if theme
2017-11-01 05:21:14 +01:00
end
2020-04-13 14:17:22 +02:00
@first_step = nil
@steps = []
2020-11-03 01:24:20 +01:00
if attrs['steps'].present?
@step_ids = attrs['steps'].map { |s| s['id'] }
end
2020-04-13 14:17:22 +02:00
@actions = []
2017-10-15 05:58:22 +02:00
end
2020-11-03 01:24:20 +01:00
def cast_bool(val)
val.nil? ? false : ActiveRecord::Type::Boolean.new.cast(val)
end
2017-10-15 05:58:22 +02:00
def create_step(step_name)
::Wizard::Step.new(step_name)
end
def append_step(step)
step = create_step(step) if step.is_a?(String)
2020-04-13 14:17:22 +02:00
2017-10-15 05:58:22 +02:00
yield step if block_given?
2020-10-31 08:05:50 +01:00
last_step = steps.last
steps << step
2020-11-03 01:24:20 +01:00
2020-10-31 08:05:50 +01:00
if steps.size == 1
2020-11-03 01:24:20 +01:00
@first_step = step
2017-10-15 05:58:22 +02:00
step.index = 0
elsif last_step.present?
last_step.next = step
step.previous = last_step
step.index = last_step.index + 1
end
end
def start
2020-10-31 08:05:50 +01:00
return nil if !user
2019-06-19 07:23:10 +02:00
2017-11-13 08:49:08 +01:00
if unfinished? && last_completed_step = ::UserHistory.where(
2020-10-31 08:05:50 +01:00
acting_user_id: user.id,
2017-11-08 09:52:50 +01:00
action: ::UserHistory.actions[:custom_wizard_step],
2020-10-31 08:05:50 +01:00
context: id,
subject: steps.map(&:id)
2017-11-13 08:49:08 +01:00
).order("created_at").last
step_id = last_completed_step.subject
2020-10-31 08:05:50 +01:00
last_index = steps.index { |s| s.id == step_id }
steps[last_index + 1]
2017-11-08 09:52:50 +01:00
else
2020-11-03 01:24:20 +01:00
@first_step
2017-10-15 05:58:22 +02:00
end
end
2020-11-03 01:24:20 +01:00
def create_updater(step_id, inputs)
2017-11-01 05:21:14 +01:00
step = @steps.find { |s| s.id == step_id }
wizard = self
2020-11-03 01:24:20 +01:00
CustomWizard::StepUpdater.new(user, wizard, step, inputs)
2017-11-01 05:21:14 +01:00
end
2017-10-15 05:58:22 +02:00
2017-11-01 05:21:14 +01:00
def unfinished?
2020-10-31 08:05:50 +01:00
return nil if !user
2019-06-19 07:23:10 +02:00
2017-11-01 05:21:14 +01:00
most_recent = ::UserHistory.where(
2020-10-31 08:05:50 +01:00
acting_user_id: user.id,
2017-10-22 05:37:58 +02:00
action: ::UserHistory.actions[:custom_wizard_step],
2020-10-31 08:05:50 +01:00
context: id,
2017-11-02 05:15:30 +01:00
).distinct.order('updated_at DESC').first
2017-11-01 05:21:14 +01:00
2019-01-22 00:57:03 +01:00
if most_recent && most_recent.subject == "reset"
2019-01-14 03:53:53 +01:00
false
elsif most_recent
2020-11-03 01:24:20 +01:00
most_recent.subject != steps.last.id
2017-11-01 05:21:14 +01:00
else
true
end
end
def completed?
2020-10-31 08:05:50 +01:00
return nil if !user
2020-04-13 14:17:22 +02:00
2017-11-01 05:21:14 +01:00
history = ::UserHistory.where(
2020-10-31 08:05:50 +01:00
acting_user_id: user.id,
2017-11-01 05:21:14 +01:00
action: ::UserHistory.actions[:custom_wizard_step],
2020-10-31 08:05:50 +01:00
context: id
2017-11-01 05:21:14 +01:00
)
2020-10-31 08:05:50 +01:00
if after_time
history = history.where("updated_at > ?", after_time_scheduled)
2017-11-01 05:21:14 +01:00
end
2020-11-03 01:24:20 +01:00
2017-11-01 05:21:14 +01:00
completed = history.distinct.order(:subject).pluck(:subject)
2020-10-31 08:05:50 +01:00
(step_ids - completed).empty?
2017-10-15 05:58:22 +02:00
end
2017-11-29 10:48:49 +01:00
def permitted?
2020-03-30 08:16:03 +02:00
return false unless user
return true if user.admin? || permitted.blank?
2020-04-06 10:36:38 +02:00
mapper = CustomWizard::Mapper.new(
inputs: permitted,
user: user,
opts: {
with_type: true,
multiple: true
}
).perform
return true if mapper.blank?
mapper.all? do |m|
2020-04-07 09:54:30 +02:00
if m[:type] === 'assignment'
GroupUser.exists?(group_id: m[:result], user_id: user.id)
elsif m[:type] === 'validation'
m[:result]
2020-04-06 10:36:38 +02:00
else
true
end
end
2017-11-29 10:48:49 +01:00
end
2020-04-13 14:17:22 +02:00
def can_access?
2020-11-03 01:24:20 +01:00
return false unless user
2020-04-13 14:17:22 +02:00
return true if user.admin
2020-04-14 07:46:06 +02:00
return permitted? && (multiple_submissions || !completed?)
2020-04-13 14:17:22 +02:00
end
2017-11-29 10:48:49 +01:00
2019-01-14 03:53:53 +01:00
def reset
::UserHistory.create(
action: ::UserHistory.actions[:custom_wizard_step],
2020-10-31 08:05:50 +01:00
acting_user_id: user.id,
context: id,
2019-01-14 03:53:53 +01:00
subject: "reset"
)
end
def categories
2020-10-31 08:05:50 +01:00
@categories ||= ::Site.new(Guardian.new(user)).categories
end
2020-03-21 18:30:11 +01:00
def groups
2020-10-31 08:05:50 +01:00
@groups ||= ::Site.new(Guardian.new(user)).groups
2020-03-21 18:30:11 +01:00
end
2020-03-30 08:16:03 +02:00
2020-04-13 14:17:22 +02:00
def submissions
2020-10-31 08:05:50 +01:00
Array.wrap(PluginStore.get("#{id}_submissions", user.id))
end
2020-11-03 01:24:20 +01:00
def current_submission
if submissions.present? && !submissions.last.key?("submitted_at")
submissions.last
else
nil
end
end
def set_submissions(submissions)
PluginStore.set("#{id}_submissions", user.id, Array.wrap(submissions))
end
def self.submissions(wizard_id, user)
new({ id: wizard_id }, user).submissions
end
def self.set_submissions(wizard_id, user, submissions)
new({ id: wizard_id }, user).set_submissions(submissions)
end
2020-10-31 08:05:50 +01:00
def self.create(wizard_id, user = nil)
if template = CustomWizard::Template.find(wizard_id)
2020-11-03 01:24:20 +01:00
new(template.to_h, user)
2020-10-31 08:05:50 +01:00
else
false
end
2020-04-13 14:17:22 +02:00
end
2020-11-03 01:24:20 +01:00
def self.list(user, template_opts: {})
return [] unless user
CustomWizard::Template.list(template_opts).reduce([]) do |result, template|
wizard = new(template, user)
result.push(wizard) if wizard.can_access?
result
end
2020-03-30 08:16:03 +02:00
end
def self.after_signup(user)
2020-11-03 01:24:20 +01:00
wizards = list(
user,
template_opts: {
setting: 'after_signup',
order: "(value::json ->> 'permitted') IS NOT NULL DESC"
}
)
wizards.any? ? wizards.first : false
2017-10-15 05:58:22 +02:00
end
2017-11-22 10:34:21 +01:00
def self.prompt_completion(user)
2020-11-03 01:24:20 +01:00
wizards = list(
user,
template_opts: {
setting: 'prompt_completion',
order: "(value::json ->> 'permitted') IS NOT NULL DESC"
}
)
if wizards.any?
wizards.map do |w|
{
id: w.id,
name: w.name
}
2017-11-22 10:34:21 +01:00
end
else
false
end
end
def self.set_submission_redirect(user, wizard_id, url)
2018-05-24 07:32:22 +02:00
PluginStore.set("#{wizard_id.underscore}_submissions", user.id, [{ redirect_to: url }])
end
2020-04-13 14:17:22 +02:00
def self.set_wizard_redirect(wizard_id, user)
2020-04-19 08:42:44 +02:00
wizard = self.create(wizard_id, user)
if wizard.permitted?
user.custom_fields['redirect_to_wizard'] = wizard_id
user.save_custom_fields(true)
else
false
end
end
2017-09-23 04:34:07 +02:00
end