From 07ed4ecaff4f7ed5f8fbef099de0401695ea3be1 Mon Sep 17 00:00:00 2001 From: Nada Elmasry Date: Mon, 21 Oct 2024 20:11:54 -0400 Subject: [PATCH 1/3] added project affiliations menu to nav bar --- php/libraries/User.class.inc | 164 +++++++++++------- smarty/templates/main.tpl | 38 +++- .../UserPageDecorationMiddleware.php | 53 +++--- 3 files changed, 159 insertions(+), 96 deletions(-) diff --git a/php/libraries/User.class.inc b/php/libraries/User.class.inc index f80bcf1962c..0ba23fe2e73 100644 --- a/php/libraries/User.class.inc +++ b/php/libraries/User.class.inc @@ -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, @@ -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)) { 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[(string)$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 * @@ -172,7 +200,6 @@ class User extends UserPermissions implements return $instance; } - /** * Inserts data into the `users` table. * @@ -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 * @@ -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|string + * @return array|string|null * @access public * @throws LorisException */ @@ -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; } } @@ -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[] */ @@ -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; } @@ -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) { @@ -382,7 +418,6 @@ class User extends UserPermissions implements return $sites; } - /** * Returns all user's sites that are StudySites * @@ -421,6 +456,7 @@ class User extends UserPermissions implements return false; } + /** * Checks that the user's email is valid * diff --git a/smarty/templates/main.tpl b/smarty/templates/main.tpl index ec4b517917c..e9bd6b17d2b 100644 --- a/smarty/templates/main.tpl +++ b/smarty/templates/main.tpl @@ -99,7 +99,8 @@ {if $bvl_feedback|default} @@ -145,6 +146,15 @@ {/if} + + + + + + + +