User preferences for plug-ins

User preferences for plug-ins

by Mustafa Hajjar -
Number of replies: 6

Has 'User preferences for plug-ins' been implemented?  I can't find it in the docs anywhere. 

If not, what are the other options?


Thank you,

Mustafa

Average of ratings: -
In reply to Mustafa Hajjar

Re: User preferences for plug-ins

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
The first line of the page you linked to states "NOTE: This page is about a potential feature that never landed, it appears completely out of date."

I've never noticed anything to do with this feature, so I would assume that heading is correct.

If you want other options, then you'll need to describe what it is that you want to do.
In reply to Davo Smith

Re: User preferences for plug-ins

by Mustafa Hajjar -
Thanks for the reply Davo;
I am developing a plugin that displays its content differently based on users' preferences (i.e. show in local time vs GMT, and more). So rather than displaying all preference options each time the plugin page loads, and using set_user_preference() each time, I was wondering if there is a way to save these preferences for the user somewhere (i.e. user profile preferences).

I looked at [local_customplugin]_myprofile_navigation() but it seems to be reserved for core plugins only.

Any help is greatly appreciated.

Thanks,

Mustafa
In reply to Mustafa Hajjar

Re: User preferences for plug-ins

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Why would you need to use set_user_preference() every time - the whole point of that function is to save the user preference, so that it is available (for that user) next time you call get_user_preference()?

PLUGIN_myprofile_navigation() is also absolutely NOT reserved for core plugins only - any plugin can use it to extend the user profile page and add extra links (to pages within your plugin).
In reply to Davo Smith

Re: User preferences for plug-ins

by Mustafa Hajjar -

I must have been doing something wrong, even when I traced through the PLUGIN_myprofile_navigation my callback was never called only the core plugins were. I’ll spend more time on it. 

Do you happen to know the best way to programmatically add a custom user profile field? The API seems to allow addition of a custom “type” only. I just want to add couple of text fields related to our organization and the plug-in.

Thanks

Mustafa

In reply to Mustafa Hajjar

Re: User preferences for plug-ins

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

You'll see that the code scans through the lib.php file of all known plugins, looking for a function 'PLUGINTYPE_PLUGINNAME_myprofile_navigation' - for each one that is found, it calls the function, with various params.

As for adding custom profile fields, I do this quite often, but there is no API for it, so it requires adding to the install.php some code that does something like:
  • Get the id of the first entry in mdl_user_profile_categories, or insert a new entry with default values and get the id
  • Get the highest 'sortorder' value and add 1, or start with 1
  • Check to see if a record already exists in mdl_user_profile_field with the target shortname (if so, skip the insert)
  • Insert a record with the calculated catid, sortorder - the rest of the fields will need to be set by you (I find the easiest way, during development, is to manually create the field in the UI, then look in the DB to see what values need to be added by the code)


In reply to Davo Smith

Re: User preferences for plug-ins

by Mustafa Hajjar -
Thanks Davo;
I will give it a try and report back... I will also work on my install.php and lookup some examples and review the DB after manual field addition as suggested.

Thanks again,
Mustafa