Skip to content
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

[Frontend] Adding line offset for code problems #976

Merged
merged 12 commits into from
Dec 15, 2023
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
12 changes: 12 additions & 0 deletions inginious/common/tasks_problems.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,18 @@ def input_is_consistent(self, task_input, default_allowed_extension, default_max

@classmethod
def parse_problem(self, problem_content):
# Checking problem edit inputs
if len(problem_content["offset"]) == 0:
del problem_content["offset"]
else:
try:
offset = int(problem_content["offset"])
if offset < 1:
raise Exception("Line offset must be positive!")
problem_content["offset"] = offset
except ValueError:
raise Exception("Line offset must be an integer!")

return Problem.parse_problem(problem_content)
AlexandreDoneux marked this conversation as resolved.
Show resolved Hide resolved

@classmethod
Expand Down
2 changes: 2 additions & 0 deletions inginious/frontend/pages/course_admin/task_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ def POST_AUTH(self, courseid, taskid): # pylint: disable=arguments-differ

# Network grading
data["network_grading"] = "network_grading" in data

AlexandreDoneux marked this conversation as resolved.
Show resolved Hide resolved

except Exception as message:
return json.dumps({"status": "error", "message": _("Your browser returned an invalid form ({})").format(message)})

Expand Down
7 changes: 5 additions & 2 deletions inginious/frontend/static/js/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function init_common()
colorizeStaticCode();
$('.code-editor').each(function(index, elem)
{
registerCodeEditor(elem, $(elem).attr('data-x-language'), $(elem).attr('data-x-lines'));
registerCodeEditor(elem, $(elem).attr('data-x-language'), $(elem).attr('data-x-lines'), $(elem).attr('data-x-first-line'));
AlexandreDoneux marked this conversation as resolved.
Show resolved Hide resolved
});

//Fix a bug with codemirror and bootstrap tabs
Expand Down Expand Up @@ -69,16 +69,19 @@ function colorizeStaticCode()
}

//Register and init a code editor (ace)
function registerCodeEditor(textarea, lang, lines)
function registerCodeEditor(textarea, lang, lines, firstline=1)
{
var mode = CodeMirror.findModeByName(lang);
if(mode == undefined)
mode = {"mode": "plain", "mime": "text/plain"};

var is_single = $(textarea).hasClass('single');



var editor = CodeMirror.fromTextArea(textarea, {
lineNumbers: true,
firstLineNumber: firstline,
mode: mode["mime"],
foldGutter: true,
styleActiveLine: true,
Expand Down
2 changes: 2 additions & 0 deletions inginious/frontend/static/js/studio.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,8 @@ function studio_init_template_code(well, pid, problem)
var default_editor = registerCodeEditor(default_tag, 'text', default_tag.tagName === "INPUT" ? 1 : 10);
if("default" in problem)
default_editor.setValue(problem["default"]);
if("offset" in problem)
$('#offset-' + pid, well).val(problem["offset"]);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion inginious/frontend/task_problems.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class DisplayableCodeProblem(CodeProblem, DisplayableProblem):

def __init__(self, problemid, content, translations, taskfs):
super(DisplayableCodeProblem, self).__init__(problemid, content, translations, taskfs)
self._first_line = content.get("offset", 1)

@classmethod
def get_type_name(cls, language):
Expand All @@ -82,7 +83,7 @@ def show_input(self, template_helper, language, seed):
header = ParsableText(self.gettext(language,self._header), "rst",
translation=self.get_translation_obj(language))
return template_helper.render("tasks/code.html", inputId=self.get_id(), header=header,
lines=8, maxChars=0, language=self._language, optional=self._optional,
lines=8, first_line=self._first_line, maxChars=0, language=self._language, optional=self._optional,
default=self._default)

@classmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,12 @@
{% endif %}
</div>
</div>

{% if multiline %}
<div class="form-group row">
<label for="offset-PID" class="col-sm-2 control-label">{{_("Code line offset")}}</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="offset-PID" name="problem[PID][offset]" placeholder="{{ _('Line offset for the code block. Must be a number.') }}"/>
</div>
</div>
{% endif %}
1 change: 1 addition & 0 deletions inginious/frontend/templates/tasks/code.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
class="code-editor form-control {% if '/' in inputId %} single {% endif %}"
data-x-language="{{language}}"
data-x-lines="{{lines}}"
data-x-first-line="{{first_line}}"
data-optional="{% if optional %}{{True}}{% else %}{{False}}{% endif %}">{{ default }}</textarea>
Loading