diff --git a/app/controllers/phases_controller.rb b/app/controllers/phases_controller.rb index e3980fd..d20b7ea 100644 --- a/app/controllers/phases_controller.rb +++ b/app/controllers/phases_controller.rb @@ -4,8 +4,8 @@ after_action :verify_authorized - # GET /plans/:plan_id/phases/:id/edit - def edit + # GET /plans/:plan_id/phases/:id/edit + def edit @plan = Plan.eager_load2(params[:plan_id]) # authorization done on plan so found in plan_policy @@ -69,12 +69,12 @@ if !user_signed_in? then respond_to do |format| - format.html { redirect_to edit_user_registration_path } - end - end - + format.html { redirect_to edit_user_registration_path } + end end + end + # GET /plans/PLANID/phases/PHASEID/status.json def status diff --git a/app/controllers/plans_controller.rb b/app/controllers/plans_controller.rb index e4bce3a..06373d9 100644 --- a/app/controllers/plans_controller.rb +++ b/app/controllers/plans_controller.rb @@ -174,7 +174,12 @@ @plan = Plan.find(params[:id]) authorize @plan @visibility = @plan.visibility.present? ? @plan.visibility.to_s : Rails.application.config.default_plan_visibility - @allow_visibility = (@plan.num_answered_questions >= 1 && !@plan.is_test?) + + min_percentage = Rails.application.config.default_plan_percentage_answered + nanswers = @plan.num_answered_questions() + nquestions = @plan.num_questions() + value=(nanswers.to_f/nquestions*100).round(2) + @allow_visibility = (value >= min_percentage && !@plan.is_test?) end @@ -290,13 +295,9 @@ respond_to do |format| if @plan.save @plan.assign_creator(current_user) - flash[:notice] = success_message(_('plan'), _('copied')) - format.js { render js: "window.location='#{plan_url(@plan)}?editing=true'" } - # format.html { redirect_to @plan, notice: _('Plan was successfully duplicated.') } - # format.json { head :no_content } + format.html { redirect_to @plan, notice: success_message(_('plan'), _('copied')) } else - flash[:alert] = failed_create_error(@plan, 'Plan') - format.js {} + format.html { redirect_to plans_path, alert: failed_create_error(@plan, 'Plan') } end end end @@ -454,7 +455,6 @@ if templates.empty? templates << Template.where(is_default: true, published: true).first end - templates = (templates.count > 0 ? templates.sort{|x,y| x.title <=> y.title} : []) end diff --git a/app/models/exported_plan.rb b/app/models/exported_plan.rb index c14979f..bc42e4b 100644 --- a/app/models/exported_plan.rb +++ b/app/models/exported_plan.rb @@ -158,7 +158,9 @@ end sections.each do |section| - output += "\n#{section.title}\n" + if question_headings + output += "\n#{section.title}\n" + end section.questions.each do |question| answer = self.plan.answer(question.id, false) #skip if question un-answered diff --git a/app/views/plans/_download_form.html.erb b/app/views/plans/_download_form.html.erb index 03449f9..b6965e8 100644 --- a/app/views/plans/_download_form.html.erb +++ b/app/views/plans/_download_form.html.erb @@ -1,5 +1,5 @@ -<%= form_tag( export_plan_path(@plan), method: :get) do |f| %> -

<%= _("Download Settings") %>

+<%= form_tag( export_plan_path(@plan), method: :get, target: '_blank', id: 'download_form') do |f| %> +

<%= _("Download settings") %>

<% if @phase_options.length > 1 %>
@@ -34,69 +34,71 @@
-

<%= _('PDF Formatting') %>

-
-
-

<%= _('Font') %>

+
+

<%= _('PDF formatting') %>

+
+
+

<%= _('Font') %>

+
+
+

<%= _('Margin (mm)') %>

+
-
-

<%= _('Margin (mm)') %>

