Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Project Affiliations Menu to Main Interface #9417

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 100 additions & 64 deletions php/libraries/User.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -55,33 +55,35 @@ class User extends UserPermissions implements
{
$obj = new User;

// right off, set the username
// Set the username
$obj->userInfo['UserID'] = $username;

// get the user's permissions
// Get the user's permissions
if ($obj->select($username) === false) {
// If user selection fails, return an AnonymousUser instance
$obj = new \LORIS\AnonymousUser();
return $obj;
};
}

// create DB object
// Create DB object
$DB = \NDB_Factory::singleton()->database();

// get user data from database
// Fetch user data from the 'users' table
$query = "SELECT * FROM users WHERE UserID = :UID";

$row = $DB->pselectRow($query, ['UID' => $username]);
$row = $DB->pselectRow($query, ['UID' => $username]);

if (is_null($row)) {
// If no user is found, return an AnonymousUser instance
$obj = new \LORIS\AnonymousUser();
return $obj;
}

//Change 'language_preference' to number rather than a string
// Convert 'language_preference' from string to integer
$row['language_preference'] = (int)$row['language_preference'];

// get user sites
$user_centerID_query = $DB->pselect(
// ------------------- Extract User Sites -------------------
// Fetch user's centers and their names from 'user_psc_rel' and 'psc' tables
$user_centerID_query = $DB->pselect(
"
SELECT
upr.CenterID,
Expand All @@ -92,70 +94,96 @@ class User extends UserPermissions implements
psc
ON (upr.CenterID = psc.CenterID)
WHERE
upr.UserID= :UID
upr.UserID = :UID
",
['UID' => $row['ID']]
);

$user_cid = [];
$sitenames = [];
$user_cid = []; // Array to store CenterID objects
$sitenames = []; // Array to store site names

foreach ($user_centerID_query as $key=>$val) {
// Convert string representation of ID to int
$user_cid[$key] = \CenterID::singleton($val['CenterID']);
$sitenames[] = $val['Name'];
}

$row['Sites'] = implode(';', $sitenames);

$user_pid = $DB->pselectCol(
"SELECT ProjectID FROM user_project_rel upr WHERE upr.UserId=:uid",
['uid' => $row['ID']]
// ------------------- Extract User Projects -------------------

$user_projects_query = $DB->pselect(
"
SELECT upr.ProjectID, p.Name
FROM user_project_rel upr
LEFT JOIN Project p ON (upr.ProjectID = p.ProjectID)
WHERE upr.UserID = :UID
",
['UID' => $row['ID']]
);
foreach ($user_pid as $k=>$pid) {
$user_pid[$k] = ProjectID::singleton(intval($pid));

$user_pid = []; // Array to store ProjectID objects
$project_names = []; // Array to store project names

foreach ($user_projects_query as $key => $val) {
// Convert ProjectID to ProjectID object, ensuring it's a string
$user_pid[$key] = \ProjectID::singleton($val['ProjectID']);
$project_names[] = $val['Name'];
}

// Get examiner information
// Assign ProjectIDs and concatenated project names to userInfo
$row['ProjectIDs'] = $user_pid;
$row['Projects'] = implode(';', $project_names);

// ------------------- Extract Examiner Information -------------------
$examiner_check = $DB->pselect(
"SELECT e.full_name,
epr.centerID,
e.radiologist,
epr.active,
epr.pending_approval
FROM examiners e
JOIN examiners_psc_rel epr ON (e.examinerID=epr.examinerID)
WHERE e.userID=:uid
AND (epr.active='Y'
OR (epr.active='N' AND epr.pending_approval='Y')
)",
FROM examiners e
JOIN examiners_psc_rel epr ON (e.examinerID = epr.examinerID)
WHERE e.userID = :uid
AND (epr.active = 'Y'
OR (epr.active = 'N' AND epr.pending_approval = 'Y'))
",
[
"uid" => $row['ID'],
]
);

$examiner_info =[];
if (!empty($examiner_check) && !is_null($examiner_check)) {
$examiner_info = [];

if (!empty($examiner_check)&& !is_null($examiner_check)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add back the space before &&

foreach ($examiner_check as $val) {
if ($val['active'] == 'Y') {
// Store pending approval status if examiner is active
$examiner_info['pending'] = $val['pending_approval'];
}
$examiner_info[$val['centerID']] =[
/*
Store examiner's centerID with active status and radiologist flag,
ensuring centerID is a string
*/
$examiner_info[$val['centerID']] = [
$val['active'],
$val['radiologist'],
];
}
}
// store user data in object property

// Assign examiner information, CenterIDs, and ProjectIDs to userInfo
$row['examiner'] = $examiner_info;
$row['CenterIDs'] = $user_cid;
$row['ProjectIDs'] = $user_pid;
$obj->userInfo = $row;

return $obj;
}


/**
* Singleton method. Retrieve the user passed as parameter, but only
* 1 instance.
* one instance exists.
*
* @param string|null $username Identifies the user
*
Expand All @@ -172,7 +200,6 @@ class User extends UserPermissions implements
return $instance;
}


/**
* Inserts data into the `users` table.
*
Expand All @@ -187,9 +214,8 @@ class User extends UserPermissions implements
\NDB_Factory::singleton()->database()->insert('users', $set);
}


/**
* Updates a user
* Updates a user in the `users` table.
*
* @param array $set The array formatted for use in a Database call
*
Expand All @@ -212,7 +238,7 @@ class User extends UserPermissions implements
* @param string $var Name of variable to get
*
* @note Call without any arguments to get the entire user data array
* @return array<array|string>|string
* @return array<array|string>|string|null
* @access public
* @throws LorisException
*/
Expand All @@ -222,18 +248,16 @@ class User extends UserPermissions implements
return $this->userInfo;
} elseif ($var === 'CenterID') {
throw new \LorisException(
"The function getData('CenterID')
is deprecated and is replaced
with getData('CenterIDs')"
"The function getData('CenterID') is deprecated and is
replaced with getData('CenterIDs')"
);
} elseif ($var === 'Site') {
throw new \LorisException(
"The function getData('Site')
is deprecated and is replaced
with getData('Sites')"
"The function getData('Site') is deprecated and is
replaced with getData('Sites')"
);
} else {
return $this->userInfo[$var];
return $this->userInfo[$var] ?? null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain the reasoning behind this change?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's to prevent an error if the key doesn't exist in the userinfo.

}
}

Expand Down Expand Up @@ -282,41 +306,55 @@ class User extends UserPermissions implements
}

/**
* Returns an array of sites a user belongs to.
* Get the user's sites' names.
*
* @return array
*/
function getSiteNames(): array
{
/* The original query to fetch userInfo in the factory() function CONCAT
* CONCATs the site names together in a string using semi-colons.
* Therefore this string must be split and returned as an array.
/* The original query in the factory() function CONCATs the site names
* together in a string using semi-colons. Therefore, this string must
* be split and returned as an array.
*/
return explode(';', $this->userInfo['Sites']);
return explode(';', $this->userInfo['Sites'] ?? '');
}

/**
* Get the user's sites' ID numbers
* Get the user's project names.
*
* @return array
*/
public function getProjectNames(): array
{
$projects = $this->userInfo['Projects'] ?? '';
if ($projects === '') {
return [];
}
return explode(';', $projects);
}

/**
* Get the user's sites' ID numbers.
*
* @return CenterID[]
*/
function getCenterIDs(): array
{
return $this->userInfo['CenterIDs'];
return $this->userInfo['CenterIDs'] ?? [];
}

/**
* Get the user's projects' ID numbers
* Get the user's projects' ID numbers.
*
* @return ProjectID[]
*/
function getProjectIDs(): array
{
return $this->userInfo['ProjectIDs'];
return $this->userInfo['ProjectIDs'] ?? [];
}

/**
* Get the user's projects
* Get the user's projects.
*
* @return Project[]
*/
Expand All @@ -329,26 +367,25 @@ class User extends UserPermissions implements
}

