Name = 'Team';
$this->Version = '1.0';
$this->Creator = 'Chronos';
$this->License = 'GNU/GPL';
$this->Description = 'Allow users to create teams similar to guilds in the game.';
$this->Dependencies = array('User');
}
function DoStart(): void
{
$this->System->RegisterPage(['team'], 'PageTeam');
Core::Cast($this->System)->RegisterMenuItem(array(
'Title' => T('Teams'),
'Hint' => T('List of translating teams'),
'Link' => $this->System->Link('/team/'),
'Permission' => LICENCE_ANONYMOUS,
'Icon' => '',
), 1);
if (array_key_exists('Search', $this->System->ModuleManager->Modules))
$this->System->ModuleManager->Modules['Search']->RegisterSearch('team',
T('Teams'), array('Name'), '`Team`', $this->System->Link('/team/?search='));
}
}
class PageTeam extends Page
{
function ShowTeamList()
{
$User = ModuleUser::Cast($this->System->GetModule('User'))->User;
$this->Title = T('Teams');
$Output = '
'.T('List of translating teams').'
';
$Output .= 'Týmy jsou seskupení překladatelů, kteří se hlásí k něčemu společnému jako např. WoW serveru, způsobu překladu, ke stejnému hernímu spolku, aj. Být členem týmu samo o sobě nemá žádný zásadní důsledek a spíše to může pomoci se lépe orientovat mezi překladateli někomu, kdo sestavuje export. ';
if (array_key_exists('search', $_GET)) $_SESSION['search'] = $_GET['search'];
else if (!array_key_exists('search', $_SESSION)) $_SESSION['search'] = '';
if (array_key_exists('search', $_GET) and ($_GET['search'] == '')) $_SESSION['search'] = '';
if ($User->Licence(LICENCE_USER))
$Output .= '
';
if ($_SESSION['search'] != '')
{
$SearchQuery = ' AND ((`Name` LIKE "%'.$_SESSION['search'].'%") OR (`Description` LIKE "%'.$_SESSION['search'].'%"))';
$Output .= '
'.
$PageList['Output'];
return $Output;
}
function TeamJoin()
{
$User = ModuleUser::Cast($this->System->GetModule('User'))->User;
if ($User->Licence(LICENCE_USER))
{
if (array_key_exists('id', $_GET))
{
$this->Database->query('UPDATE `User` SET `Team` = '.$_GET['id'].' WHERE `ID` = '.$User->Id);
$Output = ShowMessage(T('You joined new team'));
$this->System->ModuleManager->Modules['Log']->WriteLog('Uživatel vstoupil do týmu '.$_GET['id'], LOG_TYPE_USER);
// Delete all teams without users
$this->Database->query('DELETE FROM `Team` WHERE (SELECT COUNT(*) FROM `User` WHERE `User`.`Team` = `Team`.`Id`) = 0');
// Set new leader for teams where old leader went to other team
$this->Database->query('UPDATE `Team` SET `Leader`=(SELECT `Id` FROM `User` WHERE `User`.`Team`=`Team`.`Id` ORDER BY `User`.`RegistrationTime` LIMIT 1) WHERE `Leader` NOT IN (SELECT `ID` FROM `User` WHERE `User`.`Team`=`Team`.`Id`);');
$Output .= $this->ShowTeamList();
} else $Output = ShowMessage('Nutno zadat id týmu.', MESSAGE_CRITICAL);
} else $Output = ShowMessage(T('Access denied'), MESSAGE_CRITICAL);
return $Output;
}
function TeamCreateFinish()
{
$User = ModuleUser::Cast($this->System->GetModule('User'))->User;
$Output = '';
if ($User->Licence(LICENCE_USER))
{
if (array_key_exists('Name', $_POST) and array_key_exists('Description', $_POST))
{
$DbResult = $this->Database->query('SELECT COUNT(*) FROM `Team` WHERE `Name` = "'.trim($_POST['Name']).'"');
$DbRow = $DbResult->fetch_row();
$Count = $DbRow[0];
if (($Count == 0) and ($_POST['Name'] != ''))
{
$this->Database->query('INSERT INTO `Team` (`Name` ,`Description`, `URL`, `TimeCreate`, `Leader`)'.
' VALUES ("'.trim($_POST['Name']).'", "'.trim($_POST['Description']).'", "'.
$_POST['URL'].'", NOW(), '.$User->Id.')');
$this->Database->query('UPDATE `User` SET `Team` = '.$this->Database->insert_id.' WHERE `ID` = '.$User->Id);
$Output .= ShowMessage('Překladatelský tým vytvořen.');
$this->System->ModuleManager->Modules['Log']->WriteLog('Překladatelský tým vytvořen '.$_POST['Name'], LOG_TYPE_USER);
// Delete all teams without users
$this->Database->query('DELETE FROM `Team` WHERE (SELECT COUNT(*) FROM `User` WHERE `User`.`Team` = `Team`.`Id`) = 0');
} else $Output .= ShowMessage('Již existuje tým se stejným jménem', MESSAGE_CRITICAL);
} else $Output .= ShowMessage('Chybí údaje formuláře', MESSAGE_CRITICAL);
} else $Output .= ShowMessage(T('Access denied'), MESSAGE_CRITICAL);
$Output .= $this->ShowTeamList();
return $Output;
}
function TeamModify()
{
$User = ModuleUser::Cast($this->System->GetModule('User'))->User;
if ($User->Licence(LICENCE_USER))
{
if (array_key_exists('id', $_GET))
{
$DbResult = $this->Database->query('SELECT * FROM `Team` WHERE `Id`='.$_GET['id'].' AND `Leader`='.$User->Id);
if ($DbResult->num_rows > 0)
{
$Team = $DbResult->fetch_assoc();
$Output = '';
} else $Output = ShowMessage('Tým nenalezen.', MESSAGE_CRITICAL);
} else $Output = ShowMessage('Nezadáno id týmu', MESSAGE_CRITICAL);
} else $Output = ShowMessage(T('Access denied'), MESSAGE_CRITICAL);
return $Output;
}
function TeamModifyFinish()
{
$User = ModuleUser::Cast($this->System->GetModule('User'))->User;
$Output = '';
if ($User->Licence(LICENCE_USER))
{
if (array_key_exists('id', $_GET) and array_key_exists('Name', $_POST) and array_key_exists('Description', $_POST) and array_key_exists('URL', $_POST))
{
$DbResult = $this->Database->query('SELECT * FROM `Team` WHERE `Id`='.$_GET['id'].' AND `Leader`='.$User->Id);
if ($DbResult->num_rows > 0)
{
$Team = $DbResult->fetch_assoc();
$DbResult = $this->Database->query('SELECT COUNT(*) FROM `Team` WHERE `Name` = "'.trim($_POST['Name']).'"');
$DbRow = $DbResult->fetch_row();
$Count = $DbRow[0];
if (($Count == 0) and ($_POST['Name'] != ''))
{
$this->Database->query('UPDATE `Team` SET `Name`="'.$_POST['Name'].'", `Description`="'.$_POST['Description'].'", `URL`="'.$_POST['URL'].'" WHERE Id='.$Team['Id']);
$Output .= ShowMessage('Nastavení týmu uloženo.');
$this->System->ModuleManager->Modules['Log']->WriteLog('Překladatelský tým upraven '.$_POST['Name'], LOG_TYPE_USER);
} else $Output .= ShowMessage('Již existuje tým se stejným jménem.', MESSAGE_CRITICAL);
} else $Output .= ShowMessage('Tým nenalezen nebo nemáte oprávnění.', MESSAGE_CRITICAL);
} else $Output .= ShowMessage('Nezadáno id týmu nebo některé položky formuláře.', MESSAGE_CRITICAL);
} else $Output .= ShowMessage(T('Access denied'), MESSAGE_CRITICAL);
$Output .= $this->ShowTeamList();
return $Output;
}
function TeamCreate()
{
$User = ModuleUser::Cast($this->System->GetModule('User'))->User;
if ($User->Licence(LICENCE_USER))
{
$Output ='';
} else $Output = ShowMessage(T('Access denied'), MESSAGE_CRITICAL);
return $Output;
}
function TeamShow()
{
$User = ModuleUser::Cast($this->System->GetModule('User'))->User;
$Output = '';
if (array_key_exists('id', $_GET) and is_numeric($_GET['id']))
{
$DbResult = $this->Database->query('SELECT `Id`, `Name`, `Description`, `URL`, `Leader`, '.
'(SELECT COUNT(*) FROM `User` WHERE '.
'`Team` = `Team`.`Id`) AS `NumberUser`, '.
'(SELECT SUM(`TranslatedCount`) FROM `User` WHERE `Team` = `Team`.`Id`) AS `NumberTranslate`, '.
'(SELECT ROUND(AVG(`XP`)) FROM `User` WHERE `Team` = `Team`.`Id`) AS `AverageXP` FROM '.
'`Team` WHERE `Id`='.($_GET['id'] * 1));
if ($DbResult->num_rows > 0)
{
$Team = $DbResult->fetch_assoc();
$DbResult2 = $this->Database->query('SELECT `Name`, `Id` FROM `User` WHERE `ID`='.$Team['Leader']);
if ($DbResult2->num_rows > 0)
{
$Leader = $DbResult2->fetch_assoc();
} else $Leader = array('Name' => '', 'Id' => 0);
$Output .='
';
$XP = GetLevelMinMax($Team['AverageXP']);
$Output .='';
} else $Output .= ShowMessage('Tým nenalezen', MESSAGE_CRITICAL);
} else $Output .= ShowMessage('Musíte zadat id týmu', MESSAGE_CRITICAL);
return $Output;
}
function TeamLeave()
{
$User = ModuleUser::Cast($this->System->GetModule('User'))->User;
if ($User->Licence(LICENCE_USER))
{
$this->Database->query('UPDATE `User` SET `Team` = NULL WHERE `ID` = '.$User->Id);
$Output = ShowMessage(T('You are now not member of any team'));
$this->System->ModuleManager->Modules['Log']->WriteLog('Uživatel vystoupil z týmu', LOG_TYPE_USER);
// Delete all teams without users
$this->Database->query('DELETE FROM `Team` WHERE (SELECT COUNT(*) FROM `User` WHERE `User`.`Team` = `Team`.`Id`) = 0');
// Set new leader for teams where old leader went to other team
$this->Database->query('UPDATE `Team` SET `Leader`=(SELECT `Id` FROM `User` WHERE `User`.`Team`=`Team`.`Id` ORDER BY `User`.`RegistrationTime` LIMIT 1) WHERE `Leader` NOT IN (SELECT `ID` FROM `User` WHERE `User`.`Team`=`Team`.`Id`);');
$Output .= $this->ShowTeamList();
} else $Output = ShowMessage(T('Access denied'), MESSAGE_CRITICAL);
return $Output;
}
function Show(): string
{
if (array_key_exists('action', $_GET))
{
if ($_GET['action'] == 'gointeam') $Output = $this->TeamJoin();
else if ($_GET['action'] == 'finish_create') $Output = $this->TeamCreateFinish();
else if ($_GET['action'] == 'modify') $Output = $this->TeamModify();
else if ($_GET['action'] == 'finish_modify') $Output = $this->TeamModifyFinish();
else if ($_GET['action'] == 'create') $Output = $this->TeamCreate();
else if ($_GET['action'] == 'team') $Output = $this->TeamShow();
else if ($_GET['action'] == 'leave') $Output = $this->TeamLeave();
else $Output = $this->ShowTeamList();
} else $Output = $this->ShowTeamList();
return $Output;
}
}