Question Authors' Forum

Multilingual questions

 
Picture of Vladimir Ilyin
Multilingual questions
by Vladimir Ilyin - Thursday, 24 August 2017, 9:42 PM
 

Hi!

I want to give my students an opportunity to use different languages for answer.
For example use Java or C++ for an answer  a question.

What is the right way to organize that?

I can use Python with finding keywords in the solution and launching the right compiler
(or to require from my students to write special comments with the name of the language they use).
But may be there is an easy standart way to do that?

Picture of Richard Lobb
Re: Multilingual questions
by Richard Lobb - Friday, 25 August 2017, 11:40 AM
 

It's a good question to raise, Ilyin. I've vaguely wondered in the past if anyone wanted such a capability, though I've never wanted it myself. It's mostly only applicable to "write a program" questions, of course, since otherwise (e.g. "write a function ...") the spec itself becomes language dependent.

There's no "nice" way to do it at present, although the two ways you suggest are both workable. Another possibility: you could inject HTML at the end of the question (using an editor in HTML mode) to display a drop-down Language Select menu. You'd also need a script to insert the menu state into the question answer somehow when the answer is submitted. I haven't thought through how do-able that is. You might have to switch off Ace to make it work, but Ace is a bit redundant if you don't know the language. All options at present are a bit hacky, though.

I'll put this on the list of things to think about in my development burst at the end of this year. As a related aside, I have a student working on a UI plug-in that provides a graph-drawing program for input of the answer rather than an Ace editor panel. It's looking pretty promising. More generally we're exploring  the idea of having question authors specify the UI seen by the student, serialising the state into the answer box. Having a language select drop-down would fit within that model as an easy extension to a simple Ace-only UI.

Richard

Picture of Vladimir Ilyin
Re: Multilingual questions
by Vladimir Ilyin - Saturday, 26 August 2017, 2:24 AM
 

> You'd also need a script to insert the menu state into the question answer somehow when the answer is submitted. 

What  is state?
I see the button "Check" does not send any information about language.
Just the plain text of answer.

Please help me.  How can I add the information about language and then use that on server?

I was going to add another language Multi and then run special task for the choosen language.

May be there is a way to do that easy (to change task's language on the fly).

If no, say a little about adding language. Which files do I need to add/modify?

By the way, I can just modify Python task.  I am not sure about running tasks - and am going to copy
all tasks into one language task and choose commands by if-operator. 
What is the right way to call one task from another, f.e. cpp from python3?



Picture of Richard Lobb
Re: Multilingual questions
by Richard Lobb - Sunday, 27 August 2017, 11:15 PM
 

Are you sure this is worth the effort, Ilyin? Why not just have a separate version of the question for each language you're offering to the student? I agree it's not very elegant but it's way easier than all the alternatives.

However, since you ask ...

By the "state" of the drop-down language menu (an HTML select element) I meant whatever value is currently selected. Getting that state value into the question requires a script within the question text, which would need to (say) insert a line at the start of the student answer, preferably in a language-specific comment, such as

// LANGUAGE = C

or

# LANGUAGE = Python

I managed to get a script like that going in preview mode but getting it working in a general would be much harder and I really don't think it's an idea worth pursuing. Far better to tell students to insert a line like that themselves at the start of their program.

I've added a section to the documentation explaining how to add new languages by scripting in Python. See http://coderunner.org.nz/mod/book/view.php?id=193&chapterid=749

You can extend the idea explained in that section of the documentation to implement a multi-language questions. For example, here's a per-test template to run a submission in either Python or C depending on the comment line at the start of the submitted program:

""" The template for a question type that accept either C or Python answers, requiring
    that the first line of the program is a # or // comment for Python or C respectively.
"""

import subprocess
student_answer = """{{ STUDENT_ANSWER | e('py') }}"""
if student_answer.startswith('#'):
    language = 'python'
    filename = 'prog.py'
elif student_answer.startswith('//'):
    language = 'c'
    filename = 'prog.c'
else:
    raise Exception("Illegal submission. Program must start with a comment.")

# Write the student code to a file

with open(filename, "w") as src:
    print(student_answer, file=src)

# Compile if it's C
if language == 'c':
    cflags = "-std=c99 -Wall -Werror"
    return_code = subprocess.call("gcc {0} -o prog prog.c".format(cflags).split())
    if return_code != 0:
        raise Exception("** Compilation failed. Testing aborted **")
    exec_command = ["./prog"]
else: # Python doesn't need a compile phase
    exec_command = ["python3", "./prog.py"]
    
# Now run the code. Since this is a per-test template,
# stdin is already set up for the stdin text specified in the test case,
# so we can run the compiled program directly.

try:
    output = subprocess.check_output(exec_command, universal_newlines=True)
    print(output)
except subprocess.CalledProcessError as e:
    if e.returncode > 0:
        # Ignore non-zero positive return codes
        if e.output:
            print(e.output)
    else:
        # But negative return codes are signals - abort
        if e.output:
            print(e.output, file=sys.stderr)
        if e.returncode < 0:
            print("Task failed with signal", -e.returncode, file=sys.stderr)
        print("** Further testing aborted **", file=sys.stderr)

Please realise though that that's just a prototype that's had minimal testing. It will undoubtedly need refining. I've never actually used multilingual questions myself.

Richard

Picture of Vladimir Ilyin
Re: Multilingual questions
by Vladimir Ilyin - Monday, 28 August 2017, 7:32 PM
 

> Why not just have a separate version of the question for each language you're offering to the student?

Basically Algorithms are independent of language.
So we can (and we really do) teach algorithms, and let students to Implement them on their favorite language.

Thank you for the solution! It works!

I think it is great idea to add these comments by drop-down list in a question by JavaScript.

Please help me ones more. How can I get ace editor instance via JavaScript?



Picture of Richard Lobb
Re: Multilingual questions
by Richard Lobb - Tuesday, 29 August 2017, 10:10 AM
 

I suggest you turn off Ace by unchecking the "Use ace" checkbox. Ace makes it harder for you to update the student answer and you'd have to change the Ace language when the drop-down list changes. If you really want to do things like that, please look at the CodeRunner source code, particularly amd/src/aceinterface.js, and the Ace API documentation.

I'll consider implementing the drop down menu, with Ace editor language selection, in my next development binge, at the end of this year. I don't wish to spend time on workarounds in the meantime; they're likely to take more effort than doing the job properly!

Richard