public function addTeamMembership($teamid) { $team = new team($teamid); if (!$team->exists()) { return false; } // find out if team allows anyone to join if (!static::getAllowedToJoinTeam($teamid)) { return false; } if ($team->getOpen()) { // anyone can join the team // just set the teamid $this->teamid = $teamid; } else { // team is closed, valid invitation or permission to join required \invitation::deleteOldInvitations(); $invitations = \invitation::getInvitationsForTeam($teamid, $this->origUserId); // delete all invitations of team for user // A user is not meant to join using one invite then leaving and joining again foreach ($invitations as $invitation) { $invitation->delete(); } $this->teamid = $teamid; } $team->setMemberCount($team->getMemberCount() + 1); $team->update(); return true; }
public function showTeam($teamid) { global $tmpl; global $db; $team = new team($teamid); if (!$team->exists()) { $tmpl->setTemplate('NoPerm'); return; } if (!$tmpl->setTemplate('teamSystemProfile')) { $tmpl->noTemplateFound(); die; } // FIXME: implement something to avoid hardcoded paths $tmpl->assign('pmLink', '../PM/?add&teamid=' . $teamid); $tmpl->assign('status', $team->getStatus()); $tmpl->assign('title', 'Team ' . htmlent($team->getName())); // the team's leader $teamLeader = $team->getLeaderId(); $teamData = array(); $teamData['profileLink'] = './?profile=' . $team->getID(); $teamData['name'] = $team->getName(); $teamData['score'] = $team->getScore(); $teamData['scoreClass'] = $this->rankScore($teamData['score']); $teamData['matchSearchLink'] = '../Matches/?search_string=' . $teamData['name'] . '&search_type=team+name' . '&search_result_amount=200' . '&search=Search'; $teamData['matchCount'] = $team->getMatchCount(); $teamData['memberCount'] = $team->getMemberCount(); $teamData['leaderLink'] = '../Players/?profile=' . $team->getLeaderId(); $teamData['leaderName'] = (new \user($team->getLeaderId()))->getName(); $teamData['activityNew'] = $team->getActivityNew(); $teamData['activityOld'] = $team->getActivityOld(); $teamData['created'] = $team->getCreationTimestampStr(); $teamData['wins'] = $team->getMatchCount('won'); $teamData['draws'] = $team->getMatchCount('draw'); $teamData['losses'] = $team->getMatchCount('lost'); $teamData['logo'] = $team->getAvatarURI(); $tmpl->assign('teamDescription', $team->getDescription()); $tmpl->assign('team', $teamData); $tmpl->assign('teamid', $teamid); $tmpl->assign('canPMTeam', \user::getCurrentUserLoggedIn() && \user::getCurrentUserId() > 0 ? true : false); // tell template if user can edit this team $tmpl->assign('canEditTeam', \user::getCurrentUserLoggedIn() && \user::getCurrentUserId() === $teamLeader || \user::getCurrentUser()->getPermission('allow_edit_any_team_profile')); // tell template if user can delete this team // either user has deletion permission for team // or user is leader of team and there are one or less members in team $tmpl->assign('canDeleteTeam', $team->getStatus() !== 'deleted' && (\user::getCurrentUser()->getPermission('team.allowDelete ' . $team->getID()) || \user::getCurrentUser()->getPermission('allow_delete_any_team') || \user::getCurrentUserId() === $team->getLeaderId())); $showMemberActionOptions = false; if (\user::getCurrentUserId() === $teamLeader || \user::getCurrentUser()->getPermission('allow_kick_any_team_members')) { $showMemberActionOptions = true; } $members = array(); $memberids = $team->getUserIds(); foreach ($memberids as $memberid) { $user = new \user($memberid); $member = array(); // rename db result fields and assemble some additional informations // use a temporary array for better readable (but slower) code if (!$showMemberActionOptions && \user::getCurrentUserId() === $memberid) { $showMemberActionOptions = true; } $member['profileLink'] = '../Players/?profile=' . $user->getID(); $member['userName'] = $user->getName(); $member['permissions'] = $teamLeader === $memberid ? 'Leader' : 'Standard'; if ($country = $user->getCountry()) { $member['countryName'] = $country->getName(); if (strlen($country->getFlag()) > 0) { $member['countryFlag'] = $country->getFlag(); } } $member['joined'] = $user->getJoinTimestampStr(); $member['last_login'] = $user->getLastLoginTimestampStr(); // show leave/kick links if permission is given // a team leader can neither leave or be kicked // a leader must first give someone else leadership to leave if ((\user::getCurrentUserId() === $teamLeader || \user::getCurrentUser()->getPermission('allow_kick_any_team_members') || \user::getCurrentUserId() === $user->getID()) && $user->getID() !== $teamLeader) { $member['removeLink'] = './?remove=' . $user->getID() . '&team=' . $teamid; if (\user::getCurrentUserId() === $user->getID()) { $member['removeDescription'] = 'Leave team'; } else { $member['removeDescription'] = 'Kick member from team'; } } // append current member data $members[] = $member; unset($user); } $tmpl->assign('members', $members); $tmpl->assign('showMemberActionOptions', $showMemberActionOptions); // show last entered matches $matches = array(); // show available options if any available $allowEdit = \user::getCurrentUser()->getPermission('allow_edit_match'); $allowDelete = \user::getCurrentUser()->getPermission('allow_delete_match'); $tmpl->assign('showMatchActionOptions', $allowEdit || $allowDelete); $tmpl->assign('allowEdit', $allowEdit); $tmpl->assign('allowDelete', $allowDelete); // get match data // sort the data by id to find out if abusers entered a match at a long time in the past $query = $db->prepare('SELECT `timestamp`,`team1_id`,`team2_id`,' . '(SELECT `name` FROM `teams` WHERE `id`=`team1_id`) AS `team1_name`' . ',(SELECT `name` FROM `teams` WHERE `id`=`team2_id`) AS `team2_name`' . ',`team1_points`,`team2_points`,`userid`' . ',(SELECT `users`.`name` FROM `users`' . ' WHERE `users`.`id`=`matches`.`userid`)' . ' AS `username`' . ',`matches`.`id`' . ' FROM `matches` WHERE `matches`.`team1_id`=?' . ' OR `matches`.`team2_id`=?' . ' ORDER BY `id` DESC LIMIT 0,10'); $db->execute($query, array($teamid, $teamid)); while ($row = $db->fetchRow($query)) { // rename db result fields and assemble some additional informations // use a temporary array for better readable (but slower) code $prepared = array(); $prepared['time'] = $row['timestamp']; $prepared['team1Link'] = '../Teams/?profile=' . $row['team1_id']; $prepared['team2Link'] = '../Teams/?profile=' . $row['team2_id']; $prepared['team1Name'] = $row['team1_name']; $prepared['team2Name'] = $row['team2_name']; $prepared['score1'] = $row['team1_points']; $prepared['score2'] = $row['team2_points']; $prepared['lastModById'] = $row['userid']; $prepared['lastModByName'] = $row['username']; $prepared['lastModByLink'] = '../Players/?profile=' . $prepared['lastModById']; if ($allowEdit) { $prepared['editLink'] = '../Matches/?edit=' . $row['id']; } if ($allowDelete) { $prepared['deleteLink'] = '../Matches/?delete=' . $row['id']; } $matches[] = $prepared; } $tmpl->assign('matches', $matches); // invitation data visible // for team members // for users who can issue any invitation if (\user::getCurrentUser()->getMemberOfTeam($teamid) || \user::getCurrentUser()->getPermission('allow_invite_in_any_team')) { $invitationData = array(); $invitations = invitation::getInvitationsForTeam($teamid); foreach ($invitations as $invitation) { $invitationUser = $invitation->getUsers()[0]; $invitationData[] = array('userName' => $invitationUser->getName(), 'profileLink' => '../Players/?profile=' . $invitationUser->getID(), 'expiration' => $invitation->getExpiration()); } $tmpl->assign('invitations', $invitationData); } }