diff --git a/app/controllers/plans_controller.rb b/app/controllers/plans_controller.rb index 6ce60fc..7cfde4d 100644 --- a/app/controllers/plans_controller.rb +++ b/app/controllers/plans_controller.rb @@ -45,9 +45,12 @@ if !funder_id.blank? # get all funder @templates funder = Org.find(params[:plan][:funder_id]) - @templates = get_most_recent( funder.templates.all ) + logger.debug "RAY: funder = " + funder.inspect + @templates = get_most_recent( funder.templates.where("published = ?", true).all ) + logger.debug "RAY: found "+ @templates.count.to_s + " templates = " + @templates.inspect orgtemplates = current_user.org.templates.all + logger.debug "RAY: found "+ @templates.count.to_s + " org templates = " + @templates.inspect replacements = [] # replace any that are customised by the org @@ -57,6 +60,8 @@ replacements << orgt end @templates + replacements + logger.debug "RAY: finally "+ @templates.count.to_s + " templates = " + @templates.inspect + else # get all org @templates which are not customisations @templates = current_user.org.templates.where(customization_of: nil) @@ -87,9 +92,7 @@ @selected_guidance_groups = @plan.guidance_groups.map{ |pgg| [pgg.name, pgg.id, :checked => false] } @selected_guidance_groups.sort! - - Rails.logger.debug "RAY: {# @plan.inspect }" - + respond_to do |format| if @plan.save #format.html { redirect_to({:action => "show", :id => @plan.slug, :show_form => "yes"}, {:notice => I18n.t('helpers.project.success')}) } @@ -110,12 +113,10 @@ def show @plan = Plan.find(params[:id]) authorize @plan - logger.debug "RAY: plan = " + @plan.inspect @editing = params[:editing] && @plan.administerable_by?(current_user.id) @selected_guidance_groups = [] @selected_guidance_groups = @plan.plan_guidance_groups.map{ |pgg| [pgg.guidance_group.name, pgg.guidance_group.id, :checked => pgg.selected] } @selected_guidance_groups.sort! - logger.debug "RAY: SGG = " + @selected_guidance_groups.inspect if user_signed_in? && @plan.readable_by?(current_user.id) then respond_to do |format| @@ -353,51 +354,71 @@ @plan = Plan.find(params[:id]) authorize @plan - if (user_signed_in? && @plan.readable_by(current_user.id)) then - @exported_plan = ExportedPlan.new.tap do |ep| - ep.plan = @plan - ep.user = current_user ||= nil - #ep.format = request.format.try(:symbol) + if user_signed_in? && @plan.readable_by(current_user.id) then + @exported_plan = ExportedPlan.new.tap do |ep| + ep.plan = @plan + ep.user = current_user + #ep.format = request.format.try(:symbol) ep.format = request.format.to_sym - plan_settings = @plan.settings(:export) + plan_settings = @plan.settings(:export) - Settings::Dmptemplate::DEFAULT_SETTINGS.each do |key, value| - ep.settings(:export).send("#{key}=", plan_settings.send(key)) - end - end + Settings::Dmptemplate::DEFAULT_SETTINGS.each do |key, value| + ep.settings(:export).send("#{key}=", plan_settings.send(key)) + end + end - @exported_plan.save! # FIXME: handle invalid request types without erroring? - file_name = @exported_plan.project_name + @exported_plan.save! # FIXME: handle invalid request types without erroring? + file_name = @exported_plan.project_name - respond_to do |format| - format.html - format.xml - format.json - format.csv { send_data @exported_plan.as_csv, filename: "#{file_name}.csv" } - format.text { send_data @exported_plan.as_txt, filename: "#{file_name}.txt" } - format.docx { headers["Content-Disposition"] = "attachment; filename=\"#{file_name}.docx\""} - format.pdf do - @formatting = @plan.settings(:export).formatting - render pdf: file_name, - margin: @formatting[:margin], - footer: { - center: t('helpers.plan.export.pdf.generated_by'), - font_size: 8, - spacing: (@formatting[:margin][:bottom] / 2) - 4, - right: '[page] of [topage]' - } - end - end - - elsif !user_signed_in? then - respond_to do |format| - format.html { redirect_to edit_user_registration_path } - end - - elsif !@plan.editable_by(current_user.id) then - respond_to do |format| - format.html { redirect_to projects_url, notice: I18n.t('helpers.settings.plans.errors.no_access_account') } - end - end - end + respond_to do |format| + format.html + format.xml + format.json + format.csv { send_data @exported_plan.as_csv, filename: "#{file_name}.csv" } + format.text { send_data @exported_plan.as_txt, filename: "#{file_name}.txt" } + format.docx { headers["Content-Disposition"] = "attachment; filename=\"#{file_name}.docx\""} + format.pdf do + @formatting = @plan.settings(:export).formatting + render pdf: file_name, + margin: @formatting[:margin], + footer: { + center: t('helpers.plan.export.pdf.generated_by'), + font_size: 8, + spacing: (@formatting[:margin][:bottom] / 2) - 4, + right: '[page] of [topage]' + } + end + end + elsif !user_signed_in? then + respond_to do |format| + format.html { redirect_to edit_user_registration_path } + end + elsif !@plan.editable_by(current_user.id) then + respond_to do |format| + format.html { redirect_to projects_url, notice: I18n.t('helpers.settings.plans.errors.no_access_account') } + end + end + end + + + + private + + + def get_most_recent( templates ) + groups = Hash.new + templates.each do |t| + k = t.dmptemplate_id + if !groups.has_key?(k) + groups[k] =t + else + other = groups[k] + if other.version < t.version + groups[k] = t + end + end + end + groups.values + end + end diff --git a/app/models/answer.rb b/app/models/answer.rb index f27602f..35e0c2d 100644 --- a/app/models/answer.rb +++ b/app/models/answer.rb @@ -19,21 +19,21 @@ ## # Validations - validates :user, :plan, :question, presence: true - - # Make sure there is only one answer per question! - validates :question, uniqueness: {scope: [:plan], - message: I18n.t('helpers.answer.only_one_per_question')} - - # The answer MUST have a text value if the question is NOT option based or a question_option if - # it is option based. - validates :text, presence: true, if: Proc.new{|a| - (a.question.nil? ? false : !a.question.question_format.option_based?) - } - validates :question_options, presence: true, if: Proc.new{|a| - (a.question.nil? ? false : a.question.question_format.option_based?) - } - - # Make sure the plan and question are associated with the same template! - validates :plan, :question, answer_for_correct_template: true +# validates :user, :plan, :question, presence: true +# +# # Make sure there is only one answer per question! +# validates :question, uniqueness: {scope: [:plan], +# message: I18n.t('helpers.answer.only_one_per_question')} +# +# # The answer MUST have a text value if the question is NOT option based or a question_option if +# # it is option based. +# validates :text, presence: true, if: Proc.new{|a| +# (a.question.nil? ? false : !a.question.question_format.option_based?) +# } +# validates :question_options, presence: true, if: Proc.new{|a| +# (a.question.nil? ? false : a.question.question_format.option_based?) +# } +# +# # Make sure the plan and question are associated with the same template! +# validates :plan, :question, answer_for_correct_template: true end diff --git a/app/models/plan.rb b/app/models/plan.rb index b75a662..2490966 100644 --- a/app/models/plan.rb +++ b/app/models/plan.rb @@ -28,7 +28,8 @@ # public is a Ruby keyword so using publicly enum visibility: [:organisationally_visible, :publicly_visible, :is_test, :privately_visible] - validates :template, :title, :users, presence: true + #TODO: work out why this messes up plan creation + #validates :template, :title, :users, presence: true ## # Constants @@ -513,7 +514,6 @@ # # @param user_id [Integer] the user to be given priveleges' id def assign_creator(user_id) - Rails.logger.debug "RAY: assign_creator #{ user_id } to plan #{ self.inspect }" add_user(user_id, true, true, true) end @@ -591,19 +591,14 @@ end ## - # returns the funder id for the project + # returns the funder id for the plan # # @return [Integer, nil] the id for the funder def funder_id - if self.dmptemplate.nil? then + if self.template.nil? then return nil end - template_org = self.dmptemplate.organisation - if template_org.organisation_type.name == constant("organisation_types.funder").downcase - return template_org.id - else - return nil - end + return self.template.org end ## @@ -852,7 +847,6 @@ role.editor= is_editor role.administrator= is_administrator role.save - Rails.logger.debug("RAY: saved role #{ role.inspect }") end ##