From 8703281ab39204cfd16d40a8c8b3a2b8c99ace5d Mon Sep 17 00:00:00 2001
From: Baoshuo
Date: Sun, 20 Mar 2022 08:07:46 +0800
Subject: [PATCH] feat(web,db): add groups
---
db/Dockerfile | 2 +-
db/app_uoj233.sql | 27 ++++
web/app/controllers/contest_members.php | 70 ++++++++++
web/app/controllers/contest_registration.php | 6 +-
web/app/controllers/group.php | 128 +++++++++++++++++++
web/app/controllers/groups.php | 78 +++++++++++
web/app/libs/uoj-query-lib.php | 16 +++
web/app/locale/basic/en.php | 4 +
web/app/locale/basic/zh-cn.php | 4 +
web/app/route.php | 3 +
web/app/views/blog-editor.php | 4 +
web/app/views/main-nav.php | 3 +-
12 files changed, 340 insertions(+), 5 deletions(-)
create mode 100644 web/app/controllers/group.php
create mode 100644 web/app/controllers/groups.php
diff --git a/db/Dockerfile b/db/Dockerfile
index eb4c744..2ca033c 100644
--- a/db/Dockerfile
+++ b/db/Dockerfile
@@ -1,4 +1,4 @@
-FROM mysql:latest
+FROM mysql:5.7
ADD . /opt/uoj_db
WORKDIR /opt/uoj_db
diff --git a/db/app_uoj233.sql b/db/app_uoj233.sql
index 5e53a20..8d75f70 100644
--- a/db/app_uoj233.sql
+++ b/db/app_uoj233.sql
@@ -361,6 +361,33 @@ LOCK TABLES `custom_test_submissions` WRITE;
/*!40000 ALTER TABLE `custom_test_submissions` ENABLE KEYS */;
UNLOCK TABLES;
+--
+-- Table structure for table `groups`
+--
+
+/*!40101 SET @saved_cs_client = @@character_set_client */;
+/*!40101 SET character_set_client = utf8mb4 */;
+CREATE TABLE `groups` (
+ `id` int(11) NOT NULL AUTO_INCREMENT,
+ `title` text NOT NULL,
+ `is_hidden` tinyint(1) NOT NULL DEFAULT 0,
+ PRIMARY KEY (`id`)
+) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Table structure for table `groups_users`
+--
+
+/*!40101 SET @saved_cs_client = @@character_set_client */;
+/*!40101 SET character_set_client = utf8mb4 */;
+CREATE TABLE `groups_users` (
+ `group_id` int(11) NOT NULL,
+ `username` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,
+ PRIMARY KEY (`group_id`, `username`)
+) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
--
-- Table structure for table `hacks`
--
diff --git a/web/app/controllers/contest_members.php b/web/app/controllers/contest_members.php
index eab9785..3daf9bd 100644
--- a/web/app/controllers/contest_members.php
+++ b/web/app/controllers/contest_members.php
@@ -10,6 +10,68 @@
}
genMoreContestInfo($contest);
+ if (isSuperUser($myUser)) {
+ $add_new_contestant_form = new UOJForm('add_new_contestant_form');
+ $add_new_contestant_form->addInput('new_username', 'text', '用户名', '',
+ function ($x) {
+ global $contest;
+
+ if (!validateUsername($x)) return '用户名不合法';
+ $user = queryUser($x);
+ if (!$user) return '用户不存在';
+
+ if (hasRegistered($user, $contest)) {
+ return '该用户已经报名';
+ }
+ return '';
+ },
+ null
+ );
+ $add_new_contestant_form->submit_button_config['align'] = 'compressed';
+ $add_new_contestant_form->submit_button_config['text'] = '注册该用户';
+ $add_new_contestant_form->handle = function() {
+ global $contest;
+ $username = $_POST['new_username'];
+
+ $user = queryUser($username);
+ if (!$user) return;
+
+ DB::query("replace into contests_registrants (username, user_rating, contest_id, has_participated) values ('{$user['username']}', {$user['rating']}, {$contest['id']}, 0)");
+
+ updateContestPlayerNum($contest);
+ };
+ $add_new_contestant_form->runAtServer();
+
+ $add_group_to_contest_form = new UOJForm('add_group_to_contest');
+ $add_group_to_contest_form->addInput('group_id', 'text', '小组 ID', '',
+ function ($x) {
+ global $contest;
+
+ if (!validateUInt($x)) return '小组 ID 不合法';
+ $group = queryGroup($x);
+ if (!$group) return '小组不存在';
+
+ return '';
+ },
+ null
+ );
+ $add_group_to_contest_form->submit_button_config['align'] = 'compressed';
+ $add_group_to_contest_form->submit_button_config['text'] = '注册该小组中的用户';
+ $add_group_to_contest_form->handle = function() {
+ global $contest;
+ $group_id = $_POST['group_id'];
+
+ $users = DB::selectAll("select b.username as username, b.rating as rating from groups_users a inner join user_info b on a.username = b.username where a.group_id = $group_id");
+
+ foreach ($users as $user) {
+ DB::query("replace into contests_registrants (username, user_rating, contest_id, has_participated) values ('{$user['username']}', {$user['rating']}, {$contest['id']}, 0)");
+ }
+
+ updateContestPlayerNum($contest);
+ };
+ $add_group_to_contest_form->runAtServer();
+ }
+
$has_contest_permission = hasContestPermission($myUser, $contest);
$show_ip = $has_contest_permission;
@@ -86,6 +148,14 @@
array('page_len' => 100,
'get_row_index' => '',
'print_after_table' => function() {
+ global $add_new_contestant_form, $add_group_to_contest_form;
+
+ if (isset($add_new_contestant_form)) {
+ $add_new_contestant_form->printHTML();
+ }
+ if (isset($add_group_to_contest_form)) {
+ $add_group_to_contest_form->printHTML();
+ }
}
)
);
diff --git a/web/app/controllers/contest_registration.php b/web/app/controllers/contest_registration.php
index 462e5c1..34b2098 100644
--- a/web/app/controllers/contest_registration.php
+++ b/web/app/controllers/contest_registration.php
@@ -15,7 +15,7 @@
$register_form = new UOJForm('register');
$register_form->handle = function() {
global $myUser, $contest;
- DB::query("insert into contests_registrants (username, contest_id, has_participated) values ('{$myUser['username']}', {$contest['id']}, 0)");
+ DB::query("replace into contests_registrants (username, contest_id, has_participated) values ('{$myUser['username']}', {$contest['id']}, 0)");
updateContestPlayerNum($contest);
};
$register_form->submit_button_config['class_str'] = 'btn btn-primary';
@@ -28,11 +28,11 @@
比赛规则
printHTML(); ?>
diff --git a/web/app/controllers/group.php b/web/app/controllers/group.php
new file mode 100644
index 0000000..48f5817
--- /dev/null
+++ b/web/app/controllers/group.php
@@ -0,0 +1,128 @@
+name = 'group';
+ $group_editor->blog_url = null;
+ $group_editor->cur_data = array(
+ 'title' => $group['title'],
+ 'tags' => array(),
+ 'is_hidden' => $group['is_hidden']
+ );
+ $group_editor->label_text = array_merge($group_editor->label_text, array(
+ 'view blog' => '保存小组信息',
+ 'blog visibility' => '小组可见性'
+ ));
+ $group_editor->show_editor = false;
+ $group_editor->show_tags = false;
+
+ $group_editor->save = function($data) {
+ global $group_id, $group;
+ DB::update("update `groups` set title = '".DB::escape($data['title'])."' where id = {$group_id}");
+
+ if ($data['is_hidden'] != $group['is_hidden'] ) {
+ DB::update("update `groups` set is_hidden = {$data['is_hidden']} where id = {$group_id}");
+ }
+ };
+ $group_editor->runAtServer();
+
+ $add_new_user_form = new UOJForm('add_new_user');
+ $add_new_user_form->addInput('new_username', 'text', '用户名', '',
+ function ($x) {
+ global $group_id;
+
+ if (!validateUsername($x)) return '用户名不合法';
+ $user = queryUser($x);
+ if (!$user) return '用户不存在';
+
+ if (queryUserInGroup($group_id, $x)) {
+ return '该用户已经在小组中';
+ }
+
+ return '';
+ },
+ null
+ );
+ $add_new_user_form->submit_button_config['align'] = 'compressed';
+ $add_new_user_form->submit_button_config['text'] = '添加到小组';
+ $add_new_user_form->handle = function() {
+ global $group_id, $myUser;
+ $username = $_POST['new_username'];
+
+ DB::insert("insert into groups_users (group_id, username) values ({$group_id}, '{$username}')");
+ };
+ $add_new_user_form->runAtServer();
+
+ $delete_user_form = new UOJForm('delete_user');
+ $delete_user_form->addInput('del_username', 'text', '用户名', '',
+ function ($x) {
+ global $group_id;
+
+ if (!validateUsername($x)) return '用户名不合法';
+ if (!queryUserInGroup($group_id, $x)) {
+ return '该用户不在小组中';
+ }
+
+ return '';
+ },
+ null
+ );
+ $delete_user_form->submit_button_config['align'] = 'compressed';
+ $delete_user_form->submit_button_config['text'] = '从小组中删除';
+ $delete_user_form->handle = function() {
+ global $group_id, $myUser;
+ $username = $_POST['del_username'];
+
+ DB::query("delete from groups_users where username='{$username}' and group_id={$group_id}");
+ };
+ $delete_user_form->runAtServer();
+ }
+?>
+
+
+
+= $group['title'] ?>
+
(小组 ID: = $group['id'] ?>)
+ +