Adding to Twig Question Variable

Adding to Twig Question Variable

by James Bryan -
Number of replies: 4

Hi,

I'm playing around with CodeRunner to learn as to how it works, so I can potentially add to it, and also as a way of learning PHP. One thing I have been unable to find, is where the actual definition for the TWIG QUESTION, STUDENT and TESTCASE classes are, and then where data is actually added to each Twig object, so the data can be retrieved in the question template. An example being "STUDENT_ANSWER". I am unable to find as to where this object or variable is set, and where it actually gets the data from.

I have been able to find, understand and add to the regular question object for the form, however, I have not been able to find how to add to or alter the TWIG objects, so new/different data can be retrieved in the question type template section.

Thank you

Regards

James

In reply to James Bryan

Re: Adding to Twig Question Variable

by Richard Lobb -

The file you want is jobrunner.php, in the classes directory.

Twig objects are built directly from PHP objects, so for example the Twig QUESTION variable contains all the attributes of the PHP question variable.

Richard 

In reply to Richard Lobb

Re: Adding to Twig Question Variable

by James Bryan -

Hi Richard,

Perfect! Thank you for responding to all of my queries. The assistance is greatly appreciated.

Regards

James

In reply to James Bryan

Re: Adding to Twig Question Variable

by James Bryan -

Hi,

I have added a new attribute to the Questionbase object


I have added a new input box to the create/edit question form and I have gotten the inputted data saving in the database. The data correctly loads when a created question is selected to be edited.



The bottom column of mdl_question is the column I have added to store the data I have saved. (As I said, saving and loading is working as intended)


I plan on using the data stored within "insertanswerintotemplate", within the question type template through "{{QUESTION.insertanswerintotemplate))" like other attributes such as "questiontext".

However, within the "run_tests" function in "JobRunner.php", the "$question" object does not contain the 'insertanswerintotemplate' attribute.

I have added a DB call here to add data to the 'insertanswerintotemplate' attribute, however, even with the data being manually assigned to the attribute, I can not reference "{{QUESTION.insertanswerintotemplate))" within the question type template. As a test I assigned the data retrieved from the DB to the questiontext attribute after it had been assigned to "$question->insertanswerintotemplate", and then accessed that ("{{QUESTION.questiontext))") from within the question type template, and the data was displayed, 

Is there a step in the process that I may have missed to enable my new attribute to be accessible through the QUESTION Twig object through the question type template?

Sorry for the wall of information.

If you would like any other information, please ask.


Thank you for your help. It is all greatly appreciated


Kindest regards

James

In reply to James Bryan

Re: Adding to Twig Question Variable

by Richard Lobb -

Firstly, I assume you're just doing all this as an experimental way of understand the existing code, right? Because you certainly shouldn't ever alter the moodle base classes, such as questionbase, when developing plugin code.

There are a few other odd things going on in your code, but I'd rather not get lost in detail. Instead, here are some suggestions on how to debug code within Moodle.

  1. Read https://docs.moodle.org/34/en/Debugging
  2. An important tip inexplicably omitted from the above document is the function call debugging(message) which prints a traceback message to the web output, containing the given message parameter. [But note that debugging needs to be turned on, as explained in the above document.] To see the state of a particular variable at the time, such as the $question variable, use debugging(print_r($question, TRUE)). You can insert debugging calls at multiple points of interest in the code.
  3. I find the use of a debugger like XDEBUG invaluable. I use the NetBeans IDE myself, with XDEBUG turned on. This allows me to set a breakpoint in the code, inspect variables, single step, etc, just as through I was debugging code locally.
Hope that helps.

Richard