From d25177c6aa7a73086ed76286bfe3fb846623f003 Mon Sep 17 00:00:00 2001 From: Brian Riley Date: Fri, 16 Jun 2017 10:56:39 -0700 Subject: [PATCH] added check for nil owner --- app/models/plan.rb | 407 -------------------------- app/views/plans/public_index.html.erb | 4 +- 2 files changed, 2 insertions(+), 409 deletions(-) diff --git a/app/models/plan.rb b/app/models/plan.rb index b2691ff001..b8f3d35045 100644 --- a/app/models/plan.rb +++ b/app/models/plan.rb @@ -441,199 +441,6 @@ def status return status end -# TODO: Guessing this isn't in use since it still refers to Project and Version -=begin - ## - # defines and returns the details for the plan - # details consists of a hash of: project_title, phase_title, and for each section, - # section: title, question text for each question, answer type and answer value - # - # @return [Details] - def details - details = { - "project_title" => project.title, - "phase_title" => version.phase.title, - "sections" => {} - } - sections.sort_by(&:"number").each do |s| - details["sections"][s.number] = {} - details["sections"][s.number]["title"] = s.title - details["sections"][s.number]["questions"] = {} - s.questions.order("number").each do |q| - details["sections"][s.number]["questions"][q.number] = {} - details["sections"][s.number]["questions"][q.number]["question_text"] = q.text - answer = answer(q.id, false) - if ! answer.nil? then - q_format = q.question_format - if (q_format.title == t("helpers.checkbox") || q_format.title == t("helpers.multi_select_box") || - q_format.title == t("helpers.radio_buttons") || q_format.title == t("helpers.dropdown")) then - details["sections"][s.number]["questions"][q.number]["selections"] = {} - answer.options.each do |o| - details["sections"][s.number]["questions"][q.number]["selections"][o.number] = o.text - end - end - details["sections"][s.number]["questions"][q.number]["answer_text"] = answer.text - end - end - end - return details - end -=end - -# TODO: commenting this old lock stuff out since PlanSection is gone and we wanted to get rid of it -=begin - ## - # determines wether or not a specified section of a plan is locked to a specified user and returns a status hash - # - # @param section_id [Integer] the setion to determine if locked - # @param user_id [Integer] the user to determine if locked for - # @return [Hash{String => Hash{String => Boolean, nil, String, Integer}}] - def locked(section_id, user_id) - plan_section = plan_sections.where("section_id = ? AND user_id != ? AND release_time > ?", section_id, user_id, Time.now).last - if plan_section.nil? then - status = { - "locked" => false, - "locked_by" => nil, - "timestamp" => nil, - "id" => nil - } - else - status = { - "locked" => true, - "locked_by" => plan_section.user.name, - "timestamp" => plan_section.updated_at, - "id" => plan_section.id - } - end - end - - ## - # for each section, lock the section with the given user_id - # - # @param user_id [Integer] the id for the user who can use the sections - def lock_all_sections(user_id) - sections.each do |s| - lock_section(s.id, user_id, 1800) - end - end - - ## - # for each section, unlock the section - # - # @param user_id [Integer] the id for the user to unlock the sections for - def unlock_all_sections(user_id) - plan_sections.where(:user_id => user_id).order("created_at DESC").each do |lock| - lock.delete - end - end - - ## - # for each section, unlock the section - # Not sure how this is different from unlock_all_sections - # - # @param user_id [Integer] - def delete_recent_locks(user_id) - plan_sections.where(:user_id => user_id).each do |lock| - lock.delete - end - end - - ## - # Locks the specified section to only be used by the specified user, for the number of secconds specified - # - # @param section_id [Integer] the id of the section to be locked - # @param user_id [Integer] the id of the user who can use the section - # @param release_time [Integer] the number of secconds the section will be locked for, defaults to 60 - # @return [Boolean] wether or not the section was locked - def lock_section(section_id, user_id, release_time = 60) - status = locked(section_id, user_id) - if ! status["locked"] then - plan_section = PlanSection.new - plan_section.plan_id = id - plan_section.section_id = section_id - plan_section.release_time = Time.now + release_time.seconds - plan_section.user_id = user_id - plan_section.save - elsif status["current_user"] then - plan_section = PlanSection.find(status["id"]) - plan_section.release_time = Time.now + release_time.seconds - plan_section.save - else - return false - end - end - - ## - # unlocks the specified section for the specified user - # - # @param section_id [Integer] the id for the section to be unlocked - # @param user_id [Integer] the id for the user for whom the section was previously locked - # @return [Boolean] wether or not the lock was removed - def unlock_section(section_id, user_id) - plan_sections.where(:section_id => section_id, :user_id => user_id).order("created_at DESC").each do |lock| - lock.delete - end - end -=end - -# TODO: Commenting out because this method appears below as well so this one is overwritten -=begin - ## - # returns the time of either the latest answer to any question, or the latest update to the model - # - # @return [DateTime] the time at which the plan was last changed - def latest_update - if answers.any? then - last_answered = answers.order("updated_at DESC").first.updated_at - if last_answered > updated_at then - return last_answered - else - return updated_at - end - else - return updated_at - end - end -=end - -# TODO: Guessing this isn't in use since it still refers to Project and Version -=begin - ## - # returns an array of hashes. Each hash contains the question's id, the answer_id, - # the answer_text, the answer_timestamp, and the answer_options - # - # @param section_id [Integer] the section to find answers of - # @return [Array nil,String,Integer,DateTime}] - def section_answers(section_id) - section = Section.find(section_id) - section_questions = Array.new - counter = 0 - section.questions.each do |q| - section_questions[counter] = {} - section_questions[counter]["id"] = q.id - #section_questions[counter]["multiple_choice"] = q.multiple_choice - q_answer = answer(q.id, false) - if q_answer.nil? then - section_questions[counter]["answer_id"] = nil - if q.suggested_answers.find_by_organisation_id(project.organisation_id).nil? then - section_questions[counter]["answer_text"] = "" - else - section_questions[counter]["answer_text"] = q.default_value - end - section_questions[counter]["answer_timestamp"] = nil - section_questions[counter]["answer_options"] = Array.new - else - section_questions[counter]["answer_id"] = q_answer.id - section_questions[counter]["answer_text"] = q_answer.text - section_questions[counter]["answer_timestamp"] = q_answer.created_at - section_questions[counter]["answer_options"] = q_answer.options.pluck(:id) - end - counter = counter + 1 - end - return section_questions - end -=end - ## # assigns the passed user_id to the creater_role for the project @@ -645,83 +452,6 @@ def assign_creator(user_id) add_user(user_id, true, true, true) end - - -# TODO: commenting these out because they are overriden by private methods below, so this -# is unreachable -=begin - ## - # Based on the height of the text gathered so far and the available vertical - # space of the pdf, estimate a percentage of how much space has been used. - # This is highly dependent on the layout in the pdf. A more accurate approach - # would be to render the pdf and check how much space had been used, but that - # could be very slow. - # NOTE: This is only an estimate, rounded up to the nearest 5%; it is intended - # for guidance when editing plan data, not to be 100% accurate. - # - # @param used_height [Integer] an estimate of the height used so far - # @return [Integer] the estimate of space used of an A4 portrain - def estimate_space_used(used_height) - @formatting ||= self.settings(:export).formatting - - return 0 unless @formatting[:font_size] > 0 - - margin_height = @formatting[:margin][:top].to_i + @formatting[:margin][:bottom].to_i - page_height = A4_PAGE_HEIGHT - margin_height # 297mm for A4 portrait - available_height = page_height * self.template.settings(:export).max_pages - - percentage = (used_height / available_height) * 100 - (percentage / ROUNDING).ceil * ROUNDING # round up to nearest five - end - - ## - # Take a guess at the vertical height (in mm) of the given text based on the - # font-size and left/right margins stored in the plan's settings. - # This assumes a fixed-width for each glyph, which is obviously - # incorrect for the font-face choices available; the idea is that - # they'll hopefully average out to that in the long-run. - # Allows for hinting different font sizes (offset from base via font_size_inc) - # and vertical margins (i.e. for heading text) - # - # @param text [String] the text to estimate size of - # @param font_size_inc [Integer] the size of the font of the text, defaults to 0 - # @param vertical_margin [Integer] the top margin above the text, defaults to 0 - def height_of_text(text, font_size_inc = 0, vertical_margin = 0) - @formatting ||= self.settings(:export).formatting - @margin_width ||= @formatting[:margin][:left].to_i + @formatting[:margin][:right].to_i - @base_font_size ||= @formatting[:font_size] - - return 0 unless @base_font_size > 0 - - font_height = FONT_HEIGHT_CONVERSION_FACTOR * (@base_font_size + font_size_inc) - font_width = font_height * FONT_WIDTH_HEIGHT_RATIO # Assume glyph width averages at 2/5s the height - leading = font_height / 2 - - chars_in_line = (A4_PAGE_WIDTH - @margin_width) / font_width # 210mm for A4 portrait - num_lines = (text.length / chars_in_line).ceil - - (num_lines * font_height) + vertical_margin + leading - end -=end - -# TODO: What are these used for? Should just be using self.org and self.org.funder? -=begin - ## - # sets a new funder for the project - # defaults to the first dmptemplate if the current template is nill and the funder has more than one dmptemplate - # - # @param new_funder_id [Integer] the id for a new funder - # @return [Organisation] the new funder - def funder_id=(new_funder_id) - if new_funder_id != "" then - new_funder = Org.find(new_funder_id); - if new_funder.templates.count >= 1 && self.template.nil? then - self.template = new_funder.templates.first - end - end - end -=end - ## # returns the funder id for the plan # @@ -750,86 +480,6 @@ def funder end end -=begin - ## - # returns the name of the funder for the project - # - # @return [String] the name fo the funder for the project - def funder_name - if self.funder.nil? - return read_attribute(:funder_name) - else - return self.funder.name - end - end - - ## - # defines a new funder_name for the project. - # - # @param new_funder_name [String] the string name of the new funder - # @return [Integer, nil] the org_id of the new funder - def funder_name=(new_funder_name) - write_attribute(:funder_name, new_funder_name) - org_table = Org.arel_table - existing_org = Org.where(org_table[:name].matches(new_funder_name)) - if existing_org.nil? - existing_org = Org.where(org_table[:abbreviation].matches(new_funder_name)) - end - unless existing_org.empty? - self.funder_id=existing_org.id - end - end - - ## - # sets a new institution_id if there is no current organisation - # - # @param new_institution_id [Integer] the id for the new institution - # @return [Integer, Bool] false if an organisation exists, or the id of the set org if a new organisation is set - def institution_id=(new_institution_id) - if organisation.nil? then - self.organisation_id = new_institution_id - end - end - - ## - # returns the organisation which is root over the owning organisation - # - # @return [Integer, nil] the organisation_id or nil - def institution_id -# if organisation.nil? -# return nil -# else -# return organisation.root.id -# end - return template.org.id - end - - ## - # defines a new organisation_id for the project - # but is confusingly labled unit_id - # - # @param new_unit_id [Integer] - # @return [Integer, Boolean] the new organisation ID or false if no unit_id was passed - def unit_id=(new_unit_id) - unless new_unit_id.nil? ||new_unit_id == "" - self.organisation_id = new_unit_id - end - end - - ## - # returns the organisation_id or nil - # again seems redundant - # - # @return [nil, Integer] nil if no organisation, or the id if there is an organisation specified - def unit_id - if organisation.nil? || organisation.parent_id.nil? - return nil - else - return organisation_id - end - end -=end - ## # assigns the passed user_id as an editor for the project # gives the user rights to read and edit @@ -857,42 +507,6 @@ def assign_administrator(user_id) add_user(user_id, true, true) end -# TODO: ProjectGroup doesn't exist anymore so commenting these out -=begin - ## - # returns the projects which the user can atleast read - # - # @param user_id [Integer] the user to lookup projects for - # @return [Array] list of all projects the user can atleast read - def self.projects_for_user(user_id) - projects = Array.new - groups = ProjectGroup.where("user_id = ?", user_id) - unless groups.nil? then - groups.each do |group| - unless group.project.nil? then - projects << group.project - end - end - end - return projects - end - - ## - # whether or not the specified user_id created this project - # should be renamed to created_by? - # - # @param user_id [Integer] the user to check the priveleges of - # @return [Boolean] true if the user created the project - def created_by(user_id) - user = project_groups.find_by_user_id(user_id) - if (! user.nil?) && user.project_creator then - return true - else - return false - end - end -=end - ## # the datetime for the latest update of this plan # @@ -938,27 +552,6 @@ def last_edited self.latest_update.to_date end -# TODO: These next 2 reference defunct models so commenting out -=begin - ## - # whether or not the plan is shared with anybody - # - # @return [Boolean] true if the project has been shared - def shared? - self.project_groups.count > 1 - end - - alias_method :shared, :shared? - - ## - # the organisation who owns the project - # - # @return [Dmptemplate,Organisation,String] the template, it's owner, or it's owner's abreviation - def template_owner - self.dmptemplate.try(:organisation).try(:abbreviation) - end -=end - # Returns the number of answered questions from the entire plan def num_answered_questions n = 0 diff --git a/app/views/plans/public_index.html.erb b/app/views/plans/public_index.html.erb index 895f0b9cb5..bd69e83048 100644 --- a/app/views/plans/public_index.html.erb +++ b/app/views/plans/public_index.html.erb @@ -27,8 +27,8 @@ <%= plan.title %> <%= plan.template.title %> - <%= plan.owner.org.nil? ? _('Not Applicable') : plan.owner.org.name %> - <%= plan.owner.name %> + <%= (plan.owner.nil? || plan.owner.org.nil? ? _('Not Applicable') : plan.owner.org.name) %> + <%= (plan.owner.nil? ? _('Unknown') : plan.owner.name) %> <%= link_to _('PDF'), public_export_path(plan, format: :pdf),