Don't allow the manager or reviewer to be the same as the ebook producer

This commit is contained in:
Alex Cabal 2024-12-22 19:39:50 -06:00
parent 133c968fc0
commit ac56d2d896
3 changed files with 38 additions and 7 deletions

View file

@ -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);

View file

@ -401,18 +401,33 @@ class User{
', [], User::class);
}
/**
* @return array<stdClass>
*/
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<int> $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]);

View file

@ -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;
<input type="hidden" name="project-ebook-id" value="<?= $project->EbookId ?? '' ?>" />
<? } ?>
<datalist id="editors">
<? foreach($pastProducers as $row){ ?>
<option value="<?= Formatter::EscapeHtml($row->ProducerName) ?>"><?= Formatter::EscapeHtml($row->ProducerName) ?></option>
<? } ?>
</datalist>
<label class="icon user">
<span>Producer name</span>
<input
type="text"
name="project-producer-name"
list="editors"
<? if($areFieldsRequired){ ?>
required="required"
<? } ?>