Custom user fields for Module

Custom user fields for Module

by Tim Dyer -
Number of replies: 10

Is there a why to have a module install/add custom profile fields? I created a module that pills the instructor information (picture, phone, email, etc) from their profile and displays it. I wanted to add a Office Hours field, but want the module to do it.

Average of ratings: -
In reply to Tim Dyer

Re: Custom user fields for Module

by Rex Lorenzo -

We did something similar. For our plugin we made it create a table called office_hours and we have fields for the userid, courseid, office_hours, office_location, and office_phone.

You can create a new table for your plugin by generating an install.xml file using Moodle's XMLDB editor.

See: 

In reply to Rex Lorenzo

Re: Custom user fields for Module

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

If you are wanting to use the existing custom profile fields system within Moodle, instead of storing user data in a new table, then it is fairly straightforward to add them.

Create a file db/install.php, within your plugin, with a function xmldb_mod_MYPLUGIN_install() and inside that use $DB->insert_record to add a suitable entry into the user_info_field table (it's fairly obvious what to do, if you look at the table structure).

You can then retrieve the data you want by linking together the user_info_field and user_info_data tables (using the fieldid and userid fields).

If your plugin is already installed, you can add an upgrade step in upgrade.php to add the user fields instead (or you can just ask the end user to create them manually, but automatic creation is much better!).


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

Re: Custom user fields for Module

by Tim Dyer -

Awesome, Thanks! That's what I needed to hear.  

In reply to Davo Smith

Re: Custom user fields for Module

by Tim Dyer -

I got it to create the rows I needed in the user_info_field. But I'm guessing I'm missing something because I'm not seeing that field under the user profiles

In reply to Tim Dyer

Re: Custom user fields for Module

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

I can't remember all the fields off the top of my head, but you might want to try adding a custom user profile field via the Moodle UI (and then filling it in for a particular user), then compare the user_info_field and user_info_data records that were created by Moodle with those you have created yourself.


In reply to Davo Smith

Re: Custom user fields for Module

by Tim Dyer -

LOL.. You read my mind. I figured it out. It was the CategoryID. I didn't like it set to 1. 

In reply to Tim Dyer

Re: Custom user fields for Module

by Daniel Neis Araujo -
Picture of Core developers Picture of Plugin developers Picture of Translators

Hello,


i would recommend you to use the API (http://docs.moodle.org/dev/User_profile_fields) instead of doing inserts/selects direct to database to avoid having problems like MDL-44658.

Also, there is another related discussion: https://moodle.org/mod/forum/discuss.php?d=91370


Kind regards,

Daniel

Average of ratings: Useful (1)
In reply to Daniel Neis Araujo

Re: Custom user fields for Module

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

Daniel, unless I've missed something, the API you've linked to doesn't cover programatically creating a custom user profile field. There are functions for displaying and processing a form to ask an end user to create/update profile field definition and there are functions for setting/updating the value of a field for a particular user, but nothing for directly creating a new entry in the user_info_field table (which is why I've always just used manual database inserts in the past).

If there is an API for doing this, then I'd be very happy, as it'll save me some time, when next a customer requests it.

In reply to Davo Smith

Re: Custom user fields for Module

by Daniel Neis Araujo -
Picture of Core developers Picture of Plugin developers Picture of Translators

Hello, Davo


sorry for the not so correct links.

You may take a look at moodle/user/profile/definelib.php

In reply to Daniel Neis Araujo

Re: Custom user fields for Module

by Tim Dyer -

I programmed it to install the info_category and info_field. Also, matched the info_category "id" to info_field "Categoryid"