Hi guys,
I am using moodle 2.7. I want to add some extra fields in a user registration form i.e. gradutaion %. So how to create a custom fileld in user registration form & how to create table & connect with table of that db.
Thanks,
Nishant Pandya
go to system administration->users->user profile fields-> create new profile fields.
then you can have your custom fields that you wanted.
thats right,
and there you can set the custom field to be required on registration
Thanks for reply ericyullu & Torsten Händler
No, I don't want to use optional field. Actually, I want to add some custom field on different different form. That's why I am asking that how to add custom field. That's my query. Please help.
Thanks,
Nishant
Ah ok,
I think it's possible but you have to change a lot of core-code and edit the database table and I think this is to much and you have to do it again after an update. It's not to recommend.
Hey thanks Torsten Händler.
I am ready to change the core code. Please suggest.
Hey, Sry but I can't told you the details because I've never done is before, but I know that it is possible if you've enough knownledge in development.
Why wouldn't you go the easy way with custom profile fields in moodle? where is the problem with these solution?
greetings
Hi Torsten,
I can use optional profile field to create field in the user registration page. But what about other forms. I need to add some other extra field on different different form. Optional field is applicable only on the user registration page.
Hi Nishant,
Ok thats a bigger problem. The other forms are from Moodle or from extra plugins? If these are from modules you have to change the mod_form and the lib of the extra module. also the install.xml in the "db" folder of the module, also you have to update the version.php of the module and install the new version so that the database will be updated.
Hi Torsten,
It's a block. I want to add that field in a form of a block. I have check the db folder of that block, only access.php, uninstall.php & upgrade.php files are there not install.xml.
I know how to add field in any form. But in the block no install.xml file is there. So how can I create a field in db & how to make connection with db.
Hi Nishant,
There is actually a step-by-step guide to making blocks that might help you out. You can find it here: https://docs.moodle.org/dev/Blocks
Hi guys !!
I have achieved by following below ways. Please vote my answer so it's better for other :-
- Open your_module/version.php
- Update the version & copy that updated version & paste on upgrade.php
- Open your_module/upgrade.php
- Paste the below code before return true. {must right return true; if anything is written in place of true then replace that text/variable by “true”}
if ($oldversion < 2014041208) {
$table = new xmldb_table('tablename');
$field = new xmldb_field('companycontactname');
$field->set_attributes(XMLDB_TYPE_CHAR, '100', XMLDB_UNSIGNED, XMLDB_NOTNULL, null);
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
$field2 = new xmldb_field('companyemail');
$field2->set_attributes(XMLDB_TYPE_CHAR, '100', XMLDB_UNSIGNED, XMLDB_NOTNULL, null);
if (!$dbman->field_exists($table, $field2)) {
$dbman->add_field($table, $field2);
}
$field3 = new xmldb_field('companyphoneno');
$field3->set_attributes(XMLDB_TYPE_CHAR, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null);
if (!$dbman->field_exists($table, $field3)) {
$dbman->add_field($table, $field3);
}
upgrade_plugin_savepoint(true, 2014041208, 'iomad_company_admin');
}
Thanks,
Nishant Pandya