You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
225 lines
6.0 KiB
225 lines
6.0 KiB
<?php
|
|
|
|
namespace IQBall\Core\Model;
|
|
|
|
use IQBall\Core\Data\Team;
|
|
use IQBall\Core\Data\TeamInfo;
|
|
use IQBall\Core\Gateway\AccountGateway;
|
|
use IQBall\Core\Gateway\MemberGateway;
|
|
use IQBall\Core\Gateway\TeamGateway;
|
|
|
|
class TeamModel {
|
|
private AccountGateway $users;
|
|
private TeamGateway $teams;
|
|
private MemberGateway $members;
|
|
|
|
/**
|
|
* @param TeamGateway $gateway
|
|
* @param MemberGateway $members
|
|
* @param AccountGateway $users
|
|
*/
|
|
public function __construct(TeamGateway $gateway, MemberGateway $members, AccountGateway $users) {
|
|
$this->teams = $gateway;
|
|
$this->members = $members;
|
|
$this->users = $users;
|
|
}
|
|
|
|
/**
|
|
* Create a team
|
|
* @param string $name
|
|
* @param string $picture
|
|
* @param string $mainColor
|
|
* @param string $secondColor
|
|
* @return int
|
|
*/
|
|
public function createTeam(string $name, string $picture, string $mainColor, string $secondColor): int {
|
|
return $this->teams->insert($name, $picture, $mainColor, $secondColor);
|
|
}
|
|
|
|
/**
|
|
* add a member to a team
|
|
* @param string $mail
|
|
* @param int $teamId
|
|
* @param string $role
|
|
* @return int
|
|
*/
|
|
public function addMember(string $mail, int $teamId, string $role): int {
|
|
$user = $this->users->getAccountFromMail($mail);
|
|
if ($user == null) {
|
|
return -1;
|
|
}
|
|
if (!$this->members->isMemberOfTeam($teamId, $user->getUser()->getId())) {
|
|
$this->members->insert($teamId, $user->getUser()->getId(), $role);
|
|
return 1;
|
|
}
|
|
return -2;
|
|
}
|
|
|
|
/**
|
|
* @param string $name
|
|
* @param int $id
|
|
* @return TeamInfo[]
|
|
*/
|
|
public function listByName(string $name, int $id): array {
|
|
return $this->teams->listByName($name, $id);
|
|
}
|
|
|
|
/**
|
|
* @param int $idTeam
|
|
* @param int $idCurrentUser
|
|
* @return Team|null
|
|
*/
|
|
public function getTeam(int $idTeam, int $idCurrentUser): ?Team {
|
|
if (!$this->members->isMemberOfTeam($idTeam, $idCurrentUser)) {
|
|
return null;
|
|
}
|
|
$teamInfo = $this->teams->getTeamById($idTeam);
|
|
$members = $this->members->getMembersOfTeam($idTeam);
|
|
return new Team($teamInfo, $members);
|
|
}
|
|
|
|
/**
|
|
* Share tactic to a team
|
|
* @param int $teamId
|
|
* @param int $tacticId
|
|
* @return int
|
|
*/
|
|
public function shareTacticToTeam(int $teamId, int $tacticId): int {
|
|
return $this->teams->shareTacticToTeam($teamId, $tacticId);
|
|
}
|
|
|
|
/**
|
|
* Unshare tactic from every team
|
|
* @param int $tacticId
|
|
* @return int
|
|
*/
|
|
public function unshareTactic(int $tacticId): int {
|
|
return $this->teams->unshareTactic($tacticId);
|
|
}
|
|
|
|
/**
|
|
* Unshare team-shared tactic
|
|
* @param int $tacticId
|
|
* @param int $teamId
|
|
* @return int
|
|
*/
|
|
public function unshareTacticToTeam(int $tacticId, int $teamId): int {
|
|
return $this->teams->unshareTacticToTeam($tacticId, $teamId);
|
|
}
|
|
|
|
/**
|
|
* Verify if user can share tactic to a team
|
|
* @param int $teamId
|
|
* @param string $email
|
|
* @return bool
|
|
*/
|
|
public function canShareTacticToTeam(int $teamId, string $email): bool {
|
|
if($this->isCoach($teamId, $email)) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* delete a member from given team identifier
|
|
* @param int $idMember
|
|
* @param int $teamId
|
|
* @return int
|
|
*/
|
|
public function deleteMember(int $idMember, int $teamId): int {
|
|
$this->members->remove($teamId, $idMember);
|
|
if (empty($this->members->getMembersOfTeam($teamId))) {
|
|
$this->teams->deleteTeam($teamId);
|
|
return -1;
|
|
}
|
|
return $teamId;
|
|
}
|
|
|
|
/**
|
|
* Delete a team
|
|
* @param string $email
|
|
* @param int $idTeam
|
|
* @return int
|
|
*/
|
|
public function deleteTeam(string $email, int $idTeam): int {
|
|
if ($this->members->isCoach($email, $idTeam)) {
|
|
$this->teams->deleteTeam($idTeam);
|
|
return 0;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
/**
|
|
* Verify if the account associated to an email is in a specific team indicated with its id
|
|
* @param int $idTeam
|
|
* @param string $email
|
|
* @return bool
|
|
*/
|
|
public function isCoach(int $idTeam, string $email): bool {
|
|
return $this->members->isCoach($email, $idTeam);
|
|
}
|
|
|
|
/**
|
|
* Edit a team with its id, and replace the current attributes with the new ones
|
|
* @param int $idTeam
|
|
* @param string $newName
|
|
* @param string $newPicture
|
|
* @param string $newMainColor
|
|
* @param string $newSecondColor
|
|
* @return void
|
|
*/
|
|
public function editTeam(int $idTeam, string $newName, string $newPicture, string $newMainColor, string $newSecondColor) {
|
|
$this->teams->editTeam($idTeam, $newName, $newPicture, $newMainColor, $newSecondColor);
|
|
}
|
|
|
|
/**
|
|
* Get all user's teams
|
|
*
|
|
* @param integer $user
|
|
* @return array<array<string, mixed>>
|
|
*/
|
|
public function getAll(int $user): array {
|
|
return $this->teams->getAll($user);
|
|
}
|
|
|
|
/**
|
|
* Get all user's teams where user is coach
|
|
* @param int $user
|
|
* @return array<array<string, mixed>>
|
|
*/
|
|
public function getAllIsCoach(int $user): array {
|
|
return $this->teams->getAllIsCoach($user);
|
|
}
|
|
|
|
/**
|
|
* Get all tactic shared with a team
|
|
* @param int $team
|
|
* @return array<array<string, mixed>>
|
|
*/
|
|
public function getAllTeamTactic(int $team): array {
|
|
return $this->teams->getAllTeamTactic($team);
|
|
}
|
|
|
|
/**
|
|
* @param int $start
|
|
* @param int $n
|
|
* @return TeamInfo[]
|
|
*/
|
|
public function listAll(int $start, int $n) {
|
|
return $this->teams->listAll($start, $n);
|
|
}
|
|
|
|
public function countTeam(): int {
|
|
return $this->teams->countTeam();
|
|
}
|
|
|
|
/**
|
|
* @param array<Team> $selectedTeams
|
|
* @return void
|
|
*/
|
|
public function deleteTeamSelected(array $selectedTeams) {
|
|
$this->teams->deleteTeamSelected($selectedTeams);
|
|
}
|
|
|
|
}
|