Browse Source

Add discussion for problem

pull/6/head
Menci 7 years ago
parent
commit
766ae45e7e
  1. 2
      models/article.js
  2. 56
      modules/discussion.js
  3. 6
      modules/problem.js
  4. 11
      views/article.ejs
  5. 2
      views/article_edit.ejs
  6. 36
      views/discussion.ejs
  7. 6
      views/problem.ejs

2
models/article.js

@ -31,6 +31,7 @@ let model = db.define('article', {
content: { type: Sequelize.TEXT },
user_id: { type: Sequelize.INTEGER },
problem_id: { type: Sequelize.INTEGER },
public_time: { type: Sequelize.INTEGER },
update_time: { type: Sequelize.INTEGER },
@ -61,6 +62,7 @@ class Article extends Model {
content: '',
user_id: 0,
problem_id: 0,
public_time: 0,
update_time: 0,

56
modules/discussion.js

@ -19,20 +19,51 @@
'use strict';
let Problem = syzoj.model('problem');
let Article = syzoj.model('article');
let ArticleComment = syzoj.model('article-comment');
let User = syzoj.model('user');
app.get('/discussion', async (req, res) => {
try {
let paginate = syzoj.utils.paginate(await Article.count(), req.query.page, syzoj.config.page.discussion);
let articles = await Article.query(paginate, null, [['public_time', 'desc']]);
let where = { problem_id: null };
let paginate = syzoj.utils.paginate(await Article.count(where), req.query.page, syzoj.config.page.discussion);
let articles = await Article.query(paginate, where, [['public_time', 'desc']]);
for (let article of articles) await article.loadRelationships();
res.render('discussion', {
articles: articles,
paginate: paginate
paginate: paginate,
problem: null
});
} catch (e) {
syzoj.log(e);
res.render('error', {
err: e
});
}
});
app.get('/problem/:pid/discussion', async (req, res) => {
try {
let pid = parseInt(req.params.pid);
let problem = await Problem.fromID(pid);
if (!problem) throw new ErrorMessage('无此题目。');
if (!await problem.isAllowedUseBy(res.locals.user)) {
throw new ErrorMessage('您没有权限进行此操作。');
}
let where = { problem_id: pid };
let paginate = syzoj.utils.paginate(await Article.count(where), req.query.page, syzoj.config.page.discussion);
let articles = await Article.query(paginate, where, [['public_time', 'desc']]);
for (let article of articles) await article.loadRelationships();
res.render('discussion', {
articles: articles,
paginate: paginate,
problem: problem
});
} catch (e) {
syzoj.log(e);
@ -65,10 +96,19 @@ app.get('/article/:id', async (req, res) => {
await comment.loadRelationships();
}
let problem = null;
if (article.problem_id) {
problem = await Problem.fromID(article.problem_id);
if (!await problem.isAllowedUseBy(res.locals.user)) {
throw new ErrorMessage('您没有权限进行此操作。');
}
}
res.render('article', {
article: article,
comments: comments,
paginate: paginate
paginate: paginate,
problem: problem
});
} catch (e) {
syzoj.log(e);
@ -116,6 +156,14 @@ app.post('/article/:id/edit', async (req, res) => {
article = await Article.create();
article.user_id = res.locals.user.id;
article.public_time = article.sort_time = time;
if (req.query.problem_id) {
let problem = await Problem.fromID(req.query.problem_id);
if (!problem) throw new ErrorMessage('无此题目。');
article.problem_id = problem.id;
} else {
article.problem_id = null;
}
} else {
if (!await article.isAllowedEditBy(res.locals.user)) throw new ErrorMessage('您没有权限进行此操作。');
}

6
modules/problem.js

@ -25,6 +25,7 @@ let WaitingJudge = syzoj.model('waiting_judge');
let Contest = syzoj.model('contest');
let ProblemTag = syzoj.model('problem_tag');
let ProblemTagMap = syzoj.model('problem_tag_map');
let Article = syzoj.model('article');
app.get('/problems', async (req, res) => {
try {
@ -204,11 +205,14 @@ app.get('/problem/:id', async (req, res) => {
let testcases = await syzoj.utils.parseTestdata(problem.getTestdataPath(), problem.type === 'submit-answer');
let discussionCount = await Article.count({ problem_id: id });
res.render('problem', {
problem: problem,
state: state,
lastLanguage: res.locals.user ? await res.locals.user.getLastSubmitLanguage() : null,
testcases: testcases
testcases: testcases,
discussionCount: discussionCount
});
} catch (e) {
syzoj.log(e);

11
views/article.ejs

@ -6,6 +6,17 @@
}
</style>
<div class="padding">
<div class="ui breadcrumb">
<div class="section">讨论</div>
<i class="right angle icon divider"></i>
<% if (problem) { %>
<div class="section">题目</div>
<i class="right angle icon divider"></i>
<a href="<%= syzoj.utils.makeUrl(['problem', problem.id, 'discussion']) %>" class="active section"><%= problem.title %></a>
<% } else { %>
<a href="<%= syzoj.utils.makeUrl(['discussion']) %>" class="section">全局板块</a>
<% } %>
</div>
<h1><%= article.title %></h1>
<p style="font_size: 0.7em"><img style="vertical-align: middle;" src="<%= syzoj.utils.gravatar(article.user.email, 64) %>" width="32" height="32">
<a href="<%= syzoj.utils.makeUrl(['user', article.user_id]) %>"><%= article.user.username %></a><% if (article.user.nameplate) { %><%- article.user.nameplate %><% } %> 于 <%= syzoj.utils.formatDate(article.public_time) %> 发表,<%= syzoj.utils.formatDate(article.update_time) %> 最后更新

2
views/article_edit.ejs

@ -6,7 +6,7 @@
}
</style>
<div class="padding">
<form class="ui form" method="post" action="<%= syzoj.utils.makeUrl(['article', article.id, 'edit']) %>">
<form class="ui form" method="post">
<div class="ui top attached tabular menu">
<a class="item active" data-tab="edit">编辑</a>
<a class="item" data-tab="preview" id="preview_tab">预览</a>

36
views/discussion.ejs

@ -3,23 +3,45 @@
<div class="padding">
<div class="ui grid">
<div class="row">
<div class="column">
<a href="<%= syzoj.utils.makeUrl(['article', 0, 'edit']) %>" class="ui mini right floated button">发帖</a>
<div class="ten wide column">
<div class="ui breadcrumb">
<div class="section">讨论</div>
<i class="right angle icon divider"></i>
<% if (problem) { %>
<div class="section">题目</div>
<i class="right angle icon divider"></i>
<div class="active section"><%= problem.title %></div>
<% } else { %>
<div class="section">全局板块</div>
<% } %>
</div>
</div>
<div class="six wide right aligned column">
<% if (problem) { %>
<a style="margin-left: 10px; " href="<%= syzoj.utils.makeUrl(['problem', problem.id]) %>" class="ui labeled icon mini blue button">
<i class="arrow left icon"></i>
返回题目
</a>
<% } %>
<a style="margin-left: 10px; " href="<%= syzoj.utils.makeUrl(['article', 0, 'edit'], problem ? { problem_id: problem.id } : null) %>" class="ui labeled icon mini button">
<i class="write icon"></i>
发帖
</a>
</div>
</div>
</div>
<table class="ui very basic table">
<table class="ui very basic center aligned table">
<thead>
<tr>
<th>标题</th>
<th>作者</th>
<th>发表时间</th>
<th class="left aligned" style="width: 65%; ">标题</th>
<th style="width: 15%; ">作者</th>
<th style="width: 20%; ">发表时间</th>
</tr>
</thead>
<tbody>
<% for (let article of articles) { %>
<tr>
<td><a href="<%= syzoj.utils.makeUrl(['article', article.id]) %>"><%= article.title %></a></td>
<td class="left aligned"><a href="<%= syzoj.utils.makeUrl(['article', article.id]) %>"><%= article.title %></a></td>
<td><a href="<%= syzoj.utils.makeUrl(['user', article.user_id]) %>"><%= article.user.username %></a><% if (article.user.nameplate) { %><%- article.user.nameplate %><% } %></td>
<td><%= syzoj.utils.formatDate(article.public_time) %></td>
</tr>

6
views/problem.ejs

@ -92,6 +92,12 @@ div[class*=ace_br] {
<% } %>
<a class="small ui positive button" href="<%= syzoj.utils.makeUrl(['submissions'], { problem_id: problem.id }) %>">提交记录</a>
<a class="small ui orange button" href="<%= syzoj.utils.makeUrl(['problem', problem.id, 'statistics', problem.type === 'submit-answer' ? 'min' : 'fastest']) %>">统计</a>
<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>
<% } %>
</a>
<a class="small ui yellow button" href="<%= syzoj.utils.makeUrl(['problem', problem.id, 'testdata']) %>">测试数据</a>
<% if (problem.additional_file) { %>
<a class="small ui teal button" href="<%= syzoj.utils.makeUrl(['problem', problem.id, 'download', 'additional_file']) %>">附加文件</a>

Loading…
Cancel
Save