+
+
+ <%= label_tag "export[formatting][font_face]", _('Face'), class: 'control-label' %> + <%= select_tag "export[formatting][font_face]", + options_for_select(Settings::Template::VALID_FONT_FACES, + @export_settings.formatting[:font_face]), + class: 'form-control', + "data-default": @plan.template.settings(:export).formatting[:font_face] %> +
+
+ <%= label_tag "export[formatting][font_size]", _('Size') + " (pt)", class: 'control-label' %> + <%= select_tag "export[formatting][font_size]", + options_for_select(Settings::Template::VALID_FONT_SIZE_RANGE.to_a, @export_settings.formatting[:font_size]), + class: 'form-control', + "data-default": @plan.template.settings(:export).formatting[:font_size] %> +
+ +
+ <%= label_tag "export[formatting][margin][top]", _('Top'), + class: 'control-label' %> + <%= select_tag "export[formatting][margin][top]", + options_for_select(Settings::Template::VALID_MARGIN_RANGE.to_a, + @export_settings.formatting[:margin][:top]), + class: 'form-control', + "data-default": @plan.template.settings(:export).formatting[:margin][:top] %> +
+
+ <%= label_tag "export[formatting][margin][bottom]", _('Bottom'), + class: 'control-label' %> + <%= select_tag "export[formatting][margin][bottom]", + options_for_select(Settings::Template::VALID_MARGIN_RANGE.to_a, + @export_settings.formatting[:margin][:bottom]), + class: 'form-control', + "data-default": @plan.template.settings(:export).formatting[:margin][:bottom] %> +
+
+ <%= label_tag "export[formatting][margin][left]", _('Left'), + class: 'control-label' %> + <%= select_tag "export[formatting][margin][left]", + options_for_select(Settings::Template::VALID_MARGIN_RANGE.to_a, + @export_settings.formatting[:margin][:left]), + class: 'form-control', + "data-default": @plan.template.settings(:export).formatting[:margin][:left] %> +
+
+ <%= label_tag "export[formatting][margin][right]", _('Right'), + class: 'control-label' %> + <%= select_tag "export[formatting][margin][right]", + options_for_select(Settings::Template::VALID_MARGIN_RANGE.to_a, + @export_settings.formatting[:margin][:right]), + class: 'form-control', + "data-default": @plan.template.settings(:export).formatting[:margin][:rigth] %> +
-
-
- <%= label_tag "export[formatting][font_face]", _('Face'), class: 'control-label' %> - <%= select_tag "export[formatting][font_face]", - options_for_select(Settings::Template::VALID_FONT_FACES, - @export_settings.formatting[:font_face]), - class: 'form-control', - "data-default": @plan.template.settings(:export).formatting[:font_face] %> -
-
- <%= label_tag "export[formatting][font_size]", _('Size') + " (pt)", class: 'control-label' %> - <%= select_tag "export[formatting][font_size]", - options_for_select(Settings::Template::VALID_FONT_SIZE_RANGE.to_a, @export_settings.formatting[:font_size]), - class: 'form-control', - "data-default": @plan.template.settings(:export).formatting[:font_size] %> -
- -
- <%= label_tag "export[formatting][margin][top]", _('Top'), - class: 'control-label' %> - <%= select_tag "export[formatting][margin][top]", - options_for_select(Settings::Template::VALID_MARGIN_RANGE.to_a, - @export_settings.formatting[:margin][:top]), - class: 'form-control', - "data-default": @plan.template.settings(:export).formatting[:margin][:top] %> -
-
- <%= label_tag "export[formatting][margin][bottom]", _('Bottom'), - class: 'control-label' %> - <%= select_tag "export[formatting][margin][bottom]", - options_for_select(Settings::Template::VALID_MARGIN_RANGE.to_a, - @export_settings.formatting[:margin][:bottom]), - class: 'form-control', - "data-default": @plan.template.settings(:export).formatting[:margin][:bottom] %> -
-
- <%= label_tag "export[formatting][margin][left]", _('Left'), - class: 'control-label' %> - <%= select_tag "export[formatting][margin][left]", - options_for_select(Settings::Template::VALID_MARGIN_RANGE.to_a, - @export_settings.formatting[:margin][:left]), - class: 'form-control', - "data-default": @plan.template.settings(:export).formatting[:margin][:left] %> -
-
- <%= label_tag "export[formatting][margin][right]", _('Right'), - class: 'control-label' %> - <%= select_tag "export[formatting][margin][right]", - options_for_select(Settings::Template::VALID_MARGIN_RANGE.to_a, - @export_settings.formatting[:margin][:right]), - class: 'form-control', - "data-default": @plan.template.settings(:export).formatting[:margin][:rigth] %> -
-
- + <%= button_tag(_('Download Plan'), class: "btn btn-primary", type: "submit") %> <% end %> diff --git a/app/views/plans/_share_form.html.erb b/app/views/plans/_share_form.html.erb index b9bb8d7..640f622 100644 --- a/app/views/plans/_share_form.html.erb +++ b/app/views/plans/_share_form.html.erb @@ -1,5 +1,5 @@

