Class: SakaiInfo::Quiz

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

Direct Known Subclasses

PendingQuiz, PublishedQuiz

Constant Summary collapse

INACTIVE =

possible statuses

0
ACTIVE =
1
DELETED =

aka ‘DEAD’ in the Scholar source

2
RETRACTED_FOR_EDITING =
3

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, site = nil) ⇒ Quiz

a note about quizzes: they do not link directly back to sites instead, they link back only via the sam_authzdata_t table



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sakai-info/quiz.rb', line 30

def initialize(dbrow, site = nil)
  @site = nil
  if site.is_a? String
    begin
      @site = Site.find(site)
    rescue ObjectNotFoundException
      @site = nil
    end
  elsif site.is_a? Site
    @site = site
  end

  @id = dbrow[:id]
  @title = dbrow[:title]
  @dbrow = dbrow

  if @site.nil?
    # published quizzes map to site_id via the OWN_PUBLISHED_ASSESSMENT function
    # pending quizzes map to site_id via the EDIT_ASSESSMENT function
    DB.connect[:sam_authzdata_t].select(Sequel.function(:distinct, :agentid)).
      where(:qualifierid => @id).
      where(:functionid => ["OWN_PUBLISHED_ASSESSMENT","EDIT_ASSESSMENT"]).
      all.each do |row|
      begin
        site = Site.find(row[:agentid])
        @site = site
      rescue ObjectNotFoundException
        @site = nil
      end
      break if not @site.nil?
    end
  end
end

Instance Attribute Details

#dbrowObject (readonly)

Returns the value of attribute dbrow.



14
15
16
# File 'lib/sakai-info/quiz.rb', line 14

def dbrow
  @dbrow
end

#siteObject (readonly)

Returns the value of attribute site.



14
15
16
# File 'lib/sakai-info/quiz.rb', line 14

def site
  @site
end

#titleObject (readonly)

Returns the value of attribute title.



14
15
16
# File 'lib/sakai-info/quiz.rb', line 14

def title
  @title
end

Class Method Details

.all_serializationsObject



222
223
224
225
226
227
228
# File 'lib/sakai-info/quiz.rb', line 222

def self.all_serializations
  [
   :default,
   :sections,
   :items,
  ]
end

.clear_cacheObject



22
23
24
# File 'lib/sakai-info/quiz.rb', line 22

def self.clear_cache
  @@cache = {}
end

.count_by_site_id(site_id) ⇒ Object



89
90
91
92
93
94
# File 'lib/sakai-info/quiz.rb', line 89

def self.count_by_site_id(site_id)
  {
    "pending_count" => PendingQuiz.count_by_site_id(site_id),
    "published_count" => PublishedQuiz.count_by_site_id(site_id)
  }
end

.find(id) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sakai-info/quiz.rb', line 64

def self.find(id)
  id = id.to_s
  if @@cache[id].nil?
    quiz = nil
    begin
      quiz = PendingQuiz.find(id)
    rescue ObjectNotFoundException
      begin
        quiz = PublishedQuiz.find(id)
      rescue ObjectNotFoundException
        raise ObjectNotFoundException.new(Quiz, id)
      end
    end
    @@cache[id] = quiz
  end
  @@cache[id]
end

.find_by_site_id(site_id) ⇒ Object



82
83
84
85
86
87
# File 'lib/sakai-info/quiz.rb', line 82

def self.find_by_site_id(site_id)
  {
    "pending" => PendingQuiz.find_by_site_id(site_id),
    "published" => PublishedQuiz.find_by_site_id(site_id)
  }
end

Instance Method Details

#access_controlObject



131
132
133
# File 'lib/sakai-info/quiz.rb', line 131

def access_control
  @access_control ||= self.access_control_class.find(self.id)
end

#access_control_summary_serializationObject



188
189
190
191
192
# File 'lib/sakai-info/quiz.rb', line 188

def access_control_summary_serialization
  result = self.summary_serialization
  result.delete("id")
  result
