Generating files during a preprocessor run

Generating files during a preprocessor run

por Richard Lobb -
Número de respuestas: 1

A user asked me the following question, which I thought I should share with the community.

I've happily upgraded to moodle 3.11 and coderunner 4...

I've happily seen, that there is now a possibility (as you mentioned) for pre-processor code in C++, Java, Python3, ...

I assume, that these will be compiled and run in the according directory of each student, before displaying the question text and running the template - code. I failed to create files in the pre-processor, that could be read back in the template-code section, which would be very handy for creating (randomized)-files / data, that the student-codes could work on.

Am I assuming something wrong ?

My response was as follows:

Sorry, but you can't generate files from within a preprocessor run. It doesn't behave quite as you expect.

The Jobe server is essentially stateless, handling small single http-requests, which are expected to return results within at most a few tens of seconds. When a student starts a quiz, all questions get instantiated and any randomisation or other forms of question initialisation takes place at that time. The new preprocessing feature allows you to include a single stand-alone Jobe run as part of that initialisation, which allows you to define the set of template parameters for the question. These parameters define text that can be inserted by Twig into the template code for execution when an answer is being tested. In addition, if Twig All is set, the template parameters can be used to insert text into the question text, testcases, global extra etc.

It's not feasible to generate files during that preprocessor run. Because Jobe is stateless (disregarding the fact that it does maintain a file cache for efficiency reasons), these files would need to be passed back to Moodle and stored within the question or question attempt, somehow, for later use. That later use might be days/weeks/months later, when the student finally chooses to submit an answer to the question. That isn't possible (or at least not practicable).

However, if the test files are small, you could return the text of the file(s) as template parameters. For example you might have a template parameter test_file_contents. Then, within the template, do something like the following (assuming Python):

test_file = """{{ test_file_contents | e('py') }}"""
with open('test0.txt', 'w') as outfile:
    outfile.write(test_file)
The tests could then use the file test0.txt as though it were a support file for the question.

Hope that gives you a workable solution.

PS: please note that there was a bug with the preprocessor in 4.0.1 when using it in per-student mode. It broke if a student had a space or an apostrophe in their first or last name (e.g. O'Connell). This is fixed in V4.0.2, now available on github and in the Moodle plugin repository.

En respuesta a Richard Lobb

Re: Generating files during a preprocessor run

por Lars Mehnen -

Ahh :)

Now I perfectly understand the process :)

The hint is brilliant and solves a lot.

With the trick "to connect to an external server" I could even make this process state-full if needed - but slow.

thanx a lot !