Skip to content

Instantly share code, notes, and snippets.

@eric-wood
Forked from EminenceHC/surveys_to_be_taken.rb
Last active August 29, 2015 14:00
Show Gist options
  • Save eric-wood/8e08558c95fa4755ba3f to your computer and use it in GitHub Desktop.
Save eric-wood/8e08558c95fa4755ba3f to your computer and use it in GitHub Desktop.
module SurveysToBeTaken
def survey_index
@today = Date.today.strftime("%m/%d/%Y")
# Take all surveys and only fetch current_users results. This also checks
map_survey_ids = Survey.pluck(:id)
user = params[:user_id].present? ? params[:user_id] : current_user.id
map_result_ids = ResultSet.where(user_id: user).pluck(:survey_id)
# Only current users results
@compare = map_survey_ids - map_result_ids
# Fetch Surveys
runquery = Survey
runquery = runquery.where(question_type: params[:question_type]) if params[:question_type].present?
runquery = runquery.select('surveys.*')
# Ivar for admin view to view/edit surveys.
@admin_surveys = runquery
all_surveys = runquery
# Create empty list of surveys.
@surveys = []
# Check to see how many are in the list.
@ivar = 0
# For each survey do some additional checks on specific user types and add the correct results to the @surveys list. || (survey.users.map(&:id).include? current_user.id) && is_admin && | (survey.privilege == 2 || survey.privilege == 3)
all_surveys.each do |survey|
#if survey.survey_user_due_dates.present?
# survey_user_due_date_relationship = []
# # On each survey check if it has a SurveyUserDueDate relationship
# survey.survey_user_due_dates.each do |record|
# # For each SurveyUserDueDate relationship, check if it includes the User ID in array, and return only the SurveyUserDueDate that does. Then we can check by the Due Date of the returned record.
# if record.user_ids.include? "#{current_user.id}"
# survey_user_due_date_relationship << record
# end
# end
# @due_date = survey_user_due_date_relationship.first.due_date
#end
# Check if they have already taken the survey and if it is past the take date.
if survey.id.in?(@compare)&& ((survey.date1 == nil) || (survey.date1.st day))
# Check if finding missing surveys for current user, or from a particular users id in params
if params[:user_id].present?
# Students
if User.find_by(id: params[:user_id]).loginable_type == 'Student' && ((survey.privilege == 1) || (survey.privilege == 3)) || (survey.users.map(&:id).include? User.find_by(id: params[:user_id]).id) || (survey.schools.map(&:id).include? User.find_by(id: params[:user_id]).schools.map(&:id).join.to_i)
@ivar = @ivar + 1
@surveys << survey
# Counselors
elsif User.find_by(id: params[:user_id]).loginable_type == 'Counselor' && ((survey.privilege == 2) || (survey.privilege == 3)) || (survey.users.map(&:id).include? User.find_by(id: params[:user_id]).id)
@ivar = @ivar + 1
@surveys << survey
else
end
else
# Students
if is_student == true && ((survey.privilege == 1) || (survey.privilege == 3)) || (survey.users.map(&:id).include? current_user.id) || (survey.schools.map(&:id).include? current_user.schools.map(&:id).join.to_i)
@ivar = @ivar + 1
@surveys << survey
# Counselors
elsif is_counselor && ((survey.privilege == 2) || (survey.privilege == 3)) || (survey.users.map(&:id).include? current_user.id)
@ivar = @ivar + 1
@surveys << survey
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment