Need to add information to 'other' field in standard log table for certain events

Need to add information to 'other' field in standard log table for certain events

by Teresa Hardy -
Number of replies: 1

Hello -

My goal is to expand event logging information a bit with just enough human readable context that an external version of the standard log table can be used to create simple human-readable reports on the external site without the complication of exporting extra tables. To do that all I need to add, for example, is a course name to the 'other' field array when someone triggers the course_viewed event.  It seemed to me that the init() portion of the course_viewed subclass would have been the logical place to put this since init() is called by base during the creation function specifically to place initial data (in this case, crud and level) that is unique into the calling event's data list.  However, nothing I've tried so far works. 

Right now I'm editing the course_viewed.php file directly, just to see if I can get it to work, with plans to move it to a plugin once it functions.  This is the gist of what I've been trying to do:

    protected function init() {
        $this->data['crud'] = 'r';
        $this->data['edulevel'] = self::LEVEL_PARTICIPATING;
        $this->data['other'] = 'test course name';
    }

I've also tried creating an "other" array and assigning 'coursename' => "test course name" and then assigning that to $this->data['other'], and many other variations on that theme.  Nada.  I still get a null "other" entry in the standard log table for that event when it gets passed to base for event creation.  (Base doesn't even seem to see the other entry at all, so it gets set to null...)

So, I've not hit on the right format, or maybe I am barking up the wrong tree on the correct place/time to add this to an event.  Ideas?  Pointers? 

thank you to anyone who can give me a push here!


Average of ratings: -
In reply to Teresa Hardy

Re: Need to add information to 'other' field in standard log table for certain events

by Adam Olley -
Picture of Core developers Picture of Plugin developers

Hi Teresa,

When the event is created, the init() func is called, and shortly after it checks if the $data array that was passed in contains a 'other' field, and if it doesn't, sets it to null. What this means is that you can't add data the "other" field during the init() call.

If you're interested, take a look at lib/classes/event/base.php in the "create" function to see how the event is intialised.

Normally 'other' data should be passed in where the event is triggered from, but if you really want to, you could probably add the info to the data->other array from inside validate_data function in the event(s).

validate_data() is the last thing called before returning the event ready to be triggered - so the "other" data won't get blanked out after that.


I hope the above makes sense and is helpful smile