end

#authorizationsObject



139
140
141
# File 'lib/sakai-info/quiz.rb', line 139

def authorizations
  @authorizations ||= self.authorization_class.find_by_quiz_id(self.id)
end

#authorizations_serializationObject



212
213
214
215
216
# File 'lib/sakai-info/quiz.rb', line 212

def authorizations_serialization
  {
    "authorizations" => self.authorizations.collect { |a| a.serialize(:quiz_summary) }
  }
end

#auths_serializationObject



218
219
220
# File 'lib/sakai-info/quiz.rb', line 218

def auths_serialization
  self.authorizations_serialization
end

#default_serializationObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/sakai-info/quiz.rb', line 143

def default_serialization
  result = {
    "id" => self.id,
    "title" => self.title,
    "site" => nil,
    "status" => self.status,
    "type" => self.quiz_type,
    "section_count" => self.section_count,
    "item_count" => self.item_count,
    "attempt_count" => nil,
    "access_control" => self.access_control.serialize(:quiz_summary)
  }
  if not self.site.nil?
    result["site"] = self.site.serialize(:summary)
  end
  if self.respond_to? :attempt_count
    result["attempt_count"] = self.attempt_count
  else
    result.delete("attempt_count")
  end
  result
end

#item_countObject



123
124
125
# File 'lib/sakai-info/quiz.rb', line 123

def item_count
  @item_count ||= self.item_class.count_by_quiz_id(self.id)
end

#itemsObject



127
128
129
# File 'lib/sakai-info/quiz.rb', line 127

def items
  @items ||= self.item_class.find_by_quiz_id(self.id)
end

#items_serializationObject



200
201
202
203
204
# File 'lib/sakai-info/quiz.rb', line 200

def items_serialization
  {
    "items" => self.items.collect { |i| i.serialize(:quiz_summary) }
  }
end

#metadataObject



135
136
137
# File 'lib/sakai-info/quiz.rb', line 135

def 
  @metadata ||= self..find_by_quiz_id(self.id)
end

#metadata_serializationObject



206
207
208
209
210
# File 'lib/sakai-info/quiz.rb', line 206

def 
  {
    "metadata" => self..collect { |m| [m.key, m.value] }.to_h
  }
end

#quiz_typeObject



96
97
98
# File 'lib/sakai-info/quiz.rb', line 96

def quiz_type
  nil
end

#section_countObject



115
116
117
# File 'lib/sakai-info/quiz.rb', line 115

def section_count
  @section_count ||= self.section_class.count_by_quiz_id(@id)
end

#sectionsObject



119
120
121
# File 'lib/sakai-info/quiz.rb', line 119

def sections
  @sections ||= self.section_class.find_by_quiz_id(@id)
end

#sections_serializationObject



194
195
196
197
198
# File 'lib/sakai-info/quiz.rb', line 194

def sections_serialization
  {
    "sections" => self.sections.collect{|s|s.serialize(:quiz_summary)}
  }
end

#site_summary_serializationObject



180
181
182
183
184
185
186
# File 'lib/sakai-info/quiz.rb', line 180

def site_summary_serialization
  {
    "id" => self.id,
    "title" => self.title,
    "status" => self.status,
  }
end

#statusObject



105
106
107
108
109
110
111
112
113
# File 'lib/sakai-info/quiz.rb', line 105

def status
  case @dbrow[:status].to_i
  when INACTIVE then "inactive"
  when ACTIVE then "active"
  when DELETED then "deleted"
  when RETRACTED_FOR_EDITING then "retracted for editing"
  else "unknown status '#{@dbrow[:status].to_i}'"
  end
end

#summary_serializationObject



166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/sakai-info/quiz.rb', line 166

def summary_serialization
  result = {
    "id" => self.id,
    "title" => self.title,
    "site_id" => nil,
    "status" => self.status,
    "type" => self.quiz_type
  }
  if not self.site.nil?
    result["site_id"] = self.site.id
  end
  result
end