1
0
Fork 0
discourse-custom-wizard-unl.../lib/custom_wizard/wizard.rb

368 Zeilen
9 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,
:actions,
:user
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-04-13 14:17:22 +02:00
@id = attrs['id']
@name = attrs['name']
@background = attrs['background']
@save_submissions = attrs['save_submissions'] || false
@multiple_submissions = attrs['multiple_submissions'] || false
@prompt_completion = attrs['prompt_completion'] || false
@restart_on_revisit = attrs['restart_on_revisit'] || false
@after_signup = attrs['after_signup']
@after_time = attrs['after_time']
@after_time_scheduled = attrs['after_time_scheduled']
@required = attrs['required'] || false
@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']
if attrs['theme']
theme = Theme.find_by(name: attrs['theme'])
@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 = []
@actions = []
2017-10-15 05:58:22 +02:00
end
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?
last_step = @steps.last
@steps << step
2020-04-13 14:17:22 +02:00
2017-10-15 05:58:22 +02:00
# If it's the first step
if @steps.size == 1
@first_step = step
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
2019-06-19 07:23:10 +02:00
return nil if !@user
2017-11-13 08:49:08 +01:00
if unfinished? && last_completed_step = ::UserHistory.where(
2017-11-08 09:52:50 +01:00
acting_user_id: @user.id,
action: ::UserHistory.actions[:custom_wizard_step],
context: @id,
subject: @steps.map(&:id)
2017-11-13 08:49:08 +01:00
).order("created_at").last
step_id = last_completed_step.subject
2017-11-09 03:50:48 +01:00
last_index = @steps.index { |s| s.id == step_id }
@steps[last_index + 1]
2017-11-08 09:52:50 +01:00
else
@first_step
2017-10-15 05:58:22 +02:00
end
end
2017-11-01 05:21:14 +01:00
def create_updater(step_id, fields)
step = @steps.find { |s| s.id == step_id }
wizard = self
CustomWizard::StepUpdater.new(@user, wizard, step, fields)
end
2017-10-15 05:58:22 +02:00
2017-11-01 05:21:14 +01:00
def unfinished?
2019-06-19 07:23:10 +02:00
return nil if !@user
2017-11-01 05:21:14 +01:00
most_recent = ::UserHistory.where(
2017-10-15 05:58:22 +02:00
acting_user_id: @user.id,
2017-10-22 05:37:58 +02:00
action: ::UserHistory.actions[:custom_wizard_step],
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
2017-11-01 05:21:14 +01:00
last_finished_step = most_recent.subject
last_step = CustomWizard::Wizard.step_ids(@id).last
last_finished_step != last_step
else
true
end
end
def completed?
2019-06-19 07:23:10 +02:00
return nil if !@user
2020-04-13 14:17:22 +02:00
2017-11-01 05:21:14 +01:00
steps = CustomWizard::Wizard.step_ids(@id)
2017-10-15 05:58:22 +02:00
2017-11-01 05:21:14 +01:00
history = ::UserHistory.where(
acting_user_id: @user.id,
action: ::UserHistory.actions[:custom_wizard_step],
context: @id
)
if @after_time
history = history.where("updated_at > ?", @after_time_scheduled)
2017-11-01 05:21:14 +01:00
end
completed = history.distinct.order(:subject).pluck(:subject)
(steps - 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?
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],
acting_user_id: @user.id,
context: @id,
subject: "reset"
)
end
def categories
@categories ||= ::Site.new(Guardian.new(@user)).categories
end
2020-03-21 18:30:11 +01:00
def groups
@groups ||= ::Site.new(Guardian.new(@user)).groups
end
2020-03-30 08:16:03 +02:00
2020-04-13 14:17:22 +02:00
def submissions
Array.wrap(PluginStore.get("#{id}_submissions", @user.id))
end
2020-04-01 12:58:30 +02:00
def self.filter_records(filter)
PluginStoreRow.where("
plugin_name = 'custom_wizard' AND
(value::json ->> '#{filter}')::boolean IS TRUE
")
2020-03-30 08:16:03 +02:00
end
def self.after_signup(user)
2020-04-01 12:58:30 +02:00
if (records = filter_records('after_signup')).any?
result = false
2020-03-30 08:16:03 +02:00
2020-04-01 12:58:30 +02:00
records
2020-04-03 01:06:13 +02:00
.sort_by { |record| record.value['permitted'].present? ? 0 : 1 }
2020-04-01 12:58:30 +02:00
.each do |record|
2020-04-13 14:17:22 +02:00
wizard = CustomWizard::Wizard.new(JSON.parse(record.value), user)
2020-04-01 12:58:30 +02:00
2020-03-30 08:16:03 +02:00
if wizard.permitted?
2020-04-01 12:58:30 +02:00
result = wizard
2020-03-30 08:16:03 +02:00
break
end
end
2020-04-01 12:58:30 +02:00
result
2017-11-01 05:21:14 +01:00
else
false
end
2017-10-15 05:58:22 +02:00
end
2017-11-22 10:34:21 +01:00
def self.prompt_completion(user)
2020-04-01 12:58:30 +02:00
if (records = filter_records('prompt_completion')).any?
records.reduce([]) do |result, record|
2020-04-13 14:17:22 +02:00
wizard = CustomWizard::Wizard.new(::JSON.parse(record.value), user)
2020-04-14 07:46:06 +02:00
if wizard.permitted? && !wizard.completed?
result.push(id: wizard.id, name: wizard.name)
end
2018-04-25 03:15:19 +02:00
result
2017-11-22 10:34:21 +01:00
end
else
false
end
end
def self.restart_on_revisit
2020-04-01 12:58:30 +02:00
if (records = filter_records('restart_on_revisit')).any?
records.first.key
else
false
end
end
def self.steps(wizard_id)
wizard = PluginStore.get('custom_wizard', wizard_id)
wizard ? wizard['steps'] : nil
end
2017-11-01 05:21:14 +01:00
def self.step_ids(wizard_id)
steps = self.steps(wizard_id)
return [] if !steps
steps.map { |s| s['id'] }.flatten.uniq
end
def self.field_ids(wizard_id, step_id)
steps = self.steps(wizard_id)
return [] if !steps
step = steps.select { |s| s['id'] === step_id }.first
if step && fields = step['fields']
fields.map { |f| f['id'] }
else
[]
end
2017-09-23 04:34:07 +02:00
end
2017-11-07 08:40:09 +01:00
2019-12-06 10:05:19 +01:00
def self.add_wizard(obj)
wizard = obj.is_a?(String) ? ::JSON.parse(json) : obj
2017-11-07 08:40:09 +01:00
PluginStore.set('custom_wizard', wizard["id"], wizard)
end
2018-06-14 02:00:36 +02:00
def self.find(wizard_id)
PluginStore.get('custom_wizard', wizard_id)
2018-06-14 02:00:36 +02:00
end
2020-04-13 14:17:22 +02:00
def self.list(user=nil)
PluginStoreRow.where(plugin_name: 'custom_wizard').order(:id)
2020-04-15 04:47:14 +02:00
.reduce([]) do |result, record|
attrs = JSON.parse(record.value)
if attrs.present? &&
attrs.is_a?(Hash) &&
attrs['id'].present? &&
attrs['name'].present?
result.push(self.new(attrs, user))
end
result
end
2020-04-13 14:17:22 +02:00
end
def self.save(wizard)
existing_wizard = self.create(wizard[:id])
wizard[:steps].each do |step|
if step[:raw_description]
step[:description] = PrettyText.cook(step[:raw_description])
end
end
wizard = wizard.slice!(:create)
ActiveRecord::Base.transaction do
PluginStore.set('custom_wizard', wizard[:id], wizard)
if wizard[:after_time]
Jobs.cancel_scheduled_job(:set_after_time_wizard, wizard_id: wizard[:id])
2020-04-14 01:39:21 +02:00
enqueue_at = Time.parse(wizard[:after_time_scheduled]).utc
Jobs.enqueue_at(enqueue_at, :set_after_time_wizard, wizard_id: wizard[:id])
2020-04-13 14:17:22 +02:00
end
if existing_wizard && existing_wizard.after_time && !wizard[:after_time]
Jobs.cancel_scheduled_job(:set_after_time_wizard, wizard_id: wizard[:id])
Jobs.enqueue(:clear_after_time_wizard, wizard_id: wizard[:id])
end
end
wizard[:id]
end
def self.remove(wizard_id)
wizard = self.create(wizard_id)
ActiveRecord::Base.transaction do
if wizard.after_time
Jobs.cancel_scheduled_job(:set_after_time_wizard)
Jobs.enqueue(:clear_after_time_wizard, wizard_id: wizard.id)
end
PluginStore.remove('custom_wizard', wizard.id)
end
end
2018-06-14 02:00:36 +02:00
def self.exists?(wizard_id)
PluginStoreRow.exists?(plugin_name: 'custom_wizard', key: wizard_id)
end
2020-04-13 14:17:22 +02:00
def self.create(wizard_id, user = nil)
if wizard = self.find(wizard_id)
CustomWizard::Wizard.new(wizard.to_h, user)
2020-04-14 01:39:21 +02:00
else
false
2020-04-13 14:17:22 +02:00
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)
wizard = CustomWizard::Wizard.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