Skip to content

Commit

Permalink
Merge pull request #4207 from DFE-Digital/1567-publish-full-time-part…
Browse files Browse the repository at this point in the history
…-time-study-change-from-radio-buttons-to-checkboxes

[1567] full-time part-time study change from radio buttons to checkboxes
  • Loading branch information
elceebee authored May 21, 2024
2 parents edfda9b + fc7f671 commit a44fc42
Show file tree
Hide file tree
Showing 21 changed files with 235 additions and 73 deletions.
6 changes: 3 additions & 3 deletions app/controllers/publish/courses/study_mode_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def update

@course_study_mode_form = CourseStudyModeForm.new(@course, params: study_mode_params)
if @course_study_mode_form.save!
course_updated_message('Full time or part time')
course_updated_message I18n.t('publish.providers.study_mode.form.study_pattern')

redirect_to details_publish_provider_recruitment_cycle_course_path(
provider.provider_code,
Expand All @@ -33,15 +33,15 @@ def update
def study_mode_params
return { study_mode: nil } if params[:publish_course_study_mode_form].blank?

params.require(:publish_course_study_mode_form).permit(*CourseStudyModeForm::FIELDS)
params.require(:publish_course_study_mode_form).permit(study_mode: [])
end

def current_step
:full_or_part_time
end

def errors
params.dig(:course, :study_mode) ? {} : { study_mode: ['Select full time or part time'] }
params.dig(:course, :study_mode) ? {} : { study_mode: [I18n.t('activemodel.errors.models.publish/course_study_mode_form.attributes.study_mode.blank')] }
end
end
end
Expand Down
1 change: 1 addition & 0 deletions app/controllers/publish/courses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def course_params
params.require(:course)
.permit(
policy(Course.new).permitted_new_course_attributes,
study_mode: [],
sites_ids: [],
subjects_ids: [],
study_sites_ids: []
Expand Down
8 changes: 7 additions & 1 deletion app/forms/publish/course_study_mode_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ class CourseStudyModeForm < BaseModelForm
presence: true,
inclusion: { in: Course.study_modes.keys }

def study_mode_checked?(value)
mode = study_mode.nil? ? model.study_mode : study_mode

mode == value || mode == 'full_time_or_part_time'
end

private

def valid_before_save
course.ensure_site_statuses_match_study_mode if course.changed?
end

def compute_fields
course.attributes.symbolize_keys.slice(*FIELDS).merge(new_attributes)
{ study_mode: new_attributes[:study_mode]&.compact_blank&.sort&.join('_or_') }
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module StudyModeConcern
extend ActiveSupport::Concern
included do
def study_mode_options
Course.study_modes.keys
%w[full_time part_time]
end
end
end
Expand Down
1 change: 1 addition & 0 deletions app/models/concerns/publish/course_basic_detail_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def course_params
:language_ids
).permit(
policy(Course.new).permitted_new_course_attributes,
study_mode: [],
sites_ids: [],
subjects_ids: [],
study_sites_ids: []
Expand Down
20 changes: 19 additions & 1 deletion app/services/courses/creation_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ def new_course

def build_new_course
course = provider.courses.new
course.assign_attributes(course_attributes.except(:subjects_ids))
course.assign_attributes(course_attributes.except(:subjects_ids, :study_mode))

update_study_mode(course)
update_sites(course)
update_study_sites(course)
course.accrediting_provider = course.provider.accrediting_providers.first if course.provider.accredited_bodies.length == 1
Expand Down Expand Up @@ -72,10 +73,27 @@ def site_ids
@site_ids ||= course_params['sites_ids']
end

def study_mode
@study_mode ||= course_params['study_mode']&.flatten&.compact&.uniq
end

def study_site_ids
@study_site_ids ||= course_params['study_sites_ids']
end

def update_study_mode(course)
return if study_mode.nil?

if study_mode.empty?
course.errors.add(
:study_mode,
I18n.t('activemodel.errors.models.publish/course_study_mode_form.attributes.study_mode.blank')
)
else
course.study_mode = study_mode.sort.join('_or_')
end
end

def update_sites(course)
return if site_ids.nil?

Expand Down
2 changes: 1 addition & 1 deletion app/views/publish/courses/_basic_details_tab.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
end

summary_list.with_row(html_attributes: { data: { qa: "course__study_mode" } }) do |row|
row.with_key { "Full time or part time" }
row.with_key { t("publish.providers.study_mode.form.study_pattern") }
row.with_value { course.study_mode&.humanize }

if course.cannot_change_study_mode?
Expand Down
2 changes: 1 addition & 1 deletion app/views/publish/courses/confirmation.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
<% end %>

<% summary_list.with_row(html_attributes: { data: { qa: "course__study_mode" } }) do |row| %>
<% row.with_key { "Full time or part time" } %>
<% row.with_key { t("publish.providers.study_mode.form.study_pattern") } %>
<% row.with_value { course.study_mode&.humanize } %>
<% unless course.teacher_degree_apprenticeship?
row.with_action(
Expand Down
20 changes: 10 additions & 10 deletions app/views/publish/courses/study_mode/_form_fields.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
<legend class="govuk-fieldset__legend govuk-fieldset__legend--l">
<h1 class="govuk-fieldset__heading">
<%= render CaptionText.new(text: t("course.add_course")) %>
Full time or part time
<%= t("publish.providers.study_mode.form.study_pattern") %>
</h1>
</legend>
<div class="govuk-hint" id="study-pattern-hint">
<%= t("publish.providers.study_mode.form.select_all_that_apply") %>
</div>

<%= render "publish/shared/error_messages", error_keys: [:study_mode] %>
<div class="govuk-radios govuk-!-margin-top-2" data-module="govuk-radios">
<div class="govuk-checkboxes" data-module="govuk-checkboxes">
<% @course.edit_course_options["study_modes"].each do |value| %>
<div class="govuk-radios__item">
<%= form.radio_button :study_mode,
value,
class: "govuk-radios__input" %>
<%= form.label :study_mode,
t("edit_options.study_modes.#{value}.label"),
value:,
class: "govuk-label govuk-radios__label" %>
<div class="govuk-checkboxes__item">
<%= form.govuk_check_box :study_mode,
value,
label: { text: t("edit_options.study_modes.#{value}.label") },
checked: course.study_mode.in?([value, "full_time_or_part_time"]) %>
</div>
<% end %>
</div>
Expand Down
22 changes: 13 additions & 9 deletions app/views/publish/courses/study_mode/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% page_title = "Full time or part time" %>
<% page_title = t("page_titles.study_mode.edit") %>
<% content_for :page_title, title_with_error_prefix("#{page_title} – #{course.name_and_code}", @course_study_mode_form.errors.any?) %>

<% content_for :before_content do %>
Expand Down Expand Up @@ -26,14 +26,18 @@

<%= f.govuk_error_summary %>

<%= f.govuk_radio_buttons_fieldset(:study_mode, legend: { text: "#{render CaptionText.new(text: course.name_and_code)} Full time or part time?".html_safe, tag: "h1", size: "l" }) do %>
<% course.edit_course_options["study_modes"].each_with_index do |study_mode, index| %>
<%= f.govuk_radio_button(
:study_mode,
study_mode,
label: { text: t("edit_options.study_modes.#{study_mode}.label") },
link_errors: index.zero?
) %>
<%= f.govuk_check_boxes_fieldset :study_mode,
hint: { text: t("publish.providers.study_mode.form.select_all_that_apply") },
legend: { text: "#{render CaptionText.new(text: course.name_and_code)} #{t('publish.providers.study_mode.form.study_pattern')}".html_safe,
tag: "h1",
size: "l" } do %>
<% course.edit_course_options[:study_modes].each_with_index do |value, index| %>
<%= f.govuk_check_box :study_mode,
value,
label: { text: t("edit_options.study_modes.#{value}.label") },
checked: @course_study_mode_form.study_mode_checked?(value),
link_errors: index.zero? %>

<% end %>
<% end %>

Expand Down
2 changes: 1 addition & 1 deletion app/views/publish/courses/study_mode/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% content_for :page_title, title_with_error_prefix("Full time or part time", @errors && @errors.any?) %>
<% content_for :page_title, title_with_error_prefix(t("page_titles.study_mode.new"), @errors && @errors.any?) %>

<% content_for :before_content do %>
<%= govuk_back_link_to(@back_link_path) %>
Expand Down
13 changes: 8 additions & 5 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ en:
apprenticeship_and_visa: "Teaching apprenticeship and %{visa_type} visas updated"
visa: "%{visa_type} visas updated"
page_titles:
study_mode:
edit: Study pattern
new: Study pattern
funding_type:
edit: "Funding type"
skilled_worker_visas:
Expand Down Expand Up @@ -127,8 +130,6 @@ en:
label: "Part time"
full_time:
label: "Full time"
full_time_or_part_time:
label: "Full time or part time"
apprenticeship:
pg_teaching_apprenticeship:
label: "Yes"
Expand Down Expand Up @@ -292,6 +293,10 @@ en:
update: "Update user"
publish:
providers:
study_mode:
form:
select_all_that_apply: Select all that apply
study_pattern: Study pattern
study_sites:
index:
add_study_site: &add_study_site "Add study site"
Expand Down Expand Up @@ -832,7 +837,7 @@ en:
publish/course_study_mode_form:
attributes:
study_mode:
blank: "Select full time or part time"
blank: Select a study pattern
publish/course_withdrawal_form:
attributes:
confirm_course_code:
Expand Down Expand Up @@ -975,8 +980,6 @@ en:
label: "Part time"
full_time:
label: "Full time"
full_time_or_part_time:
label: "Full time or part time"
can_sponsor_skilled_worker_visas:
true:
label: "Yes"
Expand Down
65 changes: 56 additions & 9 deletions spec/features/publish/courses/editing_course_study_mode_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,40 @@
given_i_am_authenticated_as_a_provider_user
end

scenario 'i can update the course study mode' do
scenario 'I can update the course study mode to both full time and part time' do
and_there_is_a_part_time_course_i_want_to_edit
when_i_visit_the_course_study_mode_page
then_i_see_part_time_selected

and_i_choose_a_full_time_study_mode
and_i_submit
then_i_should_see_a_success_message
and_the_course_study_mode_is_updated
and_the_course_study_mode_is_updated_to_full_or_part_time

when_i_visit_the_course_study_mode_page
then_i_see_both_part_time_and_full_time_selected
end

scenario 'I can change the course study mode from part time to full time' do
and_there_is_a_part_time_course_i_want_to_edit
when_i_visit_the_course_study_mode_page
and_i_choose_a_full_time_study_mode
and_i_uncheck_part_time_study_mode
and_i_submit
then_i_should_see_a_success_message
and_the_course_study_mode_is_updated_to_full_time

when_i_visit_the_course_study_mode_page
then_i_see_full_time_selected
end

scenario 'updating with invalid data' do
and_there_is_a_course_with_no_study_mode
and_there_is_a_part_time_course_i_want_to_edit
when_i_visit_the_course_study_mode_page
and_i_uncheck_part_time_study_mode
and_i_submit
then_i_should_see_an_error_message
and_nothing_is_selected
end

def given_i_am_authenticated_as_a_provider_user
Expand All @@ -43,25 +63,52 @@ def when_i_visit_the_course_study_mode_page
end

def and_i_choose_a_full_time_study_mode
publish_course_study_mode_edit_page.full_time.choose
publish_course_study_mode_edit_page.full_time.click
end

def and_i_uncheck_part_time_study_mode
publish_course_study_mode_edit_page.part_time.click
end

def and_i_submit
publish_course_study_mode_edit_page.submit.click
end

def then_i_should_see_a_success_message
expect(page).to have_content(I18n.t('success.saved', value: 'Full time or part time'))
expect(page).to have_content(I18n.t('success.saved', value: 'Study pattern'))
end

def then_i_see_part_time_selected
expect(publish_course_study_mode_edit_page.part_time.checked?).to be true
expect(publish_course_study_mode_edit_page.full_time.checked?).to be false
end

def and_the_course_study_mode_is_updated
def then_i_see_both_part_time_and_full_time_selected
expect(publish_course_study_mode_edit_page.part_time.checked?).to be true
expect(publish_course_study_mode_edit_page.full_time.checked?).to be true
end

def and_the_course_study_mode_is_updated_to_full_or_part_time
expect(course.reload).to be_full_time_or_part_time
end

def and_the_course_study_mode_is_updated_to_full_time
expect(course.reload).to be_full_time
end

def then_i_see_full_time_selected
expect(publish_course_study_mode_edit_page.full_time.checked?).to be true
expect(publish_course_study_mode_edit_page.part_time.checked?).to be false
end

def and_nothing_is_selected
expect(publish_course_study_mode_edit_page.part_time.checked?).to be false
expect(publish_course_study_mode_edit_page.full_time.checked?).to be false
end

def then_i_should_see_an_error_message
expect(publish_course_study_mode_edit_page.error_messages).to include(
I18n.t('activemodel.errors.models.publish/course_study_mode_form.attributes.study_mode.blank')
)
expect(publish_course_study_mode_edit_page.error_messages)
.to include('Select a study pattern')
end

def provider
Expand Down
2 changes: 1 addition & 1 deletion spec/features/publish/courses/new_funding_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def provider

def then_i_am_met_with_the_full_or_part_time_page(funding_type)
expect(page).to have_current_path("/publish/organisations/#{provider.provider_code}/#{Settings.current_recruitment_cycle_year}/courses/full-part-time/new#{selected_params(funding_type)}")
expect(page).to have_content('Full time or part time')
expect(page).to have_content('Study pattern')
end

def then_i_am_met_with_errors
Expand Down
Loading

0 comments on commit a44fc42

Please sign in to comment.