/**
* Get the user's language preference
* Get the user's language preference.
*
* @return ?int
* @return ?int|null
*/
function getLanguagePreference(): ?int
{
return $this->userInfo['language_preference'];
return $this->userInfo['language_preference'] ?? null;
}


/**
* Returns all sites where Examiner is active
* Returns all sites where Examiner is active.
*
* @return array
*/
function getExaminerSites(): array
{
$site_list = $this->userInfo['examiner'];
unset($site_list['pending']);
ksort($site_list);
$site_list = $this->userInfo['examiner'] ?? [];
unset($site_list['pending']); // Remove 'pending' status if exists
ksort($site_list); // Sort sites by centerID
return $site_list;
}

Expand All @@ -368,12 +405,11 @@ class User extends UserPermissions implements
}

/**
* Returns all user's sites in associative
* array (CenterID => CenterName)
* Returns all user's sites in an associative array (CenterID => CenterName).
*
* @return array
*/
function getSiteNamesList(): array
public function getSiteNamesList(): array
{
$sites = [];
foreach ($this->getSites() as $site) {
Expand All @@ -382,7 +418,6 @@ class User extends UserPermissions implements
return $sites;
}


/**
* Returns all user's sites that are StudySites
*
Expand Down Expand Up @@ -421,6 +456,7 @@ class User extends UserPermissions implements
return false;
}


/**
* Checks that the user's email is valid
*
Expand Down
Loading
Loading