2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-25 13:14:56 -04:00
|
|
|
class Wizard
|
2017-05-12 10:18:43 -04:00
|
|
|
|
2016-09-14 16:36:08 -04:00
|
|
|
attr_reader :steps, :user
|
2017-05-12 10:18:43 -04:00
|
|
|
attr_accessor :max_topics_to_require_completion
|
2016-08-25 13:14:56 -04:00
|
|
|
|
2020-03-25 12:36:42 -04:00
|
|
|
@@excluded_steps = []
|
|
|
|
|
2016-09-09 16:51:07 -04:00
|
|
|
def initialize(user)
|
2016-08-25 13:14:56 -04:00
|
|
|
@steps = []
|
2016-09-09 16:51:07 -04:00
|
|
|
@user = user
|
2016-09-14 16:36:08 -04:00
|
|
|
@first_step = nil
|
2017-05-12 10:18:43 -04:00
|
|
|
@max_topics_to_require_completion = 15
|
2016-08-25 13:14:56 -04:00
|
|
|
end
|
|
|
|
|
2016-09-07 18:04:01 -04:00
|
|
|
def create_step(step_name)
|
|
|
|
Step.new(step_name)
|
2016-08-25 13:14:56 -04:00
|
|
|
end
|
|
|
|
|
2020-04-01 09:36:50 -04:00
|
|
|
def append_step(step, after: nil)
|
2020-03-25 12:36:42 -04:00
|
|
|
return if @@excluded_steps.include?(step)
|
|
|
|
|
2016-09-07 18:04:01 -04:00
|
|
|
step = create_step(step) if step.is_a?(String)
|
|
|
|
yield step if block_given?
|
|
|
|
|
2020-04-01 09:36:50 -04:00
|
|
|
if after
|
|
|
|
before_step = @steps.detect { |s| s.id == after }
|
|
|
|
|
|
|
|
if before_step
|
|
|
|
step.previous = before_step
|
|
|
|
step.index = before_step.index + 1
|
|
|
|
if before_step.next
|
|
|
|
step.next = before_step.next
|
|
|
|
before_step.next.previous = step
|
|
|
|
end
|
|
|
|
before_step.next = step
|
|
|
|
@steps.insert(before_step.index + 1, step)
|
|
|
|
step.index += 1 while (step = step.next)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
2016-08-25 13:14:56 -04:00
|
|
|
|
2020-04-01 09:36:50 -04:00
|
|
|
last_step = @steps.last
|
2016-08-25 13:14:56 -04:00
|
|
|
@steps << step
|
|
|
|
|
|
|
|
# If it's the first step
|
|
|
|
if @steps.size == 1
|
2016-09-14 16:36:08 -04:00
|
|
|
@first_step = step
|
2016-08-25 13:14:56 -04:00
|
|
|
step.index = 0
|
|
|
|
elsif last_step.present?
|
|
|
|
last_step.next = step
|
|
|
|
step.previous = last_step
|
|
|
|
step.index = last_step.index + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-25 12:36:42 -04:00
|
|
|
def self.exclude_step(step)
|
|
|
|
@@excluded_steps << step
|
|
|
|
end
|
|
|
|
|
2016-09-14 16:36:08 -04:00
|
|
|
def steps_with_fields
|
2016-10-14 05:09:55 -04:00
|
|
|
@steps_with_fields ||= @steps.select(&:has_fields?)
|
2016-09-14 16:36:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def start
|
|
|
|
completed = UserHistory.where(
|
|
|
|
action: UserHistory.actions[:wizard_step],
|
|
|
|
context: steps_with_fields.map(&:id)
|
|
|
|
).uniq.pluck(:context)
|
|
|
|
|
|
|
|
# First uncompleted step
|
|
|
|
steps_with_fields.each do |s|
|
|
|
|
return s unless completed.include?(s.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
@first_step
|
|
|
|
end
|
|
|
|
|
2016-09-12 14:43:00 -04:00
|
|
|
def create_updater(step_id, fields)
|
2016-10-14 05:09:55 -04:00
|
|
|
step = @steps.find { |s| s.id == step_id.dasherize }
|
2016-09-12 14:43:00 -04:00
|
|
|
Wizard::StepUpdater.new(@user, step, fields)
|
2016-08-25 13:14:56 -04:00
|
|
|
end
|
2016-09-09 16:51:07 -04:00
|
|
|
|
2016-09-14 16:36:08 -04:00
|
|
|
def completed?
|
|
|
|
completed_steps?(steps_with_fields.map(&:id))
|
|
|
|
end
|
|
|
|
|
|
|
|
def completed_steps?(steps)
|
|
|
|
steps = [steps].flatten.uniq
|
|
|
|
|
|
|
|
completed = UserHistory.where(
|
|
|
|
action: UserHistory.actions[:wizard_step],
|
|
|
|
context: steps
|
|
|
|
).distinct.order(:context).pluck(:context)
|
|
|
|
|
|
|
|
steps.sort == completed
|
|
|
|
end
|
|
|
|
|
|
|
|
def requires_completion?
|
|
|
|
return false unless SiteSetting.wizard_enabled?
|
2017-05-12 10:18:43 -04:00
|
|
|
return false if SiteSetting.bypass_wizard_check?
|
|
|
|
|
|
|
|
if Topic.limit(@max_topics_to_require_completion + 1).count > @max_topics_to_require_completion
|
|
|
|
SiteSetting.bypass_wizard_check = true
|
|
|
|
return false
|
|
|
|
end
|
2016-09-14 16:36:08 -04:00
|
|
|
|
2022-08-09 12:22:39 -04:00
|
|
|
if @user&.id && User.first_login_admin_id == @user.id
|
2016-11-01 03:24:07 -04:00
|
|
|
!Wizard::Builder.new(@user).build.completed?
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
2016-10-18 11:44:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.user_requires_completion?(user)
|
2016-11-01 03:24:07 -04:00
|
|
|
self.new(user).requires_completion?
|
2016-09-14 16:36:08 -04:00
|
|
|
end
|
|
|
|
|
2016-08-25 13:14:56 -04:00
|
|
|
end
|