diff --git a/app/controllers/annotations_controller.rb b/app/controllers/annotations_controller.rb index e83366b..73a34fb 100644 --- a/app/controllers/annotations_controller.rb +++ b/app/controllers/annotations_controller.rb @@ -16,6 +16,7 @@ # if they dont exist, no requirement for them to be saved ex_save = example_answer.present? ? example_answer.save : true guid_save = guidance.present? ? guidance.save : true + @question.section.phase.template.dirty = true if ex_save && guid_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.') @@ -73,6 +74,8 @@ @section = @question.section @phase = @section.phase + @phase.template.dirty = true + if ex_save && guid_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 @@ -95,6 +98,7 @@ @question = @example_answer.question @section = @question.section @phase = @section.phase + @phase.template.dirty = true 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 @@ -113,4 +117,4 @@ return annotation end -end \ No newline at end of file +end diff --git a/app/controllers/phases_controller.rb b/app/controllers/phases_controller.rb index 21084a9..82633c7 100644 --- a/app/controllers/phases_controller.rb +++ b/app/controllers/phases_controller.rb @@ -7,12 +7,12 @@ # GET /plans/:plan_id/phases/:id/edit def edit - @plan = Plan.eager_load2(params[:plan_id]) + @plan = Plan.load_for_phase(params[:plan_id], params[: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 + @phase = @plan.template.phases.first @readonly = !@plan.editable_by?(current_user.id) # Now we need to get all the themed guidance for the plan. @@ -46,35 +46,35 @@ end end - # create hash from question id to theme to guidance array - # so when we arerendering a question we can grab the guidance out of this - # - # question_guidance = { - # question.id => { - # theme => [ {text: "......", org: "....."} ] - # } - # } + questions = [] + # Appends all the questions for a given phase into questions Array. + @phase.sections.each do |section| + section.questions.each do |question| + questions.push(question) + end + end @question_guidance = {} - @plan.questions.each do |question| + # Puts in question_guidance (key/value) entries where key is the question id and value is a hash. + # Each question id hash has (key/value) entries where key is a theme and value is an Array of {text, org} objects + # Example hash + # question_guidance = { question.id => + # { theme => [ {text: "......", org: "....."} ] } + # } + questions.each do |question| qg = {} question.themes.each do |t| title = t.title qg[title] = theme_guidance[title] if theme_guidance.has_key?(title) end - if !@question_guidance.has_key?(question.id) - @question_guidance[question.id] = Array.new - end @question_guidance[question.id] = qg end if !user_signed_in? then respond_to do |format| format.html { redirect_to edit_user_registration_path } - end - end - + 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 f7edc1f..2c4d8c6 100644 --- a/app/controllers/plans_controller.rb +++ b/app/controllers/plans_controller.rb @@ -370,10 +370,10 @@ def template_options(org_id, funder_id) @templates = [] - if !org_id.blank? || !funder_id.blank? + if org_id.present? || funder_id.present? if funder_id.blank? # Load the org's template(s) - unless org_id.nil? + if org_id.present? org = Org.find(org_id) @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 @@ -384,20 +384,20 @@ # Load the funder's template(s) @templates = Template.valid.where(published: true, org: funder).to_a - unless org_id.blank? + if org_id.present? org = Org.find(org_id) # Swap out any organisational cusotmizations of a funder template @templates.each do |tmplt| customization = Template.valid.find_by(published: true, org: org, customization_of: tmplt.dmptemplate_id) - unless customization.nil? + if customization.present? && tmplt.updated_at < customization.created_at @templates.delete(tmplt) @templates << customization end end end - msg = _("We found multiple DMP templates corresponding to the funder.") if @templates.count > 1 + @msg = _("We found multiple DMP templates corresponding to the funder.") if @templates.count > 1 end end diff --git a/app/controllers/roles_controller.rb b/app/controllers/roles_controller.rb index 3a50d54..ad2b69b 100644 --- a/app/controllers/roles_controller.rb +++ b/app/controllers/roles_controller.rb @@ -8,21 +8,22 @@ authorize @role access_level = params[:role][:access_level].to_i set_access_level(access_level) + message = '' if params[:user].present? if @role.plan.owner.present? && @role.plan.owner.email == params[:user] 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 + user = User.where_case_insensitive('email',params[:user]).first + if Role.find_by(plan: @role.plan, user: 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]) + else if user.nil? registered = false User.invite!(email: params[:user]) - message = _('Invitation to %{email} issued successfully.') % {email: params[:user]} + message = _('Invitation to %{email} issued successfully. \n') % {email: params[:user]} user = User.find_by(email: params[:user]) end + message += _('Plan shared with %{email}.') % {email: user.email} @role.user = user if @role.save if registered then UserMailer.sharing_notification(@role, current_user).deliver_now end diff --git a/app/controllers/templates_controller.rb b/app/controllers/templates_controller.rb index 42eb4ee..2717fef 100644 --- a/app/controllers/templates_controller.rb +++ b/app/controllers/templates_controller.rb @@ -55,6 +55,7 @@ random = rand 2147483647 break random unless Template.exists?(dmptemplate_id: random) end + customisation.dirty = true customisation.save customisation.phases.includes(:sections, :questions).each do |phase| diff --git a/app/models/plan.rb b/app/models/plan.rb index 8688f34..b32f3f6 100644 --- a/app/models/plan.rb +++ b/app/models/plan.rb @@ -990,16 +990,15 @@ ]).find(id) end - def self.eager_load2(id) + def self.load_for_phase(id, phase_id) Plan.includes( - [{template: [ + [template: [ {phases: {sections: {questions: [{answers: :notes}, :annotations, :question_format, :themes]}}}, {customizations: :org}, :org - ]}, - {plans_guidance_groups: {guidance_group: {guidances: :themes}}}, - {questions: :themes} - ]).find(id) + ], + plans_guidance_groups: {guidance_group: {guidances: :themes}} + ]).where(id: id, phases: { id: phase_id }).first end diff --git a/app/models/settings/template.rb b/app/models/settings/template.rb index 59ac3de..a9fd6f8 100644 --- a/app/models/settings/template.rb +++ b/app/models/settings/template.rb @@ -1,11 +1,11 @@ module Settings class Template < RailsSettings::SettingObject - + #attr_accessible :var, :target, :target_id, :target_type VALID_FONT_FACES = [ - 'Arial, Helvetica, Sans-Serif', - '"Times New Roman", Times, Serif' + '"Times New Roman", Times, Serif', + 'Arial, Helvetica, Sans-Serif' ] VALID_FONT_SIZE_RANGE = (8..14) @@ -17,13 +17,13 @@ DEFAULT_SETTINGS = { formatting: { margin: { # in millimeters - top: 20, - bottom: 20, - left: 20, - right: 20 + top: 10, + bottom: 10, + left: 10, + right: 10 }, font_face: VALID_FONT_FACES.first, - font_size: 12 # pt + font_size: 10 # pt }, max_pages: 3, fields: { diff --git a/app/models/user.rb b/app/models/user.rb index f900030..f35abe3 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -6,8 +6,8 @@ # Include default devise modules. Others available are: # :token_authenticatable, :confirmable, # :lockable, :timeoutable and :omniauthable - devise :invitable, :database_authenticatable, :registerable, :recoverable, - :rememberable, :trackable, :validatable, :omniauthable, + devise :invitable, :database_authenticatable, :registerable, :recoverable, + :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:shibboleth, :orcid] ## @@ -26,14 +26,14 @@ q = "%#{query}%" conditions = t[:title].matches(q) columns = %i( - grant_number identifier description principal_investigator data_contact + grant_number identifier description principal_investigator data_contact ) columns = ['grant_number', 'identifier', 'description', 'principal_investigator', 'data_contact'] columns.each {|col| conditions = conditions.or(t[col].matches(q)) } self.where(conditions) end end - + has_many :user_identifiers has_many :identifier_schemes, through: :user_identifiers @@ -41,12 +41,12 @@ # Possibly needed for active_admin # -relies on protected_attributes gem as syntax depricated in rails 4.2 #accepts_nested_attributes_for :roles - #attr_accessible :password_confirmation, :encrypted_password, :remember_me, - # :id, :email, :firstname, :last_login,:login_count, :orcid_id, - # :password, :shibboleth_id, :user_status_id, :surname, - # :user_type_id, :org_id, :skip_invitation, :other_organisation, + #attr_accessible :password_confirmation, :encrypted_password, :remember_me, + # :id, :email, :firstname, :last_login,:login_count, :orcid_id, + # :password, :shibboleth_id, :user_status_id, :surname, + # :user_type_id, :org_id, :skip_invitation, :other_organisation, # :accept_terms, :role_ids, :dmponline3, :api_token, - # :organisation, :language, :language_id, :org, :perms, + # :organisation, :language, :language_id, :org, :perms, # :confirmed_at, :org_id validates :email, email: true, allow_nil: true, uniqueness: {message: _("must be unique")} @@ -62,13 +62,13 @@ # What do they do? do they do it efficiently, and do we need them? # Determines the locale set for the user or the organisation he/she belongs - # @return String or nil + # @return String or nil def get_locale if !self.language.nil? return self.language.abbreviation elsif !self.org.nil? return self.org.get_locale - else + else return nil end end @@ -126,7 +126,7 @@ def organisation=(new_org) org_id = new_org.id unless new_org.nil? end - + ## # checks if the user is a super admin # if the user has any privelege which requires them to see the super admin page @@ -144,7 +144,7 @@ # # @return [Boolean] true if the user is an organisation admin def can_org_admin? - return self.can_grant_permissions? || self.can_modify_guidance? || + return self.can_grant_permissions? || self.can_modify_guidance? || self.can_modify_templates? || self.can_modify_org_details? end @@ -223,7 +223,7 @@ return org_type end =end - + ## # removes the api_token from the user # modifies the user model @@ -254,11 +254,11 @@ # -------------------------------------------------------------- def self.from_omniauth(auth) scheme = IdentifierScheme.find_by(name: auth.provider.downcase) - + if scheme.nil? throw Exception.new('Unknown OAuth provider: ' + auth.provider) else - joins(:user_identifiers).where('user_identifiers.identifier': auth.uid, + joins(:user_identifiers).where('user_identifiers.identifier': auth.uid, 'user_identifiers.identifier_scheme_id': scheme.id).first end end @@ -269,7 +269,14 @@ def deliver_invitation(options = {}) super(options.merge(subject: _('A Data Management Plan in %{application_name} has been shared with you') % {application_name: Rails.configuration.branding[:application][:name]})) end - + ## + # Case insensitive search over User model + # @param field [string] The name of the field being queried + # @param val [string] The string to search for, case insensitive + # @return [ActiveRecord::Relation] The result of the search + def self.where_case_insensitive(field, val) + User.where("lower(#{field}) = ?", val.downcase) + end # TODO: Remove this, its never called. # this generates a reset password link for a given user @@ -278,12 +285,12 @@ =begin def reset_password_link raw, enc = Devise.token_generator.generate(self.class, :reset_password_token) - self.reset_password_token = enc + self.reset_password_token = enc self.reset_password_sent_at = Time.now.utc save(validate: false) edit_user_password_path + '?reset_password_token=' + raw end =end - + end diff --git a/app/views/templates/admin_index.html.erb b/app/views/templates/admin_index.html.erb index 2acb25a..7532bec 100644 --- a/app/views/templates/admin_index.html.erb +++ b/app/views/templates/admin_index.html.erb @@ -46,10 +46,10 @@