17 import(
'lib.pkp.classes.user.User');
20 define(
'USER_FIELD_USERID',
'user_id');
21 define(
'USER_FIELD_USERNAME',
'username');
22 define(
'USER_FIELD_EMAIL',
'email');
23 define(
'USER_FIELD_URL',
'url');
24 define(
'USER_FIELD_INTERESTS',
'interests');
25 define(
'USER_FIELD_AFFILIATION',
'affiliation');
26 define(
'USER_FIELD_NONE',
null);
44 function getById($userId, $allowDisabled =
true) {
46 'SELECT * FROM users WHERE user_id = ?' . ($allowDisabled?
'':
' AND disabled = 0'),
51 if ($result->RecordCount() != 0) {
66 'SELECT * FROM users WHERE username = ?' . ($allowDisabled?
'':
' AND disabled = 0'),
71 if ($result->RecordCount() != 0) {
85 function getBySetting($settingName, $settingValue, $allowDisabled =
true) {
87 'SELECT u.* FROM users u JOIN user_settings us ON (u.user_id = us.user_id) WHERE us.setting_name = ? AND us.setting_value = ?' . ($allowDisabled?
'':
' AND u.disabled = 0'),
88 array($settingName, $settingValue)
92 if ($result->RecordCount() != 0) {
107 'SELECT * FROM users WHERE auth_str = ?' . ($allowDisabled?
'':
' AND disabled = 0'),
112 if ($result->RecordCount() != 0) {
127 'SELECT * FROM users WHERE email = ?' . ($allowDisabled?
'':
' AND disabled = 0'),
132 if ($result->RecordCount() != 0) {
148 'SELECT * FROM users WHERE username = ? AND password = ?' . ($allowDisabled?
'':
' AND disabled = 0'),
149 array($username, $password)
153 if ($result->RecordCount() != 0) {
180 LEFT JOIN user_user_groups uug ON (uug.user_id = u.user_id)
181 LEFT JOIN user_groups ug ON (ug.user_group_id = uug.user_group_id)
182 LEFT JOIN review_assignments r ON (r.reviewer_id = u.user_id)
184 WHERE ug.context_id = ? AND
186 r.submission_id = ? AND
209 (
int) $reviewRound->getStageId(),
212 $params[] = (int) $submissionId;
213 $params[] = (int) $reviewRound->getId();
215 $nameSearchJoins =
'LEFT JOIN user_settings usgs ON (u.user_id = usgs.user_id AND usgs.setting_name = \'' . IDENTITY_SETTING_GIVENNAME .
'\')
216 LEFT JOIN user_settings usfs ON (u.user_id = usfs.user_id AND usfs.setting_name = \
'' . IDENTITY_SETTING_FAMILYNAME .
'\')
';
217 $params[] = $params[] = $params[] = $params[] = "%$name%";
220 $result = $this->retrieve(
221 'SELECT DISTINCT u.*,
222 ' . $this->getFetchColumns() .'
224 JOIN user_user_groups uug ON (uug.user_id = u.user_id)
225 JOIN user_groups ug ON (ug.user_group_id = uug.user_group_id AND ug.context_id = ? AND ug.role_id = ?)
226 JOIN user_group_stage ugs ON (ugs.user_group_id = ug.user_group_id AND ugs.stage_id = ?)
' .
227 (!empty($name) ? $nameSearchJoins : '') .'
228 ' . $this->getFetchJoins() . '
229 WHERE 0=(SELECT COUNT(r.reviewer_id)
230 FROM review_assignments r
231 WHERE r.submission_id = ? AND r.reviewer_id = u.user_id AND r.review_round_id = ?)
' .
232 (!empty($name) ?' AND (usgs.setting_value LIKE ? OR usfs.setting_value LIKE ? OR username LIKE ? OR email LIKE ?)
' : '') .'
233 ' .$this->getOrderBy(),
245 function _returnUserFromRowWithReviewerStats($row) {
246 $user = $this->_returnUserFromRowWithData($row, false);
247 $user->setData('lastAssigned
', $row['last_assigned
']);
248 $user->setData('incompleteCount
', (int) $row['incomplete_count
']);
249 $user->setData('completeCount
', (int) $row['complete_count
']);
250 $user->setData('declinedCount
', (int) $row['declined_count
']);
251 $user->setData('cancelledCount
', (int) $row['cancelled_count
']);
252 $user->setData('averageTime
', (int) $row['average_time
']);
254 // 0 values should return null. They represent a reviewer with no ratings
255 if ($row['reviewer_rating
']) {
256 $user->setData('reviewerRating
', max(1, round($row['reviewer_rating
'])));
270 function _returnUserFromRowWithData($row, $callHook = true) {
271 $user = $this->_returnUserFromRow($row, false);
272 $this->getDataObjectSettings('user_settings
', 'user_id
', $row['user_id
'], $user);
274 if (isset($row['review_id
'])) $user->review_id = $row['review_id
'];
286 function _returnUserFromRow($row, $callHook = true) {
287 $user = $this->newDataObject();
288 $user->setId($row['user_id
']);
289 $user->setUsername($row['username
']);
290 $user->setPassword($row['password
']);
291 $user->setEmail($row['email
']);
292 $user->setUrl($row['url
']);
293 $user->setPhone($row['phone
']);
294 $user->setMailingAddress($row['mailing_address
']);
295 $user->setBillingAddress($row['billing_address
']);
296 $user->setCountry($row['country
']);
297 $user->setLocales(isset($row['locales
']) && !empty($row['locales
']) ? explode(':
', $row['locales
']) : array());
298 $user->setDateLastEmail($this->datetimeFromDB($row['date_last_email
']));
299 $user->setDateRegistered($this->datetimeFromDB($row['date_registered
']));
300 $user->setDateValidated($this->datetimeFromDB($row['date_validated
']));
301 $user->setDateLastLogin($this->datetimeFromDB($row['date_last_login
']));
302 $user->setMustChangePassword($row['must_change_password
']);
303 $user->setDisabled($row['disabled
']);
304 $user->setDisabledReason($row['disabled_reason
']);
305 $user->setAuthId($row['auth_id
']);
306 $user->setAuthStr($row['auth_str
']);
307 $user->setInlineHelp($row['inline_help
']);
308 $user->setGossip($row['gossip
']);
319 function insertObject($user) {
320 if ($user->getDateRegistered() == null) {
321 $user->setDateRegistered(Core::getCurrentDate());
323 if ($user->getDateLastLogin() == null) {
324 $user->setDateLastLogin(Core::getCurrentDate());
327 sprintf('INSERT INTO users
328 (username, password, email, url, phone, mailing_address, billing_address, country, locales, date_last_email, date_registered, date_validated, date_last_login, must_change_password, disabled, disabled_reason, auth_id, auth_str, inline_help, gossip)
330 (?, ?, ?, ?, ?, ?, ?, ?, ?, %s, %s, %s, %s, ?, ?, ?, ?, ?, ?, ?)
',
331 $this->datetimeToDB($user->getDateLastEmail()), $this->datetimeToDB($user->getDateRegistered()), $this->datetimeToDB($user->getDateValidated()), $this->datetimeToDB($user->getDateLastLogin())),
333 $user->getUsername(),
334 $user->getPassword(),
338 $user->getMailingAddress(),
339 $user->getBillingAddress(),
341 join(':
', $user->getLocales()),
342 $user->getMustChangePassword() ? 1 : 0,
343 $user->getDisabled() ? 1 : 0,
344 $user->getDisabledReason(),
345 $user->getAuthId()=='' ? null : (int) $user->getAuthId(),
347 (int) $user->getInlineHelp(),
352 $user->setId($this->getInsertId());
353 $this->updateLocaleFields($user);
354 return $user->getId();
360 function getLocaleFieldNames() {
361 return array('biography
', 'signature
', 'affiliation
',
362 IDENTITY_SETTING_GIVENNAME, IDENTITY_SETTING_FAMILYNAME, 'preferredPublicName
');
368 function getAdditionalFieldNames() {
369 return array_merge(parent::getAdditionalFieldNames(), array(
379 function updateLocaleFields($user) {
380 $this->updateDataObjectSettings('user_settings
', $user, array(
381 'user_id
' => (int) $user->getId()
389 function updateObject($user) {
390 if ($user->getDateLastLogin() == null) {
391 $user->setDateLastLogin(Core::getCurrentDate());
394 $this->updateLocaleFields($user);
396 return $this->update(
397 sprintf('UPDATE users
407 date_last_email = %s,
409 date_last_login = %s,
410 must_change_password = ?,
418 $this->datetimeToDB($user->getDateLastEmail()), $this->datetimeToDB($user->getDateValidated()), $this->datetimeToDB($user->getDateLastLogin())),
420 $user->getUsername(),
421 $user->getPassword(),
425 $user->getMailingAddress(),
426 $user->getBillingAddress(),
428 join(':
', $user->getLocales()),
429 $user->getMustChangePassword() ? 1 : 0,
430 $user->getDisabled() ? 1 : 0,
431 $user->getDisabledReason(),
432 $user->getAuthId()=='' ? null : (int) $user->getAuthId(),
434 (int) $user->getInlineHelp(),
436 (int) $user->getId(),
445 function deleteObject($user) {
446 $this->deleteUserById($user->getId());
453 function deleteUserById($userId) {
454 $this->update('DELETE FROM user_settings WHERE user_id = ?
', array((int) $userId));
455 $this->update('DELETE FROM users WHERE user_id = ?
', array((int) $userId));
464 function getUserFullName($userId, $allowDisabled = true) {
465 $user = $this->getById($userId, $allowDisabled);
466 return $user?$user->getFullName():null;
475 function getUserEmail($userId, $allowDisabled = true) {
476 $result = $this->retrieve(
477 'SELECT email FROM users WHERE user_id = ?
' . ($allowDisabled?'':' AND disabled = 0
'),
482 if ($result->RecordCount()) {
483 $email = $result->fields[0];
496 function getUsersWithNoRole($allowDisabled = true, $dbResultRange = null) {
498 ' . $this->getFetchColumns() . '
500 ' . $this->getFetchJoins() . '
501 LEFT JOIN roles r ON u.user_id=r.user_id
502 WHERE r.role_id IS NULL
';
504 $orderSql = $this->getOrderBy(); // FIXME Add "sort field" parameter?
505 $params = $this->getFetchParameters();
506 $result = $this->retrieveRange($sql . ($allowDisabled?'':' AND u.disabled = 0
') . $orderSql, $params, $dbResultRange);
517 function userExistsById($userId, $allowDisabled = true) {
518 $result = $this->retrieve(
519 'SELECT COUNT(*) FROM users WHERE user_id = ?' . ($allowDisabled?'':' AND disabled = 0'),
522 $userExists = isset($result->fields[0]) && $result->fields[0] != 0;
537 'SELECT COUNT(*) FROM users WHERE username = ?' . (isset($userId) ?
' AND user_id != ?' :
'') . ($allowDisabled?
'':
' AND disabled = 0'),
538 isset($userId) ? array($username, (
int) $userId) : array($username)
540 $userExists = isset($result->fields[0]) && $result->fields[0] == 1;
555 'SELECT COUNT(*) FROM users WHERE email = ?' . (isset($userId) ?
' AND user_id != ?' :
'') . ($allowDisabled?
'':
' AND disabled = 0'),
556 isset($userId) ? array($email, (
int) $userId) : array($email)
558 $userExists = isset($result->fields[0]) && $result->fields[0] == 1;
572 $settingNames = array(IDENTITY_SETTING_GIVENNAME, IDENTITY_SETTING_FAMILYNAME,
'preferredPublicName');
573 foreach ($settingNames as $settingName) {
574 $params = array($newLocale, $settingName);
576 "DELETE from user_settings
577 WHERE locale = ? AND setting_name = ? AND setting_value = ''",
582 $params = array($newLocale, IDENTITY_SETTING_GIVENNAME, $newLocale, IDENTITY_SETTING_FAMILYNAME, $newLocale,
'preferredPublicName');
584 "SELECT DISTINCT us.user_id, usg.setting_value AS given_name, usf.setting_value AS family_name, usp.setting_value AS preferred_public_name
585 FROM user_settings us
586 LEFT JOIN user_settings usg ON (usg.user_id = us.user_id AND usg.locale = ? AND usg.setting_name = ?)
587 LEFT JOIN user_settings usf ON (usf.user_id = us.user_id AND usf.locale = ? AND usf.setting_name = ?)
588 LEFT JOIN user_settings usp ON (usp.user_id = us.user_id AND usp.locale = ? AND usp.setting_name = ?)",
591 while (!$result->EOF) {
592 $row = $result->GetRowAssoc(
false);
593 $userId = $row[
'user_id'];
594 if (empty($row[
'given_name']) && empty($row[
'family_name']) && empty($row[
'preferred_public_name'])) {
596 foreach ($settingNames as $settingName) {
597 $params = array($newLocale, $settingName, $settingName, $oldLocale, $userId);
599 "INSERT INTO user_settings (user_id, locale, setting_name, setting_value, setting_type)
600 SELECT DISTINCT us.user_id, ?, ?, us.setting_value, 'string'
601 FROM user_settings us
602 WHERE us.setting_name = ? AND us.locale = ? AND us.user_id = ?",
606 } elseif (empty($row[
'given_name'])) {
608 $params = array($newLocale, IDENTITY_SETTING_GIVENNAME, IDENTITY_SETTING_GIVENNAME, $oldLocale, $userId);
610 "INSERT INTO user_settings (user_id, locale, setting_name, setting_value, setting_type)
611 SELECT DISTINCT us.user_id, ?, ?, us.setting_value, 'string'
612 FROM user_settings us
613 WHERE us.setting_name = ? AND us.locale = ? AND us.user_id = ?",
639 $primaryLocale = $site->getPrimaryLocale();
641 IDENTITY_SETTING_GIVENNAME, $locale,
642 IDENTITY_SETTING_GIVENNAME, $primaryLocale,
643 IDENTITY_SETTING_FAMILYNAME, $locale,
644 IDENTITY_SETTING_FAMILYNAME, $primaryLocale,
653 return 'COALESCE(ugl.setting_value, ugpl.setting_value) AS user_given,
654 CASE WHEN ugl.setting_value <> \'\' THEN ufl.setting_value ELSE ufpl.setting_value END AS user_family';
662 return 'LEFT JOIN user_settings ugl ON (u.user_id = ugl.user_id AND ugl.setting_name = ? AND ugl.locale = ?)
663 LEFT JOIN user_settings ugpl ON (u.user_id = ugpl.user_id AND ugpl.setting_name = ? AND ugpl.locale = ?)
664 LEFT JOIN user_settings ufl ON (u.user_id = ufl.user_id AND ufl.setting_name = ? AND ufl.locale = ?)
665 LEFT JOIN user_settings ufpl ON (u.user_id = ufpl.user_id AND ufpl.setting_name = ? AND ufpl.locale = ?)';
673 return 'ORDER BY user_family, user_given';