Add custom field in user registration page & link with db

Add custom field in user registration page & link with db

by Nishant Pandya -
Number of replies: 11

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


Average of ratings: -
In reply to Nishant Pandya

Re: Add custom field in user registration page & link with db

by eric yullu -
Picture of Testers

go to system administration->users->user profile fields-> create new profile fields.

then you can have your custom fields that you wanted.

In reply to eric yullu

Re: Add custom field in user registration page & link with db

by Torsten Händler -

thats right, 

and there you can set the custom field to be required on registration

In reply to eric yullu

Re: Add custom field in user registration page & link with db

by Nishant Pandya -

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

In reply to Nishant Pandya

Re: Add custom field in user registration page & link with db

by Torsten Händler -

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.

In reply to Torsten Händler

Re: Add custom field in user registration page & link with db

by Nishant Pandya -

Hey thanks Torsten Händler.

I am ready to change the core code. Please suggest.

In reply to Nishant Pandya

Re: Add custom field in user registration page & link with db

by Torsten Händler -

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

In reply to Torsten Händler

Re: Add custom field in user registration page & link with db

by Nishant Pandya -

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.

In reply to Nishant Pandya

Re: Add custom field in user registration page & link with db

by Torsten Händler -

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. 

Average of ratings: Useful (1)
In reply to Torsten Händler

Re: Add custom field in user registration page & link with db

by Nishant Pandya -

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.


In reply to Nishant Pandya

Re: Add custom field in user registration page & link with db

by AL Rachels -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

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

Average of ratings: Useful (1)
In reply to AL Rachels

Re: Add custom field in user registration page & link with db

by Nishant Pandya -

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