Class: SakaiInfo::User

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

Direct Known Subclasses

MissingUser

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(*args) ⇒ User

Returns a new instance of User.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/sakai-info/user.rb', line 79

def initialize(*args)
  case args.size
  when 1
    dbrow = args[0]
    @dbrow = dbrow
    @id = dbrow[:user_id]
    @eid = User.get_eid(@id)
    @email = dbrow[:email]
    @name = ((dbrow[:first_name] || "") + " " + (dbrow[:last_name] || "")).strip
    @type = dbrow[:type]
  when 2
    user_id, placeholder_string = args
    @id = user_id
    @eid=User.get_eid(@id)
    @email = @name = @type = placeholder_string
  else
    raise ArgumentError, "This method takes 1-2 arguments"
  end
end

Instance Attribute Details

#dbrowObject (readonly)

Returns the value of attribute dbrow.



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

def dbrow
  @dbrow
end

#eidObject (readonly)

Returns the value of attribute eid.



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

def eid
  @eid
end

#emailObject (readonly)

Returns the value of attribute email.



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

def email
  @email
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Class Method Details

.all_serializationsObject



243
244
245
246
247
248
249
250
# File 'lib/sakai-info/user.rb', line 243

def self.all_serializations
  [
   :default,
   :sites,
   :pools,
   :mod,
  ]
end

.clear_cacheObject



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

def self.clear_cache
  @@cache = {}
  @@id_cache = {}
end

.countObject

finders/counters



155
156
157
# File 'lib/sakai-info/user.rb', line 155

def self.count
  DB.connect[:sakai_user].count
end

.count_by_realm_id_and_role_id(realm_id, role_id) ⇒ Object



159
160
161
162
# File 'lib/sakai-info/user.rb', line 159

def self.count_by_realm_id_and_role_id(realm_id, role_id)
  DB.connect[:sakai_realm_rl_gr].
    filter(:realm_key => realm_id, :role_key => role_id).count
end

.find(id) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sakai-info/user.rb', line 28

def self.find(id)
  if @@cache[id].nil?
    eid = User.get_eid(id)
    user_id = User.get_user_id(id)
    if eid.nil? or user_id.nil?
      raise ObjectNotFoundException.new(User, id)
    end

    row = DB.connect[:sakai_user].where(:user_id => user_id).first
    if row.nil?
      # Has sakai_user_id_map record, but not sakai_user record. Provided account!
      # TODO: replace with a ProvidedUser subclass
      @@cache[eid] = @@cache[user_id] = User.new(user_id,'Provided')
    else
      @@cache[eid] = @@cache[user_id] = User.new(row)
    end
  end
  @@cache[id]
end

.find!(id) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/sakai-info/user.rb', line 48

def self.find!(id)
  begin
    user = User.find(id)
  rescue ObjectNotFoundException => e
    if e.classname == User.name
      user = MissingUser.find(id)
    end
  end
end

.find_by_name(name) ⇒ Object



192
193
194
# File 'lib/sakai-info/user.rb', line 192

def self.find_by_name(name)
  User.query_by_name(name).all.collect{|row| @@cache[row[:user_id]] = User.new(row)}
end

.find_by_realm_id_and_role_id(realm_id, role_id) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/sakai-info/user.rb', line 164

def self.find_by_realm_id_and_role_id(realm_id, role_id)
  # TODO: implement this correctly
  #  (code below isn't going to work)
  # users = []
  # DB.connect.fetch("select first_name, last_name, type, " +
  #                  "to_char(createdon,'YYYY-MM-DD HH24:MI:SS') as created_at, " +
  #                  "to_char(modifiedon,'YYYY-MM-DD HH24:MI:SS') as modified_at " +
  #                  "from sakai_user where user_id in " +
  #                  "(select user_id from sakai_realm_rl_gr " +
  #                  "where realm_key = ? " +
  #                  "and role_key = ?)", realm_id, role_id) do |row|
  #   first_name, last_name, type, created_at, modified_at = *row
  #   first_name ||= ""
  #   last_name ||= ""
  #   @@cache[eid] =
  #     @@cache[user_id] =
  #     User.new(user_id, eid, (first_name+' '+last_name), type, created_at, modified_at)
  #   users << @@cache[eid]
  # end
  # users
  nil
end

.find_ids_by_name(name) ⇒ Object



196
197
198
# File 'lib/sakai-info/user.rb', line 196

def self.find_ids_by_name(name)
  User.query_by_name(name).select(:user_id).all.collect{|row|row[:user_id]}
