diff --git a/Gemfile b/Gemfile index 768e046..2d9b5b9 100644 --- a/Gemfile +++ b/Gemfile @@ -34,7 +34,7 @@ # ------------------------------------------------ # SUPER ADMIN SECTION -gem "administrate", :github => 'thoughtbot/administrate', :branch =>'master' #:branch =>'v0.4.0' +gem "administrate", :github => 'thoughtbot/administrate', :branch =>'v0.7.0' # ------------------------------------------------ # USERS diff --git a/Gemfile.lock b/Gemfile.lock index 31af02b..53bf3cf 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,18 +1,18 @@ GIT remote: git://github.com/thoughtbot/administrate.git - revision: 47a2f99e22be87119ef0e3a7af3956984df5842c - branch: master + revision: 6bc227907e8567ca9d8c51658abc92fc2b19ca2d + branch: v0.7.0 specs: - administrate (0.4.0) + administrate (0.7.0) + actionpack (>= 4.2, < 5.1) + actionview (>= 4.2, < 5.1) + activerecord (>= 4.2, < 5.1) autoprefixer-rails (~> 6.0) - bourbon (>= 5.0.0.beta.6) datetime_picker_rails (~> 0.0.7) - jquery-rails (~> 4.0) + jquery-rails (>= 4.0) kaminari (>= 1.0) momentjs-rails (~> 2.8) - neat (~> 1.1) normalize-rails (>= 3.0) - rails (>= 4.2, < 5.1) sass-rails (~> 5.0) selectize-rails (~> 0.6) @@ -65,7 +65,7 @@ addressable (2.4.0) ansi (1.5.0) arel (6.0.3) - autoprefixer-rails (6.7.7.1) + autoprefixer-rails (6.7.7.2) execjs bcrypt (3.1.11) better_errors (2.1.1) @@ -74,9 +74,6 @@ rack (>= 0.9.0) binding_of_caller (0.7.2) debug_inspector (>= 0.0.1) - bourbon (5.0.0.beta.7) - sass (~> 3.4.22) - thor (~> 0.19.1) builder (3.2.2) byebug (9.0.5) capybara (2.9.1) @@ -218,9 +215,6 @@ multi_xml (0.5.5) multipart-post (2.0.0) mysql2 (0.3.21) - neat (1.8.0) - sass (>= 3.3) - thor (~> 0.19) nokogiri (1.6.8) mini_portile2 (~> 2.1.0) pkg-config (~> 1.1.7) diff --git a/app/controllers/admin/annotations_controller.rb b/app/controllers/admin/annotations_controller.rb new file mode 100644 index 0000000..f0881d1 --- /dev/null +++ b/app/controllers/admin/annotations_controller.rb @@ -0,0 +1,21 @@ +module Admin + class AnnotationsController < Admin::ApplicationController + # To customize the behavior of this controller, + # you can overwrite any of the RESTful actions. For example: + # + # def index + # super + # @resources = Annotation. + # page(params[:page]). + # per(10) + # end + + # Define a custom finder by overriding the `find_resource` method: + # def find_resource(param) + # Annotation.find_by!(slug: param) + # end + + # See https://administrate-prototype.herokuapp.com/customizing_controller_actions + # for more information + end +end diff --git a/app/controllers/admin/answers_controller.rb b/app/controllers/admin/answers_controller.rb index c220635..8960f9f 100644 --- a/app/controllers/admin/answers_controller.rb +++ b/app/controllers/admin/answers_controller.rb @@ -1,7 +1,7 @@ module Admin class AnswersController < Admin::ApplicationController # To customize the behavior of this controller, - # simply overwrite any of the RESTful actions. For example: + # you can overwrite any of the RESTful actions. For example: # # def index # super diff --git a/app/controllers/admin/exported_plans_controller.rb b/app/controllers/admin/exported_plans_controller.rb index aecd7f0..e4bde90 100644 --- a/app/controllers/admin/exported_plans_controller.rb +++ b/app/controllers/admin/exported_plans_controller.rb @@ -1,7 +1,7 @@ module Admin class ExportedPlansController < Admin::ApplicationController # To customize the behavior of this controller, - # simply overwrite any of the RESTful actions. For example: + # you can overwrite any of the RESTful actions. For example: # # def index # super diff --git a/app/controllers/admin/guidance_groups_controller.rb b/app/controllers/admin/guidance_groups_controller.rb index 6b9ad4b..95d3e7c 100644 --- a/app/controllers/admin/guidance_groups_controller.rb +++ b/app/controllers/admin/guidance_groups_controller.rb @@ -1,7 +1,7 @@ module Admin class GuidanceGroupsController < Admin::ApplicationController # To customize the behavior of this controller, - # simply overwrite any of the RESTful actions. For example: + # you can overwrite any of the RESTful actions. For example: # # def index # super diff --git a/app/controllers/admin/guidances_controller.rb b/app/controllers/admin/guidances_controller.rb index 6690ff3..1f2495f 100644 --- a/app/controllers/admin/guidances_controller.rb +++ b/app/controllers/admin/guidances_controller.rb @@ -1,7 +1,7 @@ module Admin class GuidancesController < Admin::ApplicationController # To customize the behavior of this controller, - # simply overwrite any of the RESTful actions. For example: + # you can overwrite any of the RESTful actions. For example: # # def index # super diff --git a/app/controllers/admin/identifier_schemes_controller.rb b/app/controllers/admin/identifier_schemes_controller.rb index 43911d1..eb50d8a 100644 --- a/app/controllers/admin/identifier_schemes_controller.rb +++ b/app/controllers/admin/identifier_schemes_controller.rb @@ -1,7 +1,7 @@ module Admin class IdentifierSchemesController < Admin::ApplicationController # To customize the behavior of this controller, - # simply overwrite any of the RESTful actions. For example: + # you can overwrite any of the RESTful actions. For example: # # def index # super diff --git a/app/controllers/admin/languages_controller.rb b/app/controllers/admin/languages_controller.rb index e70e606..e58baa9 100644 --- a/app/controllers/admin/languages_controller.rb +++ b/app/controllers/admin/languages_controller.rb @@ -1,7 +1,7 @@ module Admin class LanguagesController < Admin::ApplicationController # To customize the behavior of this controller, - # simply overwrite any of the RESTful actions. For example: + # you can overwrite any of the RESTful actions. For example: # # def index # super diff --git a/app/controllers/admin/notes_controller.rb b/app/controllers/admin/notes_controller.rb index c740124..db832f4 100644 --- a/app/controllers/admin/notes_controller.rb +++ b/app/controllers/admin/notes_controller.rb @@ -1,7 +1,7 @@ module Admin class NotesController < Admin::ApplicationController # To customize the behavior of this controller, - # simply overwrite any of the RESTful actions. For example: + # you can overwrite any of the RESTful actions. For example: # # def index # super diff --git a/app/controllers/admin/orgs_controller.rb b/app/controllers/admin/orgs_controller.rb index 91092db..d68f358 100644 --- a/app/controllers/admin/orgs_controller.rb +++ b/app/controllers/admin/orgs_controller.rb @@ -1,7 +1,7 @@ module Admin class OrgsController < Admin::ApplicationController # To customize the behavior of this controller, - # simply overwrite any of the RESTful actions. For example: + # you can overwrite any of the RESTful actions. For example: # # def index # super diff --git a/app/controllers/admin/perms_controller.rb b/app/controllers/admin/perms_controller.rb index 4e153cc..99e9b95 100644 --- a/app/controllers/admin/perms_controller.rb +++ b/app/controllers/admin/perms_controller.rb @@ -1,7 +1,7 @@ module Admin class PermsController < Admin::ApplicationController # To customize the behavior of this controller, - # simply overwrite any of the RESTful actions. For example: + # you can overwrite any of the RESTful actions. For example: # # def index # super diff --git a/app/controllers/admin/phases_controller.rb b/app/controllers/admin/phases_controller.rb index 5877a50..de6e3c3 100644 --- a/app/controllers/admin/phases_controller.rb +++ b/app/controllers/admin/phases_controller.rb @@ -1,7 +1,7 @@ module Admin class PhasesController < Admin::ApplicationController # To customize the behavior of this controller, - # simply overwrite any of the RESTful actions. For example: + # you can overwrite any of the RESTful actions. For example: # # def index # super diff --git a/app/controllers/admin/plans_controller.rb b/app/controllers/admin/plans_controller.rb index aaa9189..424389f 100644 --- a/app/controllers/admin/plans_controller.rb +++ b/app/controllers/admin/plans_controller.rb @@ -1,7 +1,7 @@ module Admin class PlansController < Admin::ApplicationController # To customize the behavior of this controller, - # simply overwrite any of the RESTful actions. For example: + # you can overwrite any of the RESTful actions. For example: # # def index # super diff --git a/app/controllers/admin/question_formats_controller.rb b/app/controllers/admin/question_formats_controller.rb index 7644265..1338b69 100644 --- a/app/controllers/admin/question_formats_controller.rb +++ b/app/controllers/admin/question_formats_controller.rb @@ -1,7 +1,7 @@ module Admin class QuestionFormatsController < Admin::ApplicationController # To customize the behavior of this controller, - # simply overwrite any of the RESTful actions. For example: + # you can overwrite any of the RESTful actions. For example: # # def index # super diff --git a/app/controllers/admin/question_options_controller.rb b/app/controllers/admin/question_options_controller.rb index 237df25..103e543 100644 --- a/app/controllers/admin/question_options_controller.rb +++ b/app/controllers/admin/question_options_controller.rb @@ -1,7 +1,7 @@ module Admin class QuestionOptionsController < Admin::ApplicationController # To customize the behavior of this controller, - # simply overwrite any of the RESTful actions. For example: + # you can overwrite any of the RESTful actions. For example: # # def index # super diff --git a/app/controllers/admin/questions_controller.rb b/app/controllers/admin/questions_controller.rb index 1be38bf..be5d2a7 100644 --- a/app/controllers/admin/questions_controller.rb +++ b/app/controllers/admin/questions_controller.rb @@ -1,7 +1,7 @@ module Admin class QuestionsController < Admin::ApplicationController # To customize the behavior of this controller, - # simply overwrite any of the RESTful actions. For example: + # you can overwrite any of the RESTful actions. For example: # # def index # super diff --git a/app/controllers/admin/regions_controller.rb b/app/controllers/admin/regions_controller.rb index 44d2065..6e5627b 100644 --- a/app/controllers/admin/regions_controller.rb +++ b/app/controllers/admin/regions_controller.rb @@ -1,7 +1,7 @@ module Admin class RegionsController < Admin::ApplicationController # To customize the behavior of this controller, - # simply overwrite any of the RESTful actions. For example: + # you can overwrite any of the RESTful actions. For example: # # def index # super diff --git a/app/controllers/admin/roles_controller.rb b/app/controllers/admin/roles_controller.rb index 6633242..748c165 100644 --- a/app/controllers/admin/roles_controller.rb +++ b/app/controllers/admin/roles_controller.rb @@ -1,7 +1,7 @@ module Admin class RolesController < Admin::ApplicationController # To customize the behavior of this controller, - # simply overwrite any of the RESTful actions. For example: + # you can overwrite any of the RESTful actions. For example: # # def index # super diff --git a/app/controllers/admin/sections_controller.rb b/app/controllers/admin/sections_controller.rb index b06bb8c..3b94a0a 100644 --- a/app/controllers/admin/sections_controller.rb +++ b/app/controllers/admin/sections_controller.rb @@ -1,7 +1,7 @@ module Admin class SectionsController < Admin::ApplicationController # To customize the behavior of this controller, - # simply overwrite any of the RESTful actions. For example: + # you can overwrite any of the RESTful actions. For example: # # def index # super diff --git a/app/controllers/admin/splash_logs_controller.rb b/app/controllers/admin/splash_logs_controller.rb index 4cb0f85..aa3fa22 100644 --- a/app/controllers/admin/splash_logs_controller.rb +++ b/app/controllers/admin/splash_logs_controller.rb @@ -1,7 +1,7 @@ module Admin class SplashLogsController < Admin::ApplicationController # To customize the behavior of this controller, - # simply overwrite any of the RESTful actions. For example: + # you can overwrite any of the RESTful actions. For example: # # def index # super diff --git a/app/controllers/admin/suggested_answers_controller.rb b/app/controllers/admin/suggested_answers_controller.rb deleted file mode 100644 index 5ee314e..0000000 --- a/app/controllers/admin/suggested_answers_controller.rb +++ /dev/null @@ -1,21 +0,0 @@ -module Admin - class SuggestedAnswersController < Admin::ApplicationController - # To customize the behavior of this controller, - # simply overwrite any of the RESTful actions. For example: - # - # def index - # super - # @resources = SuggestedAnswer. - # page(params[:page]). - # per(10) - # end - - # Define a custom finder by overriding the `find_resource` method: - # def find_resource(param) - # SuggestedAnswer.find_by!(slug: param) - # end - - # See https://administrate-prototype.herokuapp.com/customizing_controller_actions - # for more information - end -end diff --git a/app/controllers/admin/templates_controller.rb b/app/controllers/admin/templates_controller.rb index 090a84b..8ac79e1 100644 --- a/app/controllers/admin/templates_controller.rb +++ b/app/controllers/admin/templates_controller.rb @@ -1,7 +1,7 @@ module Admin class TemplatesController < Admin::ApplicationController # To customize the behavior of this controller, - # simply overwrite any of the RESTful actions. For example: + # you can overwrite any of the RESTful actions. For example: # # def index # super diff --git a/app/controllers/admin/themes_controller.rb b/app/controllers/admin/themes_controller.rb index f15a730..9617c6c 100644 --- a/app/controllers/admin/themes_controller.rb +++ b/app/controllers/admin/themes_controller.rb @@ -1,7 +1,7 @@ module Admin class ThemesController < Admin::ApplicationController # To customize the behavior of this controller, - # simply overwrite any of the RESTful actions. For example: + # you can overwrite any of the RESTful actions. For example: # # def index # super diff --git a/app/controllers/admin/token_permission_types_controller.rb b/app/controllers/admin/token_permission_types_controller.rb index 7e90c37..37b3487 100644 --- a/app/controllers/admin/token_permission_types_controller.rb +++ b/app/controllers/admin/token_permission_types_controller.rb @@ -1,7 +1,7 @@ module Admin class TokenPermissionTypesController < Admin::ApplicationController # To customize the behavior of this controller, - # simply overwrite any of the RESTful actions. For example: + # you can overwrite any of the RESTful actions. For example: # # def index # super diff --git a/app/controllers/admin/user_identifiers_controller.rb b/app/controllers/admin/user_identifiers_controller.rb index 6699719..bdf5faa 100644 --- a/app/controllers/admin/user_identifiers_controller.rb +++ b/app/controllers/admin/user_identifiers_controller.rb @@ -1,7 +1,7 @@ module Admin class UserIdentifiersController < Admin::ApplicationController # To customize the behavior of this controller, - # simply overwrite any of the RESTful actions. For example: + # you can overwrite any of the RESTful actions. For example: # # def index # super diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index b456891..70251c0 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -1,7 +1,7 @@ module Admin class UsersController < Admin::ApplicationController # To customize the behavior of this controller, - # simply overwrite any of the RESTful actions. For example: + # you can overwrite any of the RESTful actions. For example: # # def index # super diff --git a/app/controllers/annotations_controller.rb b/app/controllers/annotations_controller.rb new file mode 100644 index 0000000..a262cb3 --- /dev/null +++ b/app/controllers/annotations_controller.rb @@ -0,0 +1,53 @@ +class AnnotationsController < ApplicationController + respond_to :html + after_action :verify_authorized + + #create annotations + def admin_create + @example_answer = Annotation.new(params[:annotation]) + authorize @example_answer + if @example_answer.save + redirect_to admin_show_phase_path(id: @example_answer.question.section.phase_id, section_id: @example_answer.question.section_id, question_id: @example_answer.question.id, edit: 'true'), notice: _('Information was successfully created.') + else + @section = @example_answer.question.section + @phase = @section.phase + @open = true + @sections = @phase.sections + @section_id = @section.id + @question_id = @example_answer.question + flash[:notice] = failed_create_error(@example_answer, _('example answer')) + render "phases/admin_show" + end + end + + + #update a example answer of a template + def admin_update + @example_answer = Annotation.includes(question: { section: {phase: :template}}).find(params[:id]) + authorize @example_answer #.question.section.phase.template + @question = @example_answer.question + @section = @question.section + @phase = @section.phase + if @example_answer.update_attributes(params[:annotation]) + redirect_to admin_show_phase_path(id: @phase.id, section_id: @section.id, question_id: @question.id, edit: 'true'), notice: _('Information was successfully updated.') + else + flash[:notice] = failed_update_error(@example_answer, _('example answer')) + render action: "phases/admin_show" + end + end + + #delete an annotation + def admin_destroy + @example_answer = Annotation.includes(question: { section: {phase: :template}}).find(params[:id]) + authorize @example_answer + @question = @example_answer.question + @section = @question.section + @phase = @section.phase + if @example_answer.destroy + redirect_to admin_show_phase_path(id: @phase.id, section_id: @section.id, edit: 'true'), notice: _('Information was successfully deleted.') + else + redirect_to admin_show_phase_path(id: @phase.id, section_id: @section.id, edit: 'true'), notice: flash[:notice] = failed_destroy_error(@example_answer, _('example answer')) + end + end + +end \ No newline at end of file diff --git a/app/controllers/answers_controller.rb b/app/controllers/answers_controller.rb index 550c56f..51f76b2 100644 --- a/app/controllers/answers_controller.rb +++ b/app/controllers/answers_controller.rb @@ -2,68 +2,50 @@ after_action :verify_authorized respond_to :html - ## # PUT/PATCH /[:locale]/answer/[:id] def update - # create a new answer based off the passed params - - ans_params = params[:answer] - plan_id = ans_params[:plan_id] - phase_id = ans_params[:phase_id] - user_id = ans_params[:user_id] - lock_version = ans_params[:lock_version] - question_id = ans_params[:question_id] + question_id = params[:answer][:question_id] + plan_id = params[:answer][:plan_id] @question = Question.find(question_id); # If an answer id is present load that answer otherwise load by plan/question @answer = Answer.find_by(plan_id: plan_id, question_id: question_id) - @old_answer, race_on_creation = nil, false + @old_answer = nil + @timestamp = nil - # This is the first answer for the question - if @answer.nil? - @answer = Answer.new(params[:answer]) + if @answer.nil? # If there is no answer for plan/question + @answer = Answer.new(permitted_params) @answer.text = params["answer-text-#{@answer.question_id}".to_sym] + authorize @answer - @answer.save - + if @answer.save + @timestamp = @answer.updated_at.iso8601 + end @lock_version = @answer.lock_version + elsif params[:answer][:id].blank? # Someone else already added an answer while the user was working + @old_answer = Marshal::load(Marshal.dump(@answer)) + @answer.text = params["answer-text-#{@answer.question_id}".to_sym] - # Someone else already added an answer while the user was working - elsif ans_params[:id].blank? - @old_answer = Marshal::load(Marshal.dump(@answer)) - @answer.text = params["answer-text-#{@answer.question_id}".to_sym] authorize @answer @lock_version = @answer.lock_version - - # We're updating an answer (let ActiveRecord check for a race condition) - else - # if you do the obvious clone here it will overwrite the old_answer text - # in the next line - #@old_answer = @answer.clone + else # We're about updating an answer (let ActiveRecord check for a race condition) @old_answer = Marshal::load(Marshal.dump(@answer)) @answer.text = params["answer-text-#{@answer.question_id}".to_sym] + authorize @answer - @answer.update(params[:answer]) - - # The save was successful so get the lock version and nil the - # old answer + if @answer.update(permitted_params) + @answer.touch # Saves the record with the updated_at set to the current time. Needed if only answer.question_options is updated + @timestamp = @answer.updated_at.iso8601 + end @lock_version = @answer.lock_version @old_answer = nil end @section_id = @answer.question.section.id - - # these are used for updating the status line @username = @answer.user.name - @timestamp = "" - - if @answer.text.present? - @timestamp = @answer.updated_at.iso8601 - end - @nquestions = 0 @nanswers = 0 @@ -71,6 +53,7 @@ @n_section_answers = 0 plan = Plan.find(plan_id) + # Problem of N+1 queries below plan.template.phases.each do |phase| phase.sections.each do |section| section.questions.each do |question| @@ -94,11 +77,20 @@ end rescue ActiveRecord::StaleObjectError - @username = @old_answer.user.name - @lock_version = @old_answer.lock_version - respond_to do |format| - format.js {} - end - end + @username = @old_answer.user.name + @lock_version = @old_answer.lock_version + respond_to do |format| + format.js {} + end + end # End update + + private + def permitted_params + permitted = params.require(:answer).permit(:id, :plan_id, :user_id, :question_id, :lock_version, :question_option_ids => []) + if !permitted[:question_option_ids].present? #If question_option_ids has been filtered out because it was a scalar value (e.g. radiobutton answer) + permitted[:question_option_ids] = [params[:answer][:question_option_ids]] # then convert to an Array + end + return permitted + end # End permitted_params end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index c952b33..a1ff6ad 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -106,4 +106,18 @@ msg end end + + ## + # Sign out of Shibboleth SP local session too. + # ------------------------------------------------------------- + def after_sign_out_path_for(resource_or_scope) + if Rails.application.config.shibboleth_enabled + return Rails.application.config.shibboleth_logout_url + root_url + super + else + super + end + end + # ------------------------------------------------------------- + end diff --git a/app/controllers/phases_controller.rb b/app/controllers/phases_controller.rb index 9d79400..99406e9 100644 --- a/app/controllers/phases_controller.rb +++ b/app/controllers/phases_controller.rb @@ -4,14 +4,16 @@ 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 authorize @plan phase_id = params[:id].to_i @phase = @plan.template.phases.select {|p| p.id == phase_id}.first + @readonly = !@plan.editable_by?(current_user.id) # the eager_load pulls in ALL answers # need to restrict to just ones for this plan @@ -43,7 +45,7 @@ # create a map from theme to array of guidances # where guidance is a hash with the text and the org name - theme_guidance = {} + theme_guidance = {} guidance_groups.each do |guidance_group| guidance_group.guidances.each do |guidance| @@ -54,7 +56,7 @@ end theme_guidance[title] << { text: guidance.text, - org: guidance_group.name + org: guidance_group.name + ':' } end end @@ -83,14 +85,14 @@ 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 + end - # GET /plans/PLANID/phases/PHASEID/status.json + # GET /plans/PLANID/phases/PHASEID/status.json def status @plan = Plan.eager_load(params[:plan_id]) authorize @plan @@ -110,9 +112,10 @@ @phase = Phase.eager_load(:sections).find_by('phases.id = ?', params[:id]) authorize @phase - @edit = (@phase.template.org == current_user.org) + @current = Template.current(@phase.template.dmptemplate_id) + @edit = (@phase.template.org == current_user.org) && (@phase.template == @current) #@edit = params[:edit] == "true" ? true : false - + #verify if there are any sections if not create one @sections = @phase.sections if !@sections.any?() || @sections.count == 0 @@ -158,13 +161,13 @@ def admin_create @phase = Phase.new(params[:phase]) authorize @phase - + @phase.description = params["phase-desc"] @phase.modifiable = true if @phase.save @phase.template.dirty = true @phase.template.save! - + redirect_to admin_show_phase_path(id: @phase.id, edit: 'true'), notice: _('Information was successfully created.') else flash[:notice] = failed_create_error(@phase, _('phase')) @@ -182,7 +185,7 @@ if @phase.update_attributes(params[:phase]) @phase.template.dirty = true @phase.template.save! - + redirect_to admin_show_phase_path(@phase), notice: _('Information was successfully updated.') else @sections = @phase.sections @@ -190,7 +193,7 @@ # These params may not be available in this context so they may need # to be set to true without the check @edit = true - @open = !params[:section_id].nil? + @open = !params[:section_id].nil? @section_id = (params[:section_id].nil? ? nil : params[:section_id].to_i) @question_id = (params[:question_id].nil? ? nil : params[:question_id].to_i) flash[:notice] = failed_update_error(@phase, _('phase')) @@ -206,15 +209,15 @@ if @phase.destroy @template.dirty = true @template.save! - + redirect_to admin_template_template_path(@template), notice: _('Information was successfully deleted.') else @sections = @phase.sections - + # These params may not be available in this context so they may need # to be set to true without the check @edit = true - @open = !params[:section_id].nil? + @open = !params[:section_id].nil? @section_id = (params[:section_id].nil? ? nil : params[:section_id].to_i) @question_id = (params[:question_id].nil? ? nil : params[:question_id].to_i) flash[:notice] = failed_destroy_error(@phase, _('phase')) diff --git a/app/controllers/plans_controller.rb b/app/controllers/plans_controller.rb index 526f76a..b17e7b7 100644 --- a/app/controllers/plans_controller.rb +++ b/app/controllers/plans_controller.rb @@ -59,6 +59,12 @@ if @plan.save @plan.assign_creator(current_user) + + # pre-select org's guidance + ggs = GuidanceGroup.where(org_id: plan_params[:org_id], + optional_subset: false, + published: true) + if !ggs.blank? then @plan.guidance_groups << ggs end default = Template.find_by(is_default: true) @@ -140,7 +146,7 @@ authorize @plan # If there was no phase specified use the template's 1st phase @phase = (params[:phase].nil? ? @plan.template.phases.first : Phase.find(params[:phase])) - @readonly = @plan.editable_by?(current_user.id) + @readonly = !@plan.editable_by?(current_user.id) respond_to :html end @@ -184,6 +190,7 @@ end end @plan.save + flash[:notice] = _('Guidance choices saved.') redirect_to action: "show" end @@ -458,21 +465,21 @@ # Load the org's template(s) unless org_id.nil? org = Org.find(org_id) - @templates = Template.where(published: true, org: org, customization_of: nil).to_a + @templates = Template.valid.where(published: true, org: org, customization_of: nil).to_a @msg = _("We found multiple DMP templates corresponding to the research organisation.") if @templates.count > 1 end else funder = Org.find(funder_id) # Load the funder's template(s) - @templates = Template.where(published: true, org: funder).to_a + @templates = Template.valid.where(published: true, org: funder).to_a unless org_id.blank? org = Org.find(org_id) # Swap out any organisational cusotmizations of a funder template @templates.each do |tmplt| - customization = Template.find_by(published: true, org: org, customization_of: tmplt.dmptemplate_id) + customization = Template.valid.find_by(published: true, org: org, customization_of: tmplt.dmptemplate_id) unless customization.nil? @templates.delete(tmplt) @templates << customization diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index 6ed74f3..14a6d21 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -6,12 +6,24 @@ def admin_create @question = Question.new(params[:question]) authorize @question - @question.guidance = params["new-question-guidance"] + example = @question.annotations.first + if example.present? + example.org_id = current_user.org_id + example.example_answer! + end + if params["new-question-guidance"].present? + guidance = @question.annotations.build + guidance.text = params["new-question-guidance"] + guidance.org_id = current_user.org_id + guidance.guidance! + guidance.save + end @question.default_value = params["new-question-default-value"] + @question.modifiable = true if @question.save @question.section.phase.template.dirty = true @question.section.phase.template.save! - + redirect_to admin_show_phase_path(id: @question.section.phase_id, section_id: @question.section_id, question_id: @question.id, edit: 'true'), notice: _('Information was successfully created.') else @edit = (@question.section.phase.template.org == current_user.org) @@ -21,7 +33,7 @@ @sections = @phase.sections @section_id = @question.section.id @question_id = @question.id - + flash[:notice] = failed_create_error(@question, _('question')) render template: 'phases/admin_show' end @@ -31,14 +43,23 @@ def admin_update @question = Question.find(params[:id]) authorize @question - @question.guidance = params["question-guidance-#{params[:id]}"] + guidance = @question.get_guidance_annotation(current_user.org_id) + if params["question-guidance-#{params[:id]}"].present? + if guidance.blank? + guidance = @question.annotations.build + guidance.type = :guidance + end + guidance.text = params["question-guidance-#{params[:id]}"] + guidance.save + end @question.default_value = params["question-default-value-#{params[:id]}"] @section = @question.section @phase = @section.phase + template = @phase.template if @question.update_attributes(params[:question]) - @question.section.phase.template.dirty = true - @question.section.phase.template.save! - + @phase.template.dirty = true + @phase.template.save! + redirect_to admin_show_phase_path(id: @phase.id, section_id: @section.id, question_id: @question.id, edit: 'true'), notice: _('Information was successfully updated.') else @edit = (@phase.template.org == current_user.org) @@ -46,7 +67,7 @@ @sections = @phase.sections @section_id = @section.id @question_id = @question.id - + flash[:notice] = failed_update_error(@question, _('question')) render template: 'phases/admin_show' end diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb index dbc8dd4..bcaf9b9 100644 --- a/app/controllers/registrations_controller.rb +++ b/app/controllers/registrations_controller.rb @@ -40,10 +40,10 @@ redirect_to after_sign_up_error_path_for(resource), alert: _('You must accept the terms and conditions to register.') else existing_user = User.find_by_email(sign_up_params[:email]) - if !existing_user.nil? then - if (existing_user.password == "" || existing_user.password.nil?) && existing_user.confirmed_at.nil? then - @user = existing_user - do_update(false, true) + if !existing_user.nil? # If email exists + if (existing_user.password == "" || existing_user.password.nil?) && existing_user.confirmed_at.nil? # If user has not accepted invitation yet + existing_user.destroy # Only solution for now + super else redirect_to after_sign_up_error_path_for(resource), alert: _('That email address is already registered.') end @@ -91,16 +91,16 @@ end def do_update(require_password = true, confirm = false) - if require_password # user is changing email or password - if current_user.email != params[:user][:email] # if user changing email - if params[:user][:current_password].blank? # password needs to be present + if require_password # user is changing email or password + if current_user.email != params[:user][:email] # if user is changing email + if params[:user][:current_password].blank? # password needs to be present message = _('Please enter your password to change email address.') successfully_updated = false else successfully_updated = current_user.update_with_password(password_update) end - elsif params[:user][:password].present? # user is changing password - successfully_updated = false # shared across first 3 conditions + elsif params[:user][:password].present? # if user is changing password + successfully_updated = false # shared across first 3 conditions if params[:user][:current_password].blank? message = _('Please enter your current password') elsif params[:user][:password_confirmation].blank? @@ -110,10 +110,10 @@ else successfully_updated = current_user.update_with_password(password_update) end - else # potentially unreachable... but I dont like to leave off the else + else # potentially unreachable... but I dont like to leave off the else successfully_updated = current_user.update_with_password(password_update) end - else # password not required + else # password not required successfully_updated = current_user.update_without_password(update_params) end @@ -125,7 +125,7 @@ #render the correct page if successfully_updated if confirm - current_user.skip_confirmation! + current_user.skip_confirmation! # will error out if confirmable is turned off in user model current_user.save! end session[:locale] = current_user.get_locale unless current_user.get_locale.nil? diff --git a/app/controllers/roles_controller.rb b/app/controllers/roles_controller.rb index 5bbc856..3a50d54 100644 --- a/app/controllers/roles_controller.rb +++ b/app/controllers/roles_controller.rb @@ -10,22 +10,26 @@ set_access_level(access_level) if params[:user].present? if @role.plan.owner.present? && @role.plan.owner.email == params[:user] - flash[:notice] = _('Impossible sharing plan with %{email} since that email matches with the owner of the plan.') % {email: params[:user]} - else - message = _('User added to project') - user = User.find_by(email: params[:user]) - if user.nil? - registered = false - User.invite!(email: params[:user]) - message = _('Invitation issued successfully.') + flash[:notice] = _('Cannot share plan with %{email} since that email matches with the owner of the plan.') % {email: params[:user]} + else + if Role.find_by(plan: @role.plan, user: User.find_by(email: params[:user])) # role already exists + flash[:notice] = _('Plan is already shared with %{email}.') % {email: params[:user]} + else + message = _('Plan shared with %{email}.') % {email: params[:user]} user = User.find_by(email: params[:user]) - end - @role.user = user - if @role.save - if registered then UserMailer.sharing_notification(@role, current_user).deliver_now end - flash[:notice] = message - else - flash[:notice] = failed_create_error(@role, _('role')) + if user.nil? + registered = false + User.invite!(email: params[:user]) + message = _('Invitation to %{email} issued successfully.') % {email: params[:user]} + user = User.find_by(email: params[:user]) + end + @role.user = user + if @role.save + if registered then UserMailer.sharing_notification(@role, current_user).deliver_now end + flash[:notice] = message + else + flash[:notice] = failed_create_error(@role, _('role')) + end end end else diff --git a/app/controllers/suggested_answers_controller.rb b/app/controllers/suggested_answers_controller.rb deleted file mode 100644 index e15d0c0..0000000 --- a/app/controllers/suggested_answers_controller.rb +++ /dev/null @@ -1,65 +0,0 @@ -class SuggestedAnswersController < ApplicationController - respond_to :html - after_action :verify_authorized - - #create suggested answers - def admin_create - @suggested_answer = SuggestedAnswer.new(params[:suggested_answer]) - authorize @suggested_answer - if @suggested_answer.save - # Set the template's dirty flag to true - @suggested_answer.question.section.phase.template.dirty = true - @suggested_answer.question.section.phase.template.save - - redirect_to admin_show_phase_path(id: @suggested_answer.question.section.phase_id, section_id: @suggested_answer.question.section_id, question_id: @suggested_answer.question.id, edit: 'true'), notice: _('Information was successfully created.') - else - @phase = @suggested_answer.question.section.phase - @section = @suggested_answer.question.section - @open = true - @sections = @phase.sections - @section_id = @section.id - @question_id = @suggested_answer.question - flash[:notice] = failed_create_error(@suggested_answer, _('suggested answer')) - render "phases/admin_show" - end - end - - - #update a suggested answer of a template - def admin_update - @suggested_answer = SuggestedAnswer.includes(question: { section: {phase: :template}}).find(params[:id]) - authorize @suggested_answer #.question.section.phase.template - @question = @suggested_answer.question - @section = @question.section - @phase = @section.phase - if @suggested_answer.update_attributes(params[:suggested_answer]) - # Set the template's dirty flag to true - @phase.template.dirty = true - @phase.template.save - - redirect_to admin_show_phase_path(id: @phase.id, section_id: @section.id, question_id: @question.id, edit: 'true'), notice: _('Information was successfully updated.') - else - flash[:notice] = failed_update_error(@suggested_answer, _('suggested answer')) - render action: "phases/admin_show" - end - end - - #delete a suggested answer - def admin_destroy - @suggested_answer = SuggestedAnswer.includes(question: { section: {phase: :template}}).find(params[:id]) - authorize @suggested_answer - @question = @suggested_answer.question - @section = @question.section - @phase = @section.phase - if @suggested_answer.destroy - # Set the template's dirty flag to true - @phase.template.dirty = true - @phase.template.save - - redirect_to admin_show_phase_path(id: @phase.id, section_id: @section.id, edit: 'true'), notice: _('Information was successfully deleted.') - else - redirect_to admin_show_phase_path(id: @phase.id, section_id: @section.id, edit: 'true'), notice: flash[:notice] = failed_destroy_error(@suggested_answer, _('suggested answer')) - end - end - -end \ No newline at end of file diff --git a/app/controllers/templates_controller.rb b/app/controllers/templates_controller.rb index 1f5efa4..76d55a5 100644 --- a/app/controllers/templates_controller.rb +++ b/app/controllers/templates_controller.rb @@ -10,30 +10,30 @@ # ----------------------------------------------------- def admin_index authorize Template - + funder_templates, org_templates, customizations = [], [], [] - + # Get all of the unique template family ids (dmptemplate_id) for each funder and the current org - funder_ids = Org.funders.includes(:templates).collect{|f| f.templates.collect{|ft| ft.dmptemplate_id } }.flatten.uniq - org_ids = current_user.org.templates.collect{|t| t.dmptemplate_id }.flatten.uniq - + funder_ids = Org.funders.includes(:templates).collect{|f| f.templates.valid.collect{|ft| ft.dmptemplate_id } }.flatten.uniq + org_ids = current_user.org.templates.valid.collect{|t| t.dmptemplate_id }.flatten.uniq + org_ids.each do |id| current = Template.current(id) live = Template.live(id) - + # If this isn't a customization of a funder template if current.customization_of.nil? org_templates << {current: current, live: live} - + # This is a customization of a funder template else funder_live = Template.live(current.customization_of) customizations << current.customization_of # Mark the customization as stale if the funder has a newer version - funder_templates << {current: current, live: live, stale: funder_live.updated_at > current.created_at} + funder_templates << {current: current, live: live, stale: funder_live.updated_at > current.created_at} end end - + # Get the funder templates funder_ids.each do |id| # If the org has a customization we don't want to load the funder version @@ -41,11 +41,11 @@ funder_templates << {current: Template.current(id), live: Template.live(id)} end end - - @funder_templates = funder_templates.sort{|x,y| + + @funder_templates = funder_templates.sort{|x,y| x[:current].title <=> y[:current].title } - @org_templates = org_templates.sort{|x,y| + @org_templates = org_templates.sort{|x,y| x[:current].title <=> y[:current].title } end @@ -55,7 +55,7 @@ def admin_customize @template = Template.find(params[:id]) authorize @template - + customisation = Template.deep_copy(@template) customisation.org = current_user.org customisation.version = 0 @@ -65,7 +65,7 @@ break random unless Template.exists?(dmptemplate_id: random) end customisation.save - + customisation.phases.includes(:sections, :questions).each do |phase| phase.modifiable = false phase.save! @@ -78,10 +78,10 @@ end end end - + redirect_to admin_template_template_path(customisation) end - + # PUT /org/admin/templates/:id/admin_publish # ----------------------------------------------------- def admin_publish @@ -89,7 +89,7 @@ authorize @template current = Template.current(@template.dmptemplate_id) - + # Only allow the current version to be updated if current != @template redirect_to admin_template_template_path(@template), notice: _('You can not publish a historical version of this template.') @@ -106,12 +106,6 @@ @template.published = true @template.save - # Create a new version - new_version = Template.deep_copy(@template) - new_version.version = (@template.version + 1) - new_version.published = false - new_version.save - flash[:notice] = _('Your template has been published and is now available to users.') redirect_to admin_index_template_path(current_user.org) @@ -134,23 +128,43 @@ @template.save flash[:notice] = _('Your template is no longer published. Users will not be able to create new DMPs for this template until you re-publish it') end - + redirect_to admin_index_template_path(current_user.org) end # GET /org/admin/templates/:id/admin_template # ----------------------------------------------------- def admin_template - @template = Template.includes(:org, phases: [sections: [questions: [:question_options, :question_format, - :suggested_answers]]]).find(params[:id]) + @template = Template.includes(:org, phases: [sections: [questions: [:question_options, :question_format, :annotations]]]).find(params[:id]) authorize @template - + @current = Template.current(@template.dmptemplate_id) - - unless @template == @current + + if @template == @current + # If the template is published + if @template.published? + # We need to create a new, editable version + new_version = Template.deep_copy(@template) + new_version.version = (@template.version + 1) + new_version.published = false + new_version.save + @template = new_version +# @current = Template.current(@template.dmptemplate_id) + end + else flash[:notice] = _('You are viewing a historical version of this template. You will not be able to make changes.') end - + + # If the template is published + if @template.published? + # We need to create a new, editable version + new_version = Template.deep_copy(@template) + new_version.version = (@template.version + 1) + new_version.published = false + new_version.save + @template = new_version + end + # once the correct template has been generated, we convert it to hash @hash = @template.to_hash end @@ -161,9 +175,9 @@ def admin_update @template = Template.find(params[:id]) authorize @template - + current = Template.current(@template.dmptemplate_id) - + # Only allow the current version to be updated if current != @template redirect_to admin_template_template_path(@template), notice: _('You can not edit a historical version of this template.') @@ -173,18 +187,18 @@ @template.title != params[:template][:title] @template.dirty = true end - + @template.description = params["template-desc"] if @template.update_attributes(params[:template]) flash[:notice] = _('Information was successfully updated.') - + else flash[:notice] = failed_update_error(@template, _('template')) end - + @hash = @template.to_hash render 'admin_template' - end + end end @@ -219,9 +233,9 @@ def admin_destroy @template = Template.find(params[:id]) authorize @template - + current = Template.current(@template.dmptemplate_id) - + # Only allow the current version to be destroyed if current == @template if @template.destroy @@ -246,4 +260,4 @@ @current = Template.current(@template.dmptemplate_id) end -end \ No newline at end of file +end diff --git a/app/controllers/users/omniauth_callbacks_controller.rb b/app/controllers/users/omniauth_callbacks_controller.rb index 0c6a600..d50e397 100644 --- a/app/controllers/users/omniauth_callbacks_controller.rb +++ b/app/controllers/users/omniauth_callbacks_controller.rb @@ -61,6 +61,7 @@ end # ------------------------------------------------------------- + def failure redirect_to root_path end diff --git a/app/dashboards/annotation_dashboard.rb b/app/dashboards/annotation_dashboard.rb new file mode 100644 index 0000000..e26b609 --- /dev/null +++ b/app/dashboards/annotation_dashboard.rb @@ -0,0 +1,60 @@ +require "administrate/base_dashboard" + +class AnnotationDashboard < Administrate::BaseDashboard + # ATTRIBUTE_TYPES + # a hash that describes the type of each of the model's fields. + # + # Each different type represents an Administrate::Field object, + # which determines how the attribute is displayed + # on pages throughout the dashboard. + ATTRIBUTE_TYPES = { + org: Field::BelongsTo, + question: Field::BelongsTo, + id: Field::Number, + text: Field::Text, + type: Field::String.with_options(searchable: false), + created_at: Field::DateTime, + updated_at: Field::DateTime, + }.freeze + + # COLLECTION_ATTRIBUTES + # an array of attributes that will be displayed on the model's index page. + # + # By default, it's limited to four items to reduce clutter on index pages. + # Feel free to add, remove, or rearrange items. + COLLECTION_ATTRIBUTES = [ + :org, + :question, + :id, + :text, + ].freeze + + # SHOW_PAGE_ATTRIBUTES + # an array of attributes that will be displayed on the model's show page. + SHOW_PAGE_ATTRIBUTES = [ + :org, + :question, + :id, + :text, + :type, + :created_at, + :updated_at, + ].freeze + + # FORM_ATTRIBUTES + # an array of attributes that will be displayed + # on the model's form (`new` and `edit`) pages. + FORM_ATTRIBUTES = [ + :org, + :question, + :text, + :type, + ].freeze + + # Overwrite this method to customize how annotations are displayed + # across all pages of the admin dashboard. + # + # def display_resource(annotation) + # "Annotation ##{annotation.id}" + # end +end diff --git a/app/dashboards/org_dashboard.rb b/app/dashboards/org_dashboard.rb index 013748f..c00db7b 100644 --- a/app/dashboards/org_dashboard.rb +++ b/app/dashboards/org_dashboard.rb @@ -12,7 +12,7 @@ guidance_groups: Field::HasMany, templates: Field::HasMany, users: Field::HasMany, - suggested_answers: Field::HasMany, + annotations: Field::HasMany, token_permission_types: Field::HasMany, id: Field::Number, name: Field::String, @@ -59,7 +59,7 @@ :contact_email, :org_type, :users, - :suggested_answers, + :annotations, :token_permission_types, :id, :target_url, @@ -84,7 +84,7 @@ :guidance_groups, # :templates, # :users, -# :suggested_answers, +# :annotations, :token_permission_types, :name, :abbreviation, diff --git a/app/dashboards/question_dashboard.rb b/app/dashboards/question_dashboard.rb index 1d1f8aa..af3d8bb 100644 --- a/app/dashboards/question_dashboard.rb +++ b/app/dashboards/question_dashboard.rb @@ -10,7 +10,7 @@ ATTRIBUTE_TYPES = { answers: Field::HasMany, question_options: Field::HasMany, - suggested_answers: Field::HasMany, + annotations: Field::HasMany, themes: Field::HasMany, section: Field::BelongsTo, question_format: Field::BelongsTo, @@ -33,7 +33,7 @@ COLLECTION_ATTRIBUTES = [ :answers, :question_options, - :suggested_answers, + :annotations, :themes, ].freeze @@ -42,7 +42,7 @@ SHOW_PAGE_ATTRIBUTES = [ :answers, :question_options, - :suggested_answers, + :annotations, :themes, :section, :question_format, @@ -63,7 +63,7 @@ FORM_ATTRIBUTES = [ :answers, :question_options, - :suggested_answers, + :annotations, :themes, :section, :question_format, diff --git a/app/dashboards/suggested_answer_dashboard.rb b/app/dashboards/suggested_answer_dashboard.rb deleted file mode 100644 index 980ef11..0000000 --- a/app/dashboards/suggested_answer_dashboard.rb +++ /dev/null @@ -1,60 +0,0 @@ -require "administrate/base_dashboard" - -class SuggestedAnswerDashboard < Administrate::BaseDashboard - # ATTRIBUTE_TYPES - # a hash that describes the type of each of the model's fields. - # - # Each different type represents an Administrate::Field object, - # which determines how the attribute is displayed - # on pages throughout the dashboard. - ATTRIBUTE_TYPES = { - org: Field::BelongsTo, - question: Field::BelongsTo, - id: Field::Number, - text: Field::Text, - is_example: Field::Boolean, - created_at: Field::DateTime, - updated_at: Field::DateTime, - }.freeze - - # COLLECTION_ATTRIBUTES - # an array of attributes that will be displayed on the model's index page. - # - # By default, it's limited to four items to reduce clutter on index pages. - # Feel free to add, remove, or rearrange items. - COLLECTION_ATTRIBUTES = [ - :org, - :question, - :id, - :text, - ].freeze - - # SHOW_PAGE_ATTRIBUTES - # an array of attributes that will be displayed on the model's show page. - SHOW_PAGE_ATTRIBUTES = [ - :org, - :question, - :id, - :text, - :is_example, - :created_at, - :updated_at, - ].freeze - - # FORM_ATTRIBUTES - # an array of attributes that will be displayed - # on the model's form (`new` and `edit`) pages. - FORM_ATTRIBUTES = [ - :org, - :question, - :text, - :is_example, - ].freeze - - # Overwrite this method to customize how suggested answers are displayed - # across all pages of the admin dashboard. - # - # def display_resource(suggested_answer) - # "SuggestedAnswer ##{suggested_answer.id}" - # end -end diff --git a/app/dashboards/user_dashboard.rb b/app/dashboards/user_dashboard.rb index 687d2b9..4ba23ad 100644 --- a/app/dashboards/user_dashboard.rb +++ b/app/dashboards/user_dashboard.rb @@ -82,7 +82,6 @@ :plans, :user_identifiers, :identifier_schemes, - :setting_objects, :orcid_id, :shibboleth_id, :created_at, @@ -122,7 +121,6 @@ # :plans, :user_identifiers, :identifier_schemes, -# :setting_objects, :firstname, :surname, :email, @@ -159,5 +157,4 @@ def display_resource(user) "#{user.firstname} #{user.surname} (##{user.id})" end - end diff --git a/app/models/annotation.rb b/app/models/annotation.rb new file mode 100644 index 0000000..8ea44dd --- /dev/null +++ b/app/models/annotation.rb @@ -0,0 +1,40 @@ +class Annotation < ActiveRecord::Base + enum type: [ :example_answer, :guidance] + ## + # Associations + belongs_to :org + belongs_to :question + + ## + # I liked type as the name for the enum so overriding inheritance column + self.inheritance_column = nil + + ## + # Possibly needed for active_admin + # -relies on protected_attributes gem as syntax depricated in rails 4.2 + attr_accessible :org_id, :question_id, :text, :type, + :org, :question, :as => [:default, :admin] + + + validates :question, :org, presence: {message: _("can't be blank")} + + ## + # returns the text from the annotation + # + # @return [String] the text from the annotation + def to_s + "#{text}" + end + + + ## + # deep copy the given annotation and all it's associations + # + # @params [Annotation] annotation to be deep copied + # @return [Annotation] the saved, copied annotation + def self.deep_copy(annotation) + annotation_copy = annotation.dup + annotation_copy.save! + return annotation_copy + end +end \ No newline at end of file diff --git a/app/models/answer.rb b/app/models/answer.rb index 54ac937..8267948 100644 --- a/app/models/answer.rb +++ b/app/models/answer.rb @@ -36,4 +36,10 @@ # # # Make sure the plan and question are associated with the same template! # validates :plan, :question, answer_for_correct_template: true + + # This method helps to decide if an answer option (:radiobuttons, :checkbox, etc ) in form views should be checked or not + # Returns true if the given option_id is present in question_options, otherwise returns false + def has_question_option(option_id) + self.question_option_ids.include?(option_id) + end end diff --git a/app/models/exported_plan.rb b/app/models/exported_plan.rb index 5684072..0bd9ef1 100644 --- a/app/models/exported_plan.rb +++ b/app/models/exported_plan.rb @@ -80,13 +80,12 @@ end def sections - phase_id = self.phase_id ||= self.plan.template.phases.first.id # Use the first phase if none was specified - sections = Phase.find(phase_id).sections - sections.sort_by(&:number) + self.phase_id ||= self.plan.template.phases.first.id + Section.where({phase_id: phase_id}).order(:number) end def questions_for_section(section_id) - questions.where(section_id: section_id).sort_by(&:number) + Question.where(id: questions).where(section_id: section_id).order(:number) end def admin_details @@ -104,16 +103,25 @@ CSV.generate do |csv| csv << [_('Section'),_('Question'),_('Answer'),_('Selected option(s)'),_('Answered by'),_('Answered at')] self.sections.each do |section| - self.questions_for_section(section).each do |question| - answer = self.plan.answer(question.id) - q_format = question.question_format - if q_format.title == _('Check box') || q_format.title == _('Multi select box') || - q_format.title == _('Radio buttons') || q_format.title == _('Dropdown') - options_string = answer.options.collect {|o| o.text}.join('; ') - else - options_string = '' + questions = self.questions_for_section(section) + if questions.present? + questions.each do |question| + answer = self.plan.answer(question.id) + q_format = question.question_format + if q_format.option_based? + options_string = answer.question_options.collect {|o| o.text}.join('; ') + else + options_string = '' + end + csv << [ + section.title, + sanitize_text(question.text), + question.option_comment_display ? sanitize_text(answer.text) : '', + options_string, + user.name, + answer.updated_at + ] end - csv << [section.title, sanitize_text(question.text), sanitize_text(answer.text), options_string, user.name, answer.updated_at] end end end @@ -122,58 +130,58 @@ def as_txt output = "#{self.plan.title}\n\n#{self.plan.template.title}\n" output += "\n"+_('Details')+"\n\n" - puts 'admin_details: '+self.admin_details.inspect self.admin_details.each do |at| value = self.send(at) if value.present? output += admin_field_t(at.to_s) + ": " + value + "\n" + else + output += admin_field_t(at.to_s) + ": " + _('-') + "\n" end end self.sections.each do |section| - output += "\n#{section.title}\n" + questions = self.questions_for_section(section) + if questions.present? + output += "\n#{section.title}\n" + questions.each do |question| + qtext = sanitize_text( question.text.gsub(/
| <%= _('Example Answer')%> | +
+
|
+
| <%= _('Example of Answer')%> | +
+
|
+
| + <%= _('Example of answer')%> + | +<%= raw annotation.text %> | +
| <%= _('Text') %> | -
+
+ |
<%= text_area_tag("guidance-text", @guidance.text, class: "tinymce") %>
diff --git a/app/views/guidances/admin_new.html.erb b/app/views/guidances/admin_new.html.erb
index eb7f7a3..95239c0 100644
--- a/app/views/guidances/admin_new.html.erb
+++ b/app/views/guidances/admin_new.html.erb
@@ -19,7 +19,8 @@
diff --git a/app/views/plans/export.pdf.erb b/app/views/plans/export.pdf.erb
index c41b244..b9ab084 100644
--- a/app/views/plans/export.pdf.erb
+++ b/app/views/plans/export.pdf.erb
@@ -21,47 +21,47 @@
<%= section.title %>-
<%= @plan.title %><% @exported_plan.admin_details.each do |field| value = @exported_plan.send(field) - if value.present? - %> -<%= admin_field_t(field.to_s) -%> <%= value -%> + if value.present? %> +<%= admin_field_t(field.to_s) -%> <%= value -%> + <% else %> +<%= admin_field_t(field.to_s) -%> <%= _('-') %> <% end %> <% end %> <% @exported_plan.sections.each do |section| %> -<%= section.title %>- <% questions = @exported_plan.questions_for_section(section.id) %> - <% questions.each_with_index do |question, idx| %> -
- <% unless idx == 0 && question.text == section.title %>
+ <% questions = @exported_plan.questions_for_section(section.id)
+ if questions.present?
+ %>
+ <%= section.title %>+ <% questions.each do |question| %> +<%= raw question.text %>- <% end %> - <% answer = @plan.answer(question.id, false) %> - <% if answer.nil? then %> -<%= _('Question not answered.') -%> - <% else %> - <% q_format = question.question_format%> - - <% if q_format.title == _('Check box') || q_format.title == _('Multi select box') || - q_format.title == _('Radio buttons') || q_format.title == _('Dropdown') then%> -
<%= _('Question not answered.') -%> + <% else %> + <% q_format = question.question_format %> + <% if q_format.option_based? %> +
|<\/td>| )*(<\/tr>| | |<\/td>| )*(<\/tr>| | |<\/td>| )*(<\/tr>| | |