CWIS Developer Documentation
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 
14 {
15  # ---- PUBLIC INTERFACE --------------------------------------------------
16 
20  public function __construct()
21  {
22  parent::__construct();
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
165 
166  # set the user ID for the resource
167  $Resource->Set("UserId", $User->Id());
168 
170  $TimestampFields = $Schema->GetFields(MetadataSchema::MDFTYPE_TIMESTAMP);
171 
172  # update timestamps as required
173  foreach ($TimestampFields as $Field)
174  {
175  if ($Field->UpdateMethod()
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 }
$ResourceFactory
The resource factory for user resources.
GetTopContributors($Limit=5)
Get a list of users sorted by how many resources they have added or edited, starting with those who h...
GetMostRecentContributors($Limit=5)
Get the users sorted by when they last added or edited a resource starting with those who added/edite...
Metadata schema (in effect a Factory class for MetadataField).
Definition: User.php:41
CWIS-specific user factory class.
const UPDATEMETHOD_ONRECORDEDIT
__construct()
Construct the user factory object.
CreateNewUser($UserName, $Password, $PasswordAgain, $EMail, $EMailAgain, $IgnoreErrorCodes=NULL)
Create a new user.
const U_OKAY
Definition: User.php:18
static GenerateUniqueUsernameFromEmail($Email)
Derive a unique username from an email address.
static Create($SchemaId)
Create a new resource.
Definition: Resource.php:47
Factory for Resource objects.
CWIS-specific user class.
Definition: CWUser.php:13