CWIS Developer Documentation

- Home
- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

CWUserFactory.php
Go to the documentation of this file.
1 <?PHP
2 #
3 # FILE: CWUserFactory.php
4 #
5 # Part of the Collection Workflow Integration System (CWIS)
6 # Copyright 2013 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu/cwis/
8 #
9
13 class CWUserFactory extends UserFactory
14 {
15 # ---- PUBLIC INTERFACE --------------------------------------------------
16
20 public function __construct()
21 {
22 parent::__construct();
23 $this->ResourceFactory = new ResourceFactory(MetadataSchema::SCHEMAID_USER);
24 }
25
34 public function GetTopContributors($Limit = 5)
35 {
36 # assume no users will be found
37 $Users = array();
38
39 $Schema = new MetadataSchema();
40 $LastModField = $Schema->GetFieldByName("Last Modified By Id");
41
42 # fetch the top contributors
43 $this->DB->Query(
44 "SELECT UserId FROM ResourceUserInts "
45 ." WHERE FieldId = ".$LastModField->Id()
46 ." GROUP BY UserId"
47 ." ORDER BY COUNT(*) DESC"
48 ." LIMIT ".intval($Limit));
49 $UserIds = $this->DB->FetchColumn("UserId");
50
51 # for each user id found
52 foreach ($UserIds as $UserId)
53 {
54 $Users[$UserId] = new CWUser($UserId);
55 }
56
57 # return the newest users
58 return $Users;
59 }
60
69 public function GetMostRecentContributors($Limit = 5)
70 {
71 # assume no users will be found
72 $Users = array();
73
74 $Schema = new MetadataSchema();
75 $LastModField = $Schema->GetFieldByName("Last Modified By Id");
76
77 # fetch the top contributors
78 $this->DB->Query(
79 "SELECT UserId FROM ResourceUserInts RU, Resources R "
80 ." WHERE RU.FieldId = ".$LastModField->Id()
81 ." AND R.ResourceId = RU.ResourceId "
82 ." GROUP BY RU.UserId"
83 ." ORDER BY MAX(R.DateLastModified) DESC"
84 ." LIMIT ".intval($Limit));
85 $UserIds = $this->DB->FetchColumn("UserId");
86
87 # for each user id found
88 foreach ($UserIds as $UserId)
89 {
90 $Users[$UserId] = new CWUser($UserId);
91 }
92
93 # return the newest users
94 return $Users;
95 }
96
102 public static function GenerateUniqueUsernameFromEmail($Email)
103 {
104 $TrialName = explode('@', $Email);
105 $TrialName = array_shift($TrialName);
106 $TrialName = preg_replace("/[^A-Za-z0-9]/", "", $TrialName);
107 $TrialName = strtolower($TrialName);
108
109 # if this email address is very short, we'll pad it with some random
110 # characters
111 if (strlen($TrialName) < 2)
112 {
113 $TrialName .= GetRandomCharacters(2, "/[^a-hj-np-z0-9]/");
114 }
115
116 # see if the specified name exists
117 $UFactory = new UserFactory();
118
119 $Name = self::AppendSuffix($TrialName, '');
120
121 while ($UFactory->UserNameExists($Name))
122 {
123 $Name = self::AppendSuffix(
124 $TrialName, GetRandomCharacters(2) );
125 }
126
127 return $Name;
128 }
129
130 # ---- OVERRIDDEN METHODS ------------------------------------------------
131
144 public function CreateNewUser(
145 $UserName, $Password, $PasswordAgain, $EMail, $EMailAgain,
146 $IgnoreErrorCodes = NULL)
147 {
148 # add the user to the APUsers table
149 $User = parent::CreateNewUser(
150 $UserName,
151 $Password,
152 $PasswordAgain,
153 $EMail,
154 $EMailAgain,
155 $IgnoreErrorCodes);
156
157 # user account creation did not succeed, so return the error codes
158 if (!($User instanceof User))
159 {
160 return $User;
161 }
162
163 # create the user resource
164 $Resource = Resource::Create(MetadataSchema::SCHEMAID_USER);
165
166 # set the user ID for the resource
167 $Resource->Set("UserId", $User->Id());
168
169 $Schema = new MetadataSchema(MetadataSchema::SCHEMAID_USER);
170 $TimestampFields = $Schema->GetFields(MetadataSchema::MDFTYPE_TIMESTAMP);
171
172 # update timestamps as required
173 foreach ($TimestampFields as $Field)
174 {
175 if ($Field->UpdateMethod()
176 == MetadataField::UPDATEMETHOD_ONRECORDEDIT)
177 {
178 $Resource->Set($Field, "now");
179 }
180 }
181
182 # make the user resource permanent
183 $Resource->IsTempResource(FALSE);
184
185 # get the CWUser object for the user
186 $CWUser = new CWUser(intval($User->Id()));
187
188 # couldn't get the CWUser object
189 if ($CWUser->Status() != U_OKAY)
190 {
191 return array($CWUser->Status());
192 }
193
194 # set up initial UI setting
195 $CWUser->Set("ActiveUI",
196 $GLOBALS["G_SysConfig"]->DefaultActiveUI());
197
198 # set up initial privileges
199 foreach ($GLOBALS["G_SysConfig"]->DefaultUserPrivs() as $Privilege)
200 {
201 $CWUser->GivePriv($Privilege);
202 }
203
204 # return new user object to caller
205 return $CWUser;
206 }
207
208 # ---- PRIVATE INTERFACE -------------------------------------------------
209
219 private static function AppendSuffix($TrialName, $Suffix, $MaxLength=24)
220 {
221 if (strlen($TrialName.$Suffix)>$MaxLength)
222 {
223 $TrialName = substr(
224 $TrialName, 0, $MaxLength - strlen($Suffix));
225 }
226
227 return $TrialName.$Suffix;
228 }
229
233 protected $ResourceFactory;
234 }
MetadataSchema\MDFTYPE_TIMESTAMP
const MDFTYPE_TIMESTAMP
Definition: MetadataSchema.php:25
CWUserFactory\$ResourceFactory
$ResourceFactory
The resource factory for user resources.
Definition: CWUserFactory.php:233
CWUserFactory\GetTopContributors
GetTopContributors($Limit=5)
Get a list of users sorted by how many resources they have added or edited, starting with those who h...
Definition: CWUserFactory.php:34
CWUserFactory\GetMostRecentContributors
GetMostRecentContributors($Limit=5)
Get the users sorted by when they last added or edited a resource starting with those who added/edite...
Definition: CWUserFactory.php:69
MetadataSchema
Metadata schema (in effect a Factory class for MetadataField).
Definition: MetadataSchema.php:13
MetadataSchema\SCHEMAID_USER
const SCHEMAID_USER
Definition: MetadataSchema.php:55
User
Definition: User.php:41
CWUserFactory
CWIS-specific user factory class.
Definition: CWUserFactory.php:13
MetadataField\UPDATEMETHOD_ONRECORDEDIT
const UPDATEMETHOD_ONRECORDEDIT
Definition: MetadataField.php:21
CWUserFactory\__construct
__construct()
Construct the user factory object.
Definition: CWUserFactory.php:20
PHP
PHP
Definition: RSSClient--Test.php:12
UserFactory
Definition: UserFactory.php:18
CWUserFactory\CreateNewUser
CreateNewUser($UserName, $Password, $PasswordAgain, $EMail, $EMailAgain, $IgnoreErrorCodes=NULL)
Create a new user.
Definition: CWUserFactory.php:144
U_OKAY
const U_OKAY
Definition: User.php:18
CWUserFactory\GenerateUniqueUsernameFromEmail
static GenerateUniqueUsernameFromEmail($Email)
Derive a unique username from an email address.
Definition: CWUserFactory.php:102
Resource\Create
static Create($SchemaId)
Create a new resource.
Definition: Resource.php:66
ResourceFactory
Factory for Resource objects.
Definition: ResourceFactory.php:13
CWUser
CWIS-specific user class.
Definition: CWUser.php:13