How to add a per-user field to courses in code?

How to add a per-user field to courses in code?

by Anne Mohr -
Number of replies: 3

I'm trying to add a per-user field to courses. It needs to be available in all courses that will ever be created.

As I'm totally new to moodle, I don't really know where to start. What I thought of so far is adding a field to completions as those are already on a per-user basis which would be a good start.

  • Is this a valid approach at all or is it better to create a new table?
  • Is it possible to simply add a field to a core table from within a plugin? E. g. by defining it in install.xml or generate it in upgrade.php? Would that interfere with backup and restore procedures so I had to handle those cases as well?

Can anyone point me in the right direction?

Thank you in advance,
Anne

Average of ratings: -
In reply to Anne Mohr

Re: How to add a per-user field to courses in code?

by Davo Smith -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
First of all, it is not a good idea to add new fields to core tables - there are all sorts of ways that can go wrong (e.g. core code later introduces a field there with the same name, but uses it for a different purpose; certain parts of the code load records from the database and throw an error if the fields don't match the ones they were expecting).

You _probably_ want to add a new table, but it's really hard to tell, as you've started with your proposed solution, rather than explaining the problem that you're trying to solve ...
In reply to Anne Mohr

Re: How to add a per-user field to courses in code?

by Michael Hughes -
Picture of Core developers Picture of Plugin developers
You don't necessarily have to add the field via code...as Davo said you've not really outlined the problem you want to solve with this field.

So...It doesn't look like it's been updated for a while, but you *could* look at https://moodle.org/plugins/local_metadata

Caveat: there doesn't appear to be an explicit Moodle 4 release for this, but it *may* continue to work without modification....

(Also it's a good example of one way of extending this sort of data, that is also along the lines of the customfield plugin type)