S2OJ/web/app/models/UOJGroup.php

153 lines
3.7 KiB
PHP
Raw Normal View History

2022-11-11 00:20:33 +00:00
<?php
class UOJGroup {
use UOJDataTrait;
public static function query($id) {
if (!isset($id) || !validateUInt($id)) {
return null;
}
$info = DB::selectFirst([
"select * from `groups`",
"where", ["id" => $id]
]);
if (!$info) {
return null;
}
return new UOJGroup($info);
}
public static function queryGroupsOfUser(array $user = null) {
if ($user == null) {
return [];
}
2022-11-11 23:10:34 +00:00
return array_filter(array_map(fn ($x) => UOJGroup::query($x['group_id']), DB::selectAll([
2022-11-11 00:20:33 +00:00
DB::lc(), "select group_id from groups_users",
"where", ['username' => $user['username']],
"order by group_id"
2022-11-11 23:10:34 +00:00
])), fn ($group) => $group->userCanView($user));
}
public static function userCanCreateGroup($user) {
if (!$user) {
return false;
}
return isSuperUser($user) || UOJUser::checkPermission($user, 'groups.create');
2022-11-11 00:20:33 +00:00
}
public function __construct($info) {
$this->info = $info;
}
public function userCanManage(array $user = null) {
2022-11-11 23:10:34 +00:00
return isSuperUser($user) || UOJUser::checkPermission($user, 'groups.manage');
2022-11-11 00:20:33 +00:00
}
public function userCanView(array $user = null, array $cfg = []) {
$cfg += ['ensure' => false];
2022-11-11 23:10:34 +00:00
2022-11-11 00:20:33 +00:00
if ($this->info['is_hidden'] && !$this->userCanManage($user)) {
$cfg['ensure'] && UOJResponse::page404();
return false;
}
2022-11-11 23:10:34 +00:00
if (!$this->hasUser($user) && !UOJUser::checkPermission($user, 'groups.view')) {
$cfg['ensure'] && UOJResponse::page403();
return false;
}
2022-11-11 00:20:33 +00:00
return true;
}
public function getUri($where = '') {
return "/group/{$this->info['id']}{$where}";
}
public function getLink($cfg = []) {
$cfg += [
'where' => '',
'class' => '',
'text' => $this->info['title'],
];
return HTML::tag('a', [
'href' => $this->getUri($cfg['where']),
'class' => $cfg['class'],
], $cfg['text']);
}
public function getUsernames() {
return array_map(fn ($x) => $x['username'], DB::selectAll([
DB::lc(), "select username from groups_users",
"where", ['group_id' => $this->info['id']],
"order by username",
]));
}
2023-02-13 03:34:43 +00:00
public function getLatestGroupMatesAcceptedSubmissions(array $user = null, int $limit = 10) {
return array_map(function ($x) {
$submission = UOJSubmission::query($x['id']);
$submission->setProblem();
return $submission;
}, DB::selectAll([
2022-11-11 00:20:33 +00:00
"select", DB::fields(["id" => "max(id)"]),
"from submissions",
"where", [
"score" => 100,
["submitter", "in", DB::rawtuple($this->getUsernames())],
UOJSubmission::sqlForUserCanView($user),
],
"group by problem_id",
"order by id desc",
DB::limit($limit),
2022-11-11 00:20:33 +00:00
]));
}
public function getAssignmentIds($limit = -1) {
return array_map(fn ($x) => $x['list_id'], DB::selectAll([
DB::lc(), "select list_id from groups_assignments",
"where", ['group_id' => $this->info['id']],
"order by end_time desc, list_id asc",
$limit == -1 ? "" : DB::limit($limit),
]));
}
public function getActiveAssignmentIds($limit = -1) {
return array_map(fn ($x) => $x['list_id'], DB::selectAll([
DB::lc(), "select list_id from groups_assignments",
"where", [
"group_id" => $this->info['id'],
2023-02-12 11:23:23 +00:00
["end_time", ">=", DB::now()],
2022-11-11 00:20:33 +00:00
],
"order by end_time desc, list_id asc",
$limit == -1 ? "" : DB::limit($limit),
]));
}
public function hasUser(array $user = null) {
if ($user == null) {
return false;
}
return DB::selectFirst([
DB::lc(), "select 1 from groups_users",
"where", [
'group_id' => $this->info['id'],
'username' => $user['username'],
],
]) != null;
}
public function hasAssignment(UOJList $assignment) {
return DB::selectFirst([
DB::lc(), "select 1 from groups_assignments",
"where", [
'group_id' => $this->info['id'],
'list_id' => $assignment->info['id']
]
]) != null;
}
}