Browse Source

Add sorting for problems.

master
t123yh 7 years ago
parent
commit
4ed96886fc
  1. 53
      modules/problem.js
  2. 10
      views/problems.ejs

53
modules/problem.js

@ -27,11 +27,22 @@ let Contest = syzoj.model('contest');
let ProblemTag = syzoj.model('problem_tag'); let ProblemTag = syzoj.model('problem_tag');
let ProblemTagMap = syzoj.model('problem_tag_map'); let ProblemTagMap = syzoj.model('problem_tag_map');
let Article = syzoj.model('article'); let Article = syzoj.model('article');
const Sequelize = require('sequelize');
let Judger = syzoj.lib('judger'); let Judger = syzoj.lib('judger');
app.get('/problems', async (req, res) => { app.get('/problems', async (req, res) => {
try { try {
const sort = req.query.sort || syzoj.config.sorting.problem.field;
const order = req.query.order || syzoj.config.sorting.problem.order;
if (!['id', 'title', 'rating', 'ac_num', 'submit_num', 'ac_rate'].includes(sort) || !['asc', 'desc'].includes(order)) {
throw new ErrorMessage('错误的排序参数。');
}
let sortVal = sort;
if (sort === 'ac_rate') {
sortVal = { raw: 'ac_num / submit_num' };
}
let where = {}; let where = {};
if (!res.locals.user || !await res.locals.user.hasPrivilege('manage_problem')) { if (!res.locals.user || !await res.locals.user.hasPrivilege('manage_problem')) {
if (res.locals.user) { if (res.locals.user) {
@ -49,7 +60,7 @@ app.get('/problems', async (req, res) => {
} }
let paginate = syzoj.utils.paginate(await Problem.count(where), req.query.page, syzoj.config.page.problem); let paginate = syzoj.utils.paginate(await Problem.count(where), req.query.page, syzoj.config.page.problem);
let problems = await Problem.query(paginate, where); let problems = await Problem.query(paginate, where, [[sortVal, order]]);
await problems.forEachAsync(async problem => { await problems.forEachAsync(async problem => {
problem.allowedEdit = await problem.isAllowedEditBy(res.locals.user); problem.allowedEdit = await problem.isAllowedEditBy(res.locals.user);
@ -60,7 +71,9 @@ app.get('/problems', async (req, res) => {
res.render('problems', { res.render('problems', {
allowedManageTag: res.locals.user && await res.locals.user.hasPrivilege('manage_problem_tag'), allowedManageTag: res.locals.user && await res.locals.user.hasPrivilege('manage_problem_tag'),
problems: problems, problems: problems,
paginate: paginate paginate: paginate,
curSort: sort,
curOrder: order === 'asc'
}); });
} catch (e) { } catch (e) {
syzoj.log(e); syzoj.log(e);
@ -533,30 +546,30 @@ app.post('/problem/:id/manage', app.multer.fields([{ name: 'testdata', maxCount:
// Set problem public // Set problem public
async function setPublic(req, res, is_public) { async function setPublic(req, res, is_public) {
try { try {
let id = parseInt(req.params.id); let id = parseInt(req.params.id);
let problem = await Problem.fromID(id); let problem = await Problem.fromID(id);
if (!problem) throw new ErrorMessage('无此题目。'); if (!problem) throw new ErrorMessage('无此题目。');
let allowedManage = await problem.isAllowedManageBy(res.locals.user); let allowedManage = await problem.isAllowedManageBy(res.locals.user);
if (!allowedManage) throw new ErrorMessage('您没有权限进行此操作。'); if (!allowedManage) throw new ErrorMessage('您没有权限进行此操作。');
problem.is_public = is_public; problem.is_public = is_public;
problem.publicizer_id = res.locals.user.id; problem.publicizer_id = res.locals.user.id;
await problem.save(); await problem.save();
res.redirect(syzoj.utils.makeUrl(['problem', id])); res.redirect(syzoj.utils.makeUrl(['problem', id]));
} catch (e) { } catch (e) {
syzoj.log(e); syzoj.log(e);
res.render('error', { res.render('error', {
err: e err: e
}); });
}
} }
}
app.post('/problem/:id/public', async (req, res) => { app.post('/problem/:id/public', async (req, res) => {
await setPublic(req, res, true); await setPublic(req, res, true);
}); });
app.post('/problem/:id/dis_public', async (req, res) => { app.post('/problem/:id/dis_public', async (req, res) => {
await setPublic(req, res, false); await setPublic(req, res, false);

10
views/problems.ejs

@ -91,11 +91,11 @@ if (typeof tags !== 'undefined') tagIDs = tags.map(x => x.id);
<% if (user) { %> <% if (user) { %>
<th class="one wide">提交状态</th> <th class="one wide">提交状态</th>
<% } %> <% } %>
<th class="one wide">编号</th> <th class="one wide"><%- createSortableTitle('id', '编号', true) %></th>
<th class="left aligned">题目名称</th> <th class="left aligned"><%- createSortableTitle('title', '题目名称', true) %></th>
<th class="one wide">通过</th> <th class="one wide"><%- createSortableTitle('ac_num', '通过', false) %></th>
<th class="one wide">提交</th> <th class="one wide"><%- createSortableTitle('submit_num', '提交', false) %></th>
<th class="one wide">通过率</th> <th class="one wide"><%- createSortableTitle('ac_rate', '通过率', false) %></th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>

Loading…
Cancel
Save