add custom fields in moodle database

add custom fields in moodle database

by shailendra j -
Number of replies: 4

Hi,

Can we add new database fields in moodle database tables? For example lets assume i want to add two new fields in course tables in database. Can we do this?

Average of ratings: -
In reply to shailendra j

Re: add custom fields in moodle database

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Yes you can add extra fields - the easiest way is to create a local plugin and then, withitn the db/install.php, add some code to insert the new fields: http://docs.moodle.org/dev/Data_definition_API

On the other hand, I would be very cautious about actually doing so - adding the fields on their own is not likely to be very useful and could cause problems during future upgrades. Without making changes to the core Moodle code (which makes upgrades difficult), nothing inside Moodle is going to make use of the new fields you have added.

If you are intending to write a plugin that uses these new fields, then you are far better off creating a new table to store the extra data and link it via the courseid. That way you don't risk any future problems caused by having non-standard fields in the course table.

Average of ratings: Useful (1)
In reply to Davo Smith

Re: add custom fields in moodle database

by Carlos Bacelar -

Hi, Davo!

I need to add the fields 'start_date' and 'end_date' to course table. So, to follow your instruction, i need create two tables: the first i put the custom fields, the second i put the data, referencing the field_id.. this is it?

thanks.

In reply to Carlos Bacelar

Re: add custom fields in moodle database

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

If the course needed a start and end date, I'd first consider whether this information should be part of the enrolment plugin, rather than adding it directly to the course itself.

If you decide it definitiely needs to be associated with a course, then create a new table, with the fields: id, courseid, startdate, enddate

Wherever your plugin references the course start/end date you can join the course table to that table to find + display the information.

In reply to Davo Smith

Re: add custom fields in moodle database

by Steven Mao -

Hi Davo,

I'm fairly new to Moodle so please forgive my ignorance if my question sounds stupid:

What are the pros and cons if we were to add the two additional fields directly into the table? E.g.

ALTER TABLE mdl_course ADD start_time bigint(10);

ALTER TABLE mdl_course ADD end_time bigint(10);

 

Thanks!

Steve