sakai-info 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,281 @@
1
+ # sakai-info/assignment.rb
2
+ # SakaiInfo::Assignment library
3
+ #
4
+ # Created 2012-02-17 daveadams@gmail.com
5
+ # Last updated 2012-02-17 daveadams@gmail.com
6
+ #
7
+ # https://github.com/daveadams/sakai-info
8
+ #
9
+ # This software is public domain.
10
+ #
11
+
12
+ module SakaiInfo
13
+ class Assignment < SakaiXMLEntity
14
+ attr_reader :site
15
+
16
+ @@cache = {}
17
+ def self.find(id)
18
+ if @@cache[id].nil?
19
+ site = nil
20
+ xml = ""
21
+ DB.connect.exec("select context, xml from assignment_assignment " +
22
+ "where assignment_id = :id", id) do |row|
23
+ site = Site.find(row[0])
24
+ REXML::Document.new(row[1].read).write(xml, 2)
25
+ end
26
+ if site.nil?
27
+ raise ObjectNotFoundException.new(Assignment, id)
28
+ end
29
+ @@cache[id] = Assignment.new(id, site, xml)
30
+ end
31
+ @@cache[id]
32
+ end
33
+
34
+ # raw data constructor
35
+ def initialize(id, site, xml)
36
+ @id = id
37
+ @site = site
38
+ @xml = xml
39
+ parse_xml
40
+ end
41
+
42
+ # set lookup
43
+ def self.find_by_site_id(site_id)
44
+ assignments = []
45
+ DB.connect.exec("select assignment_id, context, xml from assignment_assignment " +
46
+ "where context = :site_id", site_id) do |row|
47
+ id = row[0]
48
+ site = Site.find(row[1])
49
+ xml = ""
50
+ REXML::Document.new(row[2].read).write(xml, 2)
51
+ assignments << Assignment.new(id, site, xml)
52
+ end
53
+ return assignments
54
+ end
55
+
56
+ def self.count_by_site_id(site_id)
57
+ assignment_count = 0
58
+ DB.connect.exec("select count(*) from assignment_assignment " +
59
+ "where context = :site_id", site_id) do |row|
60
+ assignment_count = row[0].to_i
61
+ end
62
+ return assignment_count
63
+ end
64
+
65
+ # getters
66
+ def title
67
+ @attributes["title"]
68
+ end
69
+
70
+ def submissions
71
+ @submissions ||= AssignmentSubmission.find_by_assignment_id(@id)
72
+ end
73
+
74
+ def submission_count
75
+ @submission_count ||= AssignmentSubmission.count_by_assignment_id(@id)
76
+ end
77
+
78
+ # yaml/json serialization
79
+ def default_serialization
80
+ {
81
+ "id" => self.id,
82
+ "title" => self.title,
83
+ "site" => self.site.serialize(:summary),
84
+ "created_by" => self.created_by.eid,
85
+ "created_at" => self.created_at,
86
+ "submissions" => self.submission_count
87
+ }
88
+ end
89
+
90
+ def summary_serialization
91
+ {
92
+ "id" => self.id,
93
+ "title" => self.title,
94
+ "created_by" => self.created_by.eid,
95
+ "submissions" => self.submission_count
96
+ }
97
+ end
98
+ end
99
+
100
+ class AssignmentSubmission < SakaiXMLEntity
101
+ attr_reader :assignment, :submitter
102
+
103
+ def initialize(id, assignment, xml, submitter, submitted, graded)
104
+ @id = id
105
+ @assignment = assignment
106
+ @xml = xml
107
+ @submitter = submitter
108
+ @submitted = submitted
109
+ @graded = graded
110
+ parse_xml
111
+ end
112
+
113
+ @@cache = {}
114
+ def self.find(id)
115
+ if @@cache[id].nil?
116
+ assignment = submitter = submitted_at = submitted = graded = nil
117
+ xml = ""
118
+ DB.connect.exec("select context, xml, submitter_id, " +
119
+ "submitted, graded from assignment_submission " +
120
+ "where submission_id = :id", id) do |row|
121
+ assignment = Assignment.find(row[0])
122
+ xml = ""
123
+ REXML::Document.new(row[1].read).write(xml, 2)
124
+ submitter = User.find(row[2])
125
+ submitted = (row[3] == "true")
126
+ graded = (row[4] == "true")
127
+ end
128
+ if assignment.nil?
129
+ raise ObjectNotFoundException.new(AssignmentSubmission, id)
130
+ end
131
+ @@cache[id] = AssignmentSubmission.new(id, assignment, xml, submitter, submitted, graded)
132
+ end
133
+ @@cache[id]
134
+ end
135
+
136
+ def submitted?
137
+ ( created_by.eid == @submitter.eid && @submitted ) || false
138
+ end
139
+
140
+ def graded?
141
+ @graded || false
142
+ end
143
+
144
+ def submitted_at
145
+ @submitted_at ||= format_entity_date(@attributes["datesubmitted"])
146
+ end
147
+
148
+ def self.find_by_assignment_id(assignment_id)
149
+ submissions = []
150
+ DB.connect.exec("select submission_id, context, xml, submitter_id, " +
151
+ "submitted, graded from assignment_submission " +
152
+ "where context = :assignment_id", assignment_id) do |row|
153
+ id = row[0]
154
+ assignment = Assignment.find(row[1])
155
+ xml = ""
156
+ REXML::Document.new(row[2].read).write(xml, 2)
157
+ submitter = User.find(row[3])
158
+ submitted = (row[4] == "true")
159
+ graded = (row[5] == "true")
160
+ submissions << AssignmentSubmission.new(id, assignment, xml, submitter, submitted, graded)
161
+ end
162
+ return submissions
163
+ end
164
+
165
+ def self.count_by_assignment_id(assignment_id)
166
+ submission_count = 0
167
+ DB.connect.exec("select count(*) from assignment_submission " +
168
+ "where context = :assignment_id", assignment_id) do |row|
169
+ submission_count = row[0].to_i
170
+ end
171
+ return submission_count
172
+ end
173
+
174
+ def self.find_by_user_id(user_id)
175
+ submissions = []
176
+ DB.connect.exec("select submission_id, context, xml, submitter_id, " +
177
+ "submitted, graded from assignment_submission " +
178
+ "where submitter_id = :user_id", user_id) do |row|
179
+ id = row[0]
180
+ assignment = Assignment.find(row[1])
181
+ xml = ""
182
+ REXML::Document.new(row[2].read).write(xml, 2)
183
+ submitter = User.find(row[3])
184
+ submitted = (row[4] == "true")
185
+ graded = (row[5] == "true")
186
+ submissions << AssignmentSubmission.new(id, assignment, xml, submitter, submitted, graded)
187
+ end
188
+ return submissions
189
+ end
190
+
191
+ def self.count_by_user_id(user_id)
192
+ submission_count = 0
193
+ DB.connect.exec("select count(*) from assignment_submission " +
194
+ "where submitter_id = :user_id", user_id) do |row|
195
+ submission_count = row[0].to_i
196
+ end
197
+ return submission_count
198
+ end
199
+
200
+ # yaml/json serialization
201
+ def default_serialization
202
+ {
203
+ "id" => self.id,
204
+ "assignment" => self.assignment.serialize(:summary),
205
+ "submitter" => self.submitter.serialize(:summary),
206
+ "submitted" => self.submitted?,
207
+ "submitted_at" => self.submitted_at,
208
+ "graded" => self.graded?,
209
+ "created_by" => self.created_by.eid,
210
+ "created_at" => self.created_at,
211
+ "modified_by" => self.modified_by.eid,
212
+ "modified_at" => self.modified_at
213
+ }
214
+ end
215
+
216
+ def summary_serialization
217
+ {
218
+ "id" => self.id,
219
+ "assignment_id" => self.assignment.id,
220
+ "submitter" => self.submitter.eid,
221
+ "submitted" => self.submitted?
222
+ }
223
+ end
224
+ end
225
+
226
+ class AssignmentContent < SakaiXMLEntity
227
+ attr_reader :owner
228
+
229
+ def initialize(id, owner, xml)
230
+ @id = id
231
+ @owner = owner
232
+ @xml = xml
233
+ parse_xml
234
+ end
235
+
236
+ @@cache = {}
237
+ def self.find(id)
238
+ if @@cache[id].nil?
239
+ context = nil
240
+ xml = ""
241
+ DB.connect.exec("select context, xml from assignment_content " +
242
+ "where content_id = :id", id) do |row|
243
+ context = row[0]
244
+ REXML::Document.new(row[1].read).write(xml, 2)
245
+ end
246
+ if context.nil?
247
+ raise ObjectNotFoundException.new(AssignmentContent, id)
248
+ end
249
+ @@cache[id] = AssignmentContent.new(id, context, xml)
250
+ end
251
+ @@cache[id]
252
+ end
253
+
254
+ def self.find_by_user_id(user_id)
255
+ contents = []
256
+ DB.connect.exec("select content_id, context, xml from assignment_content " +
257
+ "where context = :user_id", user_id) do |row|
258
+ id = row[0]
259
+ context = row[1]
260
+ xml = ""
261
+ REXML::Document.new(row[2].read).write(xml, 2)
262
+ contents << AssignmentContent.new(id, context, xml)
263
+ end
264
+ return contents
265
+ end
266
+
267
+ def self.count_by_user_id(user_id)
268
+ content_count = 0
269
+ DB.connect.exec("select count(*) from assignment_content " +
270
+ "where context = :user_id", user_id) do |row|
271
+ content_count = row[0].to_i
272
+ end
273
+ content_count
274
+ end
275
+
276
+ # getters
277
+ def title
278
+ @attributes["title"]
279
+ end
280
+ end
281
+ end
@@ -0,0 +1,378 @@
1
+ # sakai-info/authz.rb
2
+ # SakaiInfo::Authz library
3
+ #
4
+ # Created 2012-02-17 daveadams@gmail.com
5
+ # Last updated 2012-02-17 daveadams@gmail.com
6
+ #
7
+ # https://github.com/daveadams/sakai-info
8
+ #
9
+ # This software is public domain.
10
+ #
11
+
12
+ module SakaiInfo
13
+ class AuthzRole < SakaiObject
14
+ attr_reader :name
15
+
16
+ def initialize(id, name)
17
+ @id = id
18
+ @name = name
19
+ end
20
+
21
+ def to_s
22
+ name
23
+ end
24
+
25
+ @@cache = {}
26
+ def self.find_by_id(id)
27
+ id = id.to_s
28
+ if @@cache[id].nil?
29
+ name = nil
30
+ DB.connect.exec("select role_name from sakai_realm_role " +
31
+ "where role_key = :id", id.to_i) do |row|
32
+ name = row[0]
33
+ end
34
+ if name.nil?
35
+ raise ObjectNotFoundException.new(AuthzRole, id)
36
+ end
37
+ @@cache[id] = AuthzRole.new(id, name)
38
+ @@cache[name] = @@cache[id]
39
+ end
40
+ @@cache[id]
41
+ end
42
+
43
+ def self.find_by_name(name)
44
+ if name.nil?
45
+ raise ObjectNotFoundException.new(AuthzRole, "")
46
+ end
47
+ if @@cache[name].nil?
48
+ id = nil
49
+ DB.connect.exec("select role_key from sakai_realm_role " +
50
+ "where role_name = :name", name) do |row|
51
+ id = row[0].to_i.to_s
52
+ end
53
+ if id.nil?
54
+ raise ObjectNotFoundException.new(AuthzRole, name)
55
+ end
56
+ @@cache[name] = AuthzRole.new(id, name)
57
+ @@cache[id] = @@cache[name]
58
+ end
59
+ @@cache[name]
60
+ end
61
+
62
+ def self.find(id_or_name)
63
+ id_or_name = id_or_name.to_s
64
+ role = nil
65
+ begin
66
+ role = AuthzRole.find_by_name(id_or_name)
67
+ rescue ObjectNotFoundException
68
+ # just in case
69
+ role = AuthzRole.find_by_id(id_or_name)
70
+ end
71
+ if role.nil?
72
+ raise ObjectNotFoundException.new(AuthzRole, id_or_name)
73
+ end
74
+ role
75
+ end
76
+
77
+ def self.find_by_realm_id_and_function_id(realm_id, function_id)
78
+ roles = []
79
+ DB.connect.exec("select role_key, role_name from " +
80
+ "sakai_realm_role where role_key in " +
81
+ "(select role_key from sakai_realm_rl_fn " +
82
+ "where realm_key=:realm_id and function_key=:function_id) " +
83
+ "order by role_name", realm_id, function_id) do |row|
84
+ role_id = row[0].to_i.to_s
85
+ role_name = row[1]
86
+ roles << AuthzRole.new(role_id, role_name)
87
+ end
88
+ roles
89
+ end
90
+ end
91
+
92
+ class AuthzFunction < SakaiObject
93
+ attr_reader :name
94
+
95
+ def initialize(id, name)
96
+ @id = id
97
+ @name = name
98
+ end
99
+
100
+ def to_s
101
+ name
102
+ end
103
+
104
+ @@cache = {}
105
+ def self.find_by_id(id)
106
+ id = id.to_s
107
+ if @@cache[id].nil?
108
+ name = nil
109
+ DB.connect.exec("select function_name from sakai_realm_function " +
110
+ "where function_key = :id", id.to_i) do |row|
111
+ name = row[0]
112
+ end
113
+ if name.nil?
114
+ raise ObjectNotFoundException.new(AuthzFunction, id)
115
+ end
116
+ @@cache[id] = AuthzFunction.new(id, name)
117
+ @@cache[name] = @@cache[id]
118
+ end
119
+ @@cache[id]
120
+ end
121
+
122
+ def self.find_by_name(name)
123
+ if @@cache[name].nil?
124
+ id = nil
125
+ DB.connect.exec("select function_key from sakai_realm_function " +
126
+ "where function_name = :name", name) do |row|
127
+ id = row[0].to_i.to_s
128
+ end
129
+ if id.nil?
130
+ raise ObjectNotFoundException.new(AuthzFunction, name)
131
+ end
132
+ @@cache[name] = AuthzFunction.new(id, name)
133
+ @@cache[id] = @@cache[name]
134
+ end
135
+ @@cache[name]
136
+ end
137
+
138
+ def self.find(id_or_name)
139
+ id_or_name = id_or_name.to_s
140
+ function = nil
141
+ begin
142
+ function = AuthzFunction.find_by_name(id_or_name)
143
+ rescue ObjectNotFoundException
144
+ # just in case
145
+ function = AuthzFunction.find_by_id(id_or_name)
146
+ end
147
+ if function.nil?
148
+ raise ObjectNotFoundException.new(AuthzFunction, id_or_name)
149
+ end
150
+ function
151
+ end
152
+
153
+ def self.count_by_realm_id_and_role_id(realm_id, role_id)
154
+ count = 0
155
+ DB.connect.exec("select count(*) from sakai_realm_rl_fn " +
156
+ "where realm_key=:realm_id and role_key=:role_id",
157
+ realm_id, role_id) do |row|
158
+ count = row[0].to_i
159
+ end
160
+ count
161
+ end
162
+
163
+ def self.find_by_realm_id_and_role_id(realm_id, role_id)
164
+ functions = []
165
+ DB.connect.exec("select function_key, function_name from " +
166
+ "sakai_realm_function where function_key in " +
167
+ "(select function_key from sakai_realm_rl_fn " +
168
+ "where realm_key=:realm_id and role_key=:role_id) " +
169
+ "order by function_name", realm_id, role_id) do |row|
170
+ function_id = row[0].to_i.to_s
171
+ function_name = row[1]
172
+ functions << AuthzFunction.new(function_id, function_name)
173
+ end
174
+ functions
175
+ end
176
+ end
177
+
178
+ class AuthzRealm < SakaiObject
179
+ attr_reader :name, :providers, :maintain_role
180
+
181
+ def initialize(id, name, providers, maintain_role)
182
+ @id = id
183
+ @name = name
184
+ if providers.nil?
185
+ @providers = nil
186
+ else
187
+ @providers = providers.split("+")
188
+ end
189
+ @maintain_role = maintain_role
190
+ end
191
+
192
+ def realm_roles
193
+ @realm_roles ||= AuthzRealmRole.find_by_realm_id(@id)
194
+ end
195
+
196
+ def to_s
197
+ name
198
+ end
199
+
200
+ def user_count
201
+ @user_count ||= AuthzRealmMembership.count_by_realm_id(@id)
202
+ end
203
+
204
+ def membership
205
+ @membership ||= AuthzRealmMembership.find_by_realm_id(@id)
206
+ end
207
+
208
+ @@cache = {}
209
+ def self.find_by_id(id)
210
+ id = id.to_s
211
+ if @@cache[id].nil?
212
+ name = providers = maintain_role = nil
213
+ DB.connect.exec("select realm_id, provider_id, maintain_role " +
214
+ "from sakai_realm " +
215
+ "where realm_key = :id", id.to_i) do |row|
216
+ name = row[0]
217
+ providers = row[1]
218
+ if row[2].nil? or row[2] == ""
219
+ maintain_role = nil
220
+ else
221
+ maintain_role = AuthzRole.find_by_id(row[2].to_i)
222
+ end
223
+ end
224
+ if name.nil?
225
+ raise ObjectNotFoundException.new(AuthzRealm, id)
226
+ end
227
+ @@cache[id] = AuthzRealm.new(id, name, providers, maintain_role)
228
+ @@cache[name] = @@cache[id]
229
+ end
230
+ @@cache[id]
231
+ end
232
+
233
+ def self.find_by_name(name)
234
+ if @@cache[name].nil?
235
+ id = providers = maintain_role = nil
236
+ DB.connect.exec("select realm_key, provider_id, maintain_role " +
237
+ "from sakai_realm " +
238
+ "where realm_id = :name", name) do |row|
239
+ id = row[0].to_i.to_s
240
+ providers = row[1]
241
+ if row[2].nil? or row[2] == ""
242
+ maintain_role = nil
243
+ else
244
+ maintain_role = AuthzRole.find_by_id(row[2].to_i)
245
+ end
246
+ end
247
+ if id.nil?
248
+ raise ObjectNotFoundException.new(AuthzRealm, name)
249
+ end
250
+ @@cache[name] = AuthzRealm.new(id, name, providers, maintain_role)
251
+ @@cache[id] = @@cache[name]
252
+ end
253
+ @@cache[name]
254
+ end
255
+
256
+ def self.find(id_or_name)
257
+ id_or_name = id_or_name.to_s
258
+ realm = nil
259
+ begin
260
+ realm = AuthzRealm.find_by_name(id_or_name)
261
+ rescue ObjectNotFoundException
262
+ # just in case
263
+ realm = AuthzRealm.find_by_id(id_or_name)
264
+ end
265
+ if realm.nil?
266
+ raise ObjectNotFoundException.new(AuthzRealm, id_or_name)
267
+ end
268
+ realm
269
+ end
270
+
271
+ def self.find_by_site_id(site_id)
272
+ AuthzRealm.find_by_name("/site/#{site_id}")
273
+ end
274
+
275
+ def self.find_by_site_id_and_group_id(site_id, group_id)
276
+ AuthzRealm.find_by_name("/site/#{site_id}/group/#{group_id}")
277
+ end
278
+ end
279
+
280
+ class AuthzRealmRole < SakaiObject
281
+ attr_reader :realm, :role
282
+
283
+ def initialize(realm, role)
284
+ @realm = realm
285
+ @role = role
286
+ end
287
+
288
+ def function_count
289
+ @function_count ||= AuthzFunction.count_by_realm_id_and_role_id(@realm.id, @role.id)
290
+ end
291
+
292
+ def functions
293
+ @functions ||= AuthzFunction.find_by_realm_id_and_role_id(@realm.id, @role.id)
294
+ end
295
+
296
+ def user_count
297
+ @user_count ||= User.count_by_realm_id_and_role_id(@realm.id, @role.id)
298
+ end
299
+
300
+ def users
301
+ @users ||= User.find_by_realm_id_and_role_id(@realm.id, @role.id)
302
+ end
303
+
304
+ def self.find_by_realm_id(realm_id)
305
+ realm_roles = []
306
+ realm = AuthzRealm.find_by_id(realm_id)
307
+ DB.connect.exec("select distinct role_key from " +
308
+ "sakai_realm_rl_fn where " +
309
+ "realm_key = :realm_id", realm_id) do |row|
310
+ begin
311
+ role = AuthzRole.find_by_id(row[0].to_i)
312
+ realm_roles << AuthzRealmRole.new(realm, role)
313
+ rescue AuthzRoleNotFoundException
314
+ end
315
+ end
316
+ realm_roles
317
+ end
318
+
319
+ def default_serialization
320
+ {
321
+ "realm_name" => self.realm.name,
322
+ "role_name" => self.role.name,
323
+ "user_count" => self.user_count,
324
+ "function_count" => self.function_count
325
+ }
326
+ end
327
+
328
+ def summary_serialization
329
+ {
330
+ "role_name" => self.role.name,
331
+ "user_count" => self.user_count,
332
+ "function_count" => self.function_count
333
+ }
334
+ end
335
+ end
336
+
337
+ class AuthzRealmMembership < SakaiObject
338
+ attr_reader :realm, :user, :role
339
+
340
+ def initialize(realm_id, user_id, role)
341
+ @realm = AuthzRealm.find(realm_id)
342
+ @user = User.find(user_id)
343
+ @role = AuthzRole.find(role)
344
+ end
345
+
346
+ def self.find_by_realm_id(realm_id)
347
+ results = []
348
+ DB.connect.exec("select srrg.user_id, srr.role_name " +
349
+ "from sakai_realm_rl_gr srrg, sakai_realm_role srr " +
350
+ "where srrg.role_key = srr.role_key " +
351
+ "and srrg.realm_key = :realm_id", realm_id) do |row|
352
+ results << AuthzRealmMembership.new(realm_id, row[0], row[1])
353
+ end
354
+ results
355
+ end
356
+
357
+ def self.count_by_realm_id(realm_id)
358
+ count = 0
359
+ DB.connect.exec("select count(*) from sakai_realm_rl_gr " +
360
+ "where realm_key = :realm_id", realm_id) do |row|
361
+ count = row[0].to_i
362
+ end
363
+ count
364
+ end
365
+
366
+ def self.find_by_user_id(user_id)
367
+ results = []
368
+ DB.connect.exec("select sr.realm_id, srr.role_name " +
369
+ "from sakai_realm_rl_gr srrg, sakai_realm_role srr, sakai_realm sr " +
370
+ "where srrg.role_key = srr.role_key " +
371
+ "and srrg.realm_key = sr.realm_key " +
372
+ "and srrg.user_id = :user_id", user_id) do |row|
373
+ results << AuthzRealmMembership.new(row[0], user_id, row[1])
374
+ end
375
+ results
376
+ end
377
+ end
378
+ end