Created
April 30, 2014 16:26
-
-
Save EminenceHC/8243b0053609b9c2ab8d to your computer and use it in GitHub Desktop.
ugly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
if params[:user_id].present? | |
map_result_ids = ResultSet.where(user_id: params[:user_id]).pluck(:survey_id) | |
else | |
map_result_ids = ResultSet.where(user_id: current_user.id).pluck(:survey_id) | |
end | |
# 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) == true && ((survey.date1 == nil) || (survey.date1.strftime("%m/%d/%y") < @today)) | |
# 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