Skip to content

Commit

Permalink
Add RatifyingCourses flow for courses namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
inulty-dfe committed Jan 14, 2025
1 parent 681f959 commit 13ca674
Show file tree
Hide file tree
Showing 37 changed files with 2,007 additions and 30 deletions.
23 changes: 23 additions & 0 deletions app/components/add_course_button_partnerships.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<% if required_organisation_details_present? %>
<%= govuk_button_link_to(
"Add course",
new_publish_provider_recruitment_cycle_course_path(
provider_code: provider.provider_code,
recruitment_cycle_year: provider.recruitment_cycle_year
),
class: "govuk-!-margin-bottom-6"
) %>

<% else %>

<div class="govuk-inset-text">
<p class="govuk-body">Before adding a course, you need to:</p>
<ul class="govuk-list govuk-list--bullet">
<% incomplete_sections.each do |section| %>
<li>
<%= govuk_link_to(section.name, section.path) %>
</li>
<% end %>
</ul>
</div>
<% end %>
69 changes: 69 additions & 0 deletions app/components/add_course_button_partnerships.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# frozen_string_literal: true

class AddCourseButtonPartnerships < ViewComponent::Base
include RecruitmentCycleHelper
attr_reader :provider

Section = Struct.new(:name, :path, keyword_init: true)

def initialize(provider:)
@provider = provider
super
end

private

def incomplete_sections
incomplete_sections_hash.keys.select { |section| send(section) }.map do |section|
Section.new(name: "add #{incomplete_section_article(section)} #{incomplete_section_label_suffix(section)}", path: incomplete_sections_hash[section])
end
end

def incomplete_sections_hash
{
site_not_present?: publish_provider_recruitment_cycle_schools_path(provider.provider_code, provider.recruitment_cycle_year),
accredited_partner_not_present?: publish_provider_recruitment_cycle_accredited_partnerships_path(provider.provider_code, provider.recruitment_cycle_year)
}
end

def incomplete_section_label_suffix(section)
labels = {
accredited_partner_not_present?: 'accredited partner',
site_not_present?: 'school'
}

labels[section]
end

def required_organisation_details_present?
accredited_partner_present? && site_present?
end

def accredited_partner_present?
return true if accredited_provider?

provider.accredited_partners.any?
end

def site_present?
provider.sites.any?
end

def accredited_partner_not_present?
return false if provider.accredited_provider?

!accredited_partner_present?
end

def site_not_present?
!site_present?
end

def accredited_provider?
provider.accredited_provider?
end

def incomplete_section_article(section)
incomplete_section_label_suffix(section) == 'accredited partner' ? 'an' : 'a'
end
end
172 changes: 172 additions & 0 deletions app/controllers/publish/courses/ratifying_provider_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
# frozen_string_literal: true

module Publish
module Courses
class RatifyingProviderController < PublishController
include CourseBasicDetailConcern
before_action :build_course, only: %i[edit update]
before_action :build_course_params, only: :continue
helper_method :accredited_partners

def accredited_partners
if Settings.features.provider_partnerships
@provider.accredited_partners
else
@provider.accredited_bodies
end
end

def show
@course = build_course&.decorate
render_not_found if @course.accrediting_provider.blank?
end

def edit
build_provider
end

def continue
authorize(@provider, :can_create_course?)

code = course_params[:accredited_provider_code]
query = @accredited_provider

@errors = errors_for_search_query(code, query)

if @errors.present?
render :new
elsif other_selected_with_no_autocompleted_code?(code)
redirect_to(
search_new_publish_provider_recruitment_cycle_courses_accredited_provider_path(
query: @accredited_provider,
course: course_params
)
)
else
params[:course][:accredited_provider_code] = @autocompleted_provider_code if @autocompleted_provider_code.present?
super
end
end

def search_new
authorize(provider, :can_create_course?)

# These are not before_action hooks as they conflict with hooks
# defined within the CourseBasicDetailConcern and cannot be overridden
# without causing failures in other routes in this controller
build_new_course
build_provider
build_previous_course_creation_params
@query = params[:query]
@provider_suggestions = recruitment_cycle.providers.with_findable_courses.provider_search(@query).limit(10)
end

def update
build_provider
begin
code = update_course_params[:accredited_provider_code]
query = update_course_params[:accredited_provider]
rescue ActionController::ParameterMissing
@errors = errors_for_search_query(code, query)
return render :edit if @errors.present?
end

if update_params[:accredited_provider_code] == 'other'
redirect_to_provider_search
elsif @course.update(update_params)
course_updated_message('Accredited provider')
redirect_to_update_successful
else
@errors = @course.errors.messages
render :edit
end
end

def search
build_course
@query = params[:query]
@provider_suggestions = recruitment_cycle.providers.with_findable_courses.provider_search(@query).limit(10)
end

private

def build_provider
@provider = RecruitmentCycle.find_by(year: params[:recruitment_cycle_year])
.providers
.find_by(provider_code: params[:provider_code])
end

def error_keys
[:accredited_provider_code]
end

def redirect_to_provider_search
redirect_to(
accredited_provider_search_provider_recruitment_cycle_course_path(
@course.provider_code,
@course.recruitment_cycle_year,
@course.course_code,
query: update_course_params[:accredited_provider]
)
)
end

def redirect_to_update_successful
redirect_to(
details_publish_provider_recruitment_cycle_course_path(
@course.provider_code,
@course.recruitment_cycle_year,
@course.course_code
)
)
end

def current_step
:accredited_provider
end

def build_course_params
@accredited_provider = params[:course].delete(:accredited_provider)
@autocompleted_provider_code = params[:course].delete(:autocompleted_provider_code)
end

def errors_for_search_query(code, query)
errors = {}

if other_selected_with_no_autocompleted_code?(code) && query.length < 2
errors = { accredited_provider: ['Accredited provider search too short, enter 2 or more characters'] }
elsif code.blank?
errors = { accredited_provider_code: ['Select an accredited provider'] }
end

errors
end

def update_course_params
params.require(:course).permit(
:autocompleted_provider_code,
:accredited_provider_code,
:accredited_provider
)
end

def update_params
autocompleted_code = update_course_params[:autocompleted_provider_code]
code = update_course_params[:accredited_provider_code]

{
accredited_provider_code: autocompleted_code.presence || code
}
end

def other_selected_with_no_autocompleted_code?(code)
code == 'other' && @autocompleted_provider_code.blank?
end

def build_course
super
authorize @course
end
end
end
end
21 changes: 15 additions & 6 deletions app/helpers/view_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def title_with_error_prefix(title, error)
def enrichment_error_url(provider_code:, course:, field:, message: nil)
base = "/publish/organisations/#{provider_code}/#{course.recruitment_cycle_year}/courses/#{course.course_code}"
provider_base = "/publish/organisations/#{provider_code}/#{course.recruitment_cycle_year}"
accrediting_provider = Settings.features.provider_partnerships ? ratifying_provider_publish_provider_recruitment_cycle_course_path(course.provider_code, course.recruitment_cycle_year, course.course_code) : accredited_provider_publish_provider_recruitment_cycle_course_path(course.provider_code, course.recruitment_cycle_year, course.course_code)