<%= _('Set plan visibility') %>

-

<%= _('Public or organisational visibility is intended for finished plans. You must answer at least one question to enable these options.') %>

+

<%= _('Public and organisational visibility are intended for finished plans. You must answer at least %{percentage}%% of the questions to enable these options. Test plans are private by default and not included in usage information.') % {percentage: Rails.application.config.default_plan_percentage_answered.round} %>

<%= form_for @plan, html: {method: :put, id: "set_visibility"} do |f| %> > diff --git a/app/views/plans/export.docx.erb b/app/views/plans/export.docx.erb index 6ae681d..e75c5a4 100644 --- a/app/views/plans/export.docx.erb +++ b/app/views/plans/export.docx.erb @@ -16,7 +16,9 @@ <% @sections.each do |section| %> -

<%= section.title %>

+ <% if @question_headings %> +

<%= section.title %>

+ <% end %> <% section.questions.each do |question| %> <% answer = @plan.answer(question.id, false) %> <% if answer.nil? && !@unanswered_questions then next end %> diff --git a/app/views/plans/export.html.erb b/app/views/plans/export.html.erb index d982182..cd8b28d 100644 --- a/app/views/plans/export.html.erb +++ b/app/views/plans/export.html.erb @@ -27,14 +27,18 @@ <% end %> <% @sections.each do |section| %> -

<%= section.title %>

+ <% if @question_headings %> +

<%= section.title %>

+ <% end %> <% if @question_headings %> - <% end %> + <% else %> + + <% end %> diff --git a/app/views/plans/export.json.jbuilder b/app/views/plans/export.json.jbuilder index 7277da5..405d931 100644 --- a/app/views/plans/export.json.jbuilder +++ b/app/views/plans/export.json.jbuilder @@ -15,7 +15,9 @@ json.sections do @exported_plan.sections.each do |section| json.set! section.number do - json.title section.title + if @question_headings + json.title section.title + end json.questions do @exported_plan.questions_for_section(section.id).each do |question| json.set! question.number do @@ -23,7 +25,7 @@ answer = @exported_plan.plan.answer(question.id, false) q_format = question.question_format - + if answer.present? if (q_format.title == "Check box" || q_format.title == "Multi select box" || q_format.title == "Radio buttons" || q_format.title == "Dropdown") diff --git a/app/views/plans/export.pdf.erb b/app/views/plans/export.pdf.erb index c23fcbe..aa5f46d 100644 --- a/app/views/plans/export.pdf.erb +++ b/app/views/plans/export.pdf.erb @@ -31,7 +31,9 @@ <% end %> <% @sections.each do |section| %> -

<%= section.title %>

+ <% if @question_headings %> +

<%= section.title %>

