app/models/user.rb
def authorization_for object, user_delegate = nil, respondent_id = nil
    authorization = {}
    authorization[:error_message] = nil
    #as the object might not be a questionnaire itself it is necessary to do this check and load the questionnaire
    questionnaire = object.is_a?(Questionnaire) ? object : object.questionnaire
    if questionnaire.closed?
      authorization[:error_message] = "questionnaire_closed"
      return authorization
    elsif questionnaire.inactive?
      authorization[:error_message] = "questionnaire_not_available"
      return authorization
    end
    #load and check authorization
    aux = self.authorized_to_answer?(questionnaire, user_delegate, respondent_id)
    if !aux
      authorization[:error_message] = "not_authorized"
      return authorization
    elsif aux.status == SubmissionStatus::SUBMITTED
      authorization[:error_message] = "questionnaire_submitted"
      return authorization
    elsif aux.status == SubmissionStatus::NOT_STARTED
      authorization[:error_message] = "questionnaire_not_available"
      return authorization
    end
    #set the authorization data
    authorization[:is_closed] = questionnaire.closed?
    authorization[:delegation_enabled] = questionnaire.delegation_enabled
    authorization[:translator_visible] = questionnaire.translator_visible
    authorization[:status] = aux.status
    authorization[:language] = aux.language
    authorization[:language_full_name] = aux.language_full_name
    authorization[:user] = aux.user
    #get the delegation details if the user is a delegate and it has been delegated with this questionnaire
    if is_delegate?
      #no check for the existence of delegation, because that is checked in the 'is_authorized_to_answer?' method
      delegation = user_delegate ? self.delegated_tasks.find_by_questionnaire_id_and_user_delegate_id(questionnaire, user_delegate) : self.delegated_tasks.find_by_questionnaire_id(questionnaire)
      #if the sections aren't defined the delegate can answer the whole questionnaire
      #otherwise the information needs to be passed to the view to disabled the non-delegated sections
      #TODO: should there be any specific behavior in case a user is both a delegate and a respondent?
      if delegation && !delegation.delegation_sections.empty?
        authorization[:sections] = {}
        delegation.delegation_sections.each do |delegation_section|
          authorization[:sections][delegation_section.section_id.to_s] = []
          if delegation_section.loop_item_names.present?
            delegation_section.loop_item_names.each do |loop_item_name|
              authorization[:sections][delegation_section.section_id.to_s] << loop_item_name.id
            end
          end
        end
      end
    end
    authorization
  end
#fill the authorization object that will be used in the pages for questionnaire submission
#object can be questionnaire, section, or even question
fill the authorization object that will be used in the pages for questionnaire submission
object can be questionnaire, section, or even question
fill the authorization object that will be used in the pages for questionnaire submission object can be questionnaire section or even question
def authorization_for object, user_delegate = nil, respondent_id = nil
    authorization = {}
    authorization[:error_message] = nil
    questionnaire = object.is_a?(Questionnaire) ? object : object.questionnaire
    if questionnaire.closed?
      authorization[:error_message] = "questionnaire_closed"
      return authorization
    elsif questionnaire.inactive?
      authorization[:error_message] = "questionnaire_not_available"
      return authorization
    end
    aux = self.authorized_to_answer?(questionnaire, user_delegate, respondent_id)
    if !aux
      authorization[:error_message] = "not_authorized"
      return authorization
    elsif aux.status == SubmissionStatus::SUBMITTED
      authorization[:error_message] = "questionnaire_submitted"
      return authorization
    elsif aux.status == SubmissionStatus::NOT_STARTED
      authorization[:error_message] = "questionnaire_not_available"
      return authorization
    end
    authorization[:is_closed] = questionnaire.closed?
    authorization[:delegation_enabled] = questionnaire.delegation_enabled
    authorization[:translator_visible] = questionnaire.translator_visible
    authorization[:status] = aux.status
    authorization[:language] = aux.language
    authorization[:language_full_name] = aux.language_full_name
    authorization[:user] = aux.user
    if is_delegate?
      delegation = user_delegate ? self.delegated_tasks.find_by_questionnaire_id_and_user_delegate_id(questionnaire, user_delegate) : self.delegated_tasks.find_by_questionnaire_id(questionnaire)
      if delegation && !delegation.delegation_sections.empty?
        authorization[:sections] = {}
        delegation.delegation_sections.each do |delegation_section|
          authorization[:sections][delegation_section.section_id.to_s] = []
          if delegation_section.loop_item_names.present?
            delegation_section.loop_item_names.each do |loop_item_name|
              authorization[:sections][delegation_section.section_id.to_s] << loop_item_name.id
            end
          end
        end
      end
    end
    authorization
  end
def authorization_for object , user_delegate = nil , respondent_id = nil authorization = { } authorization [ :error_message ] = nil questionnaire = object . is_a? ( Questionnaire ) ? object : object . questionnaire if questionnaire . closed? authorization [ :error_message ] = "questionnaire_closed" return authorization elsif questionnaire . inactive? authorization [ :error_message ] = "questionnaire_not_available" return authorization end aux = self . authorized_to_answer? ( questionnaire , user_delegate , respondent_id ) if ! aux authorization [ :error_message ] = "not_authorized" return authorization elsif aux . status == SubmissionStatus :: SUBMITTED authorization [ :error_message ] = "questionnaire_submitted" return authorization elsif aux . status == SubmissionStatus :: NOT_STARTED authorization [ :error_message ] = "questionnaire_not_available" return authorization end authorization [ :is_closed ] = questionnaire . closed? authorization [ :delegation_enabled ] = questionnaire . delegation_enabled authorization [ :translator_visible ] = questionnaire . translator_visible authorization [ :status ] = aux . status authorization [ :language ] = aux . language authorization [ :language_full_name ] = aux . language_full_name authorization [ :user ] = aux . user if is_delegate? delegation = user_delegate ? self . delegated_tasks . find_by_questionnaire_id_and_user_delegate_id ( questionnaire , user_delegate ) : self . delegated_tasks . find_by_questionnaire_id ( questionnaire ) if delegation && ! delegation . delegation_sections . empty? authorization [ :sections ] = { } delegation . delegation_sections . each do | delegation_section | authorization [ :sections ] [ delegation_section . section_id . to_s ] = [ ] if delegation_section . loop_item_names . present? delegation_section . loop_item_names . each do | loop_item_name | authorization [ :sections ] [ delegation_section . section_id . to_s ] << loop_item_name . id end end end end end authorization end
fill the authorization object that will be used in the pages for questionnaire submission
object can be questionnaire, section, or even question
fill the authorization object that will be used in the pages for questionnaire submission object can be questionnaire section or even question
#as the object might not be a questionnaire itself it is necessary to do this check and load the questionnaire #load and check authorization #set the authorization data #get the delegation details if the user is a delegate and it has been delegated with this questionnaire #no check for the existence of delegation, because that is checked in the 'is_authorized_to_answer?' method #if the sections aren't defined the delegate can answer the whole questionnaire #otherwise the information needs to be passed to the view to disabled the non-delegated sections #TODO: should there be any specific behavior in case a user is both a delegate and a respondent?
object
user_delegate
respondent_id