diff --git a/lib/Project.php b/lib/Project.php index e4383937..065b5267 100644 --- a/lib/Project.php +++ b/lib/Project.php @@ -281,9 +281,17 @@ final class Project{ * @throws Exceptions\UserNotFoundException If a manager or reviewer could not be auto-assigned. */ public function Create(): void{ + // Is this `Project` being produced by one of the editors? + try{ + $producer = User::GetByIdentifier($this->ProducerName); + } + catch(Exceptions\UserNotFoundException){ + $producer = null; + } + if(!isset($this->ManagerUserId)){ try{ - $this->Manager = User::GetByAvailableForProjectAssignment(Enums\ProjectRoleType::Manager, null); + $this->Manager = User::GetByAvailableForProjectAssignment(Enums\ProjectRoleType::Manager, [$producer->UserId ?? 0]); } catch(Exceptions\UserNotFoundException){ throw new Exceptions\UserNotFoundException('Could not auto-assign a suitable manager.'); @@ -294,7 +302,7 @@ final class Project{ if(!isset($this->ReviewerUserId)){ try{ - $this->Reviewer = User::GetByAvailableForProjectAssignment(Enums\ProjectRoleType::Reviewer, $this->Manager->UserId); + $this->Reviewer = User::GetByAvailableForProjectAssignment(Enums\ProjectRoleType::Reviewer, [$this->Manager->UserId, $producer->UserId ?? 0]); } catch(Exceptions\UserNotFoundException){ unset($this->Manager); diff --git a/lib/User.php b/lib/User.php index 02209ef2..13e177ff 100644 --- a/lib/User.php +++ b/lib/User.php @@ -401,18 +401,33 @@ class User{ ', [], User::class); } + /** + * @return array + */ + public static function GetNamesByHasProducedProject(): array{ + return Db::Query(' + SELECT + distinct (ProducerName) + from Projects + order by ProducerName asc + ', []); + } + /** * Get a random `User` who is available to be assigned to the given role. * * @param Enums\ProjectRoleType $role The role to select for. - * @param int $excludedUserId Don't include this `UserId` when selecting; `null` to not exclude any users. + * @param array $excludedUserIds Don't include these `UserId` when selecting. * * @throws Exceptions\UserNotFoundException If no `User` is available to be assigned to a `Project`. */ - public static function GetByAvailableForProjectAssignment(Enums\ProjectRoleType $role, ?int $excludedUserId): User{ + public static function GetByAvailableForProjectAssignment(Enums\ProjectRoleType $role, array $excludedUserIds = []): User{ + if(sizeof($excludedUserIds) == 0){ + $excludedUserIds = [0]; + } + // First, check if there are `User`s available for assignment. - // We use `coalesce()` to allow comparison in case `$excludedUserId` is `null` - there will never be a `UserId` of `0`. - $doUnassignedUsersExist = Db::QueryBool('SELECT exists (select * from ProjectUnassignedUsers where Role = ? and UserId != coalesce(?, 0))', [$role, $excludedUserId]); + $doUnassignedUsersExist = Db::QueryBool('SELECT exists (select * from ProjectUnassignedUsers where Role = ? and UserId not in ' . Db::CreateSetSql($excludedUserIds) . ')', array_merge([$role], $excludedUserIds)); // No unassigned `User`s left. Refill the list. if(!$doUnassignedUsersExist){ @@ -433,7 +448,7 @@ class User{ } // Now, select a random `User`. - $user = Db::Query('SELECT u.* from Users u inner join ProjectUnassignedUsers puu using (UserId) where Role = ? and UserId != coalesce(?, 0) order by rand()', [$role, $excludedUserId], User::class)[0] ?? throw new Exceptions\UserNotFoundException(); + $user = Db::Query('SELECT u.* from Users u inner join ProjectUnassignedUsers puu using (UserId) where Role = ? and UserId not in ' . Db::CreateSetSql($excludedUserIds) . ' order by rand()', array_merge([$role], $excludedUserIds), User::class)[0] ?? throw new Exceptions\UserNotFoundException(); // Delete the `User` we just got from the unassigned users list. Db::Query('DELETE from ProjectUnassignedUsers where UserId = ? and Role = ?', [$user->UserId, $role]); diff --git a/templates/ProjectForm.php b/templates/ProjectForm.php index e98034cd..7b7bedbb 100644 --- a/templates/ProjectForm.php +++ b/templates/ProjectForm.php @@ -2,6 +2,7 @@ $project = $project ?? new Project(); $managers = User::GetAllByCanManageProjects(); $reviewers = User::GetAllByCanReviewProjects(); +$pastProducers = User::GetNamesByHasProducedProject(); $areFieldsRequired = $areFieldsRequired ?? true; $isEditForm = $isEditForm ?? false; ?> @@ -9,11 +10,18 @@ $isEditForm = $isEditForm ?? false; + + + + + +