app/models/section.rb
# Update the submission state of the root of the section tree for this section and a specific user and looping_identifier (if it exists)
# It will fetch this section's state and its descendants (the one with questions) states and then analyse which state should the root have, to
# accurately reflect the state of the whole tree.
#
# @param [User] user User for which to update this root section status
# @param [Integer] looping_identifier looping_identifier associated with the section. As the section can be saved multiple times for different LoopItems. LoopItem can be nil if self is not looping
Update the submission state of the root of the section tree for this section and a specific user and looping_identifier (if it exists)
It will fetch this section's state and its descendants (the one with questions) states and then analyse which state should the root have, to
accurately reflect the state of the whole tree.
Update the submission state of the root of the section tree for this section and a specific user and looping_identifier ( if it exists ) It will fetch this section ' s state and its descendants ( the one with questions ) states and then analyse which state should the root have to accurately reflect the state of the whole tree .
def update_root_submission_state!(user, looping_identifier=nil)
    if self.questions.any?
      self.update_submission_state!(user, looping_identifier)
    end
    state_tracker = UserSectionSubmissionState.find_or_create_by_user_id_and_section_id_and_looping_identifier(user.id, self.id, looping_identifier)
    all_states = user.submission_states.find(:all, :conditions => {:section_id => self.self_and_descendants.reject{ |s| !s.questions.any? }.map{ |s| s.id }.flatten, :dont_care => false}, :select => :section_state).map{ |sub| sub.section_state }.uniq.sort
    status = if all_states.size == 1
      all_states.first
    elsif all_states.include?(1)
      1
    elsif all_states.include?(2) || (all_states.include?(0) && all_states.include?(3))
      2
    else
      all_states.min
    end
    state_tracker.update_attribute(:section_state, status)
  end
def update_root_submission_state! ( user , looping_identifier = nil ) if self . questions . any? self . update_submission_state! ( user , looping_identifier ) end state_tracker = UserSectionSubmissionState . find_or_create_by_user_id_and_section_id_and_looping_identifier ( user . id , self . id , looping_identifier ) all_states = user . submission_states . find ( :all , :conditions => { :section_id => self . self_and_descendants . reject { | s | ! s . questions . any? } . map { | s | s . id } . flatten , :dont_care => false } , :select => :section_state ) . map { | sub | sub . section_state } . uniq . sort status = if all_states . size == 1 all_states . first elsif all_states . include? ( 1 ) 1 elsif all_states . include? ( 2 ) || ( all_states . include? ( 0 ) && all_states . include? ( 3 ) ) 2 else all_states . min end state_tracker . update_attribute ( :section_state , status ) end
Update the submission state of the root of the section tree for this section and a specific user and looping_identifier (if it exists)
It will fetch this section's state and its descendants (the one with questions) states and then analyse which state should the root have, to
accurately reflect the state of the whole tree.
Update the submission state of the root of the section tree for this section and a specific user and looping_identifier ( if it exists ) It will fetch this section ' s state and its descendants ( the one with questions ) states and then analyse which state should the root have to accurately reflect the state of the whole tree .
#state is only 0 (all unanswered) or 3(all answered) if there are no other states #if there's state 1 (mandatory unanswered) return 1 otherwise return 2 (mandatory answered, and other answers exist, but still there answeres missing
user
looping_identifier
looping_identifier looping_identifier associated with the section. As the section can be saved multiple times for different LoopItems. LoopItem can be nil if self is not looping
looping_identifier looping_identifier associated with the section . As the section can be saved multiple times for different LoopItems . LoopItem can be nil if self is not looping