Class: SakaiInfo::QuizItem

Inherits:
SakaiObject show all
Includes:
ModProps
Defined in:
lib/sakai-info/quiz.rb

Direct Known Subclasses

PendingQuizItem, PublishedQuizItem

Instance Attribute Summary collapse

Attributes inherited from SakaiObject

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ModProps

included

Methods inherited from SakaiObject

#dbrow_only_serialization, #dbrow_serialization, descendants, #object_type_serialization, #serialize, #shell_serialization, #to_csv, #to_json, #to_yaml

Constructor Details

#initialize(dbrow) ⇒ QuizItem

Returns a new instance of QuizItem.



567
568
569
570
571
572
573
574
575
# File 'lib/sakai-info/quiz.rb', line 567

def initialize(dbrow)
  @dbrow = dbrow

  @id = dbrow[:itemid]
  @section = QuizSection.find(dbrow[:sectionid])
  @quiz = @section.quiz
  @sequence = dbrow[:sequence]
  @typeid = dbrow[:typeid]
end

Instance Attribute Details

#dbrowObject (readonly)

Returns the value of attribute dbrow.



554
555
556
# File 'lib/sakai-info/quiz.rb', line 554

def dbrow
  @dbrow
end

#quizObject (readonly)

Returns the value of attribute quiz.



554
555
556
# File 'lib/sakai-info/quiz.rb', line 554

def quiz
  @quiz
end

#sectionObject (readonly)

Returns the value of attribute section.



554
555
556
# File 'lib/sakai-info/quiz.rb', line 554

def section
  @section
end

#sequenceObject (readonly)

Returns the value of attribute sequence.



554
555
556
# File 'lib/sakai-info/quiz.rb', line 554

def sequence
  @sequence
end

#typeidObject (readonly)

Returns the value of attribute typeid.



554
555
556
# File 'lib/sakai-info/quiz.rb', line 554

def typeid
  @typeid
end

Class Method Details

.all_serializationsObject



706
707
708
709
710
711
712
# File 'lib/sakai-info/quiz.rb', line 706

def self.all_serializations
  [
   :default,
   :mod,
   :texts
  ]
end

.class_for_type(type) ⇒ Object



601
602
603
604
605
606
607
# File 'lib/sakai-info/quiz.rb', line 601

def self.class_for_type(type)
  if type == "pending"
    PendingQuizItem
  else
    PublishedQuizItem
  end
end

.clear_cacheObject



562
563
564
# File 'lib/sakai-info/quiz.rb', line 562

def self.clear_cache
  @@cache = {}
end

.count_by_quiz_id(quiz_id) ⇒ Object



631
632
633
# File 'lib/sakai-info/quiz.rb', line 631

def self.count_by_quiz_id(quiz_id)
  QuizItem.query_by_quiz_id(quiz_id).count
end

.count_by_section_id(section_id) ⇒ Object



614
615
616
# File 'lib/sakai-info/quiz.rb', line 614

def self.count_by_section_id(section_id)
  QuizItem.query_by_section_id(section_id).count
end

.find(id) ⇒ Object



577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
# File 'lib/sakai-info/quiz.rb', line 577

def self.find(id)
  id = id.to_s
  if @@cache[id].nil?
    begin
      @@cache[id] = PendingQuizItem.find(id)
    rescue ObjectNotFoundException
      begin
        @@cache[id] = PublishedQuizItem.find(id)
      rescue ObjectNotFoundException
        raise ObjectNotFoundException.new(QuizItem, id)
      end
    end
  end
  @@cache[id]
end

.find_by_quiz_id(quiz_id) ⇒ Object



635
636
637
638
639
640
641
642
643
644
# File 'lib/sakai-info/quiz.rb', line 635

def self.find_by_quiz_id(quiz_id)
  item_class = QuizItem.class_for_type(Quiz.find(quiz_id).quiz_type)
  QuizItem.query_by_quiz_id(quiz_id).order(:sequence).all.collect do |row|
    item_class.new(row)
  end.sort { |a,b| if a.section.sequence == b.section.sequence
                     a.sequence <=> b.sequence
                   else
                     a.section.sequence <=> b.section.sequence
                   end }
end

.find_by_section_id(section_id) ⇒ Object



618
619
620
621
622
623
# File 'lib/sakai-info/quiz.rb', line 618

def self.find_by_section_id(section_id)
  item_class = QuizItem.class_for_type(QuizSection.find(section_id).section_type)
  QuizItem.query_by_section_id(section_id).all.collect do |row|
    item_class.new(row)
  end
end

.query_by_quiz_id(quiz_id) ⇒ Object



625
626
627
628
629
# File 'lib/sakai-info/quiz.rb', line 625

def self.query_by_quiz_id(quiz_id)
  table = QuizItem.table_name_for_type(Quiz.find(quiz_id).quiz_type)
  DB.connect[table].
    where(:sectionid => Quiz.find(quiz_id).sections.collect{|s|s.id})
end

.query_by_section_id(section_id) ⇒ Object



609
610
611
612
# File 'lib/sakai-info/quiz.rb', line 609

def self.query_by_section_id(section_id)
  table = QuizItem.table_name_for_type(QuizSection.find(section_id).section_type)
  DB.connect[table].where(:sectionid => section_id).order(:sequence)
end

.table_name_for_type(type) ⇒ Object



593
594
595
596
597
598
599
# File 'lib/sakai-info/quiz.rb', line 593

def self.table_name_for_type(type)
  if type == "pending"
    :sam_item_t
  else
    :sam_publisheditem_t
  end
end

Instance Method Details

#default_serializationObject



666
667
668
669
670
671
672
673
674
675
# File 'lib/sakai-info/quiz.rb', line 666

def default_serialization
  {
    "id" => self.id,
    "quiz" => self.quiz.serialize(:summary),
    "section" => self.section.serialize(:summary),
    "sequence" => self.sequence,
    "type" => self.item_type,
    "typeid" => self.typeid
  }
end

#item_typeObject



646
647
648
# File 'lib/sakai-info/quiz.rb', line 646

def item_type
  nil
end

#itemtext_tableObject



650
651
652
# File 'lib/sakai-info/quiz.rb', line 650

def itemtext_table
  nil
end

#quiz_summary_serializationObject



685
686
687
688
689
690
691
# File 'lib/sakai-info/quiz.rb', line 685

def quiz_summary_serialization
  {
    "id" => self.id,
    "section" => self.section.sequence,
    "sequence" => self.sequence,
  }
end

#section_summary_serializationObject



693
694
695
696
697
698
# File 'lib/sakai-info/quiz.rb', line 693

def section_summary_serialization
  {
    "id" => self.id,
    "sequence" => self.sequence,
  }
end

#summary_serializationObject



677
678
679
680
681
682
683
# File 'lib/sakai-info/quiz.rb', line 677

def summary_serialization
  {
    "id" => self.id,
    "quiz_id" => self.quiz.id,
    "section_id" => self.section.id
  }
end

#textsObject



654
655
656
657
658
659
660
661
662
663
664
# File 'lib/sakai-info/quiz.rb', line 654

def texts
  if self.itemtext_table.nil?
    return []
  end

  DB.connect[self.itemtext_table].
    select(:text).
    where(:itemid => self.id).
    order(:sequence).all.
    collect { |row| row[:text].read }
end

#texts_serializationObject



700
701
702
703
704
# File 'lib/sakai-info/quiz.rb', line 700

def texts_serialization
  {
    "texts" => self.texts
  }
end