Commit 89e0008a20595be69305c81c2e4df095760854b1

Authored by Luke Baker
1 parent a624fb09

retab Question model

Showing 1 changed file with 55 additions and 71 deletions   Show diff stats
app/models/question.rb
... ... @@ -507,93 +507,77 @@ class Question < ActiveRecord::Base
507 507 def export(type, options = {})
508 508  
509 509 case type
510   - when 'votes'
511   - outfile = "ideamarketplace_#{self.id}_votes.csv"
  510 + when 'votes'
  511 + outfile = "ideamarketplace_#{self.id}_votes.csv"
512 512  
513   - headers = ['Vote ID', 'Session ID', 'Question ID','Winner ID', 'Winner Text', 'Loser ID', 'Loser Text',
514   - 'Prompt ID', 'Left Choice ID', 'Right Choice ID', 'Created at', 'Updated at', 'Appearance ID',
515   - 'Response Time (s)', 'Missing Response Time Explanation', 'Session Identifier']
  513 + headers = ['Vote ID', 'Session ID', 'Question ID','Winner ID', 'Winner Text', 'Loser ID', 'Loser Text', 'Prompt ID', 'Left Choice ID', 'Right Choice ID', 'Created at', 'Updated at', 'Appearance ID', 'Response Time (s)', 'Missing Response Time Explanation', 'Session Identifier']
516 514  
517   - when 'ideas'
518   - outfile = "ideamarketplace_#{self.id}_ideas.csv"
519   - headers = ['Ideamarketplace ID','Idea ID', 'Idea Text', 'Wins', 'Losses', 'Times involved in Cant Decide', 'Score',
520   - 'User Submitted', 'Session ID', 'Created at', 'Last Activity', 'Active',
521   - 'Appearances on Left', 'Appearances on Right']
522   - when 'non_votes'
523   - outfile = "ideamarketplace_#{self.id}_non_votes.csv"
524   - headers = ['Record Type', 'Record ID', 'Session ID', 'Question ID','Left Choice ID', 'Left Choice Text',
525   - 'Right Choice ID', 'Right Choice Text', 'Prompt ID', 'Appearance ID', 'Reason',
526   - 'Created at', 'Updated at', 'Response Time (s)', 'Missing Response Time Explanation', 'Session Identifier']
527   - else
528   - raise "Unsupported export type: #{type}"
  515 + when 'ideas'
  516 + outfile = "ideamarketplace_#{self.id}_ideas.csv"
  517 + headers = ['Ideamarketplace ID','Idea ID', 'Idea Text', 'Wins', 'Losses', 'Times involved in Cant Decide', 'Score', 'User Submitted', 'Session ID', 'Created at', 'Last Activity', 'Active', 'Appearances on Left', 'Appearances on Right']
  518 + when 'non_votes'
  519 + outfile = "ideamarketplace_#{self.id}_non_votes.csv"
  520 + headers = ['Record Type', 'Record ID', 'Session ID', 'Question ID','Left Choice ID', 'Left Choice Text', 'Right Choice ID', 'Right Choice Text', 'Prompt ID', 'Appearance ID', 'Reason', 'Created at', 'Updated at', 'Response Time (s)', 'Missing Response Time Explanation', 'Session Identifier']
  521 + else
  522 + raise "Unsupported export type: #{type}"
529 523 end
530 524  
531   - filename = File.join(File.expand_path(Rails.root), "public", "system", "exports",
532   - self.id.to_s, Digest::SHA1.hexdigest(outfile + rand(10000000).to_s) + "_" + outfile)
  525 + filename = File.join(File.expand_path(Rails.root), "public", "system", "exports", self.id.to_s, Digest::SHA1.hexdigest(outfile + rand(10000000).to_s) + "_" + outfile)
533 526  
534 527 FileUtils::mkdir_p(File.dirname(filename))
535 528 csv_data = FasterCSV.open(filename, "w") do |csv|
536   - csv << headers
537   -
538   - case type
539   - when 'votes'
540   -
541   - self.votes.find_each(:include => [:prompt, :choice, :loser_choice, :voter]) do |v|
542   - prompt = v.prompt
543   - # these may not exist
544   - loser_data = v.loser_choice.nil? ? "" : "'#{v.loser_choice.data.strip}'"
545   - left_id = v.prompt.nil? ? "" : v.prompt.left_choice_id
546   - right_id = v.prompt.nil? ? "" : v.prompt.right_choice_id
547   -
548   - time_viewed = v.time_viewed.nil? ? "NA": v.time_viewed.to_f / 1000.0
  529 + csv << headers
  530 + case type
  531 + when 'votes'
  532 +
  533 + self.votes.find_each(:include => [:prompt, :choice, :loser_choice, :voter]) do |v|
  534 + prompt = v.prompt
  535 + # these may not exist
  536 + loser_data = v.loser_choice.nil? ? "" : "'#{v.loser_choice.data.strip}'"
  537 + left_id = v.prompt.nil? ? "" : v.prompt.left_choice_id
  538 + right_id = v.prompt.nil? ? "" : v.prompt.right_choice_id
  539 +
  540 + time_viewed = v.time_viewed.nil? ? "NA": v.time_viewed.to_f / 1000.0
  541 +
  542 + csv << [ v.id, v.voter_id, v.question_id, v.choice_id, "'#{v.choice.data.strip}'", v.loser_choice_id, loser_data,
  543 + v.prompt_id, left_id, right_id, v.created_at, v.updated_at, v.appearance_id,
  544 + time_viewed, v.missing_response_time_exp , v.voter.identifier]
  545 + end