+ <% end %> <% section.questions.each do |question| %> <% answer = @plan.answer(question.id, false) %> <% if answer.nil? && !@unanswered_questions then next end %> diff --git a/config/application.rb b/config/application.rb index e19efb1..477c57e 100644 --- a/config/application.rb +++ b/config/application.rb @@ -90,5 +90,8 @@ # is_test - (NOT advisable because test plans are excluded from statistics) # privately_visible - Only the owner and people they invite can access the plan config.default_plan_visibility = 'privately_visible' + + # The percentage of answered questions needed to enable the plan visibility section of the Share plan page + config.default_plan_percentage_answered = 50.00 end end diff --git a/lib/assets/javascripts/views/plans/download.js b/lib/assets/javascripts/views/plans/download.js index 2f5af98..621525d 100644 --- a/lib/assets/javascripts/views/plans/download.js +++ b/lib/assets/javascripts/views/plans/download.js @@ -1,6 +1,13 @@ $(() => { + // Add a target="_blank" to the form when PDF or HTML are selected // Hide the PDF Formatting section if 'pdf' is not the desired format - $('select#format').on('change', (e) => { + $('#download_form select#format').on('change', (e) => { + if ($(e.currentTarget).val() === 'pdf' || $(e.currentTarget).val() === 'html') { + $('#download_form').attr('target', '_blank'); + } else { + $('#download_form').removeAttr('target'); + } + if ($(e.currentTarget).val() === 'pdf') { $('#pdf-formatting').show(); } else { diff --git a/lib/assets/javascripts/views/plans/new.js b/lib/assets/javascripts/views/plans/new.js index 939ce06..63ed8c9 100644 --- a/lib/assets/javascripts/views/plans/new.js +++ b/lib/assets/javascripts/views/plans/new.js @@ -1,6 +1,8 @@ +import debounce from '../../utils/debounce'; import ariatiseForm from '../../utils/ariatiseForm'; import initAutoComplete from '../../utils/autoComplete'; import { isObject, isArray } from '../../utils/isType'; +import { isValidText } from '../../utils/isValidInputType'; $(() => { // AJAX success function for available template search @@ -38,29 +40,27 @@ const getMethod = jQueryForm => jQueryForm.attr('method'); // When one of the autocomplete fields changes, fetch the available templates - const handleComboboxChange = () => { - if ($('#plan_org_id').length && $('#plan_funder_id').length) { - const validOrg = ($('#plan_org_id').val().trim().length > 0 || $('#plan_no_org').prop('checked')); - const validFunder = ($('#plan_funder_id').val().trim().length > 0 || $('#plan_no_funder').prop('checked')); + const handleComboboxChange = debounce(() => { + const validOrg = (isValidText($('#plan_org_id').val()) || $('#plan_no_org').prop('checked')); + const validFunder = (isValidText($('#plan_funder_id').val()) || $('#plan_no_funder').prop('checked')); - if (!validOrg || !validFunder) { - $('#available-templates').fadeOut(); - $('#plan_template_id').val(''); - } else { - // Clear out the old template dropdown contents - $('#plan_template_id option').remove(); + if (!validOrg || !validFunder) { + $('#available-templates').fadeOut(); + $('#plan_template_id').val(''); + } else { + // Clear out the old template dropdown contents + $('#plan_template_id option').remove(); - // Fetch the available templates fbased on the funder and research org selected - const jQueryForm = $('form#new_plan'); - const formElements = jQueryForm.serializeArray(); - $.ajax({ - method: getMethod(jQueryForm), - url: getAction(jQueryForm), - data: formElements, - }).done(success, error); - } + // Fetch the available templates fbased on the funder and research org selected + const jQueryForm = $('form#new_plan'); + const formElements = jQueryForm.serializeArray(); + $.ajax({ + method: getMethod(jQueryForm), + url: getAction(jQueryForm), + data: formElements, + }).done(success, error); } - }; + }, 150); // When one of the checkboxes is clicked, disable the autocomplete input and clear its contents const handleCheckboxClick = (name, checked) => { @@ -107,6 +107,7 @@ }); // Initialize the form + $('#available-templates').hide(); handleComboboxChange(); if ($('#plan_no_org').prop('checked')) { handleCheckboxClick('org', $('#plan_no_org').prop('checked')); diff --git a/test/functional/plans_controller_test.rb b/test/functional/plans_controller_test.rb index ef6a4f4..c61c47c 100644 --- a/test/functional/plans_controller_test.rb +++ b/test/functional/plans_controller_test.rb @@ -148,23 +148,22 @@ # ---------------------------------------------------------- test 'duplicate a plan' do # Should redirect user to the root path if they are not logged in! - post duplicate_plan_path(@plan, format: :js) + post duplicate_plan_path(@plan) assert_unauthorized_redirect_to_root_path # User who is does not have access to the plan sign_in User.first - put plan_path(@plan, format: :js) + put plan_path(@plan) assert_equal _('You are not authorized to perform this action.'), flash[:alert] assert_response :redirect assert_redirected_to plans_url sign_in @user - post duplicate_plan_path(@plan, format: :js) + post duplicate_plan_path(@plan) @duplicate_plan = Plan.last assert flash[:notice].start_with?('Successfully') && flash[:notice].include?('copied') - assert_response :success - assert assigns(:plan) - assert_equal 'Copy of Test Plan', @duplicate_plan.title, "Copy of" + assert_response :redirect + assert_redirected_to plan_url(@duplicate_plan) end
<%= _('Questions')%><%= _('Answers')%>