WeBWorK Main Forum

Include or import another file

Include or import another file

by Kelly Black -
Number of replies: 2
Is it possible to import or include the code from another pg file? We want to set up a question where we read in the code from one of two files depending on the value of a random number. This will display one of the questions, and different students will see different questions.
In reply to Kelly Black

Re: Include or import another file

by Danny Glin -
What you are looking for is probably either includePGproblem or includeRandomProblem.

includePGproblem allows you to include an entire PG problem from within another pg file. It looks like includeRandomProblem may do what you want already. The usage is as follows:
includeRandomProblem(file1.pg, file2.pg, file3.pg);
This will randomly select one of the two files to display to the student.

If I remember correctly, includeRandomProblem was designed to be used multiple times on the same homework set, so for example, if you used the above syntax as questions 1 and 2 in an assignment, it would make sure that two distinct pg files were chosen. The problem with this is that it depends on the question number in the assignment, so it breaks if the question number is greater than the number of files available to choose from. My workaround when wanting to select only one file from a list is to repeat the list multiple times beyond the maximum number of questions you would ever put on an assignment. e.g.
includeRandomProblem(file1.pg, file2.pg, file3.pg, file1.pg, file2.pg, file3.pg, file1.pg, file2.pg, file3.pg, file1.pg, file2.pg, file3.pg, file1.pg, file2.pg, file3.pg, file1.pg, file2.pg, file3.pg, file1.pg, file2.pg, file3.pg);

If you only need these for gateway quizzes, you can use the slightly more robust and better documented Problem Groups feature. Unfortunately this is only available in quiz mode.