end

.get_eid(id) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/sakai-info/user.rb', line 63

def self.get_eid(id)
  if ids = User.get_ids(id)
    ids[:eid]
  else
    nil
  end
end

.get_ids(id) ⇒ Object



58
59
60
61
# File 'lib/sakai-info/user.rb', line 58

def self.get_ids(id)
  @@id_cache[id] ||=
    DB.connect[:sakai_user_id_map].where(:user_id => id).or(:eid => id).first
end

.get_user_id(id) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/sakai-info/user.rb', line 71

def self.get_user_id(id)
  if ids = User.get_ids(id)
    ids[:user_id]
  else
    nil
  end
end

.query_by_name(name) ⇒ Object

by_name: uses like



188
189
190
# File 'lib/sakai-info/user.rb', line 188

def self.query_by_name(name)
  DB.connect[:sakai_user].where("upper(first_name) like ? or upper(last_name) like ? or upper(first_name)||\' \'||upper(last_name) like ?", "%#{name.upcase}%", "%#{name.upcase}%", "%#{name.upcase}%")
end

Instance Method Details

#assignment_content_countObject



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

def assignment_content_count
  @assignment_content_count ||= AssignmentContent.count_by_user_id(@id)
end

#assignment_contentsObject



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

def assignment_contents
  @assignment_contents ||= AssignmentContent.find_by_user_id(@id)
end

#assignment_submission_countObject



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

def assignment_submission_count
  @assignment_submission_count ||= AssignmentSubmission.count_by_user_id(@id)
end

#assignment_submissionsObject



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

def assignment_submissions
  @assignment_submissions ||= AssignmentSubmission.find_by_user_id(@id)
end

#default_serializationObject

yaml/json serialization



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/sakai-info/user.rb', line 201

def default_serialization
  result = {
    "id" => self.id,
    "name" => self.name,
    "eid" => self.eid,
    "email" => self.email,
    "type" => self.type,
    "user_properties" => UserProperty.find_by_user_id(self.id),
    "site_count" => self.site_count,
    "question_pool_count" => self.question_pool_count
  }
  if result["user_properties"] == {}
    result.delete("user_properties")
  end
  result
end

#membershipObject



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

def membership
  @membership ||= SiteMembership.find_by_user_id(@id)
end

#pools_serializationObject



233
234
235
236
237
238
239
240
241
# File 'lib/sakai-info/user.rb', line 233

def pools_serialization
  if self.question_pool_count > 0
    {
      "question_pools" => self.question_pools.collect { |qp| qp.serialize(:user_summary) }
    }
  else
    {}
  end
end

#preferences_xmlObject



143
144
145
146
147
148
149
150
151
152
# File 'lib/sakai-info/user.rb', line 143

def preferences_xml
  if @preferences_xml.nil?
    @preferences_xml = ""
    row = DB.connect[:sakai_preferences].filter(:preferences_id => @user_id).first
    if not row.nil?
      REXML::Document.new(row[:xml].read).write(@preferences_xml, 2)
    end
  end
  @preferences_xml
end

#propertiesObject



99
100
101
# File 'lib/sakai-info/user.rb', line 99

def properties
  @properties ||= UserProperty.find_by_user_id(@id)
end

#question_pool_countObject



111
112
113
# File 'lib/sakai-info/user.rb', line 111

def question_pool_count
  @question_pool_count ||= QuestionPool.count_by_user_id(@id)
end

#question_poolsObject



107
108
109
# File 'lib/sakai-info/user.rb', line 107

def question_pools
  @question_pools ||= QuestionPool.find_by_user_id(@id)
end

#site_countObject



135
136
137
138
139
140
141
# File 'lib/sakai-info/user.rb', line 135

def site_count
  if @membership.nil?
    Site.count_by_user_id(@id)
  else
    @membership.length
  end
end

#sites_serializationObject



227
228
229
230
231
# File 'lib/sakai-info/user.rb', line 227

def sites_serialization
  {
    "sites" => self.membership.collect { |sm| sm.serialize(:user_summary) }
  }
end

#summary_serializationObject



218
219
220
221
222
223
224
225
# File 'lib/sakai-info/user.rb', line 218

def summary_serialization
  {
    "id" => self.id,
    "eid" => self.eid,
    "name" => self.name,
    "type" => self.type
  }
end

#workspaceObject



103
104
105
# File 'lib/sakai-info/user.rb', line 103

def workspace
  @workspace ||= Site.find("~" + @id)
end