if field.to_sym == :base
base_errors_hash(provider_code, course)[message]
Expand All @@ -62,7 +63,7 @@ def enrichment_error_url(provider_code:, course:, field:, message: nil)
age_range_in_years: "#{base}/age-range?display_errors=true",
sites: "#{base}/schools?display_errors=true",
study_sites: (course.provider&.study_sites&.none? ? "#{provider_base}/study-sites" : "#{base}/study-sites").to_s,
accrediting_provider: accredited_provider_publish_provider_recruitment_cycle_course_path(course.provider_code, course.recruitment_cycle_year, course.course_code),
accrediting_provider:,
applications_open_from: "#{base}/applications-open",
a_level_subject_requirements: publish_provider_recruitment_cycle_course_a_levels_what_a_level_is_required_path(
course.provider_code,
Expand Down Expand Up @@ -165,11 +166,19 @@ def x_provider_url

def x_accrediting_provider_url
if preview?(params)
accredited_by_publish_provider_recruitment_cycle_course_path(
course.provider_code,
course.recruitment_cycle_year,
course.course_code
)
if Settings.features.provider_partnerships
ratified_by_publish_provider_recruitment_cycle_course_path(
course.provider_code,
course.recruitment_cycle_year,
course.course_code
)
else
accredited_by_publish_provider_recruitment_cycle_course_path(
course.provider_code,
course.recruitment_cycle_year,
course.course_code
)
end
else
find_accrediting_provider_path(course.provider_code, course.course_code)
end
Expand Down
8 changes: 6 additions & 2 deletions app/models/concerns/publish/course_basic_detail_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,12 @@ def course_creation_path_for(page)
end
when :applications_open
new_publish_provider_recruitment_cycle_courses_applications_open_path(path_params)
when :accredited_provider
new_publish_provider_recruitment_cycle_courses_accredited_provider_path(path_params)
when :accredited_provider, :ratifying_provider
if Settings.features.provider_partnerships
new_publish_provider_recruitment_cycle_courses_ratifying_provider_path(path_params)
else
new_publish_provider_recruitment_cycle_courses_accredited_provider_path(path_params)
end
when :can_sponsor_student_visa
new_publish_provider_recruitment_cycle_courses_student_visa_sponsorship_path(path_params)
when :can_sponsor_skilled_worker_visa
Expand Down
7 changes: 7 additions & 0 deletions app/models/course.rb
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,13 @@ def generate_name
Courses::GenerateCourseNameService.call(course: self)
end

def ratifying_provider_description
return nil unless accrediting_provider

subquery = RecruitmentCycle.current.providers.where(provider_code: accrediting_provider.provider_code).select(:id)
provider.accredited_partnerships.find_by(accredited_provider_id: subquery)&.description
end

def accrediting_provider_description
return if accrediting_provider.blank?
return if provider.accrediting_provider_enrichments.blank?
Expand Down
29 changes: 24 additions & 5 deletions app/views/publish/courses/_basic_details_tab.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -152,22 +152,41 @@
end
end

if Settings.features.provider_partnerships
heading = "Ratifying provider"
partners = @provider.accredited_partners
add_provider_text = "Add at least one accredited partner"
add_provider_link = publish_provider_recruitment_cycle_accredited_partnerships_path(@course.provider_code, @course.recruitment_cycle_year)
select_partner_text = "Select an accredited partner"
select_partner_link = ratifying_provider_publish_provider_recruitment_cycle_course_path(@course.provider_code, @course.recruitment_cycle_year, @course.course_code)
change_partner_link = ratifying_provider_publish_provider_recruitment_cycle_course_path(@provider.provider_code, course.recruitment_cycle_year, course.course_code)
# select_partner_link = ratifying_provider_publish_provider_recruitment_cycle_course_path(@course.provider_code, @course.recruitment_cycle_year, @course.course_code)
else
heading = "Accredited provider"
partners = @provider.accredited_providers
add_provider_text = "Add at least one accredited provider"
add_provider_link = publish_provider_recruitment_cycle_accredited_providers_path(@course.provider_code, @course.recruitment_cycle_year)
select_partner_text = "Select an accredited provider"
select_partner_link = accredited_provider_publish_provider_recruitment_cycle_course_path(@course.provider_code, @course.recruitment_cycle_year, @course.course_code)
change_partner_link = accredited_provider_publish_provider_recruitment_cycle_course_path(@provider.provider_code, course.recruitment_cycle_year, course.course_code)
end

unless @provider.accredited_provider?
summary_list.with_row(html_attributes: { data: { qa: "course__accredited_provider" } }) do |row|
row.with_key { "Accredited provider" }
row.with_key { heading }
row.with_value { course.accrediting_provider&.provider_name }
if course.is_published? || course.is_withdrawn?
row.with_action
elsif !course.accrediting_provider.nil?
row.with_action(href: accredited_provider_publish_provider_recruitment_cycle_course_path(@provider.provider_code, course.recruitment_cycle_year, course.course_code),
row.with_action(href: change_partner_link,
visually_hidden_text: "accredited provider")
elsif @provider.accredited_providers.any?
elsif partners.any?
row.with_value do
"<div class=\"govuk-inset-text app-inset-text--narrow-border app-inset-text--important\">#{govuk_link_to('Select an accredited provider', accredited_provider_publish_provider_recruitment_cycle_course_path(@course.provider_code, @course.recruitment_cycle_year, @course.course_code))}</div>".html_safe
"<div class=\"govuk-inset-text app-inset-text--narrow-border app-inset-text--important\">#{govuk_link_to(select_partner_text, select_partner_link)}</div>".html_safe
end
else
row.with_value do
"<div class=\"govuk-inset-text app-inset-text--narrow-border app-inset-text--important\">#{govuk_link_to('Add at least one accredited provider', publish_provider_recruitment_cycle_accredited_providers_path(@course.provider_code, @course.recruitment_cycle_year))}</div>".html_safe
"<div class=\"govuk-inset-text app-inset-text--narrow-border app-inset-text--important\">#{govuk_link_to(add_provider_text, add_provider_link)}</div>".html_safe
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/views/publish/courses/confirmation.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,12 @@

<% unless @provider.accredited_provider? || course.is_further_education? %>
<% summary_list.with_row(html_attributes: { data: { qa: "course__accredited_provider" } }) do |row| %>
<% row.with_key { "Accredited provider" } %>
<% row.with_key { "Ratifying provider" } %>
<% row.with_value { course.accrediting_provider.provider_name } %>
<% if @provider.accredited_bodies.length > 1 %>
<% row.with_action(
href: new_publish_provider_recruitment_cycle_courses_accredited_provider_path(course.provider.provider_code, course.recruitment_cycle.year, params.to_unsafe_h.merge(goto_confirmation: true)),
visually_hidden_text: "accredited provider"
visually_hidden_text: "ratifying provider"
) %>
<% else %>
<% row.with_action %>
Expand Down
Loading

0 comments on commit 13ca674

Please sign in to comment.