549 546  
550   - csv << [ v.id, v.voter_id, v.question_id, v.choice_id, "'#{v.choice.data.strip}'", v.loser_choice_id, loser_data,
551   - v.prompt_id, left_id, right_id, v.created_at, v.updated_at, v.appearance_id,
552   - time_viewed, v.missing_response_time_exp , v.voter.identifier]
553   - end
  547 + when 'ideas'
  548 + self.choices.each do |c|
  549 + user_submitted = c.user_created ? "TRUE" : "FALSE"
  550 + left_prompts_ids = c.prompts_on_the_left.ids_only
  551 + right_prompts_ids = c.prompts_on_the_right.ids_only
554 552  
555   - when 'ideas'
556   - self.choices.each do |c|
557   - user_submitted = c.user_created ? "TRUE" : "FALSE"
558   - left_prompts_ids = c.prompts_on_the_left.ids_only
559   - right_prompts_ids = c.prompts_on_the_right.ids_only
  553 + left_appearances = self.appearances.count(:conditions => {:prompt_id => left_prompts_ids})
  554 + right_appearances = self.appearances.count(:conditions => {:prompt_id => right_prompts_ids})
560 555  
561   - left_appearances = self.appearances.count(:conditions => {:prompt_id => left_prompts_ids})
562   - right_appearances = self.appearances.count(:conditions => {:prompt_id => right_prompts_ids})
  556 + num_skips = self.skips.count(:conditions => {:prompt_id => left_prompts_ids + right_prompts_ids})
563 557  
564   - num_skips = self.skips.count(:conditions => {:prompt_id => left_prompts_ids + right_prompts_ids})
  558 + csv << [c.question_id, c.id, "'#{c.data.strip}'", c.wins, c.losses, num_skips, c.score, user_submitted , c.creator_id, c.created_at, c.updated_at, c.active, left_appearances, right_appearances]
565 559  
566   - csv << [c.question_id, c.id, "'#{c.data.strip}'", c.wins, c.losses, num_skips, c.score,
567   - user_submitted , c.creator_id, c.created_at, c.updated_at, c.active,
568   - left_appearances, right_appearances]
569   - end
570   - when 'non_votes'
  560 + end
  561 + when 'non_votes'
571 562  
572   - self.appearances.find_each(:include => [:skip, :vote, :voter]) do |a|
573   - # we only display skips and orphaned appearances in this csv file
574   - unless a.vote.nil?
575   - next
576   - end
  563 + self.appearances.find_each(:include => [:skip, :vote, :voter]) do |a|
  564 + # we only display skips and orphaned appearances in this csv file
  565 + next unless a.vote.nil?
577 566  
578   - #If no skip and no vote, this is an orphaned appearance
579   - if a.skip.nil?
580   - prompt = a.prompt
581   - csv << [ "Orphaned Appearance", a.id, a.voter_id, a.question_id, a.prompt.left_choice.id, a.prompt.left_choice.data.strip,
582   - a.prompt.right_choice.id, a.prompt.right_choice.data.strip, a.prompt_id, 'N/A', 'N/A',
583   - a.created_at, a.updated_at, 'N/A', '', a.voter.identifier]
  567 + #If no skip and no vote, this is an orphaned appearance
  568 + if a.skip.nil?
  569 + prompt = a.prompt
  570 + csv << [ "Orphaned Appearance", a.id, a.voter_id, a.question_id, a.prompt.left_choice.id, a.prompt.left_choice.data.strip, a.prompt.right_choice.id, a.prompt.right_choice.data.strip, a.prompt_id, 'N/A', 'N/A', a.created_at, a.updated_at, 'N/A', '', a.voter.identifier]
584 571  
585   - else
586   -
587   - #If this appearance belongs to a skip, show information on the skip instead
588   - s = a.skip
589   - time_viewed = s.time_viewed.nil? ? "NA": s.time_viewed.to_f / 1000.0
590   - prompt = s.prompt
591   - csv << [ "Skip", s.id, s.skipper_id, s.question_id, s.prompt.left_choice.id, s.prompt.left_choice.data.strip,
592   - s.prompt.right_choice.id, s.prompt.right_choice.data.strip, s.prompt_id, s.appearance_id, s.skip_reason,
593   - s.created_at, s.updated_at, time_viewed , s.missing_response_time_exp, s.skipper.identifier]
  572 + else
  573 + # If this appearance belongs to a skip, show information on the skip instead
  574 + s = a.skip
  575 + time_viewed = s.time_viewed.nil? ? "NA": s.time_viewed.to_f / 1000.0
  576 + prompt = s.prompt
  577 + csv << [ "Skip", s.id, s.skipper_id, s.question_id, s.prompt.left_choice.id, s.prompt.left_choice.data.strip, s.prompt.right_choice.id, s.prompt.right_choice.data.strip, s.prompt_id, s.appearance_id, s.skip_reason, s.created_at, s.updated_at, time_viewed , s.missing_response_time_exp, s.skipper.identifier]
  578 + end
  579 + end
594 580 end
595   - end
596   - end
597 581  
598 582 end
599 583  
... ...