Browse Source

Add delete problem

pull/6/head
Menci 7 years ago
parent
commit
bbfcf282ce
  1. 30
      models/problem.js
  2. 19
      modules/problem.js
  3. 24
      views/problem.ejs

30
models/problem.js

@ -638,6 +638,36 @@ class Problem extends Model {
await this.save();
}
async delete() {
let fs = Promise.promisifyAll(require('fs-extra'));
let oldTestdataDir = this.getTestdataPath(), oldTestdataZip = oldTestdataDir + '.zip';
await fs.removeAsync(oldTestdataDir);
await fs.removeAsync(oldTestdataZip);
let JudgeState = syzoj.model('judge_state');
let submissions = await JudgeState.query(null, { problem_id: this.id }), submitCnt = {}, acUsers = new Set();
for (let sm of submissions) {
if (sm.status === 'Accepted') acUsers.add(sm.user_id);
if (!submitCnt[sm.user_id]) {
submitCnt[sm.user_id] = 1;
} else {
submitCnt[sm.user_id]++;
}
}
for (let u in submitCnt) {
let user = await User.fromID(u);
user.submit_num -= submitCnt[u];
if (acUsers.has(parseInt(u))) user.ac_num--;
await user.save();
}
await db.query('DELETE FROM `problem` WHERE `id` = ' + this.id);
await db.query('DELETE FROM `judge_state` WHERE `problem_id` = ' + this.id);
await db.query('DELETE FROM `problem_tag_map` WHERE `problem_id` = ' + this.id);
await db.query('DELETE FROM `article` WHERE `problem_id` = ' + this.id);
}
getModel() { return model; }
}

19
modules/problem.js

@ -652,6 +652,25 @@ app.post('/problem/:id/submit', app.multer.fields([{ name: 'answer', maxCount: 1
}
});
app.post('/problem/:id/delete', async (req, res) => {
try {
let id = parseInt(req.params.id);
let problem = await Problem.fromID(id);
if (!problem) throw new ErrorMessage('无此题目。');
if (!problem.isAllowedManageBy(res.locals.user)) throw new ErrorMessage('您没有权限进行此操作。');
await problem.delete();
res.redirect(syzoj.utils.makeUrl(['problem']));
} catch (e) {
syzoj.log(e);
res.render('error', {
err: e
});
}
});
app.get('/problem/:id/testdata', async (req, res) => {
try {
let id = parseInt(req.params.id);

24
views/problem.ejs

@ -95,7 +95,7 @@ div[class*=ace_br] {
<a class="small ui brown button" href="<%= syzoj.utils.makeUrl(['problem', problem.id, 'discussion']) %>" style="position: relative; ">
讨论
<% if (discussionCount) { %>
<div class="floating ui red tiny circular label"><%= discussionCount %></div>
<div class="floating ui red tiny circular label"><%= discussionCount %></div>
<% } %>
</a>
<a class="small ui yellow button" href="<%= syzoj.utils.makeUrl(['problem', problem.id, 'testdata']) %>">测试数据</a>
@ -116,6 +116,28 @@ div[class*=ace_br] {
<% } else { %>
<a class="small ui button" id="public" href-post="<%= syzoj.utils.makeUrl(['problem', problem.id, 'public']) %>">公开</a>
<% } %>
<div class="ui basic modal" id="modal-delete">
<div class="ui icon header">
<i class="trash icon"></i>
<p style="margin-top: 15px; ">删除题目</p>
</div>
<div class="content" style="text-align: center; ">
<p>确认删除此题目吗?提交记录、讨论以及测试数据将一并删除。<br>
删除题目导致的修改用户提交、通过数量可能会耗费一些时间。</p>
<b>警告:删除比赛中的题目会导致系统错乱!请确认没有比赛使用此题目。</b>
</div>
<div class="actions">
<div class="ui red basic cancel inverted button">
<i class="remove icon"></i>
</div>
<a class="ui green ok inverted button" href-post="<%= syzoj.utils.makeUrl(['problem', problem.id, 'delete']) %>">
<i class="checkmark icon"></i>
</a>
</div>
</div>
<div class="small ui red button" onclick="$('#modal-delete').modal('show')">删除</div>
<% } %>
</div>
<% } %>

Loading…
Cancel
Save