Skip to content

feat: Colorize students comments #218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lms/lmsdb/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,7 @@ def _by_file(cls, file_id: int):
.switch()
.join(User)
.where(cls.file == file_id)
.order_by(cls.timestamp.asc())
)

@classmethod
Expand Down
17 changes: 12 additions & 5 deletions lms/static/comments.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const DEFAULT_COMMENTED_LINE_COLOR = '#fab3b0';
const STUDENT_COMMENTED_LINE_COLOR = '#a9f6f9';
const FLAKE_COMMENTED_LINE_COLOR = '#fac4c3';
const HOVER_LINE_STYLE = '1px solid #0d0d0f';

function markLine(target, color) {
if (target.dataset && target.dataset.marked === 'true') { return; }
if (target.dataset && target.dataset.vimbackground === 'true') { return; }
function markLine(target, color, deletion = false) {
if (target.dataset && target.dataset.marked === 'true' && !deletion) { return; }
if (target.dataset && target.dataset.vimbackground === 'true' && !deletion) { return; }
target.style.background = color;
}

Expand Down Expand Up @@ -45,7 +46,7 @@ function formatCommentData(commentData) {
function addCommentToLine(line, commentData) {
const commentElement = document.querySelector(`.line[data-line="${line}"]`);
const formattedComment = formatCommentData(commentData);
const commentText = `<span class="comment" data-line="${line}" data-commentid="${commentData.id}">${formattedComment}</span>`;
const commentText = `<span class="comment" data-line="${line}" data-commentid="${commentData.id}" data-author-role="${commentData.author_role}">${formattedComment}</span>`;
let existingPopover = bootstrap.Popover.getInstance(commentElement);
if (existingPopover !== null) {
const existingContent = `${existingPopover.config.content} <hr>`;
Expand All @@ -65,13 +66,18 @@ function addCommentToLine(line, commentData) {
if (commentData.is_auto) {
markLine(commentElement, FLAKE_COMMENTED_LINE_COLOR);
} else {
markLine(commentElement, DEFAULT_COMMENTED_LINE_COLOR);
const lineColor = window.getLineColorByRole(commentData.author_role);
markLine(commentElement, lineColor, true);
commentElement.dataset.marked = true;
}

return existingPopover;
}

function getLineColorByRole(authorRole) {
return authorRole === 1 ? STUDENT_COMMENTED_LINE_COLOR : DEFAULT_COMMENTED_LINE_COLOR;
}

function treatComments(comments) {
if (comments === undefined) {
console.error('Probably bad xhr request');
Expand Down Expand Up @@ -143,6 +149,7 @@ window.markLink = markLine;
window.hoverLine = hoverLine;
window.addCommentToLine = addCommentToLine;
window.isUserGrader = isUserGrader;
window.getLineColorByRole = getLineColorByRole;
window.addEventListener('load', () => {
const codeElement = document.getElementById('code-view').dataset;
window.solutionId = codeElement.id;
Expand Down
6 changes: 5 additions & 1 deletion lms/static/grader.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,12 @@ function visuallyRemoveComment(commentId) {
removeContent = `${commentElement.outerHTML} <hr>`;
}
existingPopover.config.content = existingPopover.config.content.replace(removeContent, '');
const commentParent = commentElement.parentNode;
hr.parentNode.removeChild(hr);
commentElement.parentNode.removeChild(commentElement);
commentParent.removeChild(commentElement);
const lastAuthorRole = commentParent.lastChild.previousElementSibling.dataset.authorRole;
const newLineColor = window.getLineColorByRole(lastAuthorRole);
window.markLine(lineElement, newLineColor, true);
}
}

Expand Down