Class: SakaiInfo::User

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

Constant Summary collapse

@@cache =
{}
@@id_cache =
{}

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, #object_type_serialization, #serialize, #to_json, #to_yaml

Constructor Details

#initialize(dbrow) ⇒ User

Returns a new instance of User.



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

def initialize(dbrow)
  @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]
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



199
200
201
# File 'lib/sakai-info/user.rb', line 199

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

.countObject

finders/counters



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

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

.count_by_realm_id_and_role_id(realm_id, role_id) ⇒ Object



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

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sakai-info/user.rb', line 23

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?
      raise ObjectNotFoundException.new(User, id)
    end
    @@cache[eid] = @@cache[user_id] = User.new(row)
  end
  @@cache[id]
end

.find_by_realm_id_and_role_id(realm_id, role_id) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/sakai-info/user.rb', line 137

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

.get_eid(id) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/sakai-info/user.rb', line 46

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

.get_ids(id) ⇒ Object



41
42
43
44
# File 'lib/sakai-info/user.rb', line 41

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

.get_user_id(id) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/sakai-info/user.rb', line 54

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

Instance Method Details

#assignment_content_countObject



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

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

#assignment_contentsObject



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

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

#assignment_submission_countObject



92
93
94
# File 'lib/sakai-info/user.rb', line 92

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

#assignment_submissionsObject



88
89
90
# File 'lib/sakai-info/user.rb', line 88

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

#default_serializationObject

yaml/json serialization



161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/sakai-info/user.rb', line 161

def default_serialization
  {
    "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
  }
end

#membershipObject



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

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

#pools_serializationObject



189
190
191
192
193
194
195
196
197
# File 'lib/sakai-info/user.rb', line 189

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



116
117
118
119
120
121
122
123
124
125
# File 'lib/sakai-info/user.rb', line 116

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



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

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

#question_pool_countObject



84
85
86
# File 'lib/sakai-info/user.rb', line 84

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

#question_poolsObject



80
81
82
# File 'lib/sakai-info/user.rb', line 80

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

#site_countObject



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

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

#sites_serializationObject



183
184
185
186
187
# File 'lib/sakai-info/user.rb', line 183

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

#summary_serializationObject



174
175
176
177
178
179
180
181
# File 'lib/sakai-info/user.rb', line 174

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

#workspaceObject



76
77
78
# File 'lib/sakai-info/user.rb